VTK  9.3.20240423
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
117#include <string> // STL Header
118
119#ifdef _WIN32
120#include <sys/timeb.h> // Needed for Win32 implementation of timer
121#include <sys/types.h> // Needed for Win32 implementation of timer
122#else
123#include <sys/time.h> // Needed for unix implementation of timer
124#include <sys/times.h> // Needed for unix implementation of timer
125#include <sys/types.h> // Needed for unix implementation of timer
126#include <time.h> // Needed for unix implementation of timer
127#endif
128
129// var args
130#ifndef _WIN32
131#include <unistd.h> // Needed for unix implementation of timer
132#endif
133
134// select stuff here is for sleep method
135#ifndef NO_FD_SET
136#define SELECT_MASK fd_set
137#else
138#ifndef _AIX
139typedef long fd_mask;
140#endif
141#if defined(_IBMR2)
142#define SELECT_MASK void
143#else
144#define SELECT_MASK int
145#endif
146#endif
147
148VTK_ABI_NAMESPACE_BEGIN
150{
152 {
154 STANDALONE, // an individual, marked event
155 START, // start of a timed event
156 END, // end of a timed event
157 INSERTED // externally timed value
158 };
159 double WallTime;
161 std::string Event;
163 unsigned char Indent;
165 : WallTime(0)
166 , CpuTicks(0)
167 , Type(INVALID)
168 , Indent(0)
169 {
170 }
171};
172
173class VTKCOMMONSYSTEM_EXPORT vtkTimerLog : public vtkObject
174{
175public:
176 static vtkTimerLog* New();
177
178 vtkTypeMacro(vtkTimerLog, vtkObject);
179 void PrintSelf(ostream& os, vtkIndent indent) override;
180
185 static void SetLogging(int v) { vtkTimerLog::Logging = v; }
186 static int GetLogging() { return vtkTimerLog::Logging; }
187 static void LoggingOn() { vtkTimerLog::SetLogging(1); }
189
191
194 static void SetMaxEntries(int a);
195 static int GetMaxEntries();
197
202#ifndef __VTK_WRAP__
203 static void FormatAndMarkEvent(const char* format, ...) VTK_FORMAT_PRINTF(1, 2);
204#endif
205
207
211 static void DumpLog(VTK_FILEPATH const char* filename);
213
215
220 static void MarkStartEvent(const char* EventString);
221 static void MarkEndEvent(const char* EventString);
223
225
229 static void InsertTimedEvent(const char* EventString, double time, int cpuTicks);
231
232 static void DumpLogWithIndents(ostream* os, double threshold);
233 static void DumpLogWithIndentsAndPercentages(ostream* os);
234
236
239 static int GetNumberOfEvents();
240 static int GetEventIndent(int i);
241 static double GetEventWallTime(int i);
242 static const char* GetEventString(int i);
245
249 static void MarkEvent(const char* EventString);
250
255 static void ResetLog();
256
260 static void CleanupLog();
261
266 static double GetUniversalTime();
267
272 static double GetCPUTime();
273
278
282 void StopTimer();
283
289
290protected:
292 {
293 this->StartTime = 0;
294 this->EndTime = 0;
295 } // ensure constructor/destructor protected
296 ~vtkTimerLog() override = default;
297
298 static int Logging;
299 static int Indent;
300 static int MaxEntries;
301 static int NextEntry;
302 static int WrapFlag;
303 static int TicksPerSecond;
304
305#ifdef _WIN32
306#ifndef _WIN32_WCE
307 static timeb FirstWallTime;
308 static timeb CurrentWallTime;
309#else
310 static FILETIME FirstWallTime;
311 static FILETIME CurrentWallTime;
312#endif
313#else
314 static timeval FirstWallTime;
315 static timeval CurrentWallTime;
316 static tms FirstCpuTicks;
317 static tms CurrentCpuTicks;
318#endif
319
323 static void MarkEventInternal(const char* EventString, vtkTimerLogEntry::LogEntryType type,
324 vtkTimerLogEntry* entry = nullptr);
325
326 // instance variables to support simple timing functionality,
327 // separate from timer table logging.
328 double StartTime;
329 double EndTime;
330
332
333 static void DumpEntry(ostream& os, int index, double time, double deltatime, int tick,
334 int deltatick, const char* event);
335
336private:
337 vtkTimerLog(const vtkTimerLog&) = delete;
338 void operator=(const vtkTimerLog&) = delete;
339};
340
345{
346public:
347 vtkTimerLogScope(const char* eventString)
348 {
349 if (eventString)
350 {
351 this->EventString = eventString;
352 }
353 vtkTimerLog::MarkStartEvent(eventString);
354 }
355
357
358protected:
359 std::string EventString;
360
361private:
362 vtkTimerLogScope(const vtkTimerLogScope&) = delete;
363 void operator=(const vtkTimerLogScope&) = delete;
364};
365
366//
367// Set built-in type. Creates member Set"name"() (e.g., SetVisibility());
368//
369#define vtkTimerLogMacro(string) \
370 { \
371 vtkTimerLog::FormatAndMarkEvent( \
372 "Mark: In %s, line %d, class %s: %s", __FILE__, __LINE__, this->GetClassName(), string); \
373 }
374
375// Implementation detail for Schwarz counter idiom.
376class VTKCOMMONSYSTEM_EXPORT vtkTimerLogCleanup
377{
378public:
381
382private:
383 vtkTimerLogCleanup(const vtkTimerLogCleanup&) = delete;
384 void operator=(const vtkTimerLogCleanup&) = delete;
385};
387
388VTK_ABI_NAMESPACE_END
389#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 void static void DumpLog(VTK_FILEPATH const char *filename)
Write the timing table out to a file.
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 int MaxEntries
static void ResetLog()
Clear the timing table.
static void DumpLogWithIndentsAndPercentages(ostream *os)
void StopTimer()
Sets EndTime to the current time.
static void FormatAndMarkEvent(const char *format,...) VTK_FORMAT_PRINTF(1
Record a timing event.
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()
unsigned char Indent
LogEntryType Type
std::string Event
static vtkTimerLogCleanup vtkTimerLogCleanupInstance
#define VTK_FILEPATH