VTK  9.6.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
21
22#ifndef vtkTimerLog_h
23#define vtkTimerLog_h
24
25#include "vtkCommonSystemModule.h" // For export macro
26#include "vtkObject.h"
27#include "vtkStringFormatter.h" // For vtk::format_to_n
28
29#include <string> // STL Header
30
31#ifdef _WIN32
32#include <sys/timeb.h> // Needed for Win32 implementation of timer
33#include <sys/types.h> // Needed for Win32 implementation of timer
34#else
35#include <sys/time.h> // Needed for unix implementation of timer
36#include <sys/times.h> // Needed for unix implementation of timer
37#include <sys/types.h> // Needed for unix implementation of timer
38#include <time.h> // Needed for unix implementation of timer
39#endif
40
41// var args
42#ifndef _WIN32
43#include <unistd.h> // Needed for unix implementation of timer
44#endif
45
46// select stuff here is for sleep method
47#ifndef NO_FD_SET
48#define SELECT_MASK fd_set
49#else
50#ifndef _AIX
51typedef long fd_mask;
52#endif
53#if defined(_IBMR2)
54#define SELECT_MASK void
55#else
56#define SELECT_MASK int
57#endif
58#endif
59
60VTK_ABI_NAMESPACE_BEGIN
62{
64 {
65 INVALID = -1,
66 STANDALONE, // an individual, marked event
67 START, // start of a timed event
68 END, // end of a timed event
69 INSERTED // externally timed value
70 };
71 double WallTime;
73 std::string Event;
75 unsigned char Indent;
77 : WallTime(0)
78 , CpuTicks(0)
79 , Type(INVALID)
80 , Indent(0)
81 {
82 }
83};
84
85class VTKCOMMONSYSTEM_EXPORT vtkTimerLog : public vtkObject
86{
87public:
88 static vtkTimerLog* New();
89
90 vtkTypeMacro(vtkTimerLog, vtkObject);
91 void PrintSelf(ostream& os, vtkIndent indent) override;
92
97 static void SetLogging(int v) { vtkTimerLog::Logging = v; }
98 static int GetLogging() { return vtkTimerLog::Logging; }
99 static void LoggingOn() { vtkTimerLog::SetLogging(1); }
101
103
106 static void SetMaxEntries(int a);
107 static int GetMaxEntries();
109
115#ifndef __VTK_WRAP__
116 template <typename... T>
117 static void FormatAndMarkEvent(const char* formatArg, T&&... args)
118 {
120 {
121 return;
122 }
123 std::string format = formatArg ? vtk::to_std_format(formatArg) : "";
124 static char event[4096];
125 auto result = vtk::format_to_n(event, sizeof(event), format, std::forward<T>(args)...);
126 *result.out = '\0';
128 }
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:
284 std::string EventString;
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("Mark: In {:s}, line {:s}, class {:s}: {:s}", __FILE__, \
297 __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
313VTK_ABI_NAMESPACE_END
314#endif
a simple class to control print indentation
Definition vtkIndent.h:29
vtkTimerLogScope(const char *eventString)
std::string EventString
static double GetUniversalTime()
Returns the elapsed number of seconds since 00:00:00 Coordinated Universal Time (UTC),...
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 void FormatAndMarkEvent(const char *formatArg, T &&... args)
Record a timing event.
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 SetLogging(int v)
This flag will turn logging of events off or on.
Definition vtkTimerLog.h:97
static int GetLogging()
Definition vtkTimerLog.h:98
static timeval FirstWallTime
static tms FirstCpuTicks
static void LoggingOn()
Definition vtkTimerLog.h:99
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()
static void DumpLog(const char *filename)
Write the timing table out to a file.
VTKCOMMONCORE_EXPORT std::string to_std_format(const std::string &format)
Convert printf and std::format style format strings.
unsigned char Indent
Definition vtkTimerLog.h:75
LogEntryType Type
Definition vtkTimerLog.h:74
std::string Event
Definition vtkTimerLog.h:73
Optimized C++ utilities for formatting values to strings and files.
static vtkTimerLogCleanup vtkTimerLogCleanupInstance
#define VTK_FILEPATH