Panda3D
pStatClient.I
1 // Filename: pStatClient.I
2 // Created by: drose (16Jul00)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 
16 ////////////////////////////////////////////////////////////////////
17 // Function: PStatClient::set_client_name
18 // Access: Published
19 // Description: Sets the name of the client. This is reported to the
20 // PStatsServer, and will presumably be written in the
21 // title bar or something.
22 ////////////////////////////////////////////////////////////////////
23 INLINE void PStatClient::
24 set_client_name(const string &name) {
25  get_impl()->set_client_name(name);
26 }
27 
28 ////////////////////////////////////////////////////////////////////
29 // Function: PStatClient::get_client_name
30 // Access: Published
31 // Description: Retrieves the name of the client as set.
32 ////////////////////////////////////////////////////////////////////
33 INLINE string PStatClient::
34 get_client_name() const {
35  return get_impl()->get_client_name();
36 }
37 
38 ////////////////////////////////////////////////////////////////////
39 // Function: PStatClient::set_max_rate
40 // Access: Published
41 // Description: Controls the number of packets that will be sent to
42 // the server. Normally, one packet is sent per frame,
43 // but this can flood the server with more packets than
44 // it can handle if the frame rate is especially good
45 // (e.g. if nothing is onscreen at the moment). Set
46 // this parameter to a reasonable number to prevent this
47 // from happening.
48 //
49 // This number specifies the maximum number of packets
50 // that will be sent to the server per second, per
51 // thread.
52 ////////////////////////////////////////////////////////////////////
53 INLINE void PStatClient::
54 set_max_rate(double rate) {
55  get_impl()->set_max_rate(rate);
56 }
57 
58 ////////////////////////////////////////////////////////////////////
59 // Function: PStatClient::get_max_rate
60 // Access: Published
61 // Description: Returns the maximum number of packets that will be
62 // sent to the server per second, per thread. See
63 // set_max_rate().
64 ////////////////////////////////////////////////////////////////////
65 INLINE double PStatClient::
66 get_max_rate() const {
67  return get_impl()->get_max_rate();
68 }
69 
70 ////////////////////////////////////////////////////////////////////
71 // Function: PStatClient::get_num_collectors
72 // Access: Published
73 // Description: Returns the total number of collectors the Client
74 // knows about.
75 ////////////////////////////////////////////////////////////////////
76 INLINE int PStatClient::
77 get_num_collectors() const {
78  ReMutexHolder holder(_lock);
79  return _num_collectors;
80 }
81 
82 ////////////////////////////////////////////////////////////////////
83 // Function: PStatClient::get_collector_def
84 // Access: Published
85 // Description: Returns the definition body of the nth collector.
86 ////////////////////////////////////////////////////////////////////
87 INLINE PStatCollectorDef *PStatClient::
88 get_collector_def(int index) const {
89  nassertr(index >= 0 && index < _num_collectors, NULL);
90 
91  return get_collector_ptr(index)->get_def(this, index);
92 }
93 
94 ////////////////////////////////////////////////////////////////////
95 // Function: PStatClient::get_num_threads
96 // Access: Published
97 // Description: Returns the total number of threads the Client
98 // knows about.
99 ////////////////////////////////////////////////////////////////////
100 INLINE int PStatClient::
101 get_num_threads() const {
102  ReMutexHolder holder(_lock);
103  return _num_threads;
104 }
105 
106 ////////////////////////////////////////////////////////////////////
107 // Function: PStatClient::get_thread_name
108 // Access: Published
109 // Description: Returns the name of the indicated thread.
110 ////////////////////////////////////////////////////////////////////
111 INLINE string PStatClient::
112 get_thread_name(int index) const {
113  nassertr(index >= 0 && index < AtomicAdjust::get(_num_threads), string());
114  return get_thread_ptr(index)->_name;
115 }
116 
117 ////////////////////////////////////////////////////////////////////
118 // Function: PStatClient::get_thread_sync_name
119 // Access: Published
120 // Description: Returns the sync_name of the indicated thread.
121 ////////////////////////////////////////////////////////////////////
122 INLINE string PStatClient::
123 get_thread_sync_name(int index) const {
124  nassertr(index >= 0 && index < AtomicAdjust::get(_num_threads), string());
125  return get_thread_ptr(index)->_sync_name;
126 }
127 
128 ////////////////////////////////////////////////////////////////////
129 // Function: PStatClient::get_thread_object
130 // Access: Published
131 // Description: Returns the Panda Thread object associated with the
132 // indicated PStatThread.
133 ////////////////////////////////////////////////////////////////////
134 INLINE Thread *PStatClient::
135 get_thread_object(int index) const {
136  nassertr(index >= 0 && index < AtomicAdjust::get(_num_threads), NULL);
137  InternalThread *thread = get_thread_ptr(index);
138  if (thread->_thread.was_deleted()) {
139  return NULL;
140  }
141  return thread->_thread;
142 }
143 
144 ////////////////////////////////////////////////////////////////////
145 // Function: PStatClient::get_real_time
146 // Access: Published
147 // Description: Returns the time according to to the PStatClient's
148 // clock object. It keeps its own clock, instead of
149 // using the global clock object, so the stats won't get
150 // mucked up if you put the global clock in
151 // non-real-time mode or something.
152 ////////////////////////////////////////////////////////////////////
153 INLINE double PStatClient::
154 get_real_time() const {
155  if (has_impl()) {
156  return _impl->get_real_time();
157  }
158  return 0.0f;
159 }
160 
161 ////////////////////////////////////////////////////////////////////
162 // Function: PStatClient::connect
163 // Access: Published, Static
164 // Description: Attempts to establish a connection to the indicated
165 // PStatServer. Returns true if successful, false on
166 // failure.
167 ////////////////////////////////////////////////////////////////////
168 INLINE bool PStatClient::
169 connect(const string &hostname, int port) {
170  return get_global_pstats()->client_connect(hostname, port);
171 }
172 
173 ////////////////////////////////////////////////////////////////////
174 // Function: PStatClient::disconnect
175 // Access: Published, Static
176 // Description: Closes the connection previously established.
177 ////////////////////////////////////////////////////////////////////
178 INLINE void PStatClient::
179 disconnect() {
180  get_global_pstats()->client_disconnect();
181 }
182 
183 ////////////////////////////////////////////////////////////////////
184 // Function: PStatClient::is_connected
185 // Access: Published, Static
186 // Description: Returns true if the client believes it is connected
187 // to a working PStatServer, false otherwise.
188 ////////////////////////////////////////////////////////////////////
189 INLINE bool PStatClient::
190 is_connected() {
191  return get_global_pstats()->client_is_connected();
192 }
193 
194 ////////////////////////////////////////////////////////////////////
195 // Function: PStatClient::resume_after_pause
196 // Access: Published, Static
197 // Description: Resumes the PStatClient after the simulation has been
198 // paused for a while. This allows the stats to
199 // continue exactly where it left off, instead of
200 // leaving a big gap that would represent a chug.
201 ////////////////////////////////////////////////////////////////////
202 INLINE void PStatClient::
204  get_global_pstats()->client_resume_after_pause();
205 }
206 
207 ////////////////////////////////////////////////////////////////////
208 // Function: PStatClient::client_connect
209 // Access: Published
210 // Description: The nonstatic implementation of connect().
211 ////////////////////////////////////////////////////////////////////
212 INLINE bool PStatClient::
213 client_connect(string hostname, int port) {
214  ReMutexHolder holder(_lock);
215  client_disconnect();
216  return get_impl()->client_connect(hostname, port);
217 }
218 
219 ////////////////////////////////////////////////////////////////////
220 // Function: PStatClient::client_is_connected
221 // Access: Published
222 // Description: The nonstatic implementation of is_connected().
223 ////////////////////////////////////////////////////////////////////
224 INLINE bool PStatClient::
225 client_is_connected() const {
226  return has_impl() && _impl->client_is_connected();
227 }
228 
229 ////////////////////////////////////////////////////////////////////
230 // Function: PStatClient::client_resume_after_pause
231 // Access: Published
232 // Description: Resumes the PStatClient after the simulation has been
233 // paused for a while. This allows the stats to
234 // continue exactly where it left off, instead of
235 // leaving a big gap that would represent a chug.
236 ////////////////////////////////////////////////////////////////////
237 INLINE void PStatClient::
238 client_resume_after_pause() {
239  if (has_impl()) {
240  _impl->client_resume_after_pause();
241  }
242 }
243 
244 ////////////////////////////////////////////////////////////////////
245 // Function: PStatClient::has_impl
246 // Access: Private
247 // Description: Returns true if the PStatClientImpl object has been
248 // created for this object yet, false otherwise.
249 ////////////////////////////////////////////////////////////////////
250 INLINE bool PStatClient::
251 has_impl() const {
252  return (_impl != (PStatClientImpl *)NULL);
253 }
254 
255 ////////////////////////////////////////////////////////////////////
256 // Function: PStatClient::get_impl
257 // Access: Private
258 // Description: Returns the PStatClientImpl object for this object.
259 // If the PStatClientImpl object has not yet been
260 // created, implicitly creates it.
261 ////////////////////////////////////////////////////////////////////
262 INLINE PStatClientImpl *PStatClient::
263 get_impl() {
264  ReMutexHolder holder(_lock);
265  if (_impl == (PStatClientImpl *)NULL) {
266  _impl = new PStatClientImpl(this);
267  }
268 
269  return _impl;
270 }
271 
272 ////////////////////////////////////////////////////////////////////
273 // Function: PStatClient::get_impl
274 // Access: Private
275 // Description: Returns the PStatClientImpl object for this object.
276 // If the PStatClientImpl object has not yet been
277 // created, implicitly creates it.
278 ////////////////////////////////////////////////////////////////////
279 INLINE const PStatClientImpl *PStatClient::
280 get_impl() const {
281  return ((PStatClient *)this)->get_impl();
282 }
283 
284 ////////////////////////////////////////////////////////////////////
285 // Function: PStatClient::get_collector_ptr
286 // Access: Private
287 // Description: Returns the nth collector in a thread-safe manner,
288 // even if _lock is not held.
289 ////////////////////////////////////////////////////////////////////
290 INLINE PStatClient::Collector *PStatClient::
291 get_collector_ptr(int collector_index) const {
292  CollectorPointer *collectors = (CollectorPointer *)AtomicAdjust::get_ptr(_collectors);
293  return collectors[collector_index];
294 }
295 
296 ////////////////////////////////////////////////////////////////////
297 // Function: PStatClient::get_thread_ptr
298 // Access: Private
299 // Description: Returns the nth thread in a thread-safe manner,
300 // even if _lock is not held.
301 ////////////////////////////////////////////////////////////////////
302 INLINE PStatClient::InternalThread *PStatClient::
303 get_thread_ptr(int thread_index) const {
304  ThreadPointer *threads = (ThreadPointer *)AtomicAdjust::get_ptr(_threads);
305  return threads[thread_index];
306 }
307 
308 ////////////////////////////////////////////////////////////////////
309 // Function: PStatClient::Collector::Constructor
310 // Access: Public
311 // Description:
312 ////////////////////////////////////////////////////////////////////
313 INLINE PStatClient::Collector::
314 Collector(int parent_index, const string &name) :
315  _def(NULL),
316  _parent_index(parent_index),
317  _name(name)
318 {
319 }
320 
321 ////////////////////////////////////////////////////////////////////
322 // Function: PStatClient::Collector::get_parent_index
323 // Access: Public
324 // Description:
325 ////////////////////////////////////////////////////////////////////
326 INLINE int PStatClient::Collector::
327 get_parent_index() const {
328  return _parent_index;
329 }
330 
331 ////////////////////////////////////////////////////////////////////
332 // Function: PStatClient::Collector::get_name
333 // Access: Public
334 // Description:
335 ////////////////////////////////////////////////////////////////////
336 INLINE const string &PStatClient::Collector::
337 get_name() const {
338  return _name;
339 }
340 
341 ////////////////////////////////////////////////////////////////////
342 // Function: PStatClient::Collector::is_active
343 // Access: Public
344 // Description: Returns true if the indicated collector has been
345 // designated as active, false otherwise. This might
346 // return initially false until the collector def has
347 // actually been created.
348 ////////////////////////////////////////////////////////////////////
349 INLINE bool PStatClient::Collector::
350 is_active() const {
351  return _def != (PStatCollectorDef *)NULL && _def->_is_active;
352 }
353 
354 ////////////////////////////////////////////////////////////////////
355 // Function: PStatClient::Collector::get_def
356 // Access: Public
357 // Description: Returns the PStatCollectorDef that contains all of
358 // the information about the collector. If this object
359 // has not yet been created, creates it.
360 ////////////////////////////////////////////////////////////////////
361 INLINE PStatCollectorDef *PStatClient::Collector::
362 get_def(const PStatClient *client, int this_index) const {
363  if (_def == (PStatCollectorDef *)NULL) {
364  ((Collector *)this)->make_def(client, this_index);
365  }
366 
367  return _def;
368 }
static Pointer get_ptr(const Pointer &var)
Atomically retrieves the snapshot value of the indicated variable.
static bool connect(const string &=string(), int=-1)
Attempts to establish a connection to the indicated PStatServer.
Definition: pStatClient.h:267
static void resume_after_pause()
Resumes the PStatClient after the simulation has been paused for a while.
Definition: pStatClient.h:270
static void disconnect()
Closes the connection previously established.
Definition: pStatClient.h:268
static Integer get(const Integer &var)
Atomically retrieves the snapshot value of the indicated variable.
Similar to MutexHolder, but for a reentrant mutex.
Definition: reMutexHolder.h:27
A thread; that is, a lightweight process.
Definition: thread.h:51
static bool is_connected()
Returns true if the client believes it is connected to a working PStatServer, false otherwise...
Definition: pStatClient.h:269
Defines the details about the Collectors: the name, the suggested color, etc.
Manages the communications to report statistics via a network connection to a remote PStatServer...
Definition: pStatClient.h:261