VTK  9.6.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
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];
126 auto result = vtk::format_to_n(event, sizeof(event), format, std::forward<T>(args)...);
127 *result.out = '\0', );
129 }
130#endif
131
133
137 static void DumpLog(VTK_FILEPATH const char* filename);
139
141
146 static void MarkStartEvent(const char* EventString);
147 static void MarkEndEvent(const char* EventString);
149
151
155 static void InsertTimedEvent(const char* EventString, double time, int cpuTicks);
157
158 static void DumpLogWithIndents(ostream* os, double threshold);
159 static void DumpLogWithIndentsAndPercentages(ostream* os);
160
162
165 static int GetNumberOfEvents();
166 static int GetEventIndent(int i);
167 static double GetEventWallTime(int i);
168 static const char* GetEventString(int i);
171
175 static void MarkEvent(const char* EventString);
176
181 static void ResetLog();
182
186 static void CleanupLog();
187
192 static double GetUniversalTime();
193
198 static double GetCPUTime();
199
204
208 void StopTimer();
209
215
216protected:
218 {
219 this->StartTime = 0;
220 this->EndTime = 0;
221 } // ensure constructor/destructor protected
222 ~vtkTimerLog() override = default;
223
224 static int Logging;
225 static int Indent;
226 static int MaxEntries;
227 static int NextEntry;
228 static int WrapFlag;
229 static int TicksPerSecond;
230
231#ifdef _WIN32
232#ifndef _WIN32_WCE
233 static timeb FirstWallTime;
234 static timeb CurrentWallTime;
235#else
236 static FILETIME FirstWallTime;
237 static FILETIME CurrentWallTime;
238#endif
239#else
240 static timeval FirstWallTime;
241 static timeval CurrentWallTime;
242 static tms FirstCpuTicks;
243 static tms CurrentCpuTicks;
244#endif
245
249 static void MarkEventInternal(const char* EventString, vtkTimerLogEntry::LogEntryType type,
250 vtkTimerLogEntry* entry = nullptr);
251
252 // instance variables to support simple timing functionality,
253 // separate from timer table logging.
254 double StartTime;
255 double EndTime;
256
258
259 static void DumpEntry(ostream& os, int index, double time, double deltatime, int tick,
260 int deltatick, const char* event);
261
262private:
263 vtkTimerLog(const vtkTimerLog&) = delete;
264 void operator=(const vtkTimerLog&) = delete;
265};
266
271{
272public:
273 vtkTimerLogScope(const char* eventString)
274 {
275 if (eventString)
276 {
277 this->EventString = eventString;
278 }
279 vtkTimerLog::MarkStartEvent(eventString);
280 }
281
283
284protected:
285 std::string EventString;
286
287private:
288 vtkTimerLogScope(const vtkTimerLogScope&) = delete;
289 void operator=(const vtkTimerLogScope&) = delete;
290};
291
292//
293// Set built-in type. Creates member Set"name"() (e.g., SetVisibility());
294//
295#define vtkTimerLogMacro(string) \
296 { \
297 vtkTimerLog::FormatAndMarkEvent("Mark: In {:s}, line {:s}, class {:s}: {:s}", __FILE__, \
298 __LINE__, this->GetClassName(), string); \
299 }
300
301// Implementation detail for Schwarz counter idiom.
302class VTKCOMMONSYSTEM_EXPORT vtkTimerLogCleanup
303{
304public:
307
308private:
309 vtkTimerLogCleanup(const vtkTimerLogCleanup&) = delete;
310 void operator=(const vtkTimerLogCleanup&) = delete;
311};
313
314VTK_ABI_NAMESPACE_END
315#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.
#define VTK_FORMAT_IF_ERROR_RETURN(formatCommand, returnValue)
static vtkTimerLogCleanup vtkTimerLogCleanupInstance
#define VTK_FILEPATH