Panda3D
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 
33 class PStatClientImpl;
34 class PStatCollector;
35 class PStatCollectorDef;
36 class PStatThread;
38 
39 /**
40  * Manages the communications to report statistics via a network connection to
41  * a remote PStatServer.
42  *
43  * Normally, there is only one PStatClient in the world, although it is
44  * possible to have multiple PStatClients if extraordinary circumstances
45  * require in. Since each PStatCollector registers itself with the
46  * PStatClient when it is created, having multiple PStatClients requires
47  * special care when constructing the various PStatCollectors.
48  *
49  * If DO_PSTATS is not defined, we don't want to use stats at all. This class
50  * is therefore defined as a stub class.
51  */
52 #ifdef DO_PSTATS
53 class EXPCL_PANDA_PSTATCLIENT PStatClient : public Thread::PStatsCallback {
54 public:
55  PStatClient();
56  ~PStatClient();
57 
58 PUBLISHED:
59  void set_client_name(const std::string &name);
60  std::string get_client_name() const;
61  void set_max_rate(double rate);
62  double get_max_rate() const;
63 
64  INLINE int get_num_collectors() const;
65  PStatCollector get_collector(int index) const;
66  MAKE_SEQ(get_collectors, get_num_collectors, get_collector);
67  INLINE PStatCollectorDef *get_collector_def(int index) const;
68  std::string get_collector_name(int index) const;
69  std::string get_collector_fullname(int index) const;
70 
71  INLINE int get_num_threads() const;
72  PStatThread get_thread(int index) const;
73  MAKE_SEQ(get_threads, get_num_threads, get_thread);
74  INLINE std::string get_thread_name(int index) const;
75  INLINE std::string get_thread_sync_name(int index) const;
76  INLINE PT(Thread) get_thread_object(int index) const;
77 
78  PStatThread get_main_thread() const;
79  PStatThread get_current_thread() const;
80 
81  double get_real_time() const;
82 
83  MAKE_PROPERTY(client_name, get_client_name, set_client_name);
84  MAKE_PROPERTY(max_rate, get_max_rate, set_max_rate);
85  MAKE_SEQ_PROPERTY(collectors, get_num_collectors, get_collector);
86  MAKE_SEQ_PROPERTY(threads, get_num_threads, get_thread);
87  MAKE_PROPERTY(main_thread, get_main_thread);
88  MAKE_PROPERTY(current_thread, get_current_thread);
89  MAKE_PROPERTY(real_time, get_real_time);
90 
91  INLINE static bool connect(const std::string &hostname = std::string(), int port = -1);
92  INLINE static void disconnect();
93  INLINE static bool is_connected();
94 
95  INLINE static void resume_after_pause();
96 
97  static void main_tick();
98  static void thread_tick(const std::string &sync_name);
99 
100  void client_main_tick();
101  void client_thread_tick(const std::string &sync_name);
102  bool client_connect(std::string hostname, int port);
103  void client_disconnect();
104  bool client_is_connected() const;
105 
106  void client_resume_after_pause();
107 
108  static PStatClient *get_global_pstats();
109 
110 private:
111  INLINE bool has_impl() const;
112  INLINE PStatClientImpl *get_impl();
113  INLINE const PStatClientImpl *get_impl() const;
114  void make_impl() const;
115 
116  PStatCollector make_collector_with_relname(int parent_index, std::string relname);
117  PStatCollector make_collector_with_name(int parent_index, const std::string &name);
118  PStatThread do_get_current_thread() const;
119  PStatThread make_thread(Thread *thread);
120  PStatThread do_make_thread(Thread *thread);
121  PStatThread make_gpu_thread(const std::string &name);
122 
123  bool is_active(int collector_index, int thread_index) const;
124  bool is_started(int collector_index, int thread_index) const;
125 
126  void start(int collector_index, int thread_index);
127  void start(int collector_index, int thread_index, double as_of);
128  void stop(int collector_index, int thread_index);
129  void stop(int collector_index, int thread_index, double as_of);
130 
131  void clear_level(int collector_index, int thread_index);
132  void set_level(int collector_index, int thread_index, double level);
133  void add_level(int collector_index, int thread_index, double increment);
134  double get_level(int collector_index, int thread_index) const;
135 
136  static void start_clock_wait();
137  static void start_clock_busy_wait();
138  static void stop_clock_wait();
139 
140  class Collector;
141  class InternalThread;
142  void add_collector(Collector *collector);
143  void add_thread(InternalThread *thread);
144 
145  INLINE Collector *get_collector_ptr(int collector_index) const;
146  INLINE InternalThread *get_thread_ptr(int thread_index) const;
147 
148  virtual void deactivate_hook(Thread *thread);
149  virtual void activate_hook(Thread *thread);
150 
151 private:
152  // This mutex protects everything in this class.
153  ReMutex _lock;
154 
155  typedef pmap<std::string, int> ThingsByName;
156  typedef pmap<std::string, vector_int> MultiThingsByName;
157  MultiThingsByName _threads_by_name, _threads_by_sync_name;
158 
159  // This is for the data that is per-collector, per-thread. A vector of
160  // these is stored in each Collector object, below, indexed by thread index.
161  class PerThreadData {
162  public:
163  PerThreadData();
164  bool _has_level;
165  double _level;
166  int _nested_count;
167  };
168  typedef pvector<PerThreadData> PerThread;
169 
170  // This is where the meat of the Collector data is stored. (All the stuff
171  // in PStatCollector and PStatCollectorDef is just fluff.)
172  class EXPCL_PANDA_PSTATCLIENT Collector {
173  public:
174  INLINE Collector(int parent_index, const std::string &name);
175  INLINE int get_parent_index() const;
176  INLINE const std::string &get_name() const;
177  INLINE bool is_active() const;
178  INLINE PStatCollectorDef *get_def(const PStatClient *client, int this_index) const;
179 
180  private:
181  void make_def(const PStatClient *client, int this_index);
182 
183  private:
184  // This pointer is initially NULL, and will be filled in when it is first
185  // needed.
186  PStatCollectorDef *_def;
187 
188  // This data is used to create the PStatCollectorDef when it is needed.
189  int _parent_index;
190  std::string _name;
191 
192  public:
193  // Relations to other collectors.
194  ThingsByName _children;
195  PerThread _per_thread;
196  };
197  typedef Collector *CollectorPointer;
198  AtomicAdjust::Pointer _collectors; // CollectorPointer *_collectors;
199  AtomicAdjust::Integer _collectors_size; // size of the allocated array
200  AtomicAdjust::Integer _num_collectors; // number of in-use elements within the array
201 
202  // This defines a single thread, i.e. a separate chain of execution,
203  // independent of all other threads. Timing and level data are maintained
204  // separately for each thread.
205  class InternalThread {
206  public:
207  InternalThread(Thread *thread);
208  InternalThread(const std::string &name, const std::string &sync_name = "Main");
209 
210  WPT(Thread) _thread;
211  std::string _name;
212  std::string _sync_name;
213  PStatFrameData _frame_data;
214  bool _is_active;
215  int _frame_number;
216  double _next_packet;
217 
218  bool _thread_active;
219  BitArray _active_collectors; // no longer used.
220 
221  // This mutex is used to protect writes to _frame_data for this particular
222  // thread, as well as writes to the _per_thread data for this particular
223  // thread in the Collector class, above.
224  LightMutex _thread_lock;
225  };
226  typedef InternalThread *ThreadPointer;
227  AtomicAdjust::Pointer _threads; // ThreadPointer *_threads;
228  AtomicAdjust::Integer _threads_size; // size of the allocated array
229  AtomicAdjust::Integer _num_threads; // number of in-use elements within the array
230 
231  mutable PStatClientImpl *_impl;
232 
233  static PStatCollector _heap_total_size_pcollector;
234  static PStatCollector _heap_overhead_size_pcollector;
235  static PStatCollector _heap_single_size_pcollector;
236  static PStatCollector _heap_single_other_size_pcollector;
237  static PStatCollector _heap_array_size_pcollector;
238  static PStatCollector _heap_array_other_size_pcollector;
239  static PStatCollector _heap_external_size_pcollector;
240  static PStatCollector _mmap_size_pcollector;
241 
242  static PStatCollector _mmap_nf_unused_size_pcollector;
243  static PStatCollector _mmap_dc_active_other_size_pcollector;
244  static PStatCollector _mmap_dc_inactive_other_size_pcollector;
245  static PStatCollector _pstats_pcollector;
246  static PStatCollector _clock_wait_pcollector;
247  static PStatCollector _clock_busy_wait_pcollector;
248  static PStatCollector _thread_block_pcollector;
249 
250  static PStatClient *_global_pstats;
251 
252  friend class Collector;
253  friend class PStatCollector;
254  friend class PStatThread;
255  friend class PStatClientImpl;
256  friend class GraphicsStateGuardian;
257 };
258 
259 #include "pStatClient.I"
260 
261 #else // DO_PSTATS
262 
263 class EXPCL_PANDA_PSTATCLIENT PStatClient {
264 public:
265  PStatClient() { }
266  ~PStatClient() { }
267 
268 PUBLISHED:
269  INLINE static bool connect(const std::string & = std::string(), int = -1) { return false; }
270  INLINE static void disconnect() { }
271  INLINE static bool is_connected() { return false; }
272  INLINE static void resume_after_pause() { }
273 
274  INLINE static void main_tick() { }
275  INLINE static void thread_tick(const std::string &) { }
276 };
277 
278 #endif // DO_PSTATS
279 
280 #endif
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This is our own Panda specialization on the default STL vector.
Definition: pvector.h:42
A dynamic array with an unlimited number of bits.
Definition: bitArray.h:39
virtual void activate_hook(Thread *thread)
Called when the thread is activated (resumes execution).
Definition: thread.cxx:256
A lightweight class that represents a single element that may be timed and/or counted via stats.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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
Contains the raw timing and level data for a single frame.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
A lightweight class that represents a single thread of execution to PStats.
Definition: pStatThread.h:28
A thread; that is, a lightweight process.
Definition: thread.h:46
Encapsulates all the communication with a particular instance of a given rendering backend.
Defines the details about the Collectors: the name, the suggested color, etc.
This is a standard, non-reentrant mutex, similar to the Mutex class.
Definition: lightMutex.h:39
Manages the communications to report statistics via a network connection to a remote PStatServer.
Definition: pStatClient.h:263
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
virtual void deactivate_hook(Thread *thread)
Called when the thread is deactivated (swapped for another running thread).
Definition: thread.cxx:247
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
A reentrant mutex.
Definition: reMutex.h:32
bool start(ThreadPriority priority, bool joinable)
Starts the thread executing.
Definition: thread.cxx:184