VTK
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 #include <vector> // STL Header
42 
43 #ifdef _WIN32
44 #include <sys/types.h> // Needed for Win32 implementation of timer
45 #include <sys/timeb.h> // Needed for Win32 implementation of timer
46 #else
47 #include <time.h> // Needed for unix implementation of timer
48 #include <sys/time.h> // Needed for unix implementation of timer
49 #include <sys/types.h> // Needed for unix implementation of timer
50 #include <sys/times.h> // Needed for unix implementation of timer
51 #endif
52 
53 // var args
54 #ifndef _WIN32
55 #include <unistd.h> // Needed for unix implementation of timer
56 #endif
57 
58 // select stuff here is for sleep method
59 #ifndef NO_FD_SET
60 # define SELECT_MASK fd_set
61 #else
62 # ifndef _AIX
63  typedef long fd_mask;
64 # endif
65 # if defined(_IBMR2)
66 # define SELECT_MASK void
67 # else
68 # define SELECT_MASK int
69 # endif
70 #endif
71 
73 {
75  {
76  INVALID = -1,
77  STANDALONE, // an individual, marked event
78  START, // start of a timed event
79  END, // end of a timed event
80  INSERTED // externally timed value
81  };
82  double WallTime;
83  int CpuTicks;
86  unsigned char Indent;
87  vtkTimerLogEntry() : WallTime(0), CpuTicks(0), Type(INVALID), Indent(0)
88  {}
89 };
90 
91 class VTKCOMMONSYSTEM_EXPORT vtkTimerLog : public vtkObject
92 {
93 public:
94  static vtkTimerLog *New();
95 
96  vtkTypeMacro(vtkTimerLog,vtkObject);
97  void PrintSelf(ostream& os, vtkIndent indent) override;
98 
103  static void SetLogging(int v) {vtkTimerLog::Logging = v;}
104  static int GetLogging() {return vtkTimerLog::Logging;}
105  static void LoggingOn() {vtkTimerLog::SetLogging(1);}
107 
109 
112  static void SetMaxEntries(int a);
113  static int GetMaxEntries();
115 
120  static void FormatAndMarkEvent(const char *EventString, ...);
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(
146  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);
160  static vtkTimerLogEntry::LogEntryType GetEventType(int i);
162 
166  static void MarkEvent(const char *EventString);
167 
172  static void ResetLog();
173 
175 
179  static void AllocateLog();
181 
185  static void CleanupLog();
186 
191  static double GetUniversalTime();
192 
197  static double GetCPUTime();
198 
202  void StartTimer();
203 
207  void StopTimer();
208 
213  double GetElapsedTime();
214 
215 protected:
216  vtkTimerLog() {this->StartTime=0; this->EndTime = 0;}; //insure constructor/destructor protected
217  ~vtkTimerLog() override { };
218 
219  static int Logging;
220  static int Indent;
221  static int MaxEntries;
222  static int NextEntry;
223  static int WrapFlag;
224  static int TicksPerSecond;
225  static std::vector<vtkTimerLogEntry> TimerLog;
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(
246  const char *EventString, vtkTimerLogEntry::LogEntryType type,
247  vtkTimerLogEntry* entry = nullptr);
248 
249  // instance variables to support simple timing functionality,
250  // separate from timer table logging.
251  double StartTime;
252  double EndTime;
253 
254  static vtkTimerLogEntry* GetEvent(int i);
255 
256  static void DumpEntry(ostream& os, int index, double time, double deltatime,
257  int tick, int deltatick, const char *event);
258 
259 private:
260  vtkTimerLog(const vtkTimerLog&) = delete;
261  void operator=(const vtkTimerLog&) = delete;
262 };
263 
268 {
269 public:
270  vtkTimerLogScope(const char* eventString)
271  {
272  if (eventString)
273  {
274  this->EventString = eventString;
275  }
276  vtkTimerLog::MarkStartEvent(eventString);
277  }
278 
280  {
281  vtkTimerLog::MarkEndEvent(this->EventString.c_str());
282  };
283 
284 protected:
286 private:
287  vtkTimerLogScope(const vtkTimerLogScope&) = delete;
288  void operator=(const vtkTimerLogScope&) = delete;
289 };
290 
291 //
292 // Set built-in type. Creates member Set"name"() (e.g., SetVisibility());
293 //
294 #define vtkTimerLogMacro(string) \
295  { \
296  vtkTimerLog::FormatAndMarkEvent("Mark: In %s, line %d, class %s: %s", \
297  __FILE__, __LINE__, this->GetClassName(), string); \
298  }
299 
300 #endif
static int Indent
Definition: vtkTimerLog.h:220
abstract base class for most VTK objects
Definition: vtkObject.h:59
static std::vector< vtkTimerLogEntry > TimerLog
Definition: vtkTimerLog.h:225
static void LoggingOn()
Definition: vtkTimerLog.h:105
std::string EventString
Definition: vtkTimerLog.h:282
~vtkTimerLog() override
Definition: vtkTimerLog.h:217
Helper class to log time within scope.
Definition: vtkTimerLog.h:267
double StartTime
Definition: vtkTimerLog.h:251
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:84
Timer support and logging.
Definition: vtkTimerLog.h:91
static tms FirstCpuTicks
Definition: vtkTimerLog.h:238
static int WrapFlag
Definition: vtkTimerLog.h:223
a simple class to control print indentation
Definition: vtkIndent.h:39
static int NextEntry
Definition: vtkTimerLog.h:222
static timeval CurrentWallTime
Definition: vtkTimerLog.h:237
static void LoggingOff()
Definition: vtkTimerLog.h:106
vtkTimerLogScope(const char *eventString)
Definition: vtkTimerLog.h:270
static int Logging
Definition: vtkTimerLog.h:217
unsigned char Indent
Definition: vtkTimerLog.h:86
static void SetLogging(int v)
This flag will turn logging of events off or on.
Definition: vtkTimerLog.h:103
LogEntryType Type
Definition: vtkTimerLog.h:85
static int MaxEntries
Definition: vtkTimerLog.h:221
static int GetLogging()
Definition: vtkTimerLog.h:104
static int TicksPerSecond
Definition: vtkTimerLog.h:224
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:252