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=========================================================================*/
34#ifndef vtkTimerLog_h
35#define vtkTimerLog_h
36
37#include "vtkCommonSystemModule.h" // For export macro
38#include "vtkObject.h"
39
40#include <string> // STL Header
41
42#ifdef _WIN32
43#include <sys/timeb.h> // Needed for Win32 implementation of timer
44#include <sys/types.h> // Needed for Win32 implementation of timer
45#else
46#include <sys/time.h> // Needed for unix implementation of timer
47#include <sys/times.h> // Needed for unix implementation of timer
48#include <sys/types.h> // Needed for unix implementation of timer
49#include <time.h> // Needed for unix implementation of timer
50#endif
51
52// var args
53#ifndef _WIN32
54#include <unistd.h> // Needed for unix implementation of timer
55#endif
56
57// select stuff here is for sleep method
58#ifndef NO_FD_SET
59#define SELECT_MASK fd_set
60#else
61#ifndef _AIX
62typedef long fd_mask;
63#endif
64#if defined(_IBMR2)
65#define SELECT_MASK void
66#else
67#define SELECT_MASK int
68#endif
69#endif
70
72{
74 {
75 INVALID = -1,
76 STANDALONE, // an individual, marked event
77 START, // start of a timed event
78 END, // end of a timed event
79 INSERTED // externally timed value
80 };
81 double WallTime;
83 std::string Event;
85 unsigned char Indent;
87 : WallTime(0)
88 , CpuTicks(0)
89 , Type(INVALID)
90 , Indent(0)
91 {
92 }
93};
94
95class VTKCOMMONSYSTEM_EXPORT vtkTimerLog : public vtkObject
96{
97public:
98 static vtkTimerLog* New();
99
100 vtkTypeMacro(vtkTimerLog, vtkObject);
101 void PrintSelf(ostream& os, vtkIndent indent) override;
102
107 static void SetLogging(int v) { vtkTimerLog::Logging = v; }
108 static int GetLogging() { return vtkTimerLog::Logging; }
109 static void LoggingOn() { vtkTimerLog::SetLogging(1); }
111
113
116 static void SetMaxEntries(int a);
117 static int GetMaxEntries();
119
124#ifndef __VTK_WRAP__
125 static void FormatAndMarkEvent(const char* format, ...) VTK_FORMAT_PRINTF(1, 2);
126#endif
127
129
133 static void DumpLog(VTK_FILEPATH const char* filename);
135
137
142 static void MarkStartEvent(const char* EventString);
143 static void MarkEndEvent(const char* EventString);
145
147
151 static void InsertTimedEvent(const char* EventString, double time, int cpuTicks);
153
154 static void DumpLogWithIndents(ostream* os, double threshold);
155 static void DumpLogWithIndentsAndPercentages(ostream* os);
156
158
161 static int GetNumberOfEvents();
162 static int GetEventIndent(int i);
163 static double GetEventWallTime(int i);
164 static const char* GetEventString(int i);
167
171 static void MarkEvent(const char* EventString);
172
177 static void ResetLog();
178
182 static void CleanupLog();
183
188 static double GetUniversalTime();
189
194 static double GetCPUTime();
195
200
204 void StopTimer();
205
211
212protected:
214 {
215 this->StartTime = 0;
216 this->EndTime = 0;
217 } // ensure constructor/destructor protected
218 ~vtkTimerLog() override = default;
219
220 static int Logging;
221 static int Indent;
222 static int MaxEntries;
223 static int NextEntry;
224 static int WrapFlag;
225 static int TicksPerSecond;
226
227#ifdef _WIN32
228#ifndef _WIN32_WCE
229 static timeb FirstWallTime;
230 static timeb CurrentWallTime;
231#else
232 static FILETIME FirstWallTime;
233 static FILETIME CurrentWallTime;
234#endif
235#else
236 static timeval FirstWallTime;
237 static timeval CurrentWallTime;
238 static tms FirstCpuTicks;
239 static tms CurrentCpuTicks;
240#endif
241
245 static void MarkEventInternal(const char* EventString, vtkTimerLogEntry::LogEntryType type,
246 vtkTimerLogEntry* entry = nullptr);
247
248 // instance variables to support simple timing functionality,
249 // separate from timer table logging.
250 double StartTime;
251 double EndTime;
252
254
255 static void DumpEntry(ostream& os, int index, double time, double deltatime, int tick,
256 int deltatick, const char* event);
257
258private:
259 vtkTimerLog(const vtkTimerLog&) = delete;
260 void operator=(const vtkTimerLog&) = delete;
261};
262
267{
268public:
269 vtkTimerLogScope(const char* eventString)
270 {
271 if (eventString)
272 {
273 this->EventString = eventString;
274 }
275 vtkTimerLog::MarkStartEvent(eventString);
276 }
277
279
280protected:
281 std::string EventString;
282
283private:
284 vtkTimerLogScope(const vtkTimerLogScope&) = delete;
285 void operator=(const vtkTimerLogScope&) = delete;
286};
287
288//
289// Set built-in type. Creates member Set"name"() (e.g., SetVisibility());
290//
291#define vtkTimerLogMacro(string) \
292 { \
293 vtkTimerLog::FormatAndMarkEvent( \
294 "Mark: In %s, line %d, class %s: %s", __FILE__, __LINE__, this->GetClassName(), string); \
295 }
296
297// Implementation detail for Schwarz counter idiom.
298class VTKCOMMONSYSTEM_EXPORT vtkTimerLogCleanup
299{
300public:
303
304private:
305 vtkTimerLogCleanup(const vtkTimerLogCleanup&) = delete;
306 void operator=(const vtkTimerLogCleanup&) = delete;
307};
309
310#endif
a simple class to control print indentation
Definition: vtkIndent.h:40
abstract base class for most VTK objects
Definition: vtkObject.h:63
Helper class to log time within scope.
Definition: vtkTimerLog.h:267
vtkTimerLogScope(const char *eventString)
Definition: vtkTimerLog.h:269
std::string EventString
Definition: vtkTimerLog.h:281
Timer support and logging.
Definition: vtkTimerLog.h:96
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
Definition: vtkTimerLog.h:239
static vtkTimerLogEntry::LogEntryType GetEventType(int i)
Programmatic access to events.
double StartTime
Definition: vtkTimerLog.h:250
static int Logging
Definition: vtkTimerLog.h:220
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
Definition: vtkTimerLog.h:223
static timeval CurrentWallTime
Definition: vtkTimerLog.h:237
static int WrapFlag
Definition: vtkTimerLog.h:224
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
Definition: vtkTimerLog.h:225
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
Definition: vtkTimerLog.h:222
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.
Definition: vtkTimerLog.h:107
static int GetLogging()
Definition: vtkTimerLog.h:108
static timeval FirstWallTime
Definition: vtkTimerLog.h:236
static tms FirstCpuTicks
Definition: vtkTimerLog.h:238
static void LoggingOn()
Definition: vtkTimerLog.h:109
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
Definition: vtkTimerLog.h:221
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
Definition: vtkTimerLog.h:251
static void DumpEntry(ostream &os, int index, double time, double deltatime, int tick, int deltatick, const char *event)
static void LoggingOff()
Definition: vtkTimerLog.h:110
unsigned char Indent
Definition: vtkTimerLog.h:85
LogEntryType Type
Definition: vtkTimerLog.h:84
std::string Event
Definition: vtkTimerLog.h:83
static vtkTimerLogCleanup vtkTimerLogCleanupInstance
Definition: vtkTimerLog.h:308
#define VTK_FILEPATH