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