VTK  9.2.6
vtkTimerLog.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkTimerLog.h
5 
6  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7  All rights reserved.
8  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10  This software is distributed WITHOUT ANY WARRANTY; without even
11  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12  PURPOSE. See the above copyright notice for more information.
13 
14 =========================================================================*/
43 #ifndef vtkTimerLog_h
44 #define vtkTimerLog_h
45 
46 #include "vtkCommonSystemModule.h" // For export macro
47 #include "vtkObject.h"
48 
49 #include <string> // STL Header
50 
51 #ifdef _WIN32
52 #include <sys/timeb.h> // Needed for Win32 implementation of timer
53 #include <sys/types.h> // Needed for Win32 implementation of timer
54 #else
55 #include <sys/time.h> // Needed for unix implementation of timer
56 #include <sys/times.h> // Needed for unix implementation of timer
57 #include <sys/types.h> // Needed for unix implementation of timer
58 #include <time.h> // Needed for unix implementation of timer
59 #endif
60 
61 // var args
62 #ifndef _WIN32
63 #include <unistd.h> // Needed for unix implementation of timer
64 #endif
65 
66 // select stuff here is for sleep method
67 #ifndef NO_FD_SET
68 #define SELECT_MASK fd_set
69 #else
70 #ifndef _AIX
71 typedef long fd_mask;
72 #endif
73 #if defined(_IBMR2)
74 #define SELECT_MASK void
75 #else
76 #define SELECT_MASK int
77 #endif
78 #endif
79 
81 {
83  {
84  INVALID = -1,
85  STANDALONE, // an individual, marked event
86  START, // start of a timed event
87  END, // end of a timed event
88  INSERTED // externally timed value
89  };
90  double WallTime;
91  int CpuTicks;
94  unsigned char Indent;
96  : WallTime(0)
97  , CpuTicks(0)
98  , Type(INVALID)
99  , Indent(0)
100  {
101  }
102 };
103 
104 class VTKCOMMONSYSTEM_EXPORT vtkTimerLog : public vtkObject
105 {
106 public:
107  static vtkTimerLog* New();
108 
109  vtkTypeMacro(vtkTimerLog, vtkObject);
110  void PrintSelf(ostream& os, vtkIndent indent) override;
111 
116  static void SetLogging(int v) { vtkTimerLog::Logging = v; }
117  static int GetLogging() { return vtkTimerLog::Logging; }
118  static void LoggingOn() { vtkTimerLog::SetLogging(1); }
119  static void LoggingOff() { vtkTimerLog::SetLogging(0); }
120 
122 
125  static void SetMaxEntries(int a);
126  static int GetMaxEntries();
128 
133 #ifndef __VTK_WRAP__
134  static void FormatAndMarkEvent(const char* format, ...) VTK_FORMAT_PRINTF(1, 2);
135 #endif
136 
138 
142  static void DumpLog(VTK_FILEPATH const char* filename);
144 
146 
151  static void MarkStartEvent(const char* EventString);
152  static void MarkEndEvent(const char* EventString);
154 
156 
160  static void InsertTimedEvent(const char* EventString, double time, int cpuTicks);
162 
163  static void DumpLogWithIndents(ostream* os, double threshold);
164  static void DumpLogWithIndentsAndPercentages(ostream* os);
165 
167 
170  static int GetNumberOfEvents();
171  static int GetEventIndent(int i);
172  static double GetEventWallTime(int i);
173  static const char* GetEventString(int i);
176 
180  static void MarkEvent(const char* EventString);
181 
186  static void ResetLog();
187 
191  static void CleanupLog();
192 
197  static double GetUniversalTime();
198 
203  static double GetCPUTime();
204 
208  void StartTimer();
209 
213  void StopTimer();
214 
219  double GetElapsedTime();
220 
221 protected:
223  {
224  this->StartTime = 0;
225  this->EndTime = 0;
226  } // ensure constructor/destructor protected
227  ~vtkTimerLog() override = default;
228 
229  static int Logging;
230  static int Indent;
231  static int MaxEntries;
232  static int NextEntry;
233  static int WrapFlag;
234  static int TicksPerSecond;
235 
236 #ifdef _WIN32
237 #ifndef _WIN32_WCE
238  static timeb FirstWallTime;
239  static timeb CurrentWallTime;
240 #else
241  static FILETIME FirstWallTime;
242  static FILETIME CurrentWallTime;
243 #endif
244 #else
245  static timeval FirstWallTime;
246  static timeval CurrentWallTime;
247  static tms FirstCpuTicks;
248  static tms CurrentCpuTicks;
249 #endif
250 
254  static void MarkEventInternal(const char* EventString, vtkTimerLogEntry::LogEntryType type,
255  vtkTimerLogEntry* entry = nullptr);
256 
257  // instance variables to support simple timing functionality,
258  // separate from timer table logging.
259  double StartTime;
260  double EndTime;
261 
262  static vtkTimerLogEntry* GetEvent(int i);
263 
264  static void DumpEntry(ostream& os, int index, double time, double deltatime, int tick,
265  int deltatick, const char* event);
266 
267 private:
268  vtkTimerLog(const vtkTimerLog&) = delete;
269  void operator=(const vtkTimerLog&) = delete;
270 };
271 
276 {
277 public:
278  vtkTimerLogScope(const char* eventString)
279  {
280  if (eventString)
281  {
282  this->EventString = eventString;
283  }
284  vtkTimerLog::MarkStartEvent(eventString);
285  }
286 
288 
289 protected:
291 
292 private:
293  vtkTimerLogScope(const vtkTimerLogScope&) = delete;
294  void operator=(const vtkTimerLogScope&) = delete;
295 };
296 
297 //
298 // Set built-in type. Creates member Set"name"() (e.g., SetVisibility());
299 //
300 #define vtkTimerLogMacro(string) \
301  { \
302  vtkTimerLog::FormatAndMarkEvent( \
303  "Mark: In %s, line %d, class %s: %s", __FILE__, __LINE__, this->GetClassName(), string); \
304  }
305 
306 // Implementation detail for Schwarz counter idiom.
307 class VTKCOMMONSYSTEM_EXPORT vtkTimerLogCleanup
308 {
309 public:
312 
313 private:
314  vtkTimerLogCleanup(const vtkTimerLogCleanup&) = delete;
315  void operator=(const vtkTimerLogCleanup&) = delete;
316 };
318 
319 #endif
a simple class to control print indentation
Definition: vtkIndent.h:49
abstract base class for most VTK objects
Definition: vtkObject.h:72
Helper class to log time within scope.
Definition: vtkTimerLog.h:276
vtkTimerLogScope(const char *eventString)
Definition: vtkTimerLog.h:278
std::string EventString
Definition: vtkTimerLog.h:287
Timer support and logging.
Definition: vtkTimerLog.h:105
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:248
static vtkTimerLogEntry::LogEntryType GetEventType(int i)
Programmatic access to events.
double StartTime
Definition: vtkTimerLog.h:259
static int Logging
Definition: vtkTimerLog.h:229
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:232
static timeval CurrentWallTime
Definition: vtkTimerLog.h:246
static int WrapFlag
Definition: vtkTimerLog.h:233
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:234
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:231
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:116
static int GetLogging()
Definition: vtkTimerLog.h:117
static timeval FirstWallTime
Definition: vtkTimerLog.h:245
static tms FirstCpuTicks
Definition: vtkTimerLog.h:247
static void LoggingOn()
Definition: vtkTimerLog.h:118
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:230
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:260
static void DumpEntry(ostream &os, int index, double time, double deltatime, int tick, int deltatick, const char *event)
static void LoggingOff()
Definition: vtkTimerLog.h:119
@ time
Definition: vtkX3D.h:503
@ type
Definition: vtkX3D.h:522
@ index
Definition: vtkX3D.h:252
@ string
Definition: vtkX3D.h:496
unsigned char Indent
Definition: vtkTimerLog.h:94
LogEntryType Type
Definition: vtkTimerLog.h:93
std::string Event
Definition: vtkTimerLog.h:92
static vtkTimerLogCleanup vtkTimerLogCleanupInstance
Definition: vtkTimerLog.h:317
#define VTK_FILEPATH