VTK  9.0.1
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);
160  static vtkTimerLogEntry::LogEntryType GetEventType(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
vtkTimerLog::GetLogging
static int GetLogging()
Definition: vtkTimerLog.h:103
vtkTimerLog::LoggingOn
static void LoggingOn()
Definition: vtkTimerLog.h:104
vtkTimerLogEntry::START
@ START
Definition: vtkTimerLog.h:72
vtkX3D::type
@ type
Definition: vtkX3D.h:522
vtkTimerLog::TimerLog
static std::vector< vtkTimerLogEntry > TimerLog
Definition: vtkTimerLog.h:221
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:216
vtkTimerLogEntry
Definition: vtkTimerLog.h:66
vtkObject
abstract base class for most VTK objects
Definition: vtkObject.h:62
vtkTimerLogEntry::INSERTED
@ INSERTED
Definition: vtkTimerLog.h:74
vtkTimerLog::vtkTimerLog
vtkTimerLog()
Definition: vtkTimerLog.h:208
vtkX3D::time
@ time
Definition: vtkX3D.h:503
vtkTimerLog::~vtkTimerLog
~vtkTimerLog() override
Definition: vtkTimerLog.h:213
vtkTimerLog::StartTime
double StartTime
Definition: vtkTimerLog.h:246
vtkTimerLogEntry::Event
std::string Event
Definition: vtkTimerLog.h:78
vtkTimerLogEntry::END
@ END
Definition: vtkTimerLog.h:73
vtkTimerLog::FirstCpuTicks
static tms FirstCpuTicks
Definition: vtkTimerLog.h:234
vtkTimerLogEntry::INVALID
@ INVALID
Definition: vtkTimerLog.h:70
vtkTimerLogScope::EventString
std::string EventString
Definition: vtkTimerLog.h:274
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:218
vtkTimerLogScope::~vtkTimerLogScope
~vtkTimerLogScope()
Definition: vtkTimerLog.h:274
vtkTimerLog::Logging
static int Logging
Definition: vtkTimerLog.h:215
vtkTimerLog::WrapFlag
static int WrapFlag
Definition: vtkTimerLog.h:219
vtkIndent
a simple class to control print indentation
Definition: vtkIndent.h:33
vtkTimerLogScope::vtkTimerLogScope
vtkTimerLogScope(const char *eventString)
Definition: vtkTimerLog.h:265
vtkTimerLog::LoggingOff
static void LoggingOff()
Definition: vtkTimerLog.h:105
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:233
vtkTimerLogScope
Helper class to log time within scope.
Definition: vtkTimerLog.h:262
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:102
vtkTimerLogEntry::STANDALONE
@ STANDALONE
Definition: vtkTimerLog.h:71
vtkX3D::string
@ string
Definition: vtkX3D.h:496
vtkTimerLog
Timer support and logging.
Definition: vtkTimerLog.h:90
vtkTimerLog::TicksPerSecond
static int TicksPerSecond
Definition: vtkTimerLog.h:220
vtkTimerLog::FirstWallTime
static timeval FirstWallTime
Definition: vtkTimerLog.h:232
vtkTimerLog::MarkEndEvent
static void MarkEndEvent(const char *EventString)
vtkTimerLogEntry::CpuTicks
int CpuTicks
Definition: vtkTimerLog.h:77
vtkTimerLog::EndTime
double EndTime
Definition: vtkTimerLog.h:247
vtkTimerLog::CurrentCpuTicks
static tms CurrentCpuTicks
Definition: vtkTimerLog.h:235
vtkTimerLog::MaxEntries
static int MaxEntries
Definition: vtkTimerLog.h:217
vtkX3D::index
@ index
Definition: vtkX3D.h:252
vtkTimerLogEntry::vtkTimerLogEntry
vtkTimerLogEntry()
Definition: vtkTimerLog.h:81