VTK  9.3.20240327
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
111 #ifndef vtkTimerLog_h
112 #define vtkTimerLog_h
113 
114 #include "vtkCommonSystemModule.h" // For export macro
115 #include "vtkObject.h"
116 
117 #include <string> // STL Header
118 
119 #ifdef _WIN32
120 #include <sys/timeb.h> // Needed for Win32 implementation of timer
121 #include <sys/types.h> // Needed for Win32 implementation of timer
122 #else
123 #include <sys/time.h> // Needed for unix implementation of timer
124 #include <sys/times.h> // Needed for unix implementation of timer
125 #include <sys/types.h> // Needed for unix implementation of timer
126 #include <time.h> // Needed for unix implementation of timer
127 #endif
128 
129 // var args
130 #ifndef _WIN32
131 #include <unistd.h> // Needed for unix implementation of timer
132 #endif
133 
134 // select stuff here is for sleep method
135 #ifndef NO_FD_SET
136 #define SELECT_MASK fd_set
137 #else
138 #ifndef _AIX
139 typedef long fd_mask;
140 #endif
141 #if defined(_IBMR2)
142 #define SELECT_MASK void
143 #else
144 #define SELECT_MASK int
145 #endif
146 #endif
147 
148 VTK_ABI_NAMESPACE_BEGIN
150 {
152  {
153  INVALID = -1,
154  STANDALONE, // an individual, marked event
155  START, // start of a timed event
156  END, // end of a timed event
157  INSERTED // externally timed value
158  };
159  double WallTime;
160  int CpuTicks;
163  unsigned char Indent;
165  : WallTime(0)
166  , CpuTicks(0)
167  , Type(INVALID)
168  , Indent(0)
169  {
170  }
171 };
172 
173 class VTKCOMMONSYSTEM_EXPORT vtkTimerLog : public vtkObject
174 {
175 public:
176  static vtkTimerLog* New();
177 
178  vtkTypeMacro(vtkTimerLog, vtkObject);
179  void PrintSelf(ostream& os, vtkIndent indent) override;
180 
185  static void SetLogging(int v) { vtkTimerLog::Logging = v; }
186  static int GetLogging() { return vtkTimerLog::Logging; }
187  static void LoggingOn() { vtkTimerLog::SetLogging(1); }
188  static void LoggingOff() { vtkTimerLog::SetLogging(0); }
189 
191 
194  static void SetMaxEntries(int a);
195  static int GetMaxEntries();
197 
202 #ifndef __VTK_WRAP__
203  static void FormatAndMarkEvent(const char* format, ...) VTK_FORMAT_PRINTF(1, 2);
204 #endif
205 
207 
211  static void DumpLog(VTK_FILEPATH const char* filename);
213 
215 
220  static void MarkStartEvent(const char* EventString);
221  static void MarkEndEvent(const char* EventString);
223 
225 
229  static void InsertTimedEvent(const char* EventString, double time, int cpuTicks);
231 
232  static void DumpLogWithIndents(ostream* os, double threshold);
233  static void DumpLogWithIndentsAndPercentages(ostream* os);
234 
236 
239  static int GetNumberOfEvents();
240  static int GetEventIndent(int i);
241  static double GetEventWallTime(int i);
242  static const char* GetEventString(int i);
245 
249  static void MarkEvent(const char* EventString);
250 
255  static void ResetLog();
256 
260  static void CleanupLog();
261 
266  static double GetUniversalTime();
267 
272  static double GetCPUTime();
273 
277  void StartTimer();
278 
282  void StopTimer();
283 
288  double GetElapsedTime();
289 
290 protected:
292  {
293  this->StartTime = 0;
294  this->EndTime = 0;
295  } // ensure constructor/destructor protected
296  ~vtkTimerLog() override = default;
297 
298  static int Logging;
299  static int Indent;
300  static int MaxEntries;
301  static int NextEntry;
302  static int WrapFlag;
303  static int TicksPerSecond;
304 
305 #ifdef _WIN32
306 #ifndef _WIN32_WCE
307  static timeb FirstWallTime;
308  static timeb CurrentWallTime;
309 #else
310  static FILETIME FirstWallTime;
311  static FILETIME CurrentWallTime;
312 #endif
313 #else
314  static timeval FirstWallTime;
315  static timeval CurrentWallTime;
316  static tms FirstCpuTicks;
317  static tms CurrentCpuTicks;
318 #endif
319 
323  static void MarkEventInternal(const char* EventString, vtkTimerLogEntry::LogEntryType type,
324  vtkTimerLogEntry* entry = nullptr);
325 
326  // instance variables to support simple timing functionality,
327  // separate from timer table logging.
328  double StartTime;
329  double EndTime;
330 
331  static vtkTimerLogEntry* GetEvent(int i);
332 
333  static void DumpEntry(ostream& os, int index, double time, double deltatime, int tick,
334  int deltatick, const char* event);
335 
336 private:
337  vtkTimerLog(const vtkTimerLog&) = delete;
338  void operator=(const vtkTimerLog&) = delete;
339 };
340 
345 {
346 public:
347  vtkTimerLogScope(const char* eventString)
348  {
349  if (eventString)
350  {
351  this->EventString = eventString;
352  }
353  vtkTimerLog::MarkStartEvent(eventString);
354  }
355 
357 
358 protected:
360 
361 private:
362  vtkTimerLogScope(const vtkTimerLogScope&) = delete;
363  void operator=(const vtkTimerLogScope&) = delete;
364 };
365 
366 //
367 // Set built-in type. Creates member Set"name"() (e.g., SetVisibility());
368 //
369 #define vtkTimerLogMacro(string) \
370  { \
371  vtkTimerLog::FormatAndMarkEvent( \
372  "Mark: In %s, line %d, class %s: %s", __FILE__, __LINE__, this->GetClassName(), string); \
373  }
374 
375 // Implementation detail for Schwarz counter idiom.
376 class VTKCOMMONSYSTEM_EXPORT vtkTimerLogCleanup
377 {
378 public:
381 
382 private:
383  vtkTimerLogCleanup(const vtkTimerLogCleanup&) = delete;
384  void operator=(const vtkTimerLogCleanup&) = delete;
385 };
387 
388 VTK_ABI_NAMESPACE_END
389 #endif
a simple class to control print indentation
Definition: vtkIndent.h:108
abstract base class for most VTK objects
Definition: vtkObject.h:161
Helper class to log time within scope.
Definition: vtkTimerLog.h:345
vtkTimerLogScope(const char *eventString)
Definition: vtkTimerLog.h:347
std::string EventString
Definition: vtkTimerLog.h:359
Timer support and logging.
Definition: vtkTimerLog.h:174
static double GetUniversalTime()
Returns the elapsed number of seconds since 00:00:00 Coordinated Universal Time (UTC),...
static void static void DumpLog(VTK_FILEPATH const char *filename)
Write the timing table out to a file.
static tms CurrentCpuTicks
Definition: vtkTimerLog.h:317
static vtkTimerLogEntry::LogEntryType GetEventType(int i)
Programmatic access to events.
double StartTime
Definition: vtkTimerLog.h:328
static int Logging
Definition: vtkTimerLog.h:298
static void InsertTimedEvent(const char *EventString, double time, int cpuTicks)
Insert an event with a known wall time value (in seconds) and cpuTicks.
static int NextEntry
Definition: vtkTimerLog.h:301
static timeval CurrentWallTime
Definition: vtkTimerLog.h:315
static int WrapFlag
Definition: vtkTimerLog.h:302
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
static double GetEventWallTime(int i)
Programmatic access to events.
~vtkTimerLog() override=default
static int GetNumberOfEvents()
Programmatic access to events.
static int TicksPerSecond
Definition: vtkTimerLog.h:303
static const char * GetEventString(int i)
Programmatic access to events.
static void CleanupLog()
Remove timer log.
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.
static vtkTimerLogEntry * GetEvent(int i)
static void SetMaxEntries(int a)
Set/Get the maximum number of entries allowed in the timer log.
static int MaxEntries
Definition: vtkTimerLog.h:300
static void ResetLog()
Clear the timing table.
static void DumpLogWithIndentsAndPercentages(ostream *os)
void StopTimer()
Sets EndTime to the current time.
static void FormatAndMarkEvent(const char *format,...) VTK_FORMAT_PRINTF(1
Record a timing event.
static void SetLogging(int v)
This flag will turn logging of events off or on.
Definition: vtkTimerLog.h:185
static int GetLogging()
Definition: vtkTimerLog.h:186
static timeval FirstWallTime
Definition: vtkTimerLog.h:314
static tms FirstCpuTicks
Definition: vtkTimerLog.h:316
static void LoggingOn()
Definition: vtkTimerLog.h:187
static double GetCPUTime()
Returns the CPU time for this process On Win32 platforms this actually returns wall time.
static int GetEventIndent(int i)
Programmatic access to events.
static void MarkEvent(const char *EventString)
Record a timing event and capture wall time and cpu ticks.
static int Indent
Definition: vtkTimerLog.h:299
static void MarkEventInternal(const char *EventString, vtkTimerLogEntry::LogEntryType type, vtkTimerLogEntry *entry=nullptr)
Record a timing event and capture wall time and cpu ticks.
static int GetMaxEntries()
Set/Get the maximum number of entries allowed in the timer log.
double GetElapsedTime()
Returns the difference between StartTime and EndTime as a doubleing point value indicating the elapse...
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.
static void DumpLogWithIndents(ostream *os, double threshold)
static vtkTimerLog * New()
void StartTimer()
Set the StartTime to the current time.
double EndTime
Definition: vtkTimerLog.h:329
static void DumpEntry(ostream &os, int index, double time, double deltatime, int tick, int deltatick, const char *event)
static void LoggingOff()
Definition: vtkTimerLog.h:188
@ time
Definition: vtkX3D.h:497
@ type
Definition: vtkX3D.h:516
@ index
Definition: vtkX3D.h:246
@ string
Definition: vtkX3D.h:490
unsigned char Indent
Definition: vtkTimerLog.h:163
LogEntryType Type
Definition: vtkTimerLog.h:162
std::string Event
Definition: vtkTimerLog.h:161
static vtkTimerLogCleanup vtkTimerLogCleanupInstance
Definition: vtkTimerLog.h:386
#define VTK_FILEPATH