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=========================================================================*/
37#ifndef vtkTimerLog_h
38#define vtkTimerLog_h
39
40#include "vtkCommonSystemModule.h" // For export macro
41#include "vtkObject.h"
42
43#include <string> // STL Header
44
45#ifdef _WIN32
46#include <sys/timeb.h> // Needed for Win32 implementation of timer
47#include <sys/types.h> // Needed for Win32 implementation of timer
48#else
49#include <sys/time.h> // Needed for unix implementation of timer
50#include <sys/times.h> // Needed for unix implementation of timer
51#include <sys/types.h> // Needed for unix implementation of timer
52#include <time.h> // Needed for unix implementation of timer
53#endif
54
55// var args
56#ifndef _WIN32
57#include <unistd.h> // Needed for unix implementation of timer
58#endif
59
60// select stuff here is for sleep method
61#ifndef NO_FD_SET
62#define SELECT_MASK fd_set
63#else
64#ifndef _AIX
65typedef long fd_mask;
66#endif
67#if defined(_IBMR2)
68#define SELECT_MASK void
69#else
70#define SELECT_MASK int
71#endif
72#endif
73
75{
77 {
78 INVALID = -1,
79 STANDALONE, // an individual, marked event
80 START, // start of a timed event
81 END, // end of a timed event
82 INSERTED // externally timed value
83 };
84 double WallTime;
88 unsigned char Indent;
90 : WallTime(0)
91 , CpuTicks(0)
92 , Type(INVALID)
93 , Indent(0)
94 {
95 }
96};
97
98class VTKCOMMONSYSTEM_EXPORT vtkTimerLog : public vtkObject
99{
100public:
101 static vtkTimerLog* New();
102
103 vtkTypeMacro(vtkTimerLog, vtkObject);
104 void PrintSelf(ostream& os, vtkIndent indent) override;
105
110 static void SetLogging(int v) { vtkTimerLog::Logging = v; }
111 static int GetLogging() { return vtkTimerLog::Logging; }
112 static void LoggingOn() { vtkTimerLog::SetLogging(1); }
114
116
119 static void SetMaxEntries(int a);
120 static int GetMaxEntries();
122
127#ifndef __VTK_WRAP__
128 static void FormatAndMarkEvent(const char* format, ...) VTK_FORMAT_PRINTF(1, 2);
129#endif
130
132
136 static void DumpLog(VTK_FILEPATH const char* filename);
138
140
145 static void MarkStartEvent(const char* EventString);
146 static void MarkEndEvent(const char* EventString);
148
150
154 static void InsertTimedEvent(const char* EventString, double time, int cpuTicks);
156
157 static void DumpLogWithIndents(ostream* os, double threshold);
158 static void DumpLogWithIndentsAndPercentages(ostream* os);
159
161
164 static int GetNumberOfEvents();
165 static int GetEventIndent(int i);
166 static double GetEventWallTime(int i);
167 static const char* GetEventString(int i);
170
174 static void MarkEvent(const char* EventString);
175
180 static void ResetLog();
181
185 static void CleanupLog();
186
191 static double GetUniversalTime();
192
197 static double GetCPUTime();
198
203
207 void StopTimer();
208
214
215protected:
217 {
218 this->StartTime = 0;
219 this->EndTime = 0;
220 } // ensure constructor/destructor protected
221 ~vtkTimerLog() override = default;
222
223 static int Logging;
224 static int Indent;
225 static int MaxEntries;
226 static int NextEntry;
227 static int WrapFlag;
228 static int TicksPerSecond;
229
230#ifdef _WIN32
231#ifndef _WIN32_WCE
232 static timeb FirstWallTime;
233 static timeb CurrentWallTime;
234#else
235 static FILETIME FirstWallTime;
236 static FILETIME CurrentWallTime;
237#endif
238#else
239 static timeval FirstWallTime;
240 static timeval CurrentWallTime;
241 static tms FirstCpuTicks;
242 static tms CurrentCpuTicks;
243#endif
244
248 static void MarkEventInternal(const char* EventString, vtkTimerLogEntry::LogEntryType type,
249 vtkTimerLogEntry* entry = nullptr);
250
251 // instance variables to support simple timing functionality,
252 // separate from timer table logging.
253 double StartTime;
254 double EndTime;
255
257
258 static void DumpEntry(ostream& os, int index, double time, double deltatime, int tick,
259 int deltatick, const char* event);
260
261private:
262 vtkTimerLog(const vtkTimerLog&) = delete;
263 void operator=(const vtkTimerLog&) = delete;
264};
265
270{
271public:
272 vtkTimerLogScope(const char* eventString)
273 {
274 if (eventString)
275 {
276 this->EventString = eventString;
277 }
278 vtkTimerLog::MarkStartEvent(eventString);
279 }
280
282
283protected:
285
286private:
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( \
297 "Mark: In %s, line %d, class %s: %s", __FILE__, __LINE__, this->GetClassName(), string); \
298 }
299
300// Implementation detail for Schwarz counter idiom.
301class VTKCOMMONSYSTEM_EXPORT vtkTimerLogCleanup
302{
303public:
306
307private:
308 vtkTimerLogCleanup(const vtkTimerLogCleanup&) = delete;
309 void operator=(const vtkTimerLogCleanup&) = delete;
310};
312
313#endif
a simple class to control print indentation
Definition: vtkIndent.h:43
abstract base class for most VTK objects
Definition: vtkObject.h:63
Helper class to log time within scope.
Definition: vtkTimerLog.h:270
vtkTimerLogScope(const char *eventString)
Definition: vtkTimerLog.h:272
std::string EventString
Definition: vtkTimerLog.h:284
Timer support and logging.
Definition: vtkTimerLog.h:99
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:242
static vtkTimerLogEntry::LogEntryType GetEventType(int i)
Programmatic access to events.
double StartTime
Definition: vtkTimerLog.h:253
static int Logging
Definition: vtkTimerLog.h:223
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:226
static timeval CurrentWallTime
Definition: vtkTimerLog.h:240
static int WrapFlag
Definition: vtkTimerLog.h:227
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:228
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:225
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:110
static int GetLogging()
Definition: vtkTimerLog.h:111
static timeval FirstWallTime
Definition: vtkTimerLog.h:239
static tms FirstCpuTicks
Definition: vtkTimerLog.h:241
static void LoggingOn()
Definition: vtkTimerLog.h:112
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:224
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:254
static void DumpEntry(ostream &os, int index, double time, double deltatime, int tick, int deltatick, const char *event)
static void LoggingOff()
Definition: vtkTimerLog.h:113
@ time
Definition: vtkX3D.h:503
@ type
Definition: vtkX3D.h:522
@ index
Definition: vtkX3D.h:252
@ string
Definition: vtkX3D.h:496
unsigned char Indent
Definition: vtkTimerLog.h:88
LogEntryType Type
Definition: vtkTimerLog.h:87
std::string Event
Definition: vtkTimerLog.h:86
static vtkTimerLogCleanup vtkTimerLogCleanupInstance
Definition: vtkTimerLog.h:311
#define VTK_FILEPATH