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 =========================================================================*/
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/types.h> // Needed for Win32 implementation of timer
39 #include <sys/timeb.h> // Needed for Win32 implementation of timer
40 #else
41 #include <time.h> // Needed for unix implementation of timer
42 #include <sys/time.h> // Needed for unix implementation of timer
43 #include <sys/types.h> // Needed for unix implementation of timer
44 #include <sys/times.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  {}
83 };
84 
85 class VTKCOMMONSYSTEM_EXPORT vtkTimerLog : public vtkObject
86 {
87 public:
88  static vtkTimerLog *New();
89 
90  vtkTypeMacro(vtkTimerLog,vtkObject);
91  void PrintSelf(ostream& os, vtkIndent indent) override;
92 
97  static void SetLogging(int v) {vtkTimerLog::Logging = v;}
98  static int GetLogging() {return vtkTimerLog::Logging;}
99  static void LoggingOn() {vtkTimerLog::SetLogging(1);}
101 
103 
106  static void SetMaxEntries(int a);
107  static int GetMaxEntries();
109 
114  static void FormatAndMarkEvent(const char *EventString, ...);
115 
117 
121  static void DumpLog(const char *filename);
123 
125 
130  static void MarkStartEvent(const char *EventString);
131  static void MarkEndEvent(const char *EventString);
133 
135 
139  static void InsertTimedEvent(
140  const char *EventString, double time, int cpuTicks);
142 
143  static void DumpLogWithIndents(ostream *os, double threshold);
144  static void DumpLogWithIndentsAndPercentages(ostream *os);
145 
147 
150  static int GetNumberOfEvents();
151  static int GetEventIndent(int i);
152  static double GetEventWallTime(int i);
153  static const char* GetEventString(int i);
154  static vtkTimerLogEntry::LogEntryType GetEventType(int i);
156 
160  static void MarkEvent(const char *EventString);
161 
166  static void ResetLog();
167 
169 
173  static void AllocateLog();
175 
179  static void CleanupLog();
180 
185  static double GetUniversalTime();
186 
191  static double GetCPUTime();
192 
196  void StartTimer();
197 
201  void StopTimer();
202 
207  double GetElapsedTime();
208 
209 protected:
210  vtkTimerLog() {this->StartTime=0; this->EndTime = 0;}; //insure constructor/destructor protected
211  ~vtkTimerLog() override { };
212 
213  static int Logging;
214  static int Indent;
215  static int MaxEntries;
216  static int NextEntry;
217  static int WrapFlag;
218  static int TicksPerSecond;
219  static std::vector<vtkTimerLogEntry> TimerLog;
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(
240  const char *EventString, vtkTimerLogEntry::LogEntryType type,
241  vtkTimerLogEntry* entry = nullptr);
242 
243  // instance variables to support simple timing functionality,
244  // separate from timer table logging.
245  double StartTime;
246  double EndTime;
247 
248  static vtkTimerLogEntry* GetEvent(int i);
249 
250  static void DumpEntry(ostream& os, int index, double time, double deltatime,
251  int tick, int deltatick, const char *event);
252 
253 private:
254  vtkTimerLog(const vtkTimerLog&) = delete;
255  void operator=(const vtkTimerLog&) = delete;
256 };
257 
262 {
263 public:
264  vtkTimerLogScope(const char* eventString)
265  {
266  if (eventString)
267  {
268  this->EventString = eventString;
269  }
270  vtkTimerLog::MarkStartEvent(eventString);
271  }
272 
274  {
275  vtkTimerLog::MarkEndEvent(this->EventString.c_str());
276  };
277 
278 protected:
280 private:
281  vtkTimerLogScope(const vtkTimerLogScope&) = delete;
282  void operator=(const vtkTimerLogScope&) = delete;
283 };
284 
285 //
286 // Set built-in type. Creates member Set"name"() (e.g., SetVisibility());
287 //
288 #define vtkTimerLogMacro(string) \
289  { \
290  vtkTimerLog::FormatAndMarkEvent("Mark: In %s, line %d, class %s: %s", \
291  __FILE__, __LINE__, this->GetClassName(), string); \
292  }
293 
294 #endif
vtkTimerLog::GetLogging
static int GetLogging()
Definition: vtkTimerLog.h:98
vtkTimerLog::LoggingOn
static void LoggingOn()
Definition: vtkTimerLog.h:99
vtkTimerLogEntry::START
Definition: vtkTimerLog.h:72
vtkX3D::type
Definition: vtkX3D.h:516
vtkTimerLog::TimerLog
static std::vector< vtkTimerLogEntry > TimerLog
Definition: vtkTimerLog.h:219
vtkObject::New
static vtkObject * New()
Create an object with Debug turned off, modified time initialized to zero, and reference counting on.
vtkTimerLog::Indent
static int Indent
Definition: vtkTimerLog.h:214
vtkTimerLogEntry
Definition: vtkTimerLog.h:66
vtkObject
abstract base class for most VTK objects
Definition: vtkObject.h:53
vtkTimerLogEntry::INSERTED
Definition: vtkTimerLog.h:74
vtkTimerLog::vtkTimerLog
vtkTimerLog()
Definition: vtkTimerLog.h:210
vtkX3D::time
Definition: vtkX3D.h:497
vtkTimerLog::~vtkTimerLog
~vtkTimerLog() override
Definition: vtkTimerLog.h:211
vtkTimerLog::StartTime
double StartTime
Definition: vtkTimerLog.h:245
vtkTimerLogEntry::Event
std::string Event
Definition: vtkTimerLog.h:78
vtkTimerLogEntry::END
Definition: vtkTimerLog.h:73
vtkTimerLog::FirstCpuTicks
static tms FirstCpuTicks
Definition: vtkTimerLog.h:232
vtkTimerLogEntry::INVALID
Definition: vtkTimerLog.h:70
vtkTimerLogScope::EventString
std::string EventString
Definition: vtkTimerLog.h:276
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:216
vtkTimerLogScope::~vtkTimerLogScope
~vtkTimerLogScope()
Definition: vtkTimerLog.h:273
vtkTimerLog::Logging
static int Logging
Definition: vtkTimerLog.h:211
vtkTimerLog::WrapFlag
static int WrapFlag
Definition: vtkTimerLog.h:217
vtkIndent
a simple class to control print indentation
Definition: vtkIndent.h:33
vtkTimerLogScope::vtkTimerLogScope
vtkTimerLogScope(const char *eventString)
Definition: vtkTimerLog.h:264
vtkTimerLog::LoggingOff
static void LoggingOff()
Definition: vtkTimerLog.h:100
vtkObject::PrintSelf
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
vtkTimerLog::CurrentWallTime
static timeval CurrentWallTime
Definition: vtkTimerLog.h:231
vtkTimerLogScope
Helper class to log time within scope.
Definition: vtkTimerLog.h:261
vtkTimerLogEntry::WallTime
double WallTime
Definition: vtkTimerLog.h:76
vtkTimerLogEntry::Indent
unsigned char Indent
Definition: vtkTimerLog.h:80
vtkObject.h
vtkTimerLogEntry::Type
LogEntryType Type
Definition: vtkTimerLog.h:79
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:97
vtkTimerLogEntry::STANDALONE
Definition: vtkTimerLog.h:71
vtkX3D::string
Definition: vtkX3D.h:490
vtkTimerLog
Timer support and logging.
Definition: vtkTimerLog.h:85
vtkTimerLog::TicksPerSecond
static int TicksPerSecond
Definition: vtkTimerLog.h:218
vtkTimerLog::FirstWallTime
static timeval FirstWallTime
Definition: vtkTimerLog.h:230
vtkTimerLog::MarkEndEvent
static void MarkEndEvent(const char *EventString)
vtkTimerLogEntry::CpuTicks
int CpuTicks
Definition: vtkTimerLog.h:77
vtkTimerLog::EndTime
double EndTime
Definition: vtkTimerLog.h:246
vtkTimerLog::CurrentCpuTicks
static tms CurrentCpuTicks
Definition: vtkTimerLog.h:233
vtkTimerLog::MaxEntries
static int MaxEntries
Definition: vtkTimerLog.h:215
vtkX3D::index
Definition: vtkX3D.h:246
vtkTimerLogEntry::vtkTimerLogEntry
vtkTimerLogEntry()
Definition: vtkTimerLog.h:81