VTK: Common/System/vtkTimerLog.h Source File

| VTK 9.3.1

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
59 typedef 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
68 VTK_ABI_NAMESPACE_BEGIN
69 struct vtkTimerLogEntry
70 {
71 enum LogEntryType
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;
80 int CpuTicks;
81 std::string Event;
82 LogEntryType Type;
83 unsigned char Indent;
84 vtkTimerLogEntry()
85 : WallTime(0)
86 , CpuTicks(0)
87 , Type(INVALID)
88 , Indent(0)
89 {
90 }
91 };
92
93 class VTKCOMMONSYSTEM_EXPORT vtkTimerLog : public vtkObject
94 {
95 public:
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); }
108 static void LoggingOff() { vtkTimerLog::SetLogging(0); }
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);
163 static vtkTimerLogEntry::LogEntryType GetEventType(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
197 void StartTimer();
198
202 void StopTimer();
203
208 double GetElapsedTime();
209
210 protected:
211 vtkTimerLog()
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
251 static vtkTimerLogEntry* GetEvent(int i);
252
253 static void DumpEntry(ostream& os, int index, double time, double deltatime, int tick,
254 int deltatick, const char* event);
255
256 private:
257 vtkTimerLog(const vtkTimerLog&) = delete;
258 void operator=(const vtkTimerLog&) = delete;
259 };
260
264 class vtkTimerLogScope
265 {
266 public:
267 vtkTimerLogScope(const char* eventString)
268 {
269 if (eventString)
270 {
271 this->EventString = eventString;
272 }
273 vtkTimerLog::MarkStartEvent(eventString);
274 }
275
276 ~vtkTimerLogScope() { vtkTimerLog::MarkEndEvent(this->EventString.c_str()); };
277
278 protected:
279 std::string EventString;
280
281 private:
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.
296 class VTKCOMMONSYSTEM_EXPORT vtkTimerLogCleanup
297 {
298 public:
299 vtkTimerLogCleanup();
300 ~vtkTimerLogCleanup();
301
302 private:
303 vtkTimerLogCleanup(const vtkTimerLogCleanup&) = delete;
304 void operator=(const vtkTimerLogCleanup&) = delete;
305 };
306 static vtkTimerLogCleanup vtkTimerLogCleanupInstance;
307
308 VTK_ABI_NAMESPACE_END
309 #endif
vtkIndent
a simple class to control print indentation
Definition: vtkIndent.h:38
vtkObject
abstract base class for most VTK objects
Definition: vtkObject.h:61
vtkTimerLogCleanup
Definition: vtkTimerLog.h:297
vtkTimerLogCleanup::vtkTimerLogCleanup
vtkTimerLogCleanup()
vtkTimerLogCleanup::~vtkTimerLogCleanup
~vtkTimerLogCleanup()
vtkTimerLogScope
Helper class to log time within scope.
Definition: vtkTimerLog.h:265
vtkTimerLogScope::vtkTimerLogScope
vtkTimerLogScope(const char *eventString)
Definition: vtkTimerLog.h:267
vtkTimerLogScope::~vtkTimerLogScope
~vtkTimerLogScope()
Definition: vtkTimerLog.h:276
vtkTimerLogScope::EventString
std::string EventString
Definition: vtkTimerLog.h:276
vtkTimerLog
Timer support and logging.
Definition: vtkTimerLog.h:94
vtkTimerLog::GetUniversalTime
static double GetUniversalTime()
Returns the elapsed number of seconds since 00:00:00 Coordinated Universal Time (UTC),...
vtkTimerLog::DumpLog
static void static void DumpLog(VTK_FILEPATH const char *filename)
Write the timing table out to a file.
vtkTimerLog::CurrentCpuTicks
static tms CurrentCpuTicks
Definition: vtkTimerLog.h:237
vtkTimerLog::GetEventType
static vtkTimerLogEntry::LogEntryType GetEventType(int i)
Programmatic access to events.
vtkTimerLog::StartTime
double StartTime
Definition: vtkTimerLog.h:248
vtkTimerLog::Logging
static int Logging
Definition: vtkTimerLog.h:218
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::NextEntry
static int NextEntry
Definition: vtkTimerLog.h:221
vtkTimerLog::CurrentWallTime
static timeval CurrentWallTime
Definition: vtkTimerLog.h:235
vtkTimerLog::WrapFlag
static int WrapFlag
Definition: vtkTimerLog.h:222
vtkTimerLog::PrintSelf
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
vtkTimerLog::GetEventWallTime
static double GetEventWallTime(int i)
Programmatic access to events.
vtkTimerLog::~vtkTimerLog
~vtkTimerLog() override=default
vtkTimerLog::GetNumberOfEvents
static int GetNumberOfEvents()
Programmatic access to events.
vtkTimerLog::TicksPerSecond
static int TicksPerSecond
Definition: vtkTimerLog.h:223
vtkTimerLog::GetEventString
static const char * GetEventString(int i)
Programmatic access to events.
vtkTimerLog::CleanupLog
static void CleanupLog()
Remove timer log.
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::GetEvent
static vtkTimerLogEntry * GetEvent(int i)
vtkTimerLog::SetMaxEntries
static void SetMaxEntries(int a)
Set/Get the maximum number of entries allowed in the timer log.
vtkTimerLog::MaxEntries
static int MaxEntries
Definition: vtkTimerLog.h:220
vtkTimerLog::ResetLog
static void ResetLog()
Clear the timing table.
vtkTimerLog::DumpLogWithIndentsAndPercentages
static void DumpLogWithIndentsAndPercentages(ostream *os)
vtkTimerLog::StopTimer
void StopTimer()
Sets EndTime to the current time.
vtkTimerLog::FormatAndMarkEvent
static void FormatAndMarkEvent(const char *format,...) VTK_FORMAT_PRINTF(1
Record a timing event.
vtkTimerLog::SetLogging
static void SetLogging(int v)
This flag will turn logging of events off or on.
Definition: vtkTimerLog.h:105
vtkTimerLog::GetLogging
static int GetLogging()
Definition: vtkTimerLog.h:106
vtkTimerLog::FirstWallTime
static timeval FirstWallTime
Definition: vtkTimerLog.h:234
vtkTimerLog::FirstCpuTicks
static tms FirstCpuTicks
Definition: vtkTimerLog.h:236
vtkTimerLog::LoggingOn
static void LoggingOn()
Definition: vtkTimerLog.h:107
vtkTimerLog::vtkTimerLog
vtkTimerLog()
Definition: vtkTimerLog.h:211
vtkTimerLog::GetCPUTime
static double GetCPUTime()
Returns the CPU time for this process On Win32 platforms this actually returns wall time.
vtkTimerLog::GetEventIndent
static int GetEventIndent(int i)
Programmatic access to events.
vtkTimerLog::MarkEvent
static void MarkEvent(const char *EventString)
Record a timing event and capture wall time and cpu ticks.
vtkTimerLog::Indent
static int Indent
Definition: vtkTimerLog.h:219
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.
vtkTimerLog::GetMaxEntries
static int GetMaxEntries()
Set/Get the maximum number of entries allowed in the timer log.
vtkTimerLog::GetElapsedTime
double GetElapsedTime()
Returns the difference between StartTime and EndTime as a doubleing point value indicating the elapse...
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::DumpLogWithIndents
static void DumpLogWithIndents(ostream *os, double threshold)
vtkTimerLog::New
static vtkTimerLog * New()
vtkTimerLog::StartTimer
void StartTimer()
Set the StartTime to the current time.
vtkTimerLog::EndTime
double EndTime
Definition: vtkTimerLog.h:249
vtkTimerLog::DumpEntry
static void DumpEntry(ostream &os, int index, double time, double deltatime, int tick, int deltatick, const char *event)
vtkTimerLog::LoggingOff
static void LoggingOff()
Definition: vtkTimerLog.h:108
vtkX3D::time
@ time
Definition: vtkX3D.h:497
vtkX3D::type
@ type
Definition: vtkX3D.h:516
vtkX3D::index
@ index
Definition: vtkX3D.h:246
vtkX3D::string
@ string
Definition: vtkX3D.h:490
vtkTimerLogEntry
Definition: vtkTimerLog.h:70
vtkTimerLogEntry::Indent
unsigned char Indent
Definition: vtkTimerLog.h:83
vtkTimerLogEntry::WallTime
double WallTime
Definition: vtkTimerLog.h:79
vtkTimerLogEntry::CpuTicks
int CpuTicks
Definition: vtkTimerLog.h:80
vtkTimerLogEntry::Type
LogEntryType Type
Definition: vtkTimerLog.h:82
vtkTimerLogEntry::LogEntryType
LogEntryType
Definition: vtkTimerLog.h:72
vtkTimerLogEntry::INSERTED
@ INSERTED
Definition: vtkTimerLog.h:77
vtkTimerLogEntry::INVALID
@ INVALID
Definition: vtkTimerLog.h:73
vtkTimerLogEntry::START
@ START
Definition: vtkTimerLog.h:75
vtkTimerLogEntry::STANDALONE
@ STANDALONE
Definition: vtkTimerLog.h:74
vtkTimerLogEntry::END
@ END
Definition: vtkTimerLog.h:76
vtkTimerLogEntry::vtkTimerLogEntry
vtkTimerLogEntry()
Definition: vtkTimerLog.h:84
vtkTimerLogEntry::Event
std::string Event
Definition: vtkTimerLog.h:81
vtkObject.h
vtkTimerLogCleanupInstance
static vtkTimerLogCleanup vtkTimerLogCleanupInstance
Definition: vtkTimerLog.h:306
VTK_FILEPATH
#define VTK_FILEPATH
Definition: vtkWrappingHints.h:34