HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_PerfMonStats.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: UT_PerfMonStats.h (UT Library, C++)
7  *
8  * COMMENTS:
9  *
10  * Collection of performance monitor statistics
11  * for a particular object.
12  */
13 
14 #ifndef __UT_PerfMonStats__
15 #define __UT_PerfMonStats__
16 
17 #include "UT_API.h"
18 
19 #include "UT_JSONWriter.h"
20 #include "UT_ValArray.h"
21 #include "UT_String.h"
22 #include "UT_Pair.h"
23 #include "UT_StringArray.h"
24 #include "UT_StringMap.h"
25 #include "UT_PerfMonTypes.h"
26 
28 class UT_PerfMonStats;
33 
34 static const int UT_PERFMON_STAT_WALL_TIME = 0;
35 
36 /// Definition of a collected statistic.
38 {
39 public:
42  const char *name,
43  UT_PerfMonCategory category,
45  bool is_diff=false);
48 
49  const UT_StringHolder &name() const { return myName; }
50  UT_PerfMonCategory category() const;
51  UT_PerfMonStatType type() const;
52  bool isDiff() const;
53  void setName(const char *name);
54  void setCategory(UT_PerfMonCategory category);
55  void setType(UT_PerfMonStatType type);
56  void setIsDiff(bool is_diff);
57  void save(UT_JSONWriter &writer) const;
58 
59  bool operator==(const UT_PerfMonStatDefinition &def) const;
60 
61  /// Return the name used for the total time stats.
62  static const char *totalTimeStatName();
63 
64  /// Return the name used for the memory growth stats.
65  static const char *memoryGrowthStatName();
66 
67  static void getRootStatName(
68  const UT_PerfMonStatDefinition &def,
69  UT_WorkBuffer &root_stat_name);
70  static void getSelfStatName(
71  const char *root_stat_name,
72  UT_WorkBuffer &self_stat_name);
73  static void getCountStatName(
74  const char *root_stat_name,
75  UT_WorkBuffer &count_stat_name);
76  static void getAverageStatName(
77  const char *root_stat_name,
78  UT_WorkBuffer &avg_stat_name);
79  static void getMinStatName(
80  const char *root_stat_name,
81  UT_WorkBuffer &min_stat_name);
82  static void getMaxStatName(
83  const char *root_stat_name,
84  UT_WorkBuffer &max_stat_name);
85  static void getDiffStatName(
86  const char *src_stat_name,
87  UT_WorkBuffer &diff_stat_name);
88 
89 private:
90 
91  /// Strip out the given suffix from the stat name
92  /// and store the result in `result`.
93  static void stripStatSuffix_(
94  const char *stat_name,
95  const char *stat_suffix,
96  UT_WorkBuffer &result);
97 
98 
99  UT_StringHolder myName;
100  UT_PerfMonCategory myCategory;
101  UT_PerfMonStatType myType;
102  bool myIsDiff;
103 };
104 
105 /// A collection of statistics for a particular object (i.e. node, viewport).
107 {
108 public:
109  UT_PerfMonStats();
110  UT_PerfMonStats(const UT_PerfMonStats *stats1,
111  const UT_PerfMonStats *stats2 = NULL,
112  const char* title1 = NULL,
113  const char* title2 = NULL);
115  const char *name, const char *path, const char *icon,
117  ~UT_PerfMonStats();
118 
119  /// Return the name of the object that the stats apply to.
120  const char *name() const;
121 
122  /// Set the name of the object that the stats apply to.
123  void setName(const char *name);
124 
125  /// Return the full path of the object that the stats apply to.
126  const char *path() const;
127 
128  /// Set the path of the object that the stats apply to.
129  void setPath(const char *path);
130 
131  /// Return extra information about this stats object.
132  const char *extraInfo() const;
133 
134  /// Set extra information pertaining to this stats object.
135  void setExtraInfo(const char *extra_info);
136 
137  /// Return the type of the object that the stats apply to.
138  UT_PerfMonObjectType type() const;
139 
140  /// Set the type of the object that the stats apply to.
141  void setType(UT_PerfMonObjectType type);
142 
143  /// Return the icon name associated with the collection of stats.
144  /// Return NULL if no such icon exists.
145  const char *icon() const;
146 
147  /// Set the object icon.
148  void setIcon(const char *icon);
149 
150  /// Return true if the icon has been set.
151  /// Note that this does not mean that the icon is not NULL,
152  /// just that the object's icon has been determined.
153  bool isIconSet() const;
154 
155  /// Set the parent stat object.
156  void setParent(UT_PerfMonStats *parent);
157 
158  /// Return the object that this object is parented to.
159  /// Return NULL if this is the root object.
160  UT_PerfMonStats *parent() const;
161 
162  /// Append a child stat object.
163  void appendChild(UT_PerfMonStats *child);
164 
165  /// Return a list of the object's children.
166  const UT_PerfMonStatsList &children() const;
167 
168  /// Return a list of stat names that are tracked by this object.
169  const UT_StringArray &stats() const;
170 
171  /// Return true if the object contains the given stat.
172  /// Return false otherwise.
173  bool hasStat(const char *stat_name) const;
174 
175  /// Return the value of the given stat.
176  /// Return -1.0 if the stat does not exist.
177  /// Always call hasStat() before value().
178 
179  /// This method will always return the value over all threads and frames
180  fpreal value(const UT_StringRef &stat_name) const;
181 
182  /// This method returns the values over the given frame range and thread id
183  fpreal value(
184  const char *stat_name,
185  int first_frame,
186  int last_frame,
187  int thread_id = UT_PERFMON_STAT_WALL_TIME)
188  const;
189 
190  /// Return a list of values separated by thread.
191  UT_ValArray<UT_Pair<int, fpreal> > threadedValue(
192  const char* stat_name,
193  int first_frame= UT_PERFORMANCE_INVALID_FRAME,
194  int last_frame = UT_PERFORMANCE_INVALID_FRAME) const;
195 
196  /// Sum the value for the given stat name and thread id for ALL frames.
197  fpreal valueForThread(const char *stat_name,
198  int thread_id) const;
199 
200  /// Set the specified stat to the given value.
201  void setValue(
202  const UT_PerfMonStatDefinition *stat_def,
203  int thread_id,
204  int frame,
205  fpreal stat_value);
206 
207  /// Set the specified stat to the given value.
208  /// This method should be called for stats that are independent
209  /// of frames and threads (i.e. avg, min, max).
210  void setValue(
211  const UT_PerfMonStatDefinition *stat_def,
212  fpreal stat_value);
213 
214  /// Add the given value to the current value of the specified stat.
215  /// Use this method if you want to keep track of delta values that
216  /// can be used later to generate statistics in updateStatsFromDeltas().
217  /// Otherwise, call setValue() instead.
218  void addToValue(
219  const UT_PerfMonStatDefinition *stat_def,
220  int thread_id,
221  int frame,
222  fpreal stat_value);
223 
224  /// Return the normalized percentage of the given time value
225  /// when compared to the maximum time value reported by the
226  /// stat object's children.
227  /// Return 0 if the normalized percentage cannot be calculated.
228  fpreal normalizedPercentage(
229  const char *time_stat_name,
230  fpreal time_value) const;
231 
232  /// Save the stats using the specified JSON writer.
233  void save(UT_JSONWriter &writer) const;
234 
235  /// Update the cumulative and total time stats using the time deltas.
236  /// This method is intended to be called only from UT_PerfMonProfile
237  /// when it is updating the profile stats.
238  void updateStatsFromDeltas();
239 
240  /// Return true if the given stat object is an artificial object,
241  /// that is, it is not really an object (like a node or a viewport)
242  /// but can be attached to a real object. For example, '{drawlist}'
243  /// is an artificial stat object that is a part of either a node,
244  /// a viewport or on its own.
245  static bool isArtificialStatObject(const char *stat_obj_path);
246 
247  /// Convert the given stat object name to one that uses notation
248  /// that denotes that it is an artificial object.
249  static void convertToArtificialStatObjectName(
250  UT_WorkBuffer &artificial_obj_name,
251  const char *stat_obj_name);
252 
253  /// Convert the given stat object name to one that
254  /// does not have notation which denotes it as an artificial object.
255  static void convertToRealStatObjectName(
256  UT_String &artificial_stat_obj_name);
257 
258  /// Build a stat object path using the given real object path
259  /// and artificial object name.
260  static void buildObjectPath(
261  UT_WorkBuffer &path,
262  const char *real_object_path,
263  const char *artificial_object_name);
264 
265  /// Split the given object path into its real object path
266  /// and its artificial name. If the path does not have a real object path,
267  /// then `real_object_path.isstring()` will be false.
268  /// Likewise, if the path does not have an artificial
269  /// name, then `artificial_object_name.isstring()` will be false.
270  static void splitObjectPath(
271  const char *path,
272  UT_String &real_object_path,
273  UT_String &artificial_object_name);
274 
275  /// Split the given object path into its parent path
276  /// and object name. If the path does not have a parent,
277  /// then `parent_path.isstring()` will be false.
278  static void splitParentPath(
279  const char *path,
280  UT_String &parent_path,
282 
283  /// Return the title for the root statistics.
284  static const char *rootStatsTitle();
285 
286  /// Return the title for the unaccounted statistics.
287  static const char *unaccountedStatsTitle();
288 
289 private:
290 
291  // Simple structure that stores a change for a time stat.
292  struct ut_StatDelta
293  {
294  ut_StatDelta()
295  {
296  statDef = NULL;
297  threadId = 0;
298  frameNum = 0;
299  deltaValue = 0.0;
300  }
301 
302  ut_StatDelta(
303  const UT_PerfMonStatDefinition *stat_def, int thread_id,
304  int frame_num, fpreal delta_value)
305  {
306  statDef = stat_def;
307  threadId = thread_id;
308  frameNum = frame_num;
309  deltaValue = delta_value;
310  }
311 
312  ut_StatDelta(const ut_StatDelta &src)
313  {
314  statDef = src.statDef;
315  threadId = src.threadId;
316  frameNum = src.frameNum;
317  deltaValue = src.deltaValue;
318  }
319 
320  const UT_PerfMonStatDefinition *statDef;
321  int threadId;
322  int frameNum;
323  fpreal deltaValue;
324  };
325 
326  /// Set the specified global stat to the given value.
327  void setGlobalValue_(const UT_StringHolder &stat_name,
328  fpreal stat_value);
329 
330  /// Set the specified stat to the given value.
331  /// Replace = false adds on to any existing value
332  void setValue_(const UT_StringHolder &stat_name,
333  int thread_id, int frame,
334  fpreal stat_value, bool replace = true);
335 
336  /// Save the global stat values to the given JSON writer.
337  void saveGlobalValues_(UT_JSONWriter &writer) const;
338 
339  /// Save the stat values to the given JSON writer.
340  void saveValues_(UT_JSONWriter &writer) const;
341 
342  /// Helper method for updateStatsFromDeltas().
343  /// Update stats with the specified time delta value.
344  void updateStatsFromTimeDelta_(
345  const ut_StatDelta *delta,
346  const char *total_time_stat_name,
347  const char *total_self_time_stat_name,
348  UT_StringArray &avg_time_stats_to_update,
349  UT_StringArray &max_stats_to_update);
350 
351  /// Helper method for updateStatsFromDeltas().
352  /// Update stats with the specified memory delta value.
353  void updateStatsFromMemoryDelta_(
354  const ut_StatDelta *delta,
355  UT_StringArray &max_stats_to_update);
356 
357  /// Helper method for updateStatsFromDeltas().
358  /// Update the average times for the given list of stats.
359  void updateAverageTimeStats_(
360  const UT_StringArray &stat_names);
361 
362  /// Helper method for updateStatsFromDeltas().
363  /// Update the maximum child stat values on the parent
364  /// for the given list of stats.
365  void updateParentMaxChildStats_(
366  const UT_StringArray &stat_names);
367 
368  /// Helper method for updateStatsFromDeltas().
369  /// Update the object's maximum child stat value for the given stat
370  /// using the suggested new maximum value. Do nothing if the
371  /// suggested value is less than or equal to the object's current
372  /// maximum value.
373  void updateMaxChildStat_(
374  const UT_StringHolder &stat_name,
375  fpreal suggested_max_value);
376 
377  UT_String myName;
378  UT_String myPath;
379  UT_String myIcon;
380  bool myIsIconSet;
381  UT_PerfMonStats *myParent;
382  UT_PerfMonStatsList myChildren;
383  UT_String myExtraInfo;
384 
385  /// myStats is a three level hash table.
386  /// The first level is indexed by Thread ID
387  /// The second level is indexed by Frame Number
388  /// The third level is indexed by Stat Name
392  ThreadHashTable myStats;
393 
394  /// myGlobalStats contains the values for statistics
395  /// that are independent of frames and threads (i.e. avg, min, max).
396  StatHashTable myGlobalStats;
397 
398  /// myMaxChildStats contains the largest values for statistics
399  /// recorded by the children. This is useful when calculating
400  /// an object's stat value as a normalized percentage.
401  StatHashTable myMaxChildStats;
402 
403  UT_StringArray myStatNames;
404  UT_PerfMonObjectType myType;
405 
406  /// List of stat value changes that have to be propagated
407  /// to the object's ancestors.
408  UT_ValArray<ut_StatDelta> myStatDeltas;
409 };
410 
411 #endif
412 
UT_ValArray< UT_PerfMonStats * > UT_PerfMonStatsList
UT_ValArray< UT_PerfMonStatDefinition * > UT_PerfMonStatDefinitions
Unsorted map container.
Definition: UT_Map.h:83
GLsizei const GLchar *const * path
Definition: glcorearb.h:3340
Definition of a collected statistic.
#define UT_API
Definition: UT_API.h:13
Class which writes ASCII or binary JSON streams.
Definition: UT_JSONWriter.h:32
bool operator==(const BaseDimensions< T > &a, const BaseDimensions< Y > &b)
Definition: Dimensions.h:137
UT_PerfMonObjectType
Object types.
UT_PerfMonStatType
UT_PerfMonCategory
Categories.
GT_API const UT_StringHolder object_name
GLuint const GLchar * name
Definition: glcorearb.h:785
A collection of statistics for a particular object (i.e. node, viewport).
GLsizei const GLfloat * value
Definition: glcorearb.h:823
double fpreal
Definition: SYS_Types.h:270
UT_ValArray< const UT_PerfMonStats * > UT_ConstPerfMonStatsList
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glcorearb.h:107
const UT_StringHolder & name() const
GLenum src
Definition: glcorearb.h:1792