HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_PerfMonProfile.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_PerfMonProfile.h (UT Library, C++)
7  *
8  * COMMENTS:
9  *
10  * Performance monitor profile.
11  */
12 
13 #ifndef __UT_PerfMonProfile__
14 #define __UT_PerfMonProfile__
15 
16 #include "UT_API.h"
17 #include "UT_Array.h"
18 #include "UT_Lock.h"
20 #include "UT_PerfMonStats.h"
21 #include "UT_PerfMonTypes.h"
22 #include "UT_String.h"
23 #include "UT_StringMap.h"
24 #include "UT_ValArray.h"
25 #include <UT/UT_Optional.h>
26 #include <SYS/SYS_Types.h>
27 
28 class UT_PerfMonEvent;
30 class UT_StringArray;
31 
33 {
34 public:
35  /// `id` is the unique id generated by the performance monitor for
36  /// this profile. `title` is a user-readable profile title.
37  UT_PerfMonProfile(int id, const char *title,
38  const UT_PerfMonRecordOptions *options = nullptr);
39 
40  /// Copy constructor. Will be slow as it creates a deep copy; only suitable
41  /// for use in tests.
43 
45 
46  /// Assignment operator. Will be slow as it creates a deep copy;
47  /// only suitable for use in tests.
48  UT_PerfMonProfile& operator=(const UT_PerfMonProfile &src);
49 
50  /// ********************************************************
51  /// GENERAL.
52  /// ********************************************************
53 
54  /// Return the profile id that was assigned by the performance monitor.
55  int id() const;
56 
57  /// Return the user-readable profile title.
58  const char *title() const;
59 
60  void uiFriendlyTitle(UT_String &title) const;
61 
62  /// Set the profile title.
63  void setTitle(const char *title);
64 
65  /// ********************************************************
66  /// EVENT RECORDING.
67  /// ********************************************************
68 
69  /// Start recording events.
70  void startRecording();
71 
72  /// Stop event recording.
73  void stopRecording();
74 
75  /// Add the given event to the log.
76  /// The performance monitor is responsible for the deletion of the event.
77  void addEvent(UT_PerfMonEvent *event);
78 
79  /// Collect data from the event. The data contributes to the profile's
80  /// statistics.
81  void collectDataFromEvent(UT_PerfMonEvent *event);
82 
83  /// Return true if the profile is active.
84  /// An active profile is one that is still interested in events
85  /// but may not be recording at the moment (i.e. if it is paused).
86  bool isActive() const;
87 
88  /// Turn on/off event recording.
89  void enableRecording(bool enable);
90 
91  /// Return true if the profile is recording.
92  bool isRecording() const;
93 
94  /// Return the recording options.
95  const UT_PerfMonRecordOptions &recordOptions() const;
96 
97  /// Set the recording options.
98  void setRecordOptions(
99  const UT_PerfMonRecordOptions &options);
100 
101  /// Return the minimum frame recorded
102  int getMinFrame() const;
103 
104  /// Return the maximum frame recorded
105  int getMaxFrame() const;
106 
107  /// Return whether the current profile is a diff profile, for the purposes
108  /// of rendering differently in OPUI
109  bool getIsDiff() const;
110 
111  /// Return a list of stat definitions registered with the profile.
112  /// Return NULL if the profile has no stat definitions.
113  const UT_PerfMonStatDefinitions &statDefinitions() const;
114 
115  /// Return a list of stat names that belong to the given stat category.
116  /// Return NULL if `category` is not a valid enum value.
117  const UT_StringArray *statsByCategory(
118  UT_PerfMonCategory category) const;
119 
120  /// Return the stat definition with the given name.
121  /// Return NULL if no such definition has been registered with the profile.
122  const UT_PerfMonStatDefinition *getStatDefinition(
123  const char *stat_name) const;
124 
125  /// Return the set of stats collected by the profile.
126  /// Return NULL if the profile has not started or if the profile
127  /// is still recording.
128  const UT_PerfMonStats *stats() const;
129 
130  /// Calculate percent changes on a per stat basis.
131  fpreal statsPercentValue(const UT_PerfMonStats* stats,
132  const char *stat_name) const;
133 
134  fpreal statsPercentValue(const UT_PerfMonStats* stats,
135  const char *stat_name,
136  int first_frame,
137  int last_frame,
138  int thread_id = UT_PERFMON_STAT_WALL_TIME) const;
139 
140  fpreal diffStatsPercentValue(const UT_PerfMonStats* stats,
141  const char *stat_name) const;
142 
143  fpreal diffStatsPercentValue(const UT_PerfMonStats* stats,
144  const char *stat_name,
145  int first_frame,
146  int last_frame,
147  int thread_id = UT_PERFMON_STAT_WALL_TIME) const;
148 
149  fpreal getMaxStatValue(const char * stat_name) const;
150 
151  /// Override the generated stats (if any) in the profile
152  /// with the specified stats. The profile takes ownership of
153  /// the stat objects and the stat definitions.
154  /// `min_frame` and `max_frame` describe the frame range that the stats
155  /// reside in.
156  void setStats(
157  UT_PerfMonStats *stats,
158  const UT_PerfMonStatDefinitions &stat_defs,
159  int min_frame, int max_frame);
160 
161  void createDiffStats(const UT_PerfMonProfile *profile1,
162  const UT_PerfMonProfile *profile2);
163 
164  const UT_PerfMonStats *getStatsByPath(const char *path) const;
165 
166  const char *getDiffProfileTitle(int index) const;
167 
168  /// Export the collected stats in CSV format.
169  /// Return true if the export succeeded. Return false otherwise.
170  bool exportStatsAsCSV(const char *file_path) const;
171 
172  /// Clear recorded stats in the profile.
173  void clearStats();
174 
175 private:
176  /// Clear out contents of the profile and delete any objects
177  /// that it owns.
178  void clear_();
179 
180  /// Create a new, empty root stats object.
181  /// The caller of this method takes ownership of the new object.
182  UT_PerfMonStats *newRootStats_() const;
183 
184  /// Helper method for updateStatsFromEvents().
185  /// Update stats from the given frame event.
186  void updateStatsFromTimedFrameEvent_(
187  UT_PerfMonTimedEvent *event);
188 
189  /// Helper method for updateStatsFromEvents().
190  /// Update stats from the given event and its child events.
191  void updateStatsFromTimedEvent_(
192  UT_PerfMonTimedEvent *event);
193 
194  /// Helper method for updateStatsFromEvents().
195  /// Update stats from the given memory event.
196  void updateStatsFromMemoryEvent_(
197  UT_PerfMonEvent *event);
198 
199  /// Return the specified stat object.
200  /// Return NULL if no such object exists.
201  UT_PerfMonStats *getStats_(const char *name,
202  UT_PerfMonObjectType object_type) const;
203 
204  /// Return the specified stat object.
205  /// Create a new object if it does not exist.
206  UT_PerfMonStats *getOrCreateStats_(const char *name,
207  UT_PerfMonObjectType object_type,
208  const char *object_icon);
209 
210  /// Return the specified category stat for the given object type.
211  /// Create a new stat object if it does not exist.
212  UT_PerfMonStats *getOrCreateCategoryStats_(
214 
215  /// Helper method for createDiffStats().
216  /// Create diff stats for the category stats that appear
217  /// in the given 2 source profiles.
218  void createDiffStatsForCategories_(
219  const UT_PerfMonProfile *profile1,
220  const UT_PerfMonProfile *profile2,
221  const char *profile_title1,
222  const char *profile_title2);
223 
224  /// Helper method for createDiffStats().
225  /// Create the diff stats for the given 2 source category stats.
226  /// Register the diff stats with the profile.
227  void createCategoryDiffStats_(
228  const UT_PerfMonStats *src_category_stats1,
229  const UT_PerfMonStats *src_category_stats2,
230  const char *profile_title1,
231  const char *profile_title2);
232 
233  /// Helper method for createDiffStats().
234  /// Return a list of stat objects that need diff stats generated for them.
235  /// Only the stat object's path and type are used from the list.
236  /// Parent stat objects always appear before their children in the list.
237  void getStatsThatNeedDiffing_(
238  const UT_PerfMonProfile *profile1,
239  const UT_PerfMonProfile *profile2,
240  UT_ConstPerfMonStatsList &stats_need_diffing)
241  const;
242 
243  /// Helper method for createDiffStats().
244  /// Create a diff stats for the 2 given sources and register
245  /// the diff stats with the profile.
246  void createDiffStats_(
247  const UT_PerfMonStats *src_stats1,
248  const UT_PerfMonStats *src_stats2,
249  const char *profile_title1,
250  const char *profile_title2);
251 
252  /// Register the stat definitions from the 2 given source profiles.
253  /// The definitions are registered as diff stat definitions.
254  void registerDiffStatDefs_(
255  const UT_PerfMonProfile *src_profile1,
256  const UT_PerfMonProfile *src_profile2);
257 
258  /// Helper method for updateStatsFromEvents().
259  /// Update the collection of statistics with the given event.
260  void updateStatsFromTimedEvent_(
261  UT_PerfMonStats *stats,
262  const char *event_name,
263  UT_PerfMonCategory category,
264  fpreal event_time,
265  int thread_id,
266  int frame);
267 
268  /// Keep a record of largest seconds values for each stat-type across
269  /// all frames. Used for diff stats and diff stat bar rendering.
270  void updateMaxStats_(fpreal val, const char * stat_name);
271 
272  /// Helper function for diffStatsPercentValue
273  void getDiffIndices_(int &index1, int &index2,
274  const UT_PerfMonStats *stats,
275  const char *stat_name) const;
276 
277  /// Register the given stat definition.
278  /// If the name of the stat definition is already registered,
279  /// then don't do anything.
280  /// Returns the registered stat definition.
282  registerStat_(const char *name, UT_PerfMonCategory category,
283  UT_PerfMonStatType type, bool is_diff = false);
284 
285  /// Update the minimum and maximum frames registered in this profile
286  /// after examining the given frame number.
287  void updateMinAndMaxFrames_(int frame);
288 
289  /// Set the minimum frame to the given number.
290  void setMinFrame_(int frame);
291 
292  /// Set the maximum frame to the given number.
293  void setMaxFrame_(int frame);
294 
295  int myId;
296  int myMinFrame;
297  int myMaxFrame;
298  UT_String myTitle;
299  bool myIsActive;
300  bool myIsRecording;
301  bool myIsDiff;
302  UT_PerfMonRecordOptions myRecordOptions;
303  mutable UT_PerfMonStats *myStats;
304  mutable UT_Lock myStatsUpdateLock;
307  // maps UT_Strings to fpreals
308  UT_Optional<UT_PerfMonMaxStatValues> myMaxStatValues;
309  UT_String myDiffProfile0;
310  UT_String myDiffProfile1;
311 
312  // Table of statistics contained within the profile.
313  // Maps stat names to the stat definitions (i.e. name, type, etc.).
315  UT_PerfMonStatDefinitions *myStatDefs;
316 
317  // Table of statistics contained within the profile.
318  // Groups the statistics by category.
319  UT_Array<UT_StringArray> myStatsByCategory;
320 };
321 
323 
324 #endif
GLsizei const GLchar *const * path
Definition: glcorearb.h:3341
Definition of a collected statistic.
#define UT_API
Definition: UT_API.h:14
std::optional< T > UT_Optional
Definition: UT_Optional.h:26
struct _cl_event * event
Definition: glcorearb.h:2961
UT_PerfMonObjectType
Object types.
UT_PerfMonStatType
UT_PerfMonCategory
Categories.
GLuint id
Definition: glcorearb.h:655
GLuint const GLchar * name
Definition: glcorearb.h:786
A collection of statistics for a particular object (i.e. node, viewport).
UT_ValArray< UT_PerfMonProfile * > UT_PerfMonProfileList
fpreal64 fpreal
Definition: SYS_Types.h:277
GLuint index
Definition: glcorearb.h:786
GLuint GLfloat * val
Definition: glcorearb.h:1608
type
Definition: core.h:1059
**Note that the tasks the thread_id
Definition: thread.h:637
GLenum src
Definition: glcorearb.h:1793