VTK  9.5.20250816
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
111#ifndef vtkTimerLog_h
112#define vtkTimerLog_h
113
114#include "vtkCommonSystemModule.h" // For export macro
115#include "vtkObject.h"
116#include "vtkStringFormatter.h" // For vtk::format_to_n
117
118#include <string> // STL Header
119
120#ifdef _WIN32
121#include <sys/timeb.h> // Needed for Win32 implementation of timer
122#include <sys/types.h> // Needed for Win32 implementation of timer
123#else
124#include <sys/time.h> // Needed for unix implementation of timer
125#include <sys/times.h> // Needed for unix implementation of timer
126#include <sys/types.h> // Needed for unix implementation of timer
127#include <time.h> // Needed for unix implementation of timer
128#endif
129
130// var args
131#ifndef _WIN32
132#include <unistd.h> // Needed for unix implementation of timer
133#endif
134
135// select stuff here is for sleep method
136#ifndef NO_FD_SET
137#define SELECT_MASK fd_set
138#else
139#ifndef _AIX
140typedef long fd_mask;
141#endif
142#if defined(_IBMR2)
143#define SELECT_MASK void
144#else
145#define SELECT_MASK int
146#endif
147#endif
148
149VTK_ABI_NAMESPACE_BEGIN
151{
153 {
155 STANDALONE, // an individual, marked event
156 START, // start of a timed event
157 END, // end of a timed event
158 INSERTED // externally timed value
159 };
160 double WallTime;
162 std::string Event;
164 unsigned char Indent;
166 : WallTime(0)
167 , CpuTicks(0)
168 , Type(INVALID)
169 , Indent(0)
170 {
171 }
172};
173
174class VTKCOMMONSYSTEM_EXPORT vtkTimerLog : public vtkObject
175{
176public:
177 static vtkTimerLog* New();
178
179 vtkTypeMacro(vtkTimerLog, vtkObject);
180 void PrintSelf(ostream& os, vtkIndent indent) override;
181
186 static void SetLogging(int v) { vtkTimerLog::Logging = v; }
187 static int GetLogging() { return vtkTimerLog::Logging; }
188 static void LoggingOn() { vtkTimerLog::SetLogging(1); }
190
192
195 static void SetMaxEntries(int a);
196 static int GetMaxEntries();
198
203#ifndef __VTK_WRAP__
204 template <typename... T>
205 static void FormatAndMarkEvent(const char* formatArg, T&&... args)
206 {
208 {
209 return;
210 }
211 std::string format = formatArg ? formatArg : "";
212 if (vtk::is_printf_format(format))
213 {
214 // VTK_DEPRECATED_IN_9_6_0
215 vtkWarningWithObjectMacro(nullptr,
216 "The given format "
217 << format << " is a printf format. The format will be "
218 << "converted to std::format. This conversion has been deprecated in 9.6.0");
219 format = vtk::printf_to_std_format(format);
220 }
221 static char event[4096];
222 auto result = vtk::format_to_n(event, sizeof(event), format, std::forward<T>(args)...);
223 *result.out = '\0';
225 }
226#endif
227
229
233 static void DumpLog(VTK_FILEPATH const char* filename);
235
237
242 static void MarkStartEvent(const char* EventString);
243 static void MarkEndEvent(const char* EventString);
245
247
251 static void InsertTimedEvent(const char* EventString, double time, int cpuTicks);
253
254 static void DumpLogWithIndents(ostream* os, double threshold);
255 static void DumpLogWithIndentsAndPercentages(ostream* os);
256
258
261 static int GetNumberOfEvents();
262 static int GetEventIndent(int i);
263 static double GetEventWallTime(int i);
264 static const char* GetEventString(int i);
267
271 static void MarkEvent(const char* EventString);
272
277 static void ResetLog();
278
282 static void CleanupLog();
283
288 static double GetUniversalTime();
289
294 static double GetCPUTime();
295
300
304 void StopTimer();
305
311
312protected:
314 {
315 this->StartTime = 0;
316 this->EndTime = 0;
317 } // ensure constructor/destructor protected
318 ~vtkTimerLog() override = default;
319
320 static int Logging;
321 static int Indent;
322 static int MaxEntries;
323 static int NextEntry;
324 static int WrapFlag;
325 static int TicksPerSecond;
326
327#ifdef _WIN32
328#ifndef _WIN32_WCE
329 static timeb FirstWallTime;
330 static timeb CurrentWallTime;
331#else
332 static FILETIME FirstWallTime;
333 static FILETIME CurrentWallTime;
334#endif
335#else
336 static timeval FirstWallTime;
337 static timeval CurrentWallTime;
338 static tms FirstCpuTicks;
339 static tms CurrentCpuTicks;
340#endif
341
345 static void MarkEventInternal(const char* EventString, vtkTimerLogEntry::LogEntryType type,
346 vtkTimerLogEntry* entry = nullptr);
347
348 // instance variables to support simple timing functionality,
349 // separate from timer table logging.
350 double StartTime;
351 double EndTime;
352
354
355 static void DumpEntry(ostream& os, int index, double time, double deltatime, int tick,
356 int deltatick, const char* event);
357
358private:
359 vtkTimerLog(const vtkTimerLog&) = delete;
360 void operator=(const vtkTimerLog&) = delete;
361};
362
367{
368public:
369 vtkTimerLogScope(const char* eventString)
370 {
371 if (eventString)
372 {
373 this->EventString = eventString;
374 }
375 vtkTimerLog::MarkStartEvent(eventString);
376 }
377
379
380protected:
381 std::string EventString;
382
383private:
384 vtkTimerLogScope(const vtkTimerLogScope&) = delete;
385 void operator=(const vtkTimerLogScope&) = delete;
386};
387
388//
389// Set built-in type. Creates member Set"name"() (e.g., SetVisibility());
390//
391#define vtkTimerLogMacro(string) \
392 { \
393 vtkTimerLog::FormatAndMarkEvent("Mark: In {:s}, line {:s}, class {:s}: {:s}", __FILE__, \
394 __LINE__, this->GetClassName(), string); \
395 }
396
397// Implementation detail for Schwarz counter idiom.
398class VTKCOMMONSYSTEM_EXPORT vtkTimerLogCleanup
399{
400public:
403
404private:
405 vtkTimerLogCleanup(const vtkTimerLogCleanup&) = delete;
406 void operator=(const vtkTimerLogCleanup&) = delete;
407};
409
410VTK_ABI_NAMESPACE_END
411#endif
a simple class to control print indentation
Definition vtkIndent.h:108
abstract base class for most VTK objects
Definition vtkObject.h:162
Helper class to log time within scope.
vtkTimerLogScope(const char *eventString)
std::string EventString
Timer support and logging.
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.
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()
static void DumpLog(const char *filename)
Write the timing table out to a file.
VTKCOMMONCORE_EXPORT std::string printf_to_std_format(const std::string &printf_format)
Convert a printf style format to a std::format style format.
VTKCOMMONCORE_EXPORT bool is_printf_format(const std::string &format)
Check if the given string is a printf style format.
unsigned char Indent
LogEntryType Type
std::string Event
Optimized C++ utilities for formatting values to strings and files.
static vtkTimerLogCleanup vtkTimerLogCleanupInstance
#define VTK_FILEPATH