VTK  9.2.6
vtkTimerLog.h
Go to the documentation of this file.
1/*=========================================================================
2
3 Program: Visualization Toolkit
4 Module: vtkTimerLog.h
5
6 Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7 All rights reserved.
8 See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9
10 This software is distributed WITHOUT ANY WARRANTY; without even
11 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12 PURPOSE. See the above copyright notice for more information.
13
14=========================================================================*/
43#ifndef vtkTimerLog_h
44#define vtkTimerLog_h
45
46#include "vtkCommonSystemModule.h" // For export macro
47#include "vtkObject.h"
48
49#include <string> // STL Header
50
51#ifdef _WIN32
52#include <sys/timeb.h> // Needed for Win32 implementation of timer
53#include <sys/types.h> // Needed for Win32 implementation of timer
54#else
55#include <sys/time.h> // Needed for unix implementation of timer
56#include <sys/times.h> // Needed for unix implementation of timer
57#include <sys/types.h> // Needed for unix implementation of timer
58#include <time.h> // Needed for unix implementation of timer
59#endif
60
61// var args
62#ifndef _WIN32
63#include <unistd.h> // Needed for unix implementation of timer
64#endif
65
66// select stuff here is for sleep method
67#ifndef NO_FD_SET
68#define SELECT_MASK fd_set
69#else
70#ifndef _AIX
71typedef long fd_mask;
72#endif
73#if defined(_IBMR2)
74#define SELECT_MASK void
75#else
76#define SELECT_MASK int
77#endif
78#endif
79
81{
83 {
84 INVALID = -1,
85 STANDALONE, // an individual, marked event
86 START, // start of a timed event
87 END, // end of a timed event
88 INSERTED // externally timed value
89 };
90 double WallTime;
92 std::string Event;
94 unsigned char Indent;
96 : WallTime(0)
97 , CpuTicks(0)
98 , Type(INVALID)
99 , Indent(0)
100 {
101 }
102};
103
104class VTKCOMMONSYSTEM_EXPORT vtkTimerLog : public vtkObject
105{
106public:
107 static vtkTimerLog* New();
108
109 vtkTypeMacro(vtkTimerLog, vtkObject);
110 void PrintSelf(ostream& os, vtkIndent indent) override;
111
116 static void SetLogging(int v) { vtkTimerLog::Logging = v; }
117 static int GetLogging() { return vtkTimerLog::Logging; }
118 static void LoggingOn() { vtkTimerLog::SetLogging(1); }
120
122
125 static void SetMaxEntries(int a);
126 static int GetMaxEntries();
128
133#ifndef __VTK_WRAP__
134 static void FormatAndMarkEvent(const char* format, ...) VTK_FORMAT_PRINTF(1, 2);
135#endif
136
138
142 static void DumpLog(VTK_FILEPATH const char* filename);
144
146
151 static void MarkStartEvent(const char* EventString);
152 static void MarkEndEvent(const char* EventString);
154
156
160 static void InsertTimedEvent(const char* EventString, double time, int cpuTicks);
162
163 static void DumpLogWithIndents(ostream* os, double threshold);
164 static void DumpLogWithIndentsAndPercentages(ostream* os);
165
167
170 static int GetNumberOfEvents();
171 static int GetEventIndent(int i);
172 static double GetEventWallTime(int i);
173 static const char* GetEventString(int i);
176
180 static void MarkEvent(const char* EventString);
181
186 static void ResetLog();
187
191 static void CleanupLog();
192
197 static double GetUniversalTime();
198
203 static double GetCPUTime();
204
209
213 void StopTimer();
214
220
221protected:
223 {
224 this->StartTime = 0;
225 this->EndTime = 0;
226 } // ensure constructor/destructor protected
227 ~vtkTimerLog() override = default;
228
229 static int Logging;
230 static int Indent;
231 static int MaxEntries;
232 static int NextEntry;
233 static int WrapFlag;
234 static int TicksPerSecond;
235
236#ifdef _WIN32
237#ifndef _WIN32_WCE
238 static timeb FirstWallTime;
239 static timeb CurrentWallTime;
240#else
241 static FILETIME FirstWallTime;
242 static FILETIME CurrentWallTime;
243#endif
244#else
245 static timeval FirstWallTime;
246 static timeval CurrentWallTime;
247 static tms FirstCpuTicks;
248 static tms CurrentCpuTicks;
249#endif
250
254 static void MarkEventInternal(const char* EventString, vtkTimerLogEntry::LogEntryType type,
255 vtkTimerLogEntry* entry = nullptr);
256
257 // instance variables to support simple timing functionality,
258 // separate from timer table logging.
259 double StartTime;
260 double EndTime;
261
263
264 static void DumpEntry(ostream& os, int index, double time, double deltatime, int tick,
265 int deltatick, const char* event);
266
267private:
268 vtkTimerLog(const vtkTimerLog&) = delete;
269 void operator=(const vtkTimerLog&) = delete;
270};
271
276{
277public:
278 vtkTimerLogScope(const char* eventString)
279 {
280 if (eventString)
281 {
282 this->EventString = eventString;
283 }
284 vtkTimerLog::MarkStartEvent(eventString);
285 }
286
288
289protected:
290 std::string EventString;
291
292private:
293 vtkTimerLogScope(const vtkTimerLogScope&) = delete;
294 void operator=(const vtkTimerLogScope&) = delete;
295};
296
297//
298// Set built-in type. Creates member Set"name"() (e.g., SetVisibility());
299//
300#define vtkTimerLogMacro(string) \
301 { \
302 vtkTimerLog::FormatAndMarkEvent( \
303 "Mark: In %s, line %d, class %s: %s", __FILE__, __LINE__, this->GetClassName(), string); \
304 }
305
306// Implementation detail for Schwarz counter idiom.
307class VTKCOMMONSYSTEM_EXPORT vtkTimerLogCleanup
308{
309public:
312
313private:
314 vtkTimerLogCleanup(const vtkTimerLogCleanup&) = delete;
315 void operator=(const vtkTimerLogCleanup&) = delete;
316};
318
319#endif
a simple class to control print indentation
Definition vtkIndent.h:49
abstract base class for most VTK objects
Definition vtkObject.h:72
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
Definition vtkTimerLog.h:94
LogEntryType Type
Definition vtkTimerLog.h:93
std::string Event
Definition vtkTimerLog.h:92
static vtkTimerLogCleanup vtkTimerLogCleanupInstance
#define VTK_FILEPATH