VTK  9.6.20260404
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
110
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
204#ifndef __VTK_WRAP__
205 template <typename... T>
206 static void FormatAndMarkEvent(const char* formatArg, T&&... args)
207 {
209 {
210 return;
211 }
212 std::string format = formatArg ? vtk::to_std_format(formatArg) : "";
213 static char event[4096];
215 auto result = vtk::format_to_n(event, sizeof(event), format, std::forward<T>(args)...);
216 *result.out = '\0', );
218 }
219#endif
220
222
226 static void DumpLog(VTK_FILEPATH const char* filename);
228
230
235 static void MarkStartEvent(const char* EventString);
236 static void MarkEndEvent(const char* EventString);
238
240
244 static void InsertTimedEvent(const char* EventString, double time, int cpuTicks);
246
247 static void DumpLogWithIndents(ostream* os, double threshold);
248 static void DumpLogWithIndentsAndPercentages(ostream* os);
249
251
254 static int GetNumberOfEvents();
255 static int GetEventIndent(int i);
256 static double GetEventWallTime(int i);
257 static const char* GetEventString(int i);
260
264 static void MarkEvent(const char* EventString);
265
270 static void ResetLog();
271
275 static void CleanupLog();
276
281 static double GetUniversalTime();
282
287 static double GetCPUTime();
288
293
297 void StopTimer();
298
304
305protected:
307 {
308 this->StartTime = 0;
309 this->EndTime = 0;
310 } // ensure constructor/destructor protected
311 ~vtkTimerLog() override = default;
312
313 static int Logging;
314 static int Indent;
315 static int MaxEntries;
316 static int NextEntry;
317 static int WrapFlag;
318 static int TicksPerSecond;
319
320#ifdef _WIN32
321#ifndef _WIN32_WCE
322 static timeb FirstWallTime;
323 static timeb CurrentWallTime;
324#else
325 static FILETIME FirstWallTime;
326 static FILETIME CurrentWallTime;
327#endif
328#else
329 static timeval FirstWallTime;
330 static timeval CurrentWallTime;
331 static tms FirstCpuTicks;
332 static tms CurrentCpuTicks;
333#endif
334
338 static void MarkEventInternal(const char* EventString, vtkTimerLogEntry::LogEntryType type,
339 vtkTimerLogEntry* entry = nullptr);
340
341 // instance variables to support simple timing functionality,
342 // separate from timer table logging.
343 double StartTime;
344 double EndTime;
345
347
348 static void DumpEntry(ostream& os, int index, double time, double deltatime, int tick,
349 int deltatick, const char* event);
350
351private:
352 vtkTimerLog(const vtkTimerLog&) = delete;
353 void operator=(const vtkTimerLog&) = delete;
354};
355
360{
361public:
362 vtkTimerLogScope(const char* eventString)
363 {
364 if (eventString)
365 {
366 this->EventString = eventString;
367 }
368 vtkTimerLog::MarkStartEvent(eventString);
369 }
370
372
373protected:
374 std::string EventString;
375
376private:
377 vtkTimerLogScope(const vtkTimerLogScope&) = delete;
378 void operator=(const vtkTimerLogScope&) = delete;
379};
380
381//
382// Set built-in type. Creates member Set"name"() (e.g., SetVisibility());
383//
384#define vtkTimerLogMacro(string) \
385 { \
386 vtkTimerLog::FormatAndMarkEvent("Mark: In {:s}, line {:s}, class {:s}: {:s}", __FILE__, \
387 __LINE__, this->GetClassName(), string); \
388 }
389
390// Implementation detail for Schwarz counter idiom.
391class VTKCOMMONSYSTEM_EXPORT vtkTimerLogCleanup
392{
393public:
396
397private:
398 vtkTimerLogCleanup(const vtkTimerLogCleanup&) = delete;
399 void operator=(const vtkTimerLogCleanup&) = delete;
400};
402
403VTK_ABI_NAMESPACE_END
404#endif
a simple class to control print indentation
Definition vtkIndent.h:108
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.
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 to_std_format(const std::string &format)
Convert printf and std::format style format strings.
unsigned char Indent
LogEntryType Type
std::string Event
Optimized C++ utilities for formatting values to strings and files.
#define VTK_FORMAT_IF_ERROR_RETURN(formatCommand, returnValue)
static vtkTimerLogCleanup vtkTimerLogCleanupInstance
#define VTK_FILEPATH