Panda3D
pStatClient.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 pStatClient.I
10  * @author drose
11  * @date 2000-07-16
12  */
13 
14 /**
15  * Returns the total number of collectors the Client knows about.
16  */
17 INLINE int PStatClient::
18 get_num_collectors() const {
19  ReMutexHolder holder(_lock);
20  return (int)_num_collectors;
21 }
22 
23 /**
24  * Returns the definition body of the nth collector.
25  */
26 INLINE PStatCollectorDef *PStatClient::
27 get_collector_def(int index) const {
28  nassertr(index >= 0 && index < _num_collectors, nullptr);
29 
30  return get_collector_ptr(index)->get_def(this, index);
31 }
32 
33 /**
34  * Returns the total number of threads the Client knows about.
35  */
36 INLINE int PStatClient::
37 get_num_threads() const {
38  ReMutexHolder holder(_lock);
39  return (int)_num_threads;
40 }
41 
42 /**
43  * Returns the name of the indicated thread.
44  */
45 INLINE std::string PStatClient::
46 get_thread_name(int index) const {
47  nassertr(index >= 0 && index < AtomicAdjust::get(_num_threads), std::string());
48  return get_thread_ptr(index)->_name;
49 }
50 
51 /**
52  * Returns the sync_name of the indicated thread.
53  */
54 INLINE std::string PStatClient::
55 get_thread_sync_name(int index) const {
56  nassertr(index >= 0 && index < AtomicAdjust::get(_num_threads), std::string());
57  return get_thread_ptr(index)->_sync_name;
58 }
59 
60 /**
61  * Returns the Panda Thread object associated with the indicated PStatThread.
62  */
63 INLINE PT(Thread) PStatClient::
64 get_thread_object(int index) const {
65  nassertr(index >= 0 && index < AtomicAdjust::get(_num_threads), nullptr);
66  InternalThread *thread = get_thread_ptr(index);
67  return thread->_thread.lock();
68 }
69 
70 /**
71  * Attempts to establish a connection to the indicated PStatServer. Returns
72  * true if successful, false on failure.
73  */
74 INLINE bool PStatClient::
75 connect(const std::string &hostname, int port) {
76  return get_global_pstats()->client_connect(hostname, port);
77 }
78 
79 /**
80  * Closes the connection previously established.
81  */
82 INLINE void PStatClient::
83 disconnect() {
84  get_global_pstats()->client_disconnect();
85 }
86 
87 /**
88  * Returns true if the client believes it is connected to a working
89  * PStatServer, false otherwise.
90  */
91 INLINE bool PStatClient::
92 is_connected() {
93  return get_global_pstats()->client_is_connected();
94 }
95 
96 /**
97  * Resumes the PStatClient after the simulation has been paused for a while.
98  * This allows the stats to continue exactly where it left off, instead of
99  * leaving a big gap that would represent a chug.
100  */
101 INLINE void PStatClient::
103  get_global_pstats()->client_resume_after_pause();
104 }
105 
106 /**
107  * Returns true if the PStatClientImpl object has been created for this object
108  * yet, false otherwise.
109  */
110 INLINE bool PStatClient::
111 has_impl() const {
112  return (_impl != nullptr);
113 }
114 
115 /**
116  * Returns the PStatClientImpl object for this object. If the PStatClientImpl
117  * object has not yet been created, implicitly creates it.
118  */
119 INLINE PStatClientImpl *PStatClient::
120 get_impl() {
121  ReMutexHolder holder(_lock);
122  if (_impl == nullptr) {
123  make_impl();
124  }
125  return _impl;
126 }
127 
128 /**
129  * Returns the PStatClientImpl object for this object. If the PStatClientImpl
130  * object has not yet been created, implicitly creates it.
131  */
132 INLINE const PStatClientImpl *PStatClient::
133 get_impl() const {
134  ReMutexHolder holder(_lock);
135  if (_impl == nullptr) {
136  make_impl();
137  }
138  return _impl;
139 }
140 
141 /**
142  * Returns the nth collector in a thread-safe manner, even if _lock is not
143  * held.
144  */
145 INLINE PStatClient::Collector *PStatClient::
146 get_collector_ptr(int collector_index) const {
147  CollectorPointer *collectors = (CollectorPointer *)AtomicAdjust::get_ptr(_collectors);
148  return collectors[collector_index];
149 }
150 
151 /**
152  * Returns the nth thread in a thread-safe manner, even if _lock is not held.
153  */
154 INLINE PStatClient::InternalThread *PStatClient::
155 get_thread_ptr(int thread_index) const {
156  ThreadPointer *threads = (ThreadPointer *)AtomicAdjust::get_ptr(_threads);
157  return threads[thread_index];
158 }
159 
160 /**
161  *
162  */
163 INLINE PStatClient::Collector::
164 Collector(int parent_index, const std::string &name) :
165  _def(nullptr),
166  _parent_index(parent_index),
167  _name(name)
168 {
169 }
170 
171 /**
172  *
173  */
174 INLINE int PStatClient::Collector::
175 get_parent_index() const {
176  return _parent_index;
177 }
178 
179 /**
180  *
181  */
182 INLINE const std::string &PStatClient::Collector::
183 get_name() const {
184  return _name;
185 }
186 
187 /**
188  * Returns true if the indicated collector has been designated as active,
189  * false otherwise. This might return initially false until the collector def
190  * has actually been created.
191  */
192 INLINE bool PStatClient::Collector::
193 is_active() const {
194  return _def != nullptr && _def->_is_active;
195 }
196 
197 /**
198  * Returns the PStatCollectorDef that contains all of the information about
199  * the collector. If this object has not yet been created, creates it.
200  */
201 INLINE PStatCollectorDef *PStatClient::Collector::
202 get_def(const PStatClient *client, int this_index) const {
203  if (_def == nullptr) {
204  ((Collector *)this)->make_def(client, this_index);
205  }
206 
207  return _def;
208 }
static Pointer get_ptr(const Pointer &var)
Atomically retrieves the snapshot value of the indicated variable.
static void resume_after_pause()
Resumes the PStatClient after the simulation has been paused for a while.
Definition: pStatClient.h:272
static void disconnect()
Closes the connection previously established.
Definition: pStatClient.h:270
PT(Thread) PStatClient
Returns the Panda Thread object associated with the indicated PStatThread.
Definition: pStatClient.I:63
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:25
A thread; that is, a lightweight process.
Definition: thread.h:46
static bool is_connected()
Returns true if the client believes it is connected to a working PStatServer, false otherwise.
Definition: pStatClient.h:271
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:263