VTK  9.7.1
vtkTimerLog.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2 // SPDX-License-Identifier: BSD-3-Clause
22 #ifndef vtkTimerLog_h
23 #define vtkTimerLog_h
24 
25 #include "vtkCommonSystemModule.h" // For export macro
26 #include "vtkObject.h"
27 #include "vtkStringFormatter.h" // For vtk::format_to_n
28 
29 #include <string> // STL Header
30 
31 #ifdef _WIN32
32 #include <sys/timeb.h> // Needed for Win32 implementation of timer
33 #include <sys/types.h> // Needed for Win32 implementation of timer
34 #else
35 #include <sys/time.h> // Needed for unix implementation of timer
36 #include <sys/times.h> // Needed for unix implementation of timer
37 #include <sys/types.h> // Needed for unix implementation of timer
38 #include <time.h> // Needed for unix implementation of timer
39 #endif
40 
41 // var args
42 #ifndef _WIN32
43 #include <unistd.h> // Needed for unix implementation of timer
44 #endif
45 
46 // select stuff here is for sleep method
47 #ifndef NO_FD_SET
48 #define SELECT_MASK fd_set
49 #else
50 #ifndef _AIX
51 typedef long fd_mask;
52 #endif
53 #if defined(_IBMR2)
54 #define SELECT_MASK void
55 #else
56 #define SELECT_MASK int
57 #endif
58 #endif
59 
60 VTK_ABI_NAMESPACE_BEGIN
62 {
64  {
65  INVALID = -1,
66  STANDALONE, // an individual, marked event
67  START, // start of a timed event
68  END, // end of a timed event
69  INSERTED // externally timed value
70  };
71  double WallTime;
72  int CpuTicks;
75  unsigned char Indent;
77  : WallTime(0)
78  , CpuTicks(0)
79  , Type(INVALID)
80  , Indent(0)
81  {
82  }
83 };
84 
85 class VTKCOMMONSYSTEM_EXPORT vtkTimerLog : public vtkObject
86 {
87 public:
88  static vtkTimerLog* New();
89 
90  vtkTypeMacro(vtkTimerLog, vtkObject);
91  void PrintSelf(ostream& os, vtkIndent indent) override;
92 
97  static void SetLogging(int v) { vtkTimerLog::Logging = v; }
98  static int GetLogging() { return vtkTimerLog::Logging; }
99  static void LoggingOn() { vtkTimerLog::SetLogging(1); }
100  static void LoggingOff() { vtkTimerLog::SetLogging(0); }
101 
103 
106  static void SetMaxEntries(int a);
107  static int GetMaxEntries();
109 
115 #ifndef __VTK_WRAP__
116  template <typename... T>
117  static void FormatAndMarkEvent(const char* formatArg, T&&... args)
118  {
120  {
121  return;
122  }
123  std::string format = formatArg ? vtk::to_std_format(formatArg) : "";
124  static char event[4096];
125  VTK_FORMAT_IF_ERROR_RETURN(auto result = vtk::format_to_n(event, sizeof(event),
126  vtk::runtime(format), std::forward<T>(args)...);
127  *result.out = '\0', );
129  }
130 #endif
131 
133 
137  static void DumpLog(VTK_FILEPATH const char* filename);
139 
141 
146  static void MarkStartEvent(const char* EventString);
147  static void MarkEndEvent(const char* EventString);
149 
151 
155  static void InsertTimedEvent(const char* EventString, double time, int cpuTicks);
157 
158  static void DumpLogWithIndents(ostream* os, double threshold);
159  static void DumpLogWithIndentsAndPercentages(ostream* os);
160 
162 
165  static int GetNumberOfEvents();
166  static int GetEventIndent(int i);
167  static double GetEventWallTime(int i);
168  static const char* GetEventString(int i);
169  static vtkTimerLogEntry::LogEntryType GetEventType(int i);
171 
175  static void MarkEvent(const char* EventString);
176 
181  static void ResetLog();
182 
186  static void CleanupLog();
187 
192  static double GetUniversalTime();
193 
198  static double GetCPUTime();
199 
203  void StartTimer();
204 
208  void StopTimer();
209 
214  double GetElapsedTime();
215 
216 protected:
218  {
219  this->StartTime = 0;
220  this->EndTime = 0;
221  } // ensure constructor/destructor protected
222  ~vtkTimerLog() override = default;
223 
224  static int Logging;
225  static int Indent;
226  static int MaxEntries;
227  static int NextEntry;
228  static int WrapFlag;
229  static int TicksPerSecond;
230 
231 #ifdef _WIN32
232 #ifndef _WIN32_WCE
233  static timeb FirstWallTime;
234  static timeb CurrentWallTime;
235 #else
236  static FILETIME FirstWallTime;
237  static FILETIME CurrentWallTime;
238 #endif
239 #else
240  static timeval FirstWallTime;
241  static timeval CurrentWallTime;
242  static tms FirstCpuTicks;
243  static tms CurrentCpuTicks;
244 #endif
245 
249  static void MarkEventInternal(const char* EventString, vtkTimerLogEntry::LogEntryType type,
250  vtkTimerLogEntry* entry = nullptr);
251 
252  // instance variables to support simple timing functionality,
253  // separate from timer table logging.
254  double StartTime;
255  double EndTime;
256 
257  static vtkTimerLogEntry* GetEvent(int i);
258 
259  static void DumpEntry(ostream& os, int index, double time, double deltatime, int tick,
260  int deltatick, const char* event);
261 
262 private:
263  vtkTimerLog(const vtkTimerLog&) = delete;
264  void operator=(const vtkTimerLog&) = delete;
265 };
266 
271 {
272 public:
273  vtkTimerLogScope(const char* eventString)
274  {
275  if (eventString)
276  {
277  this->EventString = eventString;
278  }
279  vtkTimerLog::MarkStartEvent(eventString);
280  }
281 
283 
284 protected:
286 
287 private:
288  vtkTimerLogScope(const vtkTimerLogScope&) = delete;
289  void operator=(const vtkTimerLogScope&) = delete;
290 };
291 
292 //
293 // Set built-in type. Creates member Set"name"() (e.g., SetVisibility());
294 //
295 #define vtkTimerLogMacro(string) \
296  { \
297  vtkTimerLog::FormatAndMarkEvent("Mark: In {:s}, line {:s}, class {:s}: {:s}", __FILE__, \
298  __LINE__, this->GetClassName(), string); \
299  }
300 
301 // Implementation detail for Schwarz counter idiom.
302 class VTKCOMMONSYSTEM_EXPORT vtkTimerLogCleanup
303 {
304 public:
307 
308 private:
309  vtkTimerLogCleanup(const vtkTimerLogCleanup&) = delete;
310  void operator=(const vtkTimerLogCleanup&) = delete;
311 };
313 
314 VTK_ABI_NAMESPACE_END
315 #endif
VTKCOMMONCORE_EXPORT std::string to_std_format(const std::string &format)
Convert printf and std::format style format strings.
static int Indent
Definition: vtkTimerLog.h:225
abstract base class for most VTK objects
Definition: vtkObject.h:52
static void FormatAndMarkEvent(const char *formatArg, T &&...args)
Record a timing event.
Definition: vtkTimerLog.h:117
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
static vtkTimerLogCleanup vtkTimerLogCleanupInstance
Definition: vtkTimerLog.h:312
static void LoggingOn()
Definition: vtkTimerLog.h:99
static void MarkEventInternal(const char *EventString, vtkTimerLogEntry::LogEntryType type, vtkTimerLogEntry *entry=nullptr)
Record a timing event and capture wall time and cpu ticks.
std::string EventString
Definition: vtkTimerLog.h:285
Helper class to log time within scope.
Definition: vtkTimerLog.h:270
double StartTime
Definition: vtkTimerLog.h:254
static void MarkStartEvent(const char *EventString)
I want to time events, so I am creating this interface to mark events that have a start and an end...
std::string Event
Definition: vtkTimerLog.h:73
Timer support and logging.
Definition: vtkTimerLog.h:85
static tms FirstCpuTicks
Definition: vtkTimerLog.h:242
static int WrapFlag
Definition: vtkTimerLog.h:228
a simple class to control print indentation
Definition: vtkIndent.h:28
static int NextEntry
Definition: vtkTimerLog.h:227
static timeval CurrentWallTime
Definition: vtkTimerLog.h:241
#define VTK_FORMAT_IF_ERROR_RETURN(formatCommand, returnValue)
static void LoggingOff()
Definition: vtkTimerLog.h:100
vtkTimerLogScope(const char *eventString)
Definition: vtkTimerLog.h:273
static int Logging
Definition: vtkTimerLog.h:224
#define VTK_FILEPATH
unsigned char Indent
Definition: vtkTimerLog.h:75
static void SetLogging(int v)
This flag will turn logging of events off or on.
Definition: vtkTimerLog.h:97
LogEntryType Type
Definition: vtkTimerLog.h:74
static int MaxEntries
Definition: vtkTimerLog.h:226
static int GetLogging()
Definition: vtkTimerLog.h:98
static vtkObject * New()
Create an object with Debug turned off, modified time initialized to zero, and reference counting on...
static int TicksPerSecond
Definition: vtkTimerLog.h:229
static timeval FirstWallTime
Definition: vtkTimerLog.h:240
Optimized C++ utilities for formatting values to strings and files.
static tms CurrentCpuTicks
Definition: vtkTimerLog.h:243
static void MarkEndEvent(const char *EventString)
I want to time events, so I am creating this interface to mark events that have a start and an end...
double EndTime
Definition: vtkTimerLog.h:255