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
22#ifndef vtkTimerLog_h
23#define vtkTimerLog_h
24
25#include "vtkCommonSystemModule.h" // For export macro
26#include "vtkObject.h"
27
28#include <string> // STL Header
29
30#ifdef _WIN32
31#include <sys/timeb.h> // Needed for Win32 implementation of timer
32#include <sys/types.h> // Needed for Win32 implementation of timer
33#else
34#include <sys/time.h> // Needed for unix implementation of timer
35#include <sys/times.h> // Needed for unix implementation of timer
36#include <sys/types.h> // Needed for unix implementation of timer
37#include <time.h> // Needed for unix implementation of timer
38#endif
39
40// var args
41#ifndef _WIN32
42#include <unistd.h> // Needed for unix implementation of timer
43#endif
44
45// select stuff here is for sleep method
46#ifndef NO_FD_SET
47#define SELECT_MASK fd_set
48#else
49#ifndef _AIX
50typedef long fd_mask;
51#endif
52#if defined(_IBMR2)
53#define SELECT_MASK void
54#else
55#define SELECT_MASK int
56#endif
57#endif
58
59VTK_ABI_NAMESPACE_BEGIN
61{
63 {
64 INVALID = -1,
65 STANDALONE, // an individual, marked event
66 START, // start of a timed event
67 END, // end of a timed event
68 INSERTED // externally timed value
69 };
70 double WallTime;
72 std::string Event;
74 unsigned char Indent;
76 : WallTime(0)
77 , CpuTicks(0)
78 , Type(INVALID)
79 , Indent(0)
80 {
81 }
82};
83
84class VTKCOMMONSYSTEM_EXPORT vtkTimerLog : public vtkObject
85{
86public:
87 static vtkTimerLog* New();
88
89 vtkTypeMacro(vtkTimerLog, vtkObject);
90 void PrintSelf(ostream& os, vtkIndent indent) override;
91
96 static void SetLogging(int v) { vtkTimerLog::Logging = v; }
97 static int GetLogging() { return vtkTimerLog::Logging; }
98 static void LoggingOn() { vtkTimerLog::SetLogging(1); }
99 static void LoggingOff() { vtkTimerLog::SetLogging(0); }
100
102
105 static void SetMaxEntries(int a);
106 static int GetMaxEntries();
108
113#ifndef __VTK_WRAP__
114 static void FormatAndMarkEvent(const char* format, ...) VTK_FORMAT_PRINTF(1, 2);
115#endif
116
118
122 static void DumpLog(VTK_FILEPATH const char* filename);
124
126
131 static void MarkStartEvent(const char* EventString);
132 static void MarkEndEvent(const char* EventString);
134
136
140 static void InsertTimedEvent(const char* EventString, double time, int cpuTicks);
142
143 static void DumpLogWithIndents(ostream* os, double threshold);
144 static void DumpLogWithIndentsAndPercentages(ostream* os);
145
147
150 static int GetNumberOfEvents();
151 static int GetEventIndent(int i);
152 static double GetEventWallTime(int i);
153 static const char* GetEventString(int i);
156
160 static void MarkEvent(const char* EventString);
161
166 static void ResetLog();
167
171 static void CleanupLog();
172
177 static double GetUniversalTime();
178
183 static double GetCPUTime();
184
189
193 void StopTimer();
194
200
201protected:
203 {
204 this->StartTime = 0;
205 this->EndTime = 0;
206 } // ensure constructor/destructor protected
207 ~vtkTimerLog() override = default;
208
209 static int Logging;
210 static int Indent;
211 static int MaxEntries;
212 static int NextEntry;
213 static int WrapFlag;
214 static int TicksPerSecond;
215
216#ifdef _WIN32
217#ifndef _WIN32_WCE
218 static timeb FirstWallTime;
219 static timeb CurrentWallTime;
220#else
221 static FILETIME FirstWallTime;
222 static FILETIME CurrentWallTime;
223#endif
224#else
225 static timeval FirstWallTime;
226 static timeval CurrentWallTime;
227 static tms FirstCpuTicks;
228 static tms CurrentCpuTicks;
229#endif
230
234 static void MarkEventInternal(const char* EventString, vtkTimerLogEntry::LogEntryType type,
235 vtkTimerLogEntry* entry = nullptr);
236
237 // instance variables to support simple timing functionality,
238 // separate from timer table logging.
239 double StartTime;
240 double EndTime;
241
243
244 static void DumpEntry(ostream& os, int index, double time, double deltatime, int tick,
245 int deltatick, const char* event);
246
247private:
248 vtkTimerLog(const vtkTimerLog&) = delete;
249 void operator=(const vtkTimerLog&) = delete;
250};
251
256{
257public:
258 vtkTimerLogScope(const char* eventString)
259 {
260 if (eventString)
261 {
262 this->EventString = eventString;
263 }
264 vtkTimerLog::MarkStartEvent(eventString);
265 }
266
268
269protected:
270 std::string EventString;
271
272private:
273 vtkTimerLogScope(const vtkTimerLogScope&) = delete;
274 void operator=(const vtkTimerLogScope&) = delete;
275};
276
277//
278// Set built-in type. Creates member Set"name"() (e.g., SetVisibility());
279//
280#define vtkTimerLogMacro(string) \
281 { \
282 vtkTimerLog::FormatAndMarkEvent( \
283 "Mark: In %s, line %d, class %s: %s", __FILE__, __LINE__, this->GetClassName(), string); \
284 }
285
286// Implementation detail for Schwarz counter idiom.
287class VTKCOMMONSYSTEM_EXPORT vtkTimerLogCleanup
288{
289public:
292
293private:
294 vtkTimerLogCleanup(const vtkTimerLogCleanup&) = delete;
295 void operator=(const vtkTimerLogCleanup&) = delete;
296};
298
299VTK_ABI_NAMESPACE_END
300#endif
a simple class to control print indentation
Definition vtkIndent.h:29
abstract base class for most VTK objects
Definition vtkObject.h:49
Helper class to log time within scope.
vtkTimerLogScope(const char *eventString)
std::string EventString
Timer support and logging.
Definition vtkTimerLog.h:85
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.
Definition vtkTimerLog.h:96
static int GetLogging()
Definition vtkTimerLog.h:97
static timeval FirstWallTime
static tms FirstCpuTicks
static void LoggingOn()
Definition vtkTimerLog.h:98
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()
Definition vtkTimerLog.h:99
unsigned char Indent
Definition vtkTimerLog.h:74
LogEntryType Type
Definition vtkTimerLog.h:73
std::string Event
Definition vtkTimerLog.h:72
static vtkTimerLogCleanup vtkTimerLogCleanupInstance
#define VTK_FILEPATH