Panda3D

Loading...
Searching...
No Matches
pStatClient.h
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.h
10 * @author drose
11 * @date 2000-07-09
12 */
13
14#ifndef PSTATCLIENT_H
15#define PSTATCLIENT_H
16
17#include "pandabase.h"
18
19#include "pStatFrameData.h"
20#include "pStatCollectorDef.h"
21#include "reMutex.h"
22#include "lightMutex.h"
23#include "reMutexHolder.h"
24#include "lightMutexHolder.h"
25#include "pmap.h"
26#include "thread.h"
27#include "weakPointerTo.h"
28#include "vector_int.h"
29#include "atomicAdjust.h"
30#include "numeric_types.h"
31#include "bitArray.h"
32#include "extension.h"
33
34 class PStatClientImpl;
35 class PStatCollector;
36 class PStatCollectorDef;
37 class PStatThread;
38 class GraphicsStateGuardian;
39
40/**
41 * Manages the communications to report statistics via a network connection to
42 * a remote PStatServer.
43 *
44 * Normally, there is only one PStatClient in the world, although it is
45 * possible to have multiple PStatClients if extraordinary circumstances
46 * require in. Since each PStatCollector registers itself with the
47 * PStatClient when it is created, having multiple PStatClients requires
48 * special care when constructing the various PStatCollectors.
49 *
50 * If DO_PSTATS is not defined, we don't want to use stats at all. This class
51 * is therefore defined as a stub class.
52 */
53#ifdef DO_PSTATS
54 class EXPCL_PANDA_PSTATCLIENT PStatClient : public Thread::PStatsCallback {
55 public:
56 PStatClient();
57 ~PStatClient();
58
59PUBLISHED:
60 void set_client_name(const std::string &name);
61 std::string get_client_name() const;
62 void set_max_rate(double rate);
63 double get_max_rate() const;
64
65 INLINE int get_num_collectors() const;
66 PStatCollector get_collector(int index) const;
67 MAKE_SEQ(get_collectors, get_num_collectors, get_collector);
68 INLINE PStatCollectorDef *get_collector_def(int index) const;
69 std::string get_collector_name(int index) const;
70 std::string get_collector_fullname(int index) const;
71
72 INLINE int get_num_threads() const;
73 PStatThread get_thread(int index) const;
74 MAKE_SEQ(get_threads, get_num_threads, get_thread);
75 INLINE std::string get_thread_name(int index) const;
76 INLINE std::string get_thread_sync_name(int index) const;
77 INLINE PT(Thread) get_thread_object(int index) const;
78
79 PStatThread get_main_thread() const;
80 PStatThread get_current_thread() const;
81
82 double get_real_time() const;
83
84 MAKE_PROPERTY(client_name, get_client_name, set_client_name);
85 MAKE_PROPERTY(max_rate, get_max_rate, set_max_rate);
86 MAKE_SEQ_PROPERTY(collectors, get_num_collectors, get_collector);
87 MAKE_SEQ_PROPERTY(threads, get_num_threads, get_thread);
88 MAKE_PROPERTY(main_thread, get_main_thread);
89 MAKE_PROPERTY(current_thread, get_current_thread);
90 MAKE_PROPERTY(real_time, get_real_time);
91
92 EXTEND INLINE static bool connect(const std::string &hostname = std::string(), int port = -1);
93 EXTEND INLINE static void disconnect();
94 INLINE static bool is_connected();
95
96 INLINE static void resume_after_pause();
97
98 static void main_tick();
99 static void thread_tick(const std::string &sync_name);
100
101 void client_main_tick();
102 void client_thread_tick(const std::string &sync_name);
103 EXTEND bool client_connect(std::string hostname, int port);
104 EXTEND void client_disconnect();
105 bool client_is_connected() const;
106
107 void client_resume_after_pause();
108
109 static PStatClient *get_global_pstats();
110
111 private:
112 INLINE bool has_impl() const;
113 INLINE PStatClientImpl *get_impl();
114 INLINE const PStatClientImpl *get_impl() const;
115 void make_impl() const;
116
117 PStatCollector make_collector_with_relname(int parent_index, std::string relname);
118 PStatCollector make_collector_with_name(int parent_index, const std::string &name);
119 PStatThread do_get_current_thread() const;
120 PStatThread make_thread(Thread *thread);
121 PStatThread do_make_thread(Thread *thread);
122 PStatThread make_gpu_thread(const std::string &name);
123
124 bool is_active(int collector_index, int thread_index) const;
125 bool is_started(int collector_index, int thread_index) const;
126
127 void start(int collector_index, int thread_index);
128 void start(int collector_index, int thread_index, double as_of);
129 void stop(int collector_index, int thread_index);
130 void stop(int collector_index, int thread_index, double as_of);
131
132 void clear_level(int collector_index, int thread_index);
133 void set_level(int collector_index, int thread_index, double level);
134 void add_level(int collector_index, int thread_index, double increment);
135 double get_level(int collector_index, int thread_index) const;
136
137 static void start_clock_wait();
138 static void start_clock_busy_wait();
139 static void stop_clock_wait();
140
141 class Collector;
142 class InternalThread;
143 void add_collector(Collector *collector);
144 void add_thread(InternalThread *thread);
145
146 INLINE Collector *get_collector_ptr(int collector_index) const;
147 INLINE InternalThread *get_thread_ptr(int thread_index) const;
148
149 virtual void deactivate_hook(Thread *thread);
150 virtual void activate_hook(Thread *thread);
151
152 private:
153 // This mutex protects everything in this class.
154 ReMutex _lock;
155
156 typedef pmap<std::string, int> ThingsByName;
157 typedef pmap<std::string, vector_int> MultiThingsByName;
158 MultiThingsByName _threads_by_name, _threads_by_sync_name;
159
160 // This is for the data that is per-collector, per-thread. A vector of
161 // these is stored in each Collector object, below, indexed by thread index.
162 class PerThreadData {
163 public:
164 PerThreadData();
165 bool _has_level;
166 double _level;
167 int _nested_count;
168 };
169 typedef pvector<PerThreadData> PerThread;
170
171 // This is where the meat of the Collector data is stored. (All the stuff
172 // in PStatCollector and PStatCollectorDef is just fluff.)
173 class EXPCL_PANDA_PSTATCLIENT Collector {
174 public:
175 INLINE Collector(int parent_index, const std::string &name);
176 INLINE int get_parent_index() const;
177 INLINE const std::string &get_name() const;
178 INLINE bool is_active() const;
179 INLINE PStatCollectorDef *get_def(const PStatClient *client, int this_index) const;
180
181 private:
182 void make_def(const PStatClient *client, int this_index);
183
184 private:
185 // This pointer is initially NULL, and will be filled in when it is first
186 // needed.
187 PStatCollectorDef *_def;
188
189 // This data is used to create the PStatCollectorDef when it is needed.
190 int _parent_index;
191 std::string _name;
192
193 public:
194 // Relations to other collectors.
195 ThingsByName _children;
196 PerThread _per_thread;
197 };
198 typedef Collector *CollectorPointer;
199 AtomicAdjust::Pointer _collectors; // CollectorPointer *_collectors;
200 AtomicAdjust::Integer _collectors_size; // size of the allocated array
201 AtomicAdjust::Integer _num_collectors; // number of in-use elements within the array
202
203 // This defines a single thread, i.e. a separate chain of execution,
204 // independent of all other threads. Timing and level data are maintained
205 // separately for each thread.
206 class InternalThread {
207 public:
208 InternalThread(Thread *thread);
209 InternalThread(const std::string &name, const std::string &sync_name = "Main");
210
211 WPT(Thread) _thread;
212 std::string _name;
213 std::string _sync_name;
214 PStatFrameData _frame_data;
215 bool _is_active;
216 int _frame_number;
217 double _next_packet;
218
219 bool _thread_active;
220 BitArray _active_collectors; // no longer used.
221
222 // This mutex is used to protect writes to _frame_data for this particular
223 // thread, as well as writes to the _per_thread data for this particular
224 // thread in the Collector class, above.
225 LightMutex _thread_lock;
226 };
227 typedef InternalThread *ThreadPointer;
228 AtomicAdjust::Pointer _threads; // ThreadPointer *_threads;
229 AtomicAdjust::Integer _threads_size; // size of the allocated array
230 AtomicAdjust::Integer _num_threads; // number of in-use elements within the array
231
232 mutable PStatClientImpl *_impl;
233
234 static PStatCollector _heap_total_size_pcollector;
235 static PStatCollector _heap_overhead_size_pcollector;
236 static PStatCollector _heap_single_size_pcollector;
237 static PStatCollector _heap_single_other_size_pcollector;
238 static PStatCollector _heap_array_size_pcollector;
239 static PStatCollector _heap_array_other_size_pcollector;
240 static PStatCollector _heap_external_size_pcollector;
241 static PStatCollector _mmap_size_pcollector;
242
243 static PStatCollector _mmap_nf_unused_size_pcollector;
244 static PStatCollector _mmap_dc_active_other_size_pcollector;
245 static PStatCollector _mmap_dc_inactive_other_size_pcollector;
246 static PStatCollector _pstats_pcollector;
247 static PStatCollector _clock_wait_pcollector;
248 static PStatCollector _clock_busy_wait_pcollector;
249 static PStatCollector _thread_block_pcollector;
250
251 static PStatClient *_global_pstats;
252
253 friend class Collector;
254 friend class PStatCollector;
255 friend class PStatThread;
256 friend class PStatClientImpl;
257 friend class GraphicsStateGuardian;
258
259 friend class Extension<PStatClient>;
260};
261
262#include "pStatClient.I"
263
264#else // DO_PSTATS
265
266 class EXPCL_PANDA_PSTATCLIENT PStatClient {
267 public:
268 PStatClient() { }
269 ~PStatClient() { }
270
271 void set_client_name(const std::string &name);
272 std::string get_client_name() const;
273 void set_max_rate(double rate);
274 double get_max_rate() const;
275
276 PStatCollector get_collector(int index) const;
277 std::string get_collector_name(int index) const;
278 std::string get_collector_fullname(int index) const;
279
280 INLINE int get_num_threads() const { return 0; }
281 PStatThread get_thread(int index) const;
282 INLINE std::string get_thread_name(int index) const { return ""; }
283 INLINE std::string get_thread_sync_name(int index) const { return ""; }
284 INLINE PT(Thread) get_thread_object(int index) const { return nullptr; }
285
286 PStatThread get_main_thread() const;
287 PStatThread get_current_thread() const;
288
289 double get_real_time() const;
290
291PUBLISHED:
292 INLINE static bool connect(const std::string & = std::string(), int = -1) { return false; }
293 INLINE static void disconnect() { }
294 INLINE static bool is_connected() { return false; }
295 INLINE static void resume_after_pause() { }
296
297 static void main_tick();
298 static void thread_tick(const std::string &);
299
300 public:
301 void client_main_tick();
302 void client_thread_tick(const std::string &sync_name);
303 bool client_connect(std::string hostname, int port);
304 void client_disconnect();
305 bool client_is_connected() const;
306
307 void client_resume_after_pause();
308
309 static PStatClient *get_global_pstats();
310
311 private:
312 // These are used by inline PStatCollector methods, so they need to be
313 // stubbed out for ABI compatibility.
314 PStatCollector make_collector_with_relname(int parent_index, std::string relname);
315 PStatThread make_thread(Thread *thread);
316
317 bool is_active(int collector_index, int thread_index) const;
318 bool is_started(int collector_index, int thread_index) const;
319
320 void start(int collector_index, int thread_index);
321 void start(int collector_index, int thread_index, double as_of);
322 void stop(int collector_index, int thread_index);
323 void stop(int collector_index, int thread_index, double as_of);
324
325 void clear_level(int collector_index, int thread_index);
326 void set_level(int collector_index, int thread_index, double level);
327 void add_level(int collector_index, int thread_index, double increment);
328 double get_level(int collector_index, int thread_index) const;
329};
330
331#endif // DO_PSTATS
332
333#endif
atomicAdjust.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
bitArray.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
GraphicsStateGuardian
Encapsulates all the communication with a particular instance of a given rendering backend.
Definition graphicsStateGuardian.h:65
PStatClient
Manages the communications to report statistics via a network connection to a remote PStatServer.
Definition pStatClient.h:266
PStatClient::get_thread_sync_name
std::string get_thread_sync_name(int index) const
Returns the sync_name of the indicated thread.
Definition pStatClient.h:283
PStatClient::resume_after_pause
static void resume_after_pause()
Resumes the PStatClient after the simulation has been paused for a while.
Definition pStatClient.h:295
PStatClient::disconnect
static void disconnect()
Closes the connection previously established.
Definition pStatClient.h:293
PStatClient::get_num_threads
int get_num_threads() const
Returns the total number of threads the Client knows about.
Definition pStatClient.h:280
PStatClient::is_connected
static bool is_connected()
Returns true if the client believes it is connected to a working PStatServer, false otherwise.
Definition pStatClient.h:294
PStatClient::get_thread_name
std::string get_thread_name(int index) const
Returns the name of the indicated thread.
Definition pStatClient.h:282
PStatCollectorDef
Defines the details about the Collectors: the name, the suggested color, etc.
Definition pStatCollectorDef.h:29
PStatCollector
A lightweight class that represents a single element that may be timed and/or counted via stats.
Definition pStatCollector.h:43
PStatThread
A lightweight class that represents a single thread of execution to PStats.
Definition pStatThread.h:28
Thread::PStatsCallback
Definition thread.h:123
Thread::PStatsCallback::deactivate_hook
virtual void deactivate_hook(Thread *thread)
Called when the thread is deactivated (swapped for another running thread).
Definition thread.cxx:247
Thread::PStatsCallback::activate_hook
virtual void activate_hook(Thread *thread)
Called when the thread is activated (resumes execution).
Definition thread.cxx:256
Thread
A thread; that is, a lightweight process.
Definition thread.h:46
Thread::get_main_thread
get_main_thread
Returns a pointer to the "main" Thread object–this is the Thread that started the whole process.
Definition thread.h:107
Thread::start
bool start(ThreadPriority priority, bool joinable)
Starts the thread executing.
Definition thread.cxx:184
Thread::get_current_thread
get_current_thread
Returns a pointer to the currently-executing Thread object.
Definition thread.h:109
Thread::is_started
is_started
Returns true if the thread has been started, false if it has not, or if join() has already been calle...
Definition thread.h:116
extension.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
lightMutexHolder.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
lightMutex.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
is_connected
bool is_connected(MObject &node, const string &attribute_name)
Returns true if the named connection exists on the node and is connected to anything,...
Definition maya_funcs.cxx:72
numeric_types.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
pStatClient.I
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
pStatCollectorDef.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
pStatFrameData.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
pandabase.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
pmap.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
reMutexHolder.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
reMutex.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
thread.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
vector_int.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
weakPointerTo.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.