VTK  9.1.0
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 =========================================================================*/
137 #ifndef vtkTimerLog_h
138 #define vtkTimerLog_h
139 
140 #include "vtkCommonSystemModule.h" // For export macro
141 #include "vtkObject.h"
142 
143 #include <string> // STL Header
144 
145 #ifdef _WIN32
146 #include <sys/timeb.h> // Needed for Win32 implementation of timer
147 #include <sys/types.h> // Needed for Win32 implementation of timer
148 #else
149 #include <sys/time.h> // Needed for unix implementation of timer
150 #include <sys/times.h> // Needed for unix implementation of timer
151 #include <sys/types.h> // Needed for unix implementation of timer
152 #include <time.h> // Needed for unix implementation of timer
153 #endif
154 
155 // var args
156 #ifndef _WIN32
157 #include <unistd.h> // Needed for unix implementation of timer
158 #endif
159 
160 // select stuff here is for sleep method
161 #ifndef NO_FD_SET
162 #define SELECT_MASK fd_set
163 #else
164 #ifndef _AIX
165 typedef long fd_mask;
166 #endif
167 #if defined(_IBMR2)
168 #define SELECT_MASK void
169 #else
170 #define SELECT_MASK int
171 #endif
172 #endif
173 
175 {
177  {
178  INVALID = -1,
179  STANDALONE, // an individual, marked event
180  START, // start of a timed event
181  END, // end of a timed event
182  INSERTED // externally timed value
183  };
184  double WallTime;
185  int CpuTicks;
188  unsigned char Indent;
190  : WallTime(0)
191  , CpuTicks(0)
192  , Type(INVALID)
193  , Indent(0)
194  {
195  }
196 };
197 
198 class VTKCOMMONSYSTEM_EXPORT vtkTimerLog : public vtkObject
199 {
200 public:
201  static vtkTimerLog* New();
202 
203  vtkTypeMacro(vtkTimerLog, vtkObject);
204  void PrintSelf(ostream& os, vtkIndent indent) override;
205 
210  static void SetLogging(int v) { vtkTimerLog::Logging = v; }
211  static int GetLogging() { return vtkTimerLog::Logging; }
212  static void LoggingOn() { vtkTimerLog::SetLogging(1); }
213  static void LoggingOff() { vtkTimerLog::SetLogging(0); }
214 
216 
219  static void SetMaxEntries(int a);
220  static int GetMaxEntries();
222 
227 #ifndef __VTK_WRAP__
228  static void FormatAndMarkEvent(const char* format, ...) VTK_FORMAT_PRINTF(1, 2);
229 #endif
230 
232 
236  static void DumpLog(VTK_FILEPATH const char* filename);
238 
240 
245  static void MarkStartEvent(const char* EventString);
246  static void MarkEndEvent(const char* EventString);
248 
250 
254  static void InsertTimedEvent(const char* EventString, double time, int cpuTicks);
256 
257  static void DumpLogWithIndents(ostream* os, double threshold);
258  static void DumpLogWithIndentsAndPercentages(ostream* os);
259 
261 
264  static int GetNumberOfEvents();
265  static int GetEventIndent(int i);
266  static double GetEventWallTime(int i);
267  static const char* GetEventString(int i);
270 
274  static void MarkEvent(const char* EventString);
275 
280  static void ResetLog();
281 
285  static void CleanupLog();
286 
291  static double GetUniversalTime();
292 
297  static double GetCPUTime();
298 
302  void StartTimer();
303 
307  void StopTimer();
308 
313  double GetElapsedTime();
314 
315 protected:
317  {
318  this->StartTime = 0;
319  this->EndTime = 0;
320  } // ensure constructor/destructor protected
321  ~vtkTimerLog() override = default;
322 
323  static int Logging;
324  static int Indent;
325  static int MaxEntries;
326  static int NextEntry;
327  static int WrapFlag;
328  static int TicksPerSecond;
329 
330 #ifdef _WIN32
331 #ifndef _WIN32_WCE
332  static timeb FirstWallTime;
333  static timeb CurrentWallTime;
334 #else
335  static FILETIME FirstWallTime;
336  static FILETIME CurrentWallTime;
337 #endif
338 #else
339  static timeval FirstWallTime;
340  static timeval CurrentWallTime;
341  static tms FirstCpuTicks;
342  static tms CurrentCpuTicks;
343 #endif
344 
348  static void MarkEventInternal(const char* EventString, vtkTimerLogEntry::LogEntryType type,
349  vtkTimerLogEntry* entry = nullptr);
350 
351  // instance variables to support simple timing functionality,
352  // separate from timer table logging.
353  double StartTime;
354  double EndTime;
355 
356  static vtkTimerLogEntry* GetEvent(int i);
357 
358  static void DumpEntry(ostream& os, int index, double time, double deltatime, int tick,
359  int deltatick, const char* event);
360 
361 private:
362  vtkTimerLog(const vtkTimerLog&) = delete;
363  void operator=(const vtkTimerLog&) = delete;
364 };
365 
370 {
371 public:
372  vtkTimerLogScope(const char* eventString)
373  {
374  if (eventString)
375  {
376  this->EventString = eventString;
377  }
378  vtkTimerLog::MarkStartEvent(eventString);
379  }
380 
382 
383 protected:
385 
386 private:
387  vtkTimerLogScope(const vtkTimerLogScope&) = delete;
388  void operator=(const vtkTimerLogScope&) = delete;
389 };
390 
391 //
392 // Set built-in type. Creates member Set"name"() (e.g., SetVisibility());
393 //
394 #define vtkTimerLogMacro(string) \
395  { \
396  vtkTimerLog::FormatAndMarkEvent( \
397  "Mark: In %s, line %d, class %s: %s", __FILE__, __LINE__, this->GetClassName(), string); \
398  }
399 
400 // Implementation detail for Schwarz counter idiom.
401 class VTKCOMMONSYSTEM_EXPORT vtkTimerLogCleanup
402 {
403 public:
406 
407 private:
408  vtkTimerLogCleanup(const vtkTimerLogCleanup&) = delete;
409  void operator=(const vtkTimerLogCleanup&) = delete;
410 };
412 
413 #endif
vtkTimerLog::GetLogging
static int GetLogging()
Definition: vtkTimerLog.h:211
vtkTimerLog::LoggingOn
static void LoggingOn()
Definition: vtkTimerLog.h:212
vtkTimerLog::FormatAndMarkEvent
static void FormatAndMarkEvent(const char *format,...) VTK_FORMAT_PRINTF(1
Record a timing event.
vtkTimerLog::GetCPUTime
static double GetCPUTime()
Returns the CPU time for this process On Win32 platforms this actually returns wall time.
vtkTimerLogEntry::START
@ START
Definition: vtkTimerLog.h:180
vtkTimerLog::GetEventIndent
static int GetEventIndent(int i)
Programmatic access to events.
VTK_FILEPATH
#define VTK_FILEPATH
Definition: vtkWrappingHints.h:46
vtkX3D::type
@ type
Definition: vtkX3D.h:522
vtkTimerLog::StopTimer
void StopTimer()
Sets EndTime to the current time.
vtkTimerLog::DumpLog
static void static void DumpLog(VTK_FILEPATH const char *filename)
Write the timing table out to a file.
vtkTimerLog::Indent
static int Indent
Definition: vtkTimerLog.h:324
vtkTimerLog::InsertTimedEvent
static void InsertTimedEvent(const char *EventString, double time, int cpuTicks)
Insert an event with a known wall time value (in seconds) and cpuTicks.
vtkTimerLog::MarkEventInternal
static void MarkEventInternal(const char *EventString, vtkTimerLogEntry::LogEntryType type, vtkTimerLogEntry *entry=nullptr)
Record a timing event and capture wall time and cpu ticks.
vtkTimerLogEntry
Definition: vtkTimerLog.h:175
vtkObject
abstract base class for most VTK objects
Definition: vtkObject.h:82
vtkTimerLogEntry::INSERTED
@ INSERTED
Definition: vtkTimerLog.h:182
vtkTimerLog::StartTimer
void StartTimer()
Set the StartTime to the current time.
vtkTimerLog::vtkTimerLog
vtkTimerLog()
Definition: vtkTimerLog.h:316
vtkX3D::time
@ time
Definition: vtkX3D.h:503
vtkTimerLog::GetEventWallTime
static double GetEventWallTime(int i)
Programmatic access to events.
vtkTimerLog::StartTime
double StartTime
Definition: vtkTimerLog.h:353
vtkTimerLogEntry::Event
std::string Event
Definition: vtkTimerLog.h:186
vtkTimerLog::New
static vtkTimerLog * New()
vtkTimerLogEntry::END
@ END
Definition: vtkTimerLog.h:181
vtkTimerLog::GetEventType
static vtkTimerLogEntry::LogEntryType GetEventType(int i)
Programmatic access to events.
vtkTimerLog::FirstCpuTicks
static tms FirstCpuTicks
Definition: vtkTimerLog.h:341
vtkTimerLogEntry::INVALID
@ INVALID
Definition: vtkTimerLog.h:178
vtkTimerLogScope::EventString
std::string EventString
Definition: vtkTimerLog.h:381
vtkTimerLog::GetUniversalTime
static double GetUniversalTime()
Returns the elapsed number of seconds since 00:00:00 Coordinated Universal Time (UTC),...
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:326
vtkTimerLog::DumpLogWithIndents
static void DumpLogWithIndents(ostream *os, double threshold)
vtkTimerLog::GetEvent
static vtkTimerLogEntry * GetEvent(int i)
vtkTimerLogScope::~vtkTimerLogScope
~vtkTimerLogScope()
Definition: vtkTimerLog.h:381
vtkTimerLog::Logging
static int Logging
Definition: vtkTimerLog.h:323
vtkTimerLog::WrapFlag
static int WrapFlag
Definition: vtkTimerLog.h:327
vtkIndent
a simple class to control print indentation
Definition: vtkIndent.h:113
vtkTimerLogScope::vtkTimerLogScope
vtkTimerLogScope(const char *eventString)
Definition: vtkTimerLog.h:372
vtkTimerLog::CleanupLog
static void CleanupLog()
Remove timer log.
vtkTimerLog::GetEventString
static const char * GetEventString(int i)
Programmatic access to events.
vtkTimerLog::LoggingOff
static void LoggingOff()
Definition: vtkTimerLog.h:213
vtkTimerLog::CurrentWallTime
static timeval CurrentWallTime
Definition: vtkTimerLog.h:340
vtkTimerLogScope
Helper class to log time within scope.
Definition: vtkTimerLog.h:370
vtkTimerLogCleanup::~vtkTimerLogCleanup
~vtkTimerLogCleanup()
vtkTimerLogCleanupInstance
static vtkTimerLogCleanup vtkTimerLogCleanupInstance
Definition: vtkTimerLog.h:411
vtkTimerLogEntry::WallTime
double WallTime
Definition: vtkTimerLog.h:184
vtkTimerLogEntry::Indent
unsigned char Indent
Definition: vtkTimerLog.h:188
vtkTimerLog::DumpLogWithIndentsAndPercentages
static void DumpLogWithIndentsAndPercentages(ostream *os)
vtkObject.h
vtkTimerLog::MarkEvent
static void MarkEvent(const char *EventString)
Record a timing event and capture wall time and cpu ticks.
vtkTimerLog::GetMaxEntries
static int GetMaxEntries()
Set/Get the maximum number of entries allowed in the timer log.
vtkTimerLogEntry::Type
LogEntryType Type
Definition: vtkTimerLog.h:187
vtkTimerLog::~vtkTimerLog
~vtkTimerLog() override=default
vtkTimerLogEntry::LogEntryType
LogEntryType
Definition: vtkTimerLog.h:177
vtkTimerLog::SetLogging
static void SetLogging(int v)
This flag will turn logging of events off or on.
Definition: vtkTimerLog.h:210
vtkTimerLogEntry::STANDALONE
@ STANDALONE
Definition: vtkTimerLog.h:179
vtkX3D::string
@ string
Definition: vtkX3D.h:496
vtkTimerLog
Timer support and logging.
Definition: vtkTimerLog.h:199
vtkTimerLogCleanup
Definition: vtkTimerLog.h:402
vtkTimerLog::TicksPerSecond
static int TicksPerSecond
Definition: vtkTimerLog.h:328
vtkTimerLog::FirstWallTime
static timeval FirstWallTime
Definition: vtkTimerLog.h:339
vtkTimerLog::GetElapsedTime
double GetElapsedTime()
Returns the difference between StartTime and EndTime as a doubleing point value indicating the elapse...
vtkTimerLog::PrintSelf
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
vtkTimerLog::MarkEndEvent
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.
vtkTimerLog::GetNumberOfEvents
static int GetNumberOfEvents()
Programmatic access to events.
vtkTimerLog::SetMaxEntries
static void SetMaxEntries(int a)
Set/Get the maximum number of entries allowed in the timer log.
vtkTimerLogEntry::CpuTicks
int CpuTicks
Definition: vtkTimerLog.h:185
vtkTimerLog::EndTime
double EndTime
Definition: vtkTimerLog.h:354
vtkTimerLog::CurrentCpuTicks
static tms CurrentCpuTicks
Definition: vtkTimerLog.h:342
vtkTimerLog::MaxEntries
static int MaxEntries
Definition: vtkTimerLog.h:325
vtkX3D::index
@ index
Definition: vtkX3D.h:252
vtkTimerLogCleanup::vtkTimerLogCleanup
vtkTimerLogCleanup()
vtkTimerLogEntry::vtkTimerLogEntry
vtkTimerLogEntry()
Definition: vtkTimerLog.h:189
vtkTimerLog::DumpEntry
static void DumpEntry(ostream &os, int index, double time, double deltatime, int tick, int deltatick, const char *event)
vtkTimerLog::ResetLog
static void ResetLog()
Clear the timing table.