VTK  9.3.0
vtkTimerLog.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2// SPDX-License-Identifier: BSD-3-Clause
31#ifndef vtkTimerLog_h
32#define vtkTimerLog_h
33
34#include "vtkCommonSystemModule.h" // For export macro
35#include "vtkObject.h"
36
37#include <string> // STL Header
38
39#ifdef _WIN32
40#include <sys/timeb.h> // Needed for Win32 implementation of timer
41#include <sys/types.h> // Needed for Win32 implementation of timer
42#else
43#include <sys/time.h> // Needed for unix implementation of timer
44#include <sys/times.h> // Needed for unix implementation of timer
45#include <sys/types.h> // Needed for unix implementation of timer
46#include <time.h> // Needed for unix implementation of timer
47#endif
48
49// var args
50#ifndef _WIN32
51#include <unistd.h> // Needed for unix implementation of timer
52#endif
53
54// select stuff here is for sleep method
55#ifndef NO_FD_SET
56#define SELECT_MASK fd_set
57#else
58#ifndef _AIX
59typedef long fd_mask;
60#endif
61#if defined(_IBMR2)
62#define SELECT_MASK void
63#else
64#define SELECT_MASK int
65#endif
66#endif
67
68VTK_ABI_NAMESPACE_BEGIN
70{
72 {
73 INVALID = -1,
74 STANDALONE, // an individual, marked event
75 START, // start of a timed event
76 END, // end of a timed event
77 INSERTED // externally timed value
78 };
79 double WallTime;
81 std::string Event;
83 unsigned char Indent;
85 : WallTime(0)
86 , CpuTicks(0)
87 , Type(INVALID)
88 , Indent(0)
89 {
90 }
91};
92
93class VTKCOMMONSYSTEM_EXPORT vtkTimerLog : public vtkObject
94{
95public:
96 static vtkTimerLog* New();
97
98 vtkTypeMacro(vtkTimerLog, vtkObject);
99 void PrintSelf(ostream& os, vtkIndent indent) override;
100
105 static void SetLogging(int v) { vtkTimerLog::Logging = v; }
106 static int GetLogging() { return vtkTimerLog::Logging; }
107 static void LoggingOn() { vtkTimerLog::SetLogging(1); }
109
111
114 static void SetMaxEntries(int a);
115 static int GetMaxEntries();
117
122#ifndef __VTK_WRAP__
123 static void FormatAndMarkEvent(const char* format, ...) VTK_FORMAT_PRINTF(1, 2);
124#endif
125
127
131 static void DumpLog(VTK_FILEPATH const char* filename);
133
135
140 static void MarkStartEvent(const char* EventString);
141 static void MarkEndEvent(const char* EventString);
143
145
149 static void InsertTimedEvent(const char* EventString, double time, int cpuTicks);
151
152 static void DumpLogWithIndents(ostream* os, double threshold);
153 static void DumpLogWithIndentsAndPercentages(ostream* os);
154
156
159 static int GetNumberOfEvents();
160 static int GetEventIndent(int i);
161 static double GetEventWallTime(int i);
162 static const char* GetEventString(int i);
165
169 static void MarkEvent(const char* EventString);
170
175 static void ResetLog();
176
180 static void CleanupLog();
181
186 static double GetUniversalTime();
187
192 static double GetCPUTime();
193
198
202 void StopTimer();
203
209
210protected:
212 {
213 this->StartTime = 0;
214 this->EndTime = 0;
215 } // ensure constructor/destructor protected
216 ~vtkTimerLog() override = default;
217
218 static int Logging;
219 static int Indent;
220 static int MaxEntries;
221 static int NextEntry;
222 static int WrapFlag;
223 static int TicksPerSecond;
224
225#ifdef _WIN32
226#ifndef _WIN32_WCE
227 static timeb FirstWallTime;
228 static timeb CurrentWallTime;
229#else
230 static FILETIME FirstWallTime;
231 static FILETIME CurrentWallTime;
232#endif
233#else
234 static timeval FirstWallTime;
235 static timeval CurrentWallTime;
236 static tms FirstCpuTicks;
237 static tms CurrentCpuTicks;
238#endif
239
243 static void MarkEventInternal(const char* EventString, vtkTimerLogEntry::LogEntryType type,
244 vtkTimerLogEntry* entry = nullptr);
245
246 // instance variables to support simple timing functionality,
247 // separate from timer table logging.
248 double StartTime;
249 double EndTime;
250
252
253 static void DumpEntry(ostream& os, int index, double time, double deltatime, int tick,
254 int deltatick, const char* event);
255
256private:
257 vtkTimerLog(const vtkTimerLog&) = delete;
258 void operator=(const vtkTimerLog&) = delete;
259};
260
265{
266public:
267 vtkTimerLogScope(const char* eventString)
268 {
269 if (eventString)
270 {
271 this->EventString = eventString;
272 }
273 vtkTimerLog::MarkStartEvent(eventString);
274 }
275
277
278protected:
279 std::string EventString;
280
281private:
282 vtkTimerLogScope(const vtkTimerLogScope&) = delete;
283 void operator=(const vtkTimerLogScope&) = delete;
284};
285
286//
287// Set built-in type. Creates member Set"name"() (e.g., SetVisibility());
288//
289#define vtkTimerLogMacro(string) \
290 { \
291 vtkTimerLog::FormatAndMarkEvent( \
292 "Mark: In %s, line %d, class %s: %s", __FILE__, __LINE__, this->GetClassName(), string); \
293 }
294
295// Implementation detail for Schwarz counter idiom.
296class VTKCOMMONSYSTEM_EXPORT vtkTimerLogCleanup
297{
298public:
301
302private:
303 vtkTimerLogCleanup(const vtkTimerLogCleanup&) = delete;
304 void operator=(const vtkTimerLogCleanup&) = delete;
305};
307
308VTK_ABI_NAMESPACE_END
309#endif
a simple class to control print indentation
Definition vtkIndent.h:38
abstract base class for most VTK objects
Definition vtkObject.h:58
Helper class to log time within scope.
vtkTimerLogScope(const char *eventString)
std::string EventString
Timer support and logging.
Definition vtkTimerLog.h:94
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
static vtkTimerLogEntry::LogEntryType GetEventType(int i)
Programmatic access to events.
double StartTime
static int Logging
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
static timeval CurrentWallTime
static int WrapFlag
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
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
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.
static int GetLogging()
static timeval FirstWallTime
static tms FirstCpuTicks
static void LoggingOn()
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
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
static void DumpEntry(ostream &os, int index, double time, double deltatime, int tick, int deltatick, const char *event)
static void LoggingOff()
unsigned char Indent
Definition vtkTimerLog.h:83
LogEntryType Type
Definition vtkTimerLog.h:82
std::string Event
Definition vtkTimerLog.h:81
static vtkTimerLogCleanup vtkTimerLogCleanupInstance
#define VTK_FILEPATH