VTK  9.5.20251215
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];
214 auto result = vtk::format_to_n(event, sizeof(event), format, std::forward<T>(args)...);
215 *result.out = '\0';
217 }
218#endif
219
221
225 static void DumpLog(VTK_FILEPATH const char* filename);
227
229
234 static void MarkStartEvent(const char* EventString);
235 static void MarkEndEvent(const char* EventString);
237
239
243 static void InsertTimedEvent(const char* EventString, double time, int cpuTicks);
245
246 static void DumpLogWithIndents(ostream* os, double threshold);
247 static void DumpLogWithIndentsAndPercentages(ostream* os);
248
250
253 static int GetNumberOfEvents();
254 static int GetEventIndent(int i);
255 static double GetEventWallTime(int i);
256 static const char* GetEventString(int i);
259
263 static void MarkEvent(const char* EventString);
264
269 static void ResetLog();
270
274 static void CleanupLog();
275
280 static double GetUniversalTime();
281
286 static double GetCPUTime();
287
292
296 void StopTimer();
297
303
304protected:
306 {
307 this->StartTime = 0;
308 this->EndTime = 0;
309 } // ensure constructor/destructor protected
310 ~vtkTimerLog() override = default;
311
312 static int Logging;
313 static int Indent;
314 static int MaxEntries;
315 static int NextEntry;
316 static int WrapFlag;
317 static int TicksPerSecond;
318
319#ifdef _WIN32
320#ifndef _WIN32_WCE
321 static timeb FirstWallTime;
322 static timeb CurrentWallTime;
323#else
324 static FILETIME FirstWallTime;
325 static FILETIME CurrentWallTime;
326#endif
327#else
328 static timeval FirstWallTime;
329 static timeval CurrentWallTime;
330 static tms FirstCpuTicks;
331 static tms CurrentCpuTicks;
332#endif
333
337 static void MarkEventInternal(const char* EventString, vtkTimerLogEntry::LogEntryType type,
338 vtkTimerLogEntry* entry = nullptr);
339
340 // instance variables to support simple timing functionality,
341 // separate from timer table logging.
342 double StartTime;
343 double EndTime;
344
346
347 static void DumpEntry(ostream& os, int index, double time, double deltatime, int tick,
348 int deltatick, const char* event);
349
350private:
351 vtkTimerLog(const vtkTimerLog&) = delete;
352 void operator=(const vtkTimerLog&) = delete;
353};
354
359{
360public:
361 vtkTimerLogScope(const char* eventString)
362 {
363 if (eventString)
364 {
365 this->EventString = eventString;
366 }
367 vtkTimerLog::MarkStartEvent(eventString);
368 }
369
371
372protected:
373 std::string EventString;
374
375private:
376 vtkTimerLogScope(const vtkTimerLogScope&) = delete;
377 void operator=(const vtkTimerLogScope&) = delete;
378};
379
380//
381// Set built-in type. Creates member Set"name"() (e.g., SetVisibility());
382//
383#define vtkTimerLogMacro(string) \
384 { \
385 vtkTimerLog::FormatAndMarkEvent("Mark: In {:s}, line {:s}, class {:s}: {:s}", __FILE__, \
386 __LINE__, this->GetClassName(), string); \
387 }
388
389// Implementation detail for Schwarz counter idiom.
390class VTKCOMMONSYSTEM_EXPORT vtkTimerLogCleanup
391{
392public:
395
396private:
397 vtkTimerLogCleanup(const vtkTimerLogCleanup&) = delete;
398 void operator=(const vtkTimerLogCleanup&) = delete;
399};
401
402VTK_ABI_NAMESPACE_END
403#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.
static vtkTimerLogCleanup vtkTimerLogCleanupInstance
#define VTK_FILEPATH