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=========================================================================*/
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
36#ifdef _WIN32
37#include <sys/timeb.h> // Needed for Win32 implementation of timer
38#include <sys/types.h> // Needed for Win32 implementation of timer
39#else
40#include <sys/time.h> // Needed for unix implementation of timer
41#include <sys/times.h> // Needed for unix implementation of timer
42#include <sys/types.h> // Needed for unix implementation of timer
43#include <time.h> // Needed for unix implementation of timer
44#endif
45
46// var args
47#ifndef _WIN32
48#include <unistd.h> // Needed for unix implementation of timer
49#endif
50
51// select stuff here is for sleep method
52#ifndef NO_FD_SET
53#define SELECT_MASK fd_set
54#else
55#ifndef _AIX
56typedef long fd_mask;
57#endif
58#if defined(_IBMR2)
59#define SELECT_MASK void
60#else
61#define SELECT_MASK int
62#endif
63#endif
64
66{
68 {
69 INVALID = -1,
70 STANDALONE, // an individual, marked event
71 START, // start of a timed event
72 END, // end of a timed event
73 INSERTED // externally timed value
74 };
75 double WallTime;
77 std::string Event;
79 unsigned char Indent;
81 : WallTime(0)
82 , CpuTicks(0)
83 , Type(INVALID)
84 , Indent(0)
85 {
86 }
87};
88
89class VTKCOMMONSYSTEM_EXPORT vtkTimerLog : public vtkObject
90{
91public:
92 static vtkTimerLog* New();
93
94 vtkTypeMacro(vtkTimerLog, vtkObject);
95 void PrintSelf(ostream& os, vtkIndent indent) override;
96
101 static void SetLogging(int v) { vtkTimerLog::Logging = v; }
102 static int GetLogging() { return vtkTimerLog::Logging; }
103 static void LoggingOn() { vtkTimerLog::SetLogging(1); }
105
107
110 static void SetMaxEntries(int a);
111 static int GetMaxEntries();
113
118#ifndef __VTK_WRAP__
119 static void FormatAndMarkEvent(const char* format, ...) VTK_FORMAT_PRINTF(1, 2);
120#endif
121
123
127 static void DumpLog(VTK_FILEPATH 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(const char* EventString, double time, int cpuTicks);
147
148 static void DumpLogWithIndents(ostream* os, double threshold);
149 static void DumpLogWithIndentsAndPercentages(ostream* os);
150
152
155 static int GetNumberOfEvents();
156 static int GetEventIndent(int i);
157 static double GetEventWallTime(int i);
158 static const char* GetEventString(int i);
161
165 static void MarkEvent(const char* EventString);
166
171 static void ResetLog();
172
176 static void CleanupLog();
177
182 static double GetUniversalTime();
183
188 static double GetCPUTime();
189
194
198 void StopTimer();
199
205
206protected:
208 {
209 this->StartTime = 0;
210 this->EndTime = 0;
211 } // ensure constructor/destructor protected
212 ~vtkTimerLog() override = default;
213
214 static int Logging;
215 static int Indent;
216 static int MaxEntries;
217 static int NextEntry;
218 static int WrapFlag;
219 static int TicksPerSecond;
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(const char* EventString, vtkTimerLogEntry::LogEntryType type,
240 vtkTimerLogEntry* entry = nullptr);
241
242 // instance variables to support simple timing functionality,
243 // separate from timer table logging.
244 double StartTime;
245 double EndTime;
246
248
249 static void DumpEntry(ostream& os, int index, double time, double deltatime, int tick,
250 int deltatick, const char* event);
251
252private:
253 vtkTimerLog(const vtkTimerLog&) = delete;
254 void operator=(const vtkTimerLog&) = delete;
255};
256
261{
262public:
263 vtkTimerLogScope(const char* eventString)
264 {
265 if (eventString)
266 {
267 this->EventString = eventString;
268 }
269 vtkTimerLog::MarkStartEvent(eventString);
270 }
271
273
274protected:
275 std::string EventString;
276
277private:
278 vtkTimerLogScope(const vtkTimerLogScope&) = delete;
279 void operator=(const vtkTimerLogScope&) = delete;
280};
281
282//
283// Set built-in type. Creates member Set"name"() (e.g., SetVisibility());
284//
285#define vtkTimerLogMacro(string) \
286 { \
287 vtkTimerLog::FormatAndMarkEvent( \
288 "Mark: In %s, line %d, class %s: %s", __FILE__, __LINE__, this->GetClassName(), string); \
289 }
290
291// Implementation detail for Schwarz counter idiom.
292class VTKCOMMONSYSTEM_EXPORT vtkTimerLogCleanup
293{
294public:
297
298private:
299 vtkTimerLogCleanup(const vtkTimerLogCleanup&) = delete;
300 void operator=(const vtkTimerLogCleanup&) = delete;
301};
303
304#endif
a simple class to control print indentation
Definition: vtkIndent.h:34
abstract base class for most VTK objects
Definition: vtkObject.h:63
Helper class to log time within scope.
Definition: vtkTimerLog.h:261
vtkTimerLogScope(const char *eventString)
Definition: vtkTimerLog.h:263
std::string EventString
Definition: vtkTimerLog.h:275
Timer support and logging.
Definition: vtkTimerLog.h:90
static double GetUniversalTime()
Returns the elapsed number of seconds since 00:00:00 Coordinated Universal Time (UTC),...
static void static void DumpLog(VTK_FILEPATH const char *filename)
Write the timing table out to a file.
static tms CurrentCpuTicks
Definition: vtkTimerLog.h:233
static vtkTimerLogEntry::LogEntryType GetEventType(int i)
Programmatic access to events.
double StartTime
Definition: vtkTimerLog.h:244
static int Logging
Definition: vtkTimerLog.h:214
static void InsertTimedEvent(const char *EventString, double time, int cpuTicks)
Insert an event with a known wall time value (in seconds) and cpuTicks.
static int NextEntry
Definition: vtkTimerLog.h:217
static timeval CurrentWallTime
Definition: vtkTimerLog.h:231
static int WrapFlag
Definition: vtkTimerLog.h:218
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
static const char * GetEventString(int i)
Programmatic access to events.
static vtkTimerLogEntry * GetEvent(int i)
static double GetEventWallTime(int i)
Programmatic access to events.
~vtkTimerLog() override=default
static int GetNumberOfEvents()
Programmatic access to events.
static int TicksPerSecond
Definition: vtkTimerLog.h:219
static void CleanupLog()
Remove timer log.
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.
static void SetMaxEntries(int a)
Set/Get the maximum number of entries allowed in the timer log.
static int MaxEntries
Definition: vtkTimerLog.h:216
static void ResetLog()
Clear the timing table.
static void DumpLogWithIndentsAndPercentages(ostream *os)
void StopTimer()
Sets EndTime to the current time.
static void FormatAndMarkEvent(const char *format,...) VTK_FORMAT_PRINTF(1
Record a timing event.
static void SetLogging(int v)
This flag will turn logging of events off or on.
Definition: vtkTimerLog.h:101
static int GetLogging()
Definition: vtkTimerLog.h:102
static timeval FirstWallTime
Definition: vtkTimerLog.h:230
static tms FirstCpuTicks
Definition: vtkTimerLog.h:232
static void LoggingOn()
Definition: vtkTimerLog.h:103
static double GetCPUTime()
Returns the CPU time for this process On Win32 platforms this actually returns wall time.
static int GetEventIndent(int i)
Programmatic access to events.
static vtkTimerLog * New()
static void MarkEvent(const char *EventString)
Record a timing event and capture wall time and cpu ticks.
static int Indent
Definition: vtkTimerLog.h:215
static void MarkEventInternal(const char *EventString, vtkTimerLogEntry::LogEntryType type, vtkTimerLogEntry *entry=nullptr)
Record a timing event and capture wall time and cpu ticks.
static int GetMaxEntries()
Set/Get the maximum number of entries allowed in the timer log.
double GetElapsedTime()
Returns the difference between StartTime and EndTime as a doubleing point value indicating the elapse...
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.
static void DumpLogWithIndents(ostream *os, double threshold)
void StartTimer()
Set the StartTime to the current time.
double EndTime
Definition: vtkTimerLog.h:245
static void DumpEntry(ostream &os, int index, double time, double deltatime, int tick, int deltatick, const char *event)
static void LoggingOff()
Definition: vtkTimerLog.h:104
unsigned char Indent
Definition: vtkTimerLog.h:79
LogEntryType Type
Definition: vtkTimerLog.h:78
std::string Event
Definition: vtkTimerLog.h:77
static vtkTimerLogCleanup vtkTimerLogCleanupInstance
Definition: vtkTimerLog.h:302
#define VTK_FILEPATH