VTK  9.0.20210510
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 =========================================================================*/
28 #ifndef vtkTimerLog_h
29 #define vtkTimerLog_h
30 
31 #include "vtkCommonSystemModule.h" // For export macro
32 #include "vtkObject.h"
33 
34 #include <string> // STL Header
35 
36 #ifdef _WIN32
37 #include <sys/timeb.h> // Needed for Win32 implementation of timer
38 #include <sys/types.h> // Needed for Win32 implementation of timer
39 #else
40 #include <sys/time.h> // Needed for unix implementation of timer
41 #include <sys/times.h> // Needed for unix implementation of timer
42 #include <sys/types.h> // Needed for unix implementation of timer
43 #include <time.h> // Needed for unix implementation of timer
44 #endif
45 
46 // var args
47 #ifndef _WIN32
48 #include <unistd.h> // Needed for unix implementation of timer
49 #endif
50 
51 // select stuff here is for sleep method
52 #ifndef NO_FD_SET
53 #define SELECT_MASK fd_set
54 #else
55 #ifndef _AIX
56 typedef long fd_mask;
57 #endif
58 #if defined(_IBMR2)
59 #define SELECT_MASK void
60 #else
61 #define SELECT_MASK int
62 #endif
63 #endif
64 
66 {
68  {
69  INVALID = -1,
70  STANDALONE, // an individual, marked event
71  START, // start of a timed event
72  END, // end of a timed event
73  INSERTED // externally timed value
74  };
75  double WallTime;
76  int CpuTicks;
79  unsigned char Indent;
81  : WallTime(0)
82  , CpuTicks(0)
83  , Type(INVALID)
84  , Indent(0)
85  {
86  }
87 };
88 
89 class VTKCOMMONSYSTEM_EXPORT vtkTimerLog : public vtkObject
90 {
91 public:
92  static vtkTimerLog* New();
93 
94  vtkTypeMacro(vtkTimerLog, vtkObject);
95  void PrintSelf(ostream& os, vtkIndent indent) override;
96 
101  static void SetLogging(int v) { vtkTimerLog::Logging = v; }
102  static int GetLogging() { return vtkTimerLog::Logging; }
103  static void LoggingOn() { vtkTimerLog::SetLogging(1); }
104  static void LoggingOff() { vtkTimerLog::SetLogging(0); }
105 
107 
110  static void SetMaxEntries(int a);
111  static int GetMaxEntries();
113 
118 #ifndef __VTK_WRAP__
119  static void FormatAndMarkEvent(const char* format, ...) VTK_FORMAT_PRINTF(1, 2);
120 #endif
121 
123 
127  static void DumpLog(const char* filename);
129 
131 
136  static void MarkStartEvent(const char* EventString);
137  static void MarkEndEvent(const char* EventString);
139 
141 
145  static void InsertTimedEvent(const char* EventString, double time, int cpuTicks);
147 
148  static void DumpLogWithIndents(ostream* os, double threshold);
149  static void DumpLogWithIndentsAndPercentages(ostream* os);
150 
152 
155  static int GetNumberOfEvents();
156  static int GetEventIndent(int i);
157  static double GetEventWallTime(int i);
158  static const char* GetEventString(int i);
161 
165  static void MarkEvent(const char* EventString);
166 
171  static void ResetLog();
172 
176  static void CleanupLog();
177 
182  static double GetUniversalTime();
183 
188  static double GetCPUTime();
189 
193  void StartTimer();
194 
198  void StopTimer();
199 
204  double GetElapsedTime();
205 
206 protected:
208  {
209  this->StartTime = 0;
210  this->EndTime = 0;
211  } // ensure constructor/destructor protected
212  ~vtkTimerLog() override = default;
213 
214  static int Logging;
215  static int Indent;
216  static int MaxEntries;
217  static int NextEntry;
218  static int WrapFlag;
219  static int TicksPerSecond;
220 
221 #ifdef _WIN32
222 #ifndef _WIN32_WCE
223  static timeb FirstWallTime;
224  static timeb CurrentWallTime;
225 #else
226  static FILETIME FirstWallTime;
227  static FILETIME CurrentWallTime;
228 #endif
229 #else
230  static timeval FirstWallTime;
231  static timeval CurrentWallTime;
232  static tms FirstCpuTicks;
233  static tms CurrentCpuTicks;
234 #endif
235 
239  static void MarkEventInternal(const char* EventString, vtkTimerLogEntry::LogEntryType type,
240  vtkTimerLogEntry* entry = nullptr);
241 
242  // instance variables to support simple timing functionality,
243  // separate from timer table logging.
244  double StartTime;
245  double EndTime;
246 
247  static vtkTimerLogEntry* GetEvent(int i);
248 
249  static void DumpEntry(ostream& os, int index, double time, double deltatime, int tick,
250  int deltatick, const char* event);
251 
252 private:
253  vtkTimerLog(const vtkTimerLog&) = delete;
254  void operator=(const vtkTimerLog&) = delete;
255 };
256 
261 {
262 public:
263  vtkTimerLogScope(const char* eventString)
264  {
265  if (eventString)
266  {
267  this->EventString = eventString;
268  }
269  vtkTimerLog::MarkStartEvent(eventString);
270  }
271 
273 
274 protected:
276 
277 private:
278  vtkTimerLogScope(const vtkTimerLogScope&) = delete;
279  void operator=(const vtkTimerLogScope&) = delete;
280 };
281 
282 //
283 // Set built-in type. Creates member Set"name"() (e.g., SetVisibility());
284 //
285 #define vtkTimerLogMacro(string) \
286  { \
287  vtkTimerLog::FormatAndMarkEvent( \
288  "Mark: In %s, line %d, class %s: %s", __FILE__, __LINE__, this->GetClassName(), string); \
289  }
290 
291 // Implementation detail for Schwarz counter idiom.
292 class VTKCOMMONSYSTEM_EXPORT vtkTimerLogCleanup
293 {
294 public:
297 
298 private:
299  vtkTimerLogCleanup(const vtkTimerLogCleanup&) = delete;
300  void operator=(const vtkTimerLogCleanup&) = delete;
301 };
303 
304 #endif
vtkTimerLog::GetLogging
static int GetLogging()
Definition: vtkTimerLog.h:102
vtkTimerLog::LoggingOn
static void LoggingOn()
Definition: vtkTimerLog.h:103
vtkTimerLog::FormatAndMarkEvent
static void FormatAndMarkEvent(const char *format,...) VTK_FORMAT_PRINTF(1
Record a timing event.
vtkTimerLog::GetCPUTime
static double GetCPUTime()
Returns the CPU time for this process On Win32 platforms this actually returns wall time.
vtkTimerLogEntry::START
@ START
Definition: vtkTimerLog.h:71
vtkTimerLog::GetEventIndent
static int GetEventIndent(int i)
Programmatic access to events.
vtkX3D::type
@ type
Definition: vtkX3D.h:522
vtkTimerLog::StopTimer
void StopTimer()
Sets EndTime to the current time.
vtkTimerLog::Indent
static int Indent
Definition: vtkTimerLog.h:215
vtkTimerLog::InsertTimedEvent
static void InsertTimedEvent(const char *EventString, double time, int cpuTicks)
Insert an event with a known wall time value (in seconds) and cpuTicks.
vtkTimerLog::MarkEventInternal
static void MarkEventInternal(const char *EventString, vtkTimerLogEntry::LogEntryType type, vtkTimerLogEntry *entry=nullptr)
Record a timing event and capture wall time and cpu ticks.
vtkTimerLogEntry
Definition: vtkTimerLog.h:66
vtkObject
abstract base class for most VTK objects
Definition: vtkObject.h:63
vtkTimerLogEntry::INSERTED
@ INSERTED
Definition: vtkTimerLog.h:73
vtkTimerLog::StartTimer
void StartTimer()
Set the StartTime to the current time.
vtkTimerLog::vtkTimerLog
vtkTimerLog()
Definition: vtkTimerLog.h:207
vtkX3D::time
@ time
Definition: vtkX3D.h:503
vtkTimerLog::GetEventWallTime
static double GetEventWallTime(int i)
Programmatic access to events.
vtkTimerLog::StartTime
double StartTime
Definition: vtkTimerLog.h:244
vtkTimerLogEntry::Event
std::string Event
Definition: vtkTimerLog.h:77
vtkTimerLog::New
static vtkTimerLog * New()
vtkTimerLogEntry::END
@ END
Definition: vtkTimerLog.h:72
vtkTimerLog::GetEventType
static vtkTimerLogEntry::LogEntryType GetEventType(int i)
Programmatic access to events.
vtkTimerLog::FirstCpuTicks
static tms FirstCpuTicks
Definition: vtkTimerLog.h:232
vtkTimerLogEntry::INVALID
@ INVALID
Definition: vtkTimerLog.h:69
vtkTimerLogScope::EventString
std::string EventString
Definition: vtkTimerLog.h:272
vtkTimerLog::GetUniversalTime
static double GetUniversalTime()
Returns the elapsed number of seconds since 00:00:00 Coordinated Universal Time (UTC),...
vtkTimerLog::MarkStartEvent
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.
vtkTimerLog::NextEntry
static int NextEntry
Definition: vtkTimerLog.h:217
vtkTimerLog::DumpLogWithIndents
static void DumpLogWithIndents(ostream *os, double threshold)
vtkTimerLog::GetEvent
static vtkTimerLogEntry * GetEvent(int i)
vtkTimerLogScope::~vtkTimerLogScope
~vtkTimerLogScope()
Definition: vtkTimerLog.h:272
vtkTimerLog::Logging
static int Logging
Definition: vtkTimerLog.h:214
vtkTimerLog::WrapFlag
static int WrapFlag
Definition: vtkTimerLog.h:218
vtkIndent
a simple class to control print indentation
Definition: vtkIndent.h:34
vtkTimerLogScope::vtkTimerLogScope
vtkTimerLogScope(const char *eventString)
Definition: vtkTimerLog.h:263
vtkTimerLog::CleanupLog
static void CleanupLog()
Remove timer log.
vtkTimerLog::GetEventString
static const char * GetEventString(int i)
Programmatic access to events.
vtkTimerLog::LoggingOff
static void LoggingOff()
Definition: vtkTimerLog.h:104
vtkTimerLog::CurrentWallTime
static timeval CurrentWallTime
Definition: vtkTimerLog.h:231
vtkTimerLogScope
Helper class to log time within scope.
Definition: vtkTimerLog.h:261
vtkTimerLogCleanup::~vtkTimerLogCleanup
~vtkTimerLogCleanup()
vtkTimerLogCleanupInstance
static vtkTimerLogCleanup vtkTimerLogCleanupInstance
Definition: vtkTimerLog.h:302
vtkTimerLogEntry::WallTime
double WallTime
Definition: vtkTimerLog.h:75
vtkTimerLogEntry::Indent
unsigned char Indent
Definition: vtkTimerLog.h:79
vtkTimerLog::DumpLogWithIndentsAndPercentages
static void DumpLogWithIndentsAndPercentages(ostream *os)
vtkObject.h
vtkTimerLog::MarkEvent
static void MarkEvent(const char *EventString)
Record a timing event and capture wall time and cpu ticks.
vtkTimerLog::GetMaxEntries
static int GetMaxEntries()
Set/Get the maximum number of entries allowed in the timer log.
vtkTimerLogEntry::Type
LogEntryType Type
Definition: vtkTimerLog.h:78
vtkTimerLog::~vtkTimerLog
~vtkTimerLog() override=default
vtkTimerLogEntry::LogEntryType
LogEntryType
Definition: vtkTimerLog.h:68
vtkTimerLog::SetLogging
static void SetLogging(int v)
This flag will turn logging of events off or on.
Definition: vtkTimerLog.h:101
vtkTimerLogEntry::STANDALONE
@ STANDALONE
Definition: vtkTimerLog.h:70
vtkX3D::string
@ string
Definition: vtkX3D.h:496
vtkTimerLog
Timer support and logging.
Definition: vtkTimerLog.h:90
vtkTimerLogCleanup
Definition: vtkTimerLog.h:293
vtkTimerLog::TicksPerSecond
static int TicksPerSecond
Definition: vtkTimerLog.h:219
vtkTimerLog::DumpLog
static void static void DumpLog(const char *filename)
Write the timing table out to a file.
vtkTimerLog::FirstWallTime
static timeval FirstWallTime
Definition: vtkTimerLog.h:230
vtkTimerLog::GetElapsedTime
double GetElapsedTime()
Returns the difference between StartTime and EndTime as a doubleing point value indicating the elapse...
vtkTimerLog::PrintSelf
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
vtkTimerLog::MarkEndEvent
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.
vtkTimerLog::GetNumberOfEvents
static int GetNumberOfEvents()
Programmatic access to events.
vtkTimerLog::SetMaxEntries
static void SetMaxEntries(int a)
Set/Get the maximum number of entries allowed in the timer log.
vtkTimerLogEntry::CpuTicks
int CpuTicks
Definition: vtkTimerLog.h:76
vtkTimerLog::EndTime
double EndTime
Definition: vtkTimerLog.h:245
vtkTimerLog::CurrentCpuTicks
static tms CurrentCpuTicks
Definition: vtkTimerLog.h:233
vtkTimerLog::MaxEntries
static int MaxEntries
Definition: vtkTimerLog.h:216
vtkX3D::index
@ index
Definition: vtkX3D.h:252
vtkTimerLogCleanup::vtkTimerLogCleanup
vtkTimerLogCleanup()
vtkTimerLogEntry::vtkTimerLogEntry
vtkTimerLogEntry()
Definition: vtkTimerLog.h:80
vtkTimerLog::DumpEntry
static void DumpEntry(ostream &os, int index, double time, double deltatime, int tick, int deltatick, const char *event)
vtkTimerLog::ResetLog
static void ResetLog()
Clear the timing table.