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 =========================================================================*/
34 #ifndef vtkTimerLog_h
35 #define vtkTimerLog_h
36 
37 #include "vtkCommonSystemModule.h" // For export macro
38 #include "vtkObject.h"
39 
40 #include <string> // STL Header
41 
42 #ifdef _WIN32
43 #include <sys/timeb.h> // Needed for Win32 implementation of timer
44 #include <sys/types.h> // Needed for Win32 implementation of timer
45 #else
46 #include <sys/time.h> // Needed for unix implementation of timer
47 #include <sys/times.h> // Needed for unix implementation of timer
48 #include <sys/types.h> // Needed for unix implementation of timer
49 #include <time.h> // Needed for unix implementation of timer
50 #endif
51 
52 // var args
53 #ifndef _WIN32
54 #include <unistd.h> // Needed for unix implementation of timer
55 #endif
56 
57 // select stuff here is for sleep method
58 #ifndef NO_FD_SET
59 #define SELECT_MASK fd_set
60 #else
61 #ifndef _AIX
62 typedef long fd_mask;
63 #endif
64 #if defined(_IBMR2)
65 #define SELECT_MASK void
66 #else
67 #define SELECT_MASK int
68 #endif
69 #endif
70 
72 {
74  {
75  INVALID = -1,
76  STANDALONE, // an individual, marked event
77  START, // start of a timed event
78  END, // end of a timed event
79  INSERTED // externally timed value
80  };
81  double WallTime;
82  int CpuTicks;
85  unsigned char Indent;
87  : WallTime(0)
88  , CpuTicks(0)
89  , Type(INVALID)
90  , Indent(0)
91  {
92  }
93 };
94 
95 class VTKCOMMONSYSTEM_EXPORT vtkTimerLog : public vtkObject
96 {
97 public:
98  static vtkTimerLog* New();
99 
100  vtkTypeMacro(vtkTimerLog, vtkObject);
101  void PrintSelf(ostream& os, vtkIndent indent) override;
102 
107  static void SetLogging(int v) { vtkTimerLog::Logging = v; }
108  static int GetLogging() { return vtkTimerLog::Logging; }
109  static void LoggingOn() { vtkTimerLog::SetLogging(1); }
110  static void LoggingOff() { vtkTimerLog::SetLogging(0); }
111 
113 
116  static void SetMaxEntries(int a);
117  static int GetMaxEntries();
119 
124 #ifndef __VTK_WRAP__
125  static void FormatAndMarkEvent(const char* format, ...) VTK_FORMAT_PRINTF(1, 2);
126 #endif
127 
129 
133  static void DumpLog(VTK_FILEPATH const char* filename);
135 
137 
142  static void MarkStartEvent(const char* EventString);
143  static void MarkEndEvent(const char* EventString);
145 
147 
151  static void InsertTimedEvent(const char* EventString, double time, int cpuTicks);
153 
154  static void DumpLogWithIndents(ostream* os, double threshold);
155  static void DumpLogWithIndentsAndPercentages(ostream* os);
156 
158 
161  static int GetNumberOfEvents();
162  static int GetEventIndent(int i);
163  static double GetEventWallTime(int i);
164  static const char* GetEventString(int i);
165  static vtkTimerLogEntry::LogEntryType GetEventType(int i);
167 
171  static void MarkEvent(const char* EventString);
172 
177  static void ResetLog();
178 
182  static void CleanupLog();
183 
188  static double GetUniversalTime();
189 
194  static double GetCPUTime();
195 
199  void StartTimer();
200 
204  void StopTimer();
205 
210  double GetElapsedTime();
211 
212 protected:
214  {
215  this->StartTime = 0;
216  this->EndTime = 0;
217  } // ensure constructor/destructor protected
218  ~vtkTimerLog() override = default;
219 
220  static int Logging;
221  static int Indent;
222  static int MaxEntries;
223  static int NextEntry;
224  static int WrapFlag;
225  static int TicksPerSecond;
226 
227 #ifdef _WIN32
228 #ifndef _WIN32_WCE
229  static timeb FirstWallTime;
230  static timeb CurrentWallTime;
231 #else
232  static FILETIME FirstWallTime;
233  static FILETIME CurrentWallTime;
234 #endif
235 #else
236  static timeval FirstWallTime;
237  static timeval CurrentWallTime;
238  static tms FirstCpuTicks;
239  static tms CurrentCpuTicks;
240 #endif
241 
245  static void MarkEventInternal(const char* EventString, vtkTimerLogEntry::LogEntryType type,
246  vtkTimerLogEntry* entry = nullptr);
247 
248  // instance variables to support simple timing functionality,
249  // separate from timer table logging.
250  double StartTime;
251  double EndTime;
252 
253  static vtkTimerLogEntry* GetEvent(int i);
254 
255  static void DumpEntry(ostream& os, int index, double time, double deltatime, int tick,
256  int deltatick, const char* event);
257 
258 private:
259  vtkTimerLog(const vtkTimerLog&) = delete;
260  void operator=(const vtkTimerLog&) = delete;
261 };
262 
267 {
268 public:
269  vtkTimerLogScope(const char* eventString)
270  {
271  if (eventString)
272  {
273  this->EventString = eventString;
274  }
275  vtkTimerLog::MarkStartEvent(eventString);
276  }
277 
279 
280 protected:
282 
283 private:
284  vtkTimerLogScope(const vtkTimerLogScope&) = delete;
285  void operator=(const vtkTimerLogScope&) = delete;
286 };
287 
288 //
289 // Set built-in type. Creates member Set"name"() (e.g., SetVisibility());
290 //
291 #define vtkTimerLogMacro(string) \
292  { \
293  vtkTimerLog::FormatAndMarkEvent( \
294  "Mark: In %s, line %d, class %s: %s", __FILE__, __LINE__, this->GetClassName(), string); \
295  }
296 
297 // Implementation detail for Schwarz counter idiom.
298 class VTKCOMMONSYSTEM_EXPORT vtkTimerLogCleanup
299 {
300 public:
303 
304 private:
305  vtkTimerLogCleanup(const vtkTimerLogCleanup&) = delete;
306  void operator=(const vtkTimerLogCleanup&) = delete;
307 };
309 
310 #endif
static int Indent
Definition: vtkTimerLog.h:221
abstract base class for most VTK objects
Definition: vtkObject.h:62
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:308
static void LoggingOn()
Definition: vtkTimerLog.h:109
std::string EventString
Definition: vtkTimerLog.h:278
Helper class to log time within scope.
Definition: vtkTimerLog.h:266
double StartTime
Definition: vtkTimerLog.h:250
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:83
Timer support and logging.
Definition: vtkTimerLog.h:95
static tms FirstCpuTicks
Definition: vtkTimerLog.h:238
static int WrapFlag
Definition: vtkTimerLog.h:224
a simple class to control print indentation
Definition: vtkIndent.h:39
static int NextEntry
Definition: vtkTimerLog.h:223
static timeval CurrentWallTime
Definition: vtkTimerLog.h:237
static void LoggingOff()
Definition: vtkTimerLog.h:110
vtkTimerLogScope(const char *eventString)
Definition: vtkTimerLog.h:269
static int Logging
Definition: vtkTimerLog.h:220
#define VTK_FILEPATH
unsigned char Indent
Definition: vtkTimerLog.h:85
static void SetLogging(int v)
This flag will turn logging of events off or on.
Definition: vtkTimerLog.h:107
LogEntryType Type
Definition: vtkTimerLog.h:84
static int MaxEntries
Definition: vtkTimerLog.h:222
static int GetLogging()
Definition: vtkTimerLog.h:108
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:225
static timeval FirstWallTime
Definition: vtkTimerLog.h:236
static tms CurrentCpuTicks
Definition: vtkTimerLog.h:239
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:251