VTK  9.0.3
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 #include <vector> // STL Header
36 
37 #ifdef _WIN32
38 #include <sys/timeb.h> // Needed for Win32 implementation of timer
39 #include <sys/types.h> // Needed for Win32 implementation of timer
40 #else
41 #include <sys/time.h> // Needed for unix implementation of timer
42 #include <sys/times.h> // Needed for unix implementation of timer
43 #include <sys/types.h> // Needed for unix implementation of timer
44 #include <time.h> // Needed for unix implementation of timer
45 #endif
46 
47 // var args
48 #ifndef _WIN32
49 #include <unistd.h> // Needed for unix implementation of timer
50 #endif
51 
52 // select stuff here is for sleep method
53 #ifndef NO_FD_SET
54 #define SELECT_MASK fd_set
55 #else
56 #ifndef _AIX
57 typedef long fd_mask;
58 #endif
59 #if defined(_IBMR2)
60 #define SELECT_MASK void
61 #else
62 #define SELECT_MASK int
63 #endif
64 #endif
65 
67 {
69  {
70  INVALID = -1,
71  STANDALONE, // an individual, marked event
72  START, // start of a timed event
73  END, // end of a timed event
74  INSERTED // externally timed value
75  };
76  double WallTime;
77  int CpuTicks;
80  unsigned char Indent;
82  : WallTime(0)
83  , CpuTicks(0)
84  , Type(INVALID)
85  , Indent(0)
86  {
87  }
88 };
89 
90 class VTKCOMMONSYSTEM_EXPORT vtkTimerLog : public vtkObject
91 {
92 public:
93  static vtkTimerLog* New();
94 
95  vtkTypeMacro(vtkTimerLog, vtkObject);
96  void PrintSelf(ostream& os, vtkIndent indent) override;
97 
102  static void SetLogging(int v) { vtkTimerLog::Logging = v; }
103  static int GetLogging() { return vtkTimerLog::Logging; }
104  static void LoggingOn() { vtkTimerLog::SetLogging(1); }
105  static void LoggingOff() { vtkTimerLog::SetLogging(0); }
106 
108 
111  static void SetMaxEntries(int a);
112  static int GetMaxEntries();
114 
119 #ifndef __VTK_WRAP__
120  static void FormatAndMarkEvent(const char* EventString, ...) VTK_FORMAT_PRINTF(1, 2);
121 #endif
122 
124 
128  static void DumpLog(const char* filename);
130 
132 
137  static void MarkStartEvent(const char* EventString);
138  static void MarkEndEvent(const char* EventString);
140 
142 
146  static void InsertTimedEvent(const char* EventString, double time, int cpuTicks);
148 
149  static void DumpLogWithIndents(ostream* os, double threshold);
150  static void DumpLogWithIndentsAndPercentages(ostream* os);
151 
153 
156  static int GetNumberOfEvents();
157  static int GetEventIndent(int i);
158  static double GetEventWallTime(int i);
159  static const char* GetEventString(int i);
162 
166  static void MarkEvent(const char* EventString);
167 
172  static void ResetLog();
173 
177  static void CleanupLog();
178 
183  static double GetUniversalTime();
184 
189  static double GetCPUTime();
190 
194  void StartTimer();
195 
199  void StopTimer();
200 
205  double GetElapsedTime();
206 
207 protected:
209  {
210  this->StartTime = 0;
211  this->EndTime = 0;
212  } // insure constructor/destructor protected
213  ~vtkTimerLog() override {}
214 
215  static int Logging;
216  static int Indent;
217  static int MaxEntries;
218  static int NextEntry;
219  static int WrapFlag;
220  static int TicksPerSecond;
221  static std::vector<vtkTimerLogEntry> TimerLog;
222 
223 #ifdef _WIN32
224 #ifndef _WIN32_WCE
225  static timeb FirstWallTime;
226  static timeb CurrentWallTime;
227 #else
228  static FILETIME FirstWallTime;
229  static FILETIME CurrentWallTime;
230 #endif
231 #else
232  static timeval FirstWallTime;
233  static timeval CurrentWallTime;
234  static tms FirstCpuTicks;
235  static tms CurrentCpuTicks;
236 #endif
237 
241  static void MarkEventInternal(const char* EventString, vtkTimerLogEntry::LogEntryType type,
242  vtkTimerLogEntry* entry = nullptr);
243 
244  // instance variables to support simple timing functionality,
245  // separate from timer table logging.
246  double StartTime;
247  double EndTime;
248 
249  static vtkTimerLogEntry* GetEvent(int i);
250 
251  static void DumpEntry(ostream& os, int index, double time, double deltatime, int tick,
252  int deltatick, const char* event);
253 
254 private:
255  vtkTimerLog(const vtkTimerLog&) = delete;
256  void operator=(const vtkTimerLog&) = delete;
257 };
258 
263 {
264 public:
265  vtkTimerLogScope(const char* eventString)
266  {
267  if (eventString)
268  {
269  this->EventString = eventString;
270  }
271  vtkTimerLog::MarkStartEvent(eventString);
272  }
273 
275 
276 protected:
278 
279 private:
280  vtkTimerLogScope(const vtkTimerLogScope&) = delete;
281  void operator=(const vtkTimerLogScope&) = delete;
282 };
283 
284 //
285 // Set built-in type. Creates member Set"name"() (e.g., SetVisibility());
286 //
287 #define vtkTimerLogMacro(string) \
288  { \
289  vtkTimerLog::FormatAndMarkEvent( \
290  "Mark: In %s, line %d, class %s: %s", __FILE__, __LINE__, this->GetClassName(), string); \
291  }
292 
293 #endif
a simple class to control print indentation
Definition: vtkIndent.h:34
abstract base class for most VTK objects
Definition: vtkObject.h:63
Helper class to log time within scope.
Definition: vtkTimerLog.h:263
vtkTimerLogScope(const char *eventString)
Definition: vtkTimerLog.h:265
std::string EventString
Definition: vtkTimerLog.h:274
Timer support and logging.
Definition: vtkTimerLog.h:91
static double GetUniversalTime()
Returns the elapsed number of seconds since 00:00:00 Coordinated Universal Time (UTC),...
static void static void DumpLog(const char *filename)
Write the timing table out to a file.
static tms CurrentCpuTicks
Definition: vtkTimerLog.h:235
static vtkTimerLogEntry::LogEntryType GetEventType(int i)
double StartTime
Definition: vtkTimerLog.h:246
static int Logging
Definition: vtkTimerLog.h:215
static void InsertTimedEvent(const char *EventString, double time, int cpuTicks)
Insert an event with a known wall time value (in seconds) and cpuTicks.
static std::vector< vtkTimerLogEntry > TimerLog
Definition: vtkTimerLog.h:221
static int NextEntry
Definition: vtkTimerLog.h:218
static timeval CurrentWallTime
Definition: vtkTimerLog.h:233
static int WrapFlag
Definition: vtkTimerLog.h:219
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
static double GetEventWallTime(int i)
static int GetNumberOfEvents()
Programmatic access to events.
static int TicksPerSecond
Definition: vtkTimerLog.h:220
static const char * GetEventString(int i)
static void CleanupLog()
Remove timer log.
static void MarkEndEvent(const char *EventString)
static vtkTimerLogEntry * GetEvent(int i)
static void SetMaxEntries(int a)
Set/Get the maximum number of entries allowed in the timer log.
static void FormatAndMarkEvent(const char *EventString,...) VTK_FORMAT_PRINTF(1
Record a timing event.
static int MaxEntries
Definition: vtkTimerLog.h:217
static void ResetLog()
Clear the timing table.
static void DumpLogWithIndentsAndPercentages(ostream *os)
void StopTimer()
Sets EndTime to the current time.
static void SetLogging(int v)
This flag will turn logging of events off or on.
Definition: vtkTimerLog.h:102
static int GetLogging()
Definition: vtkTimerLog.h:103
static timeval FirstWallTime
Definition: vtkTimerLog.h:232
~vtkTimerLog() override
Definition: vtkTimerLog.h:213
static tms FirstCpuTicks
Definition: vtkTimerLog.h:234
static void LoggingOn()
Definition: vtkTimerLog.h:104
static double GetCPUTime()
Returns the CPU time for this process On Win32 platforms this actually returns wall time.
static int GetEventIndent(int i)
static void MarkEvent(const char *EventString)
Record a timing event and capture wall time and cpu ticks.
static int Indent
Definition: vtkTimerLog.h:216
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()
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:247
static void DumpEntry(ostream &os, int index, double time, double deltatime, int tick, int deltatick, const char *event)
static void LoggingOff()
Definition: vtkTimerLog.h:105
@ 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:80
LogEntryType Type
Definition: vtkTimerLog.h:79
std::string Event
Definition: vtkTimerLog.h:78