Panda3D

pStatCollector.I
Go to the documentation of this file.
1 /**
2 * PANDA 3D SOFTWARE
3 * Copyright (c) Carnegie Mellon University. All rights reserved.
4 *
5 * All use of this software is subject to the terms of the revised BSD
6 * license. You should have received a copy of this license along
7 * with this source code in a file named "LICENSE."
8 *
9 * @file pStatCollector.I
10 * @author drose
11 * @date 2000-07-10
12 */
13
14 #ifdef DO_PSTATS
15
16 /**
17 * Normally, this constructor is called only from PStatClient. Use one of the
18 * constructors below to create your own Collector.
19 */
20 INLINE PStatCollector::
21 PStatCollector(PStatClient *client, int index) :
22 _client(client),
23 _index(index),
24 _level(0.0f)
25 {
26 }
27
28 /**
29 * Creates a new PStatCollector, ready to start accumulating data. The name
30 * of the collector uniquely identifies it among the other collectors; if two
31 * collectors share the same name then they are really the same collector.
32 *
33 * The name may also be a compound name, something like "Cull:Sort", which
34 * indicates that this is a collector named "Sort", a child of the collector
35 * named "Cull". The parent may also be named explicitly by reference in the
36 * other flavor of the constructor; see further comments on this for that
37 * constructor.
38 *
39 * If the client pointer is non-null, it specifies a particular client to
40 * register the collector with; otherwise, the global client is used.
41 */
42 INLINE PStatCollector::
43 PStatCollector(const std::string &name, PStatClient *client) :
44 _level(0.0f)
45 {
46 if (client == nullptr) {
47 client = PStatClient::get_global_pstats();
48 }
49 (*this) = client->make_collector_with_relname(0, name);
50 }
51
52 /**
53 * Creates a new PStatCollector, ready to start accumulating data. The name
54 * of the collector uniquely identifies it among the other collectors; if two
55 * collectors share the same name then they are really the same collector.
56 *
57 * The parent is the collector that conceptually includes all of the time
58 * measured for this collector. For instance, a particular character's
59 * animation time is owned by the "Animation" collector, which is in turn
60 * owned by the "Frame" collector. It is not strictly necessary that all of
61 * the time spent in a particular collector is completely nested within time
62 * spent in its parent's collector. If parent is the empty string, the
63 * collector is owned by "Frame".
64 *
65 * This constructor does not take a client pointer; it always creates the new
66 * collector on the same client as its parent.
67 */
68 INLINE PStatCollector::
69 PStatCollector(const PStatCollector &parent, const std::string &name) :
70 _level(0.0f)
71 {
72 nassertv(parent._client != nullptr);
73 (*this) =
74 parent._client->make_collector_with_relname(parent._index, name);
75 }
76
77 /**
78 *
79 */
80 INLINE PStatCollector::
81 PStatCollector(const PStatCollector &copy) :
82 _client(copy._client),
83 _index(copy._index),
84 _level(0.0f)
85 {
86 }
87
88 /**
89 *
90 */
91 INLINE void PStatCollector::
92 operator = (const PStatCollector &copy) {
93 _client = copy._client;
94 _index = copy._index;
95 }
96
97 /**
98 * Returns true if collector is valid and may be used, or false if it was
99 * constructed with the default constructor (in which case any attempt to use
100 * it will crash).
101 */
102 INLINE bool PStatCollector::
103 is_valid() const {
104 return (_client != nullptr);
105 }
106
107 /**
108 * Returns the local name of this collector. This is the rightmost part of
109 * the fullname, after the rightmost colon.
110 */
111 INLINE std::string PStatCollector::
112 get_name() const {
113 if (_client != nullptr) {
114 return _client->get_collector_name(_index);
115 }
116 return std::string();
117 }
118
119 /**
120 * Returns the full name of this collector. This includes the names of all
121 * the collector's parents, concatenated together with colons.
122 */
123 INLINE std::string PStatCollector::
124 get_fullname() const {
125 if (_client != nullptr) {
126 return _client->get_collector_fullname(_index);
127 }
128 return std::string();
129 }
130
131 /**
132 *
133 */
134 INLINE void PStatCollector::
135 output(std::ostream &out) const {
136 out << "PStatCollector(\"" << get_fullname() << "\")";
137 }
138
139 /**
140 * Returns true if this particular collector is active on the default thread,
141 * and we are currently transmitting PStats data.
142 */
143 INLINE bool PStatCollector::
144 is_active() {
145 #ifndef HAVE_THREADS
146 return _client->is_active(_index, 0);
147 #else // HAVE_THREADS
148 return is_active(_client->get_current_thread());
149 #endif // HAVE_THREADS
150 }
151
152 /**
153 * Returns true if this particular collector has been started on the default
154 * thread, or false otherwise.
155 */
156 INLINE bool PStatCollector::
157 is_started() {
158 #ifndef HAVE_THREADS
159 return _client->is_started(_index, 0);
160 #else // HAVE_THREADS
161 return is_started(_client->get_current_thread());
162 #endif // HAVE_THREADS
163 }
164
165 /**
166 * Starts this particular timer ticking. This should be called before the
167 * code you want to measure.
168 */
169 INLINE void PStatCollector::
170 start() {
171 #ifndef HAVE_THREADS
172 _client->start(_index, 0);
173 #else // HAVE_THREADS
174 start(_client->get_current_thread());
175 #endif // HAVE_THREADS
176 }
177
178 /**
179 * Stops this timer. This should be called after the code you want to
180 * measure.
181 */
182 INLINE void PStatCollector::
183 stop() {
184 #ifndef HAVE_THREADS
185 _client->stop(_index, 0);
186 #else // HAVE_THREADS
187 stop(_client->get_current_thread());
188 #endif // HAVE_THREADS
189 }
190
191 /**
192 * Removes the level setting associated with this collector for the main
193 * thread. The collector will no longer show up on any level graphs in the
194 * main thread. This implicitly calls flush_level().
195 */
196 INLINE void PStatCollector::
197 clear_level() {
198 _client->clear_level(_index, 0);
199 _level = 0.0f;
200 }
201
202 /**
203 * Sets the level setting associated with this collector for the main thread
204 * to the indicated value. This implicitly calls flush_level().
205 */
206 INLINE void PStatCollector::
207 set_level(double level) {
208 _client->set_level(_index, 0, level);
209 _level = 0.0f;
210 }
211
212 /**
213 * Adds the indicated increment (which may be negative) to the level setting
214 * associated with this collector for the main thread. If the collector did
215 * not already have a level setting for the main thread, it is initialized to
216 * 0.
217 *
218 * As an optimization, the data is not immediately set to the PStatClient. It
219 * will be sent the next time flush_level() is called.
220 */
221 INLINE void PStatCollector::
222 add_level(double increment) {
223 _level += increment;
224 }
225
226 /**
227 * Subtracts the indicated decrement (which may be negative) to the level
228 * setting associated with this collector for the main thread. If the
229 * collector did not already have a level setting for the main thread, it is
230 * initialized to 0.
231 *
232 * As an optimization, the data is not immediately set to the PStatClient. It
233 * will be sent the next time flush_level() is called.
234 */
235 INLINE void PStatCollector::
236 sub_level(double decrement) {
237 _level -= decrement;
238 }
239
240 /**
241 * Calls add_level() and immediately calls flush_level().
242 */
243 INLINE void PStatCollector::
244 add_level_now(double increment) {
245 add_level(increment);
246 flush_level();
247 }
248
249 /**
250 * Calls sub_level() and immediately calls flush_level().
251 */
252 INLINE void PStatCollector::
253 sub_level_now(double decrement) {
254 sub_level(decrement);
255 flush_level();
256 }
257
258 /**
259 * Updates the PStatClient with the recent results from add_level() and
260 * sub_level().
261 */
262 INLINE void PStatCollector::
263 flush_level() {
264 if (_level != 0.0f) {
265 _client->add_level(_index, 0, _level);
266 _level = 0.0f;
267 }
268 }
269
270 /**
271 * Returns the current level value of the given collector in the main thread.
272 * This implicitly calls flush_level().
273 */
274 INLINE double PStatCollector::
275 get_level() {
276 flush_level();
277 return _client->get_level(_index, 0);
278 }
279
280 /**
281 * Removes the level setting associated with this collector for the current
282 * thread. The collector will no longer show up on any level graphs in the
283 * current thread.
284 */
285 INLINE void PStatCollector::
286 clear_thread_level() {
287 #ifndef HAVE_THREADS
288 _client->clear_level(_index, 0);
289 #else // HAVE_THREADS
290 clear_level(_client->get_current_thread());
291 #endif // HAVE_THREADS
292 }
293
294 /**
295 * Sets the level setting associated with this collector for the current
296 * thread to the indicated value.
297 */
298 INLINE void PStatCollector::
299 set_thread_level(double level) {
300 #ifndef HAVE_THREADS
301 _client->set_level(_index, 0, level);
302 #else // HAVE_THREADS
303 set_level(_client->get_current_thread(), level);
304 #endif // HAVE_THREADS
305 }
306
307 /**
308 * Adds the indicated increment (which may be negative) to the level setting
309 * associated with this collector for the current thread. If the collector
310 * did not already have a level setting for the current thread, it is
311 * initialized to 0.
312 */
313 INLINE void PStatCollector::
314 add_thread_level(double increment) {
315 #ifndef HAVE_THREADS
316 _client->add_level(_index, 0, increment);
317 #else // HAVE_THREADS
318 add_level(_client->get_current_thread(), increment);
319 #endif // HAVE_THREADS
320 }
321
322 /**
323 * Subtracts the indicated decrement (which may be negative) to the level
324 * setting associated with this collector for the current thread. If the
325 * collector did not already have a level setting for the current thread, it
326 * is initialized to 0.
327 */
328 INLINE void PStatCollector::
329 sub_thread_level(double decrement) {
330 #ifndef HAVE_THREADS
331 _client->add_level(_index, 0, -decrement);
332 #else // HAVE_THREADS
333 sub_level(_client->get_current_thread(), decrement);
334 #endif // HAVE_THREADS
335 }
336
337 /**
338 * Returns the current level value of the given collector in the current
339 * thread.
340 */
341 INLINE double PStatCollector::
342 get_thread_level() {
343 #ifndef HAVE_THREADS
344 return _client->get_level(_index, 0);
345 #else // HAVE_THREADS
346 return get_level(_client->get_current_thread());
347 #endif // HAVE_THREADS
348 }
349
350 /**
351 * Returns true if this particular collector is active on the indicated
352 * thread, and we are currently transmitting PStats data.
353 */
354 INLINE bool PStatCollector::
355 is_active(const PStatThread &thread) {
356 return _client->is_active(_index, thread._index);
357 }
358
359 /**
360 * Returns true if this particular collector has been started on the indicated
361 * thread, or false otherwise.
362 */
363 INLINE bool PStatCollector::
364 is_started(const PStatThread &thread) {
365 return _client->is_started(_index, thread._index);
366 }
367
368 /**
369 * Starts this timer ticking within a particular thread.
370 */
371 INLINE void PStatCollector::
372 start(const PStatThread &thread) {
373 nassertv(_client != nullptr);
374 _client->start(_index, thread._index);
375 }
376
377 /**
378 * Marks that the timer should have been started as of the indicated time.
379 * This must be a time based on the PStatClient's clock (see
380 * PStatClient::get_clock()), and care should be taken that all such calls
381 * exhibit a monotonically increasing series of time values.
382 */
383 INLINE void PStatCollector::
384 start(const PStatThread &thread, double as_of) {
385 _client->start(_index, thread._index, as_of);
386 }
387
388 /**
389 * Stops this timer within a particular thread.
390 */
391 INLINE void PStatCollector::
392 stop(const PStatThread &thread) {
393 _client->stop(_index, thread._index);
394 }
395
396 /**
397 * Marks that the timer should have been stopped as of the indicated time.
398 * This must be a time based on the PStatClient's clock (see
399 * PStatClient::get_clock()), and care should be taken that all such calls
400 * exhibit a monotonically increasing series of time values.
401 */
402 INLINE void PStatCollector::
403 stop(const PStatThread &thread, double as_of) {
404 _client->stop(_index, thread._index, as_of);
405 }
406
407 /**
408 * Removes the level setting associated with this collector for the indicated
409 * thread. The collector will no longer show up on any level graphs in this
410 * thread.
411 */
412 INLINE void PStatCollector::
413 clear_level(const PStatThread &thread) {
414 _client->clear_level(_index, thread._index);
415 }
416
417 /**
418 * Sets the level setting associated with this collector for the indicated
419 * thread to the indicated value.
420 */
421 INLINE void PStatCollector::
422 set_level(const PStatThread &thread, double level) {
423 _client->set_level(_index, thread._index, level);
424 }
425
426 /**
427 * Adds the indicated increment (which may be negative) to the level setting
428 * associated with this collector for the indicated thread. If the collector
429 * did not already have a level setting for this thread, it is initialized to
430 * 0.
431 */
432 INLINE void PStatCollector::
433 add_level(const PStatThread &thread, double increment) {
434 _client->add_level(_index, thread._index, increment);
435 }
436
437 /**
438 * Subtracts the indicated decrement (which may be negative) to the level
439 * setting associated with this collector for the indicated thread. If the
440 * collector did not already have a level setting for this thread, it is
441 * initialized to 0.
442 */
443 INLINE void PStatCollector::
444 sub_level(const PStatThread &thread, double decrement) {
445 _client->add_level(_index, thread._index, -decrement);
446 }
447
448 /**
449 * Returns the current level value of the given collector.
450 */
451 INLINE double PStatCollector::
452 get_level(const PStatThread &thread) {
453 return _client->get_level(_index, thread._index);
454 }
455
456 /**
457 * Returns the index number of this particular collector within the
458 * PStatClient.
459 */
460 INLINE int PStatCollector::
461 get_index() const {
462 return _index;
463 }
464
465 #else // DO_PSTATS
466
467 /**
468 * Creates an invalid PStatCollector. Any attempt to use this collector will
469 * crash messily.
470 *
471 * You can reassign it to a different, valid one later.
472 */
473 INLINE PStatCollector::
474 PStatCollector()
475 {
476 }
477
478 /**
479 * This bogus version of the function is only defined if DO_PSTATS is not
480 * defined, meaning all these functions should compile to nothing.
481 */
482 INLINE PStatCollector::
483 PStatCollector(const std::string &, PStatClient *client) {
484 // We need this bogus comparison just to prevent the SGI compiler from
485 // dumping core. It's perfectly meaningless.
486 #ifdef mips
487 if (client == nullptr) {
488 return;
489 }
490 #endif
491 }
492
493 /**
494 * This bogus version of the function is only defined if DO_PSTATS is not
495 * defined, meaning all these functions should compile to nothing.
496 */
497 INLINE PStatCollector::
498 PStatCollector(const PStatCollector &parent, const std::string &) {
499 // We need this bogus comparison just to prevent the SGI compiler from
500 // dumping core. It's perfectly meaningless.
501 #ifdef mips
502 if (&parent == nullptr) {
503 return;
504 }
505 #endif
506 }
507
508
509 #endif // DO_PSTATS
PStatClient
Manages the communications to report statistics via a network connection to a remote PStatServer.
Definition: pStatClient.h:263
PStatCollector
A lightweight class that represents a single element that may be timed and/or counted via stats.
Definition: pStatCollector.h:43
PStatCollector::PStatCollector
PStatCollector()
Creates an invalid PStatCollector.
Definition: pStatCollector.I:474
PStatThread
A lightweight class that represents a single thread of execution to PStats.
Definition: pStatThread.h:28