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;
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
vtkTimerLog::GetLogging
static int GetLogging()
Definition: vtkTimerLog.h:104
vtkTimerLog::LoggingOn
static void LoggingOn()
Definition: vtkTimerLog.h:105
vtkTimerLogEntry::START
Definition: vtkTimerLog.h:78
vtkX3D::type
Definition: vtkX3D.h:516
vtkTimerLog::TimerLog
static std::vector< vtkTimerLogEntry > TimerLog
Definition: vtkTimerLog.h:225
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:220
vtkTimerLogEntry
Definition: vtkTimerLog.h:72
vtkObject
abstract base class for most VTK objects
Definition: vtkObject.h:59
vtkTimerLogEntry::INSERTED
Definition: vtkTimerLog.h:80
vtkTimerLog::vtkTimerLog
vtkTimerLog()
Definition: vtkTimerLog.h:216
vtkX3D::time
Definition: vtkX3D.h:497
vtkTimerLog::~vtkTimerLog
~vtkTimerLog() override
Definition: vtkTimerLog.h:217
vtkTimerLog::StartTime
double StartTime
Definition: vtkTimerLog.h:251
vtkTimerLogEntry::Event
std::string Event
Definition: vtkTimerLog.h:84
vtkTimerLogEntry::END
Definition: vtkTimerLog.h:79
vtkTimerLog::FirstCpuTicks
static tms FirstCpuTicks
Definition: vtkTimerLog.h:238
vtkTimerLogEntry::INVALID
Definition: vtkTimerLog.h:76
vtkTimerLogScope::EventString
std::string EventString
Definition: vtkTimerLog.h:282
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:222
vtkTimerLogScope::~vtkTimerLogScope
~vtkTimerLogScope()
Definition: vtkTimerLog.h:279
vtkTimerLog::Logging
static int Logging
Definition: vtkTimerLog.h:217
vtkTimerLog::WrapFlag
static int WrapFlag
Definition: vtkTimerLog.h:223
vtkIndent
a simple class to control print indentation
Definition: vtkIndent.h:39
vtkTimerLogScope::vtkTimerLogScope
vtkTimerLogScope(const char *eventString)
Definition: vtkTimerLog.h:270
vtkTimerLog::LoggingOff
static void LoggingOff()
Definition: vtkTimerLog.h:106
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:237
vtkTimerLogScope
Helper class to log time within scope.
Definition: vtkTimerLog.h:267
vtkTimerLogEntry::WallTime
double WallTime
Definition: vtkTimerLog.h:82
vtkTimerLogEntry::Indent
unsigned char Indent
Definition: vtkTimerLog.h:86
vtkObject.h
vtkTimerLogEntry::Type
LogEntryType Type
Definition: vtkTimerLog.h:85
vtkTimerLogEntry::LogEntryType
LogEntryType
Definition: vtkTimerLog.h:74
vtkTimerLog::SetLogging
static void SetLogging(int v)
This flag will turn logging of events off or on.
Definition: vtkTimerLog.h:103
vtkTimerLogEntry::STANDALONE
Definition: vtkTimerLog.h:77
vtkX3D::string
Definition: vtkX3D.h:490
vtkTimerLog
Timer support and logging.
Definition: vtkTimerLog.h:91
vtkTimerLog::TicksPerSecond
static int TicksPerSecond
Definition: vtkTimerLog.h:224
vtkTimerLog::FirstWallTime
static timeval FirstWallTime
Definition: vtkTimerLog.h:236
vtkTimerLog::MarkEndEvent
static void MarkEndEvent(const char *EventString)
vtkTimerLogEntry::CpuTicks
int CpuTicks
Definition: vtkTimerLog.h:83
vtkTimerLog::EndTime
double EndTime
Definition: vtkTimerLog.h:252
vtkTimerLog::CurrentCpuTicks
static tms CurrentCpuTicks
Definition: vtkTimerLog.h:239
vtkTimerLog::MaxEntries
static int MaxEntries
Definition: vtkTimerLog.h:221
vtkX3D::index
Definition: vtkX3D.h:246
vtkTimerLogEntry::vtkTimerLogEntry
vtkTimerLogEntry()
Definition: vtkTimerLog.h:87