VTK  9.1.0
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=========================================================================*/
137#ifndef vtkTimerLog_h
138#define vtkTimerLog_h
139
140#include "vtkCommonSystemModule.h" // For export macro
141#include "vtkObject.h"
142
143#include <string> // STL Header
144
145#ifdef _WIN32
146#include <sys/timeb.h> // Needed for Win32 implementation of timer
147#include <sys/types.h> // Needed for Win32 implementation of timer
148#else
149#include <sys/time.h> // Needed for unix implementation of timer
150#include <sys/times.h> // Needed for unix implementation of timer
151#include <sys/types.h> // Needed for unix implementation of timer
152#include <time.h> // Needed for unix implementation of timer
153#endif
154
155// var args
156#ifndef _WIN32
157#include <unistd.h> // Needed for unix implementation of timer
158#endif
159
160// select stuff here is for sleep method
161#ifndef NO_FD_SET
162#define SELECT_MASK fd_set
163#else
164#ifndef _AIX
165typedef long fd_mask;
166#endif
167#if defined(_IBMR2)
168#define SELECT_MASK void
169#else
170#define SELECT_MASK int
171#endif
172#endif
173
175{
177 {
179 STANDALONE, // an individual, marked event
180 START, // start of a timed event
181 END, // end of a timed event
182 INSERTED // externally timed value
183 };
184 double WallTime;
188 unsigned char Indent;
190 : WallTime(0)
191 , CpuTicks(0)
192 , Type(INVALID)
193 , Indent(0)
194 {
195 }
196};
197
198class VTKCOMMONSYSTEM_EXPORT vtkTimerLog : public vtkObject
199{
200public:
201 static vtkTimerLog* New();
202
203 vtkTypeMacro(vtkTimerLog, vtkObject);
204 void PrintSelf(ostream& os, vtkIndent indent) override;
205
210 static void SetLogging(int v) { vtkTimerLog::Logging = v; }
211 static int GetLogging() { return vtkTimerLog::Logging; }
212 static void LoggingOn() { vtkTimerLog::SetLogging(1); }
214
216
219 static void SetMaxEntries(int a);
220 static int GetMaxEntries();
222
227#ifndef __VTK_WRAP__
228 static void FormatAndMarkEvent(const char* format, ...) VTK_FORMAT_PRINTF(1, 2);
229#endif
230
232
236 static void DumpLog(VTK_FILEPATH const char* filename);
238
240
245 static void MarkStartEvent(const char* EventString);
246 static void MarkEndEvent(const char* EventString);
248
250
254 static void InsertTimedEvent(const char* EventString, double time, int cpuTicks);
256
257 static void DumpLogWithIndents(ostream* os, double threshold);
258 static void DumpLogWithIndentsAndPercentages(ostream* os);
259
261
264 static int GetNumberOfEvents();
265 static int GetEventIndent(int i);
266 static double GetEventWallTime(int i);
267 static const char* GetEventString(int i);
270
274 static void MarkEvent(const char* EventString);
275
280 static void ResetLog();
281
285 static void CleanupLog();
286
291 static double GetUniversalTime();
292
297 static double GetCPUTime();
298
303
307 void StopTimer();
308
314
315protected:
317 {
318 this->StartTime = 0;
319 this->EndTime = 0;
320 } // ensure constructor/destructor protected
321 ~vtkTimerLog() override = default;
322
323 static int Logging;
324 static int Indent;
325 static int MaxEntries;
326 static int NextEntry;
327 static int WrapFlag;
328 static int TicksPerSecond;
329
330#ifdef _WIN32
331#ifndef _WIN32_WCE
332 static timeb FirstWallTime;
333 static timeb CurrentWallTime;
334#else
335 static FILETIME FirstWallTime;
336 static FILETIME CurrentWallTime;
337#endif
338#else
339 static timeval FirstWallTime;
340 static timeval CurrentWallTime;
341 static tms FirstCpuTicks;
342 static tms CurrentCpuTicks;
343#endif
344
348 static void MarkEventInternal(const char* EventString, vtkTimerLogEntry::LogEntryType type,
349 vtkTimerLogEntry* entry = nullptr);
350
351 // instance variables to support simple timing functionality,
352 // separate from timer table logging.
353 double StartTime;
354 double EndTime;
355
357
358 static void DumpEntry(ostream& os, int index, double time, double deltatime, int tick,
359 int deltatick, const char* event);
360
361private:
362 vtkTimerLog(const vtkTimerLog&) = delete;
363 void operator=(const vtkTimerLog&) = delete;
364};
365
370{
371public:
372 vtkTimerLogScope(const char* eventString)
373 {
374 if (eventString)
375 {
376 this->EventString = eventString;
377 }
378 vtkTimerLog::MarkStartEvent(eventString);
379 }
380
382
383protected:
385
386private:
387 vtkTimerLogScope(const vtkTimerLogScope&) = delete;
388 void operator=(const vtkTimerLogScope&) = delete;
389};
390
391//
392// Set built-in type. Creates member Set"name"() (e.g., SetVisibility());
393//
394#define vtkTimerLogMacro(string) \
395 { \
396 vtkTimerLog::FormatAndMarkEvent( \
397 "Mark: In %s, line %d, class %s: %s", __FILE__, __LINE__, this->GetClassName(), string); \
398 }
399
400// Implementation detail for Schwarz counter idiom.
401class VTKCOMMONSYSTEM_EXPORT vtkTimerLogCleanup
402{
403public:
406
407private:
408 vtkTimerLogCleanup(const vtkTimerLogCleanup&) = delete;
409 void operator=(const vtkTimerLogCleanup&) = delete;
410};
412
413#endif
a simple class to control print indentation
Definition: vtkIndent.h:113
abstract base class for most VTK objects
Definition: vtkObject.h:73
Helper class to log time within scope.
Definition: vtkTimerLog.h:370
vtkTimerLogScope(const char *eventString)
Definition: vtkTimerLog.h:372
std::string EventString
Definition: vtkTimerLog.h:384
Timer support and logging.
Definition: vtkTimerLog.h:199
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:342
static vtkTimerLogEntry::LogEntryType GetEventType(int i)
Programmatic access to events.
double StartTime
Definition: vtkTimerLog.h:353
static int Logging
Definition: vtkTimerLog.h:323
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:326
static timeval CurrentWallTime
Definition: vtkTimerLog.h:340
static int WrapFlag
Definition: vtkTimerLog.h:327
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:328
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:325
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:210
static int GetLogging()
Definition: vtkTimerLog.h:211
static timeval FirstWallTime
Definition: vtkTimerLog.h:339
static tms FirstCpuTicks
Definition: vtkTimerLog.h:341
static void LoggingOn()
Definition: vtkTimerLog.h:212
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:324
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:354
static void DumpEntry(ostream &os, int index, double time, double deltatime, int tick, int deltatick, const char *event)
static void LoggingOff()
Definition: vtkTimerLog.h:213
@ time
Definition: vtkX3D.h:503
@ type
Definition: vtkX3D.h:522
@ index
Definition: vtkX3D.h:252
@ string
Definition: vtkX3D.h:496
unsigned char Indent
Definition: vtkTimerLog.h:188
LogEntryType Type
Definition: vtkTimerLog.h:187
std::string Event
Definition: vtkTimerLog.h:186
static vtkTimerLogCleanup vtkTimerLogCleanupInstance
Definition: vtkTimerLog.h:411
#define VTK_FILEPATH