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