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