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=========================================================================*/
143#ifndef vtkTimerLog_h
144#define vtkTimerLog_h
145
146#include "vtkCommonSystemModule.h" // For export macro
147#include "vtkObject.h"
148
149#include <string> // STL Header
150
151#ifdef _WIN32
152#include <sys/timeb.h> // Needed for Win32 implementation of timer
153#include <sys/types.h> // Needed for Win32 implementation of timer
154#else
155#include <sys/time.h> // Needed for unix implementation of timer
156#include <sys/times.h> // Needed for unix implementation of timer
157#include <sys/types.h> // Needed for unix implementation of timer
158#include <time.h> // Needed for unix implementation of timer
159#endif
160
161// var args
162#ifndef _WIN32
163#include <unistd.h> // Needed for unix implementation of timer
164#endif
165
166// select stuff here is for sleep method
167#ifndef NO_FD_SET
168#define SELECT_MASK fd_set
169#else
170#ifndef _AIX
171typedef long fd_mask;
172#endif
173#if defined(_IBMR2)
174#define SELECT_MASK void
175#else
176#define SELECT_MASK int
177#endif
178#endif
179
181{
183 {
185 STANDALONE, // an individual, marked event
186 START, // start of a timed event
187 END, // end of a timed event
188 INSERTED // externally timed value
189 };
190 double WallTime;
194 unsigned char Indent;
196 : WallTime(0)
197 , CpuTicks(0)
198 , Type(INVALID)
199 , Indent(0)
200 {
201 }
202};
203
204class VTKCOMMONSYSTEM_EXPORT vtkTimerLog : public vtkObject
205{
206public:
207 static vtkTimerLog* New();
208
209 vtkTypeMacro(vtkTimerLog, vtkObject);
210 void PrintSelf(ostream& os, vtkIndent indent) override;
211
216 static void SetLogging(int v) { vtkTimerLog::Logging = v; }
217 static int GetLogging() { return vtkTimerLog::Logging; }
218 static void LoggingOn() { vtkTimerLog::SetLogging(1); }
220
222
225 static void SetMaxEntries(int a);
226 static int GetMaxEntries();
228
233#ifndef __VTK_WRAP__
234 static void FormatAndMarkEvent(const char* format, ...) VTK_FORMAT_PRINTF(1, 2);
235#endif
236
238
242 static void DumpLog(VTK_FILEPATH const char* filename);
244
246
251 static void MarkStartEvent(const char* EventString);
252 static void MarkEndEvent(const char* EventString);
254
256
260 static void InsertTimedEvent(const char* EventString, double time, int cpuTicks);
262
263 static void DumpLogWithIndents(ostream* os, double threshold);
264 static void DumpLogWithIndentsAndPercentages(ostream* os);
265
267
270 static int GetNumberOfEvents();
271 static int GetEventIndent(int i);
272 static double GetEventWallTime(int i);
273 static const char* GetEventString(int i);
276
280 static void MarkEvent(const char* EventString);
281
286 static void ResetLog();
287
291 static void CleanupLog();
292
297 static double GetUniversalTime();
298
303 static double GetCPUTime();
304
309
313 void StopTimer();
314
320
321protected:
323 {
324 this->StartTime = 0;
325 this->EndTime = 0;
326 } // ensure constructor/destructor protected
327 ~vtkTimerLog() override = default;
328
329 static int Logging;
330 static int Indent;
331 static int MaxEntries;
332 static int NextEntry;
333 static int WrapFlag;
334 static int TicksPerSecond;
335
336#ifdef _WIN32
337#ifndef _WIN32_WCE
338 static timeb FirstWallTime;
339 static timeb CurrentWallTime;
340#else
341 static FILETIME FirstWallTime;
342 static FILETIME CurrentWallTime;
343#endif
344#else
345 static timeval FirstWallTime;
346 static timeval CurrentWallTime;
347 static tms FirstCpuTicks;
348 static tms CurrentCpuTicks;
349#endif
350
354 static void MarkEventInternal(const char* EventString, vtkTimerLogEntry::LogEntryType type,
355 vtkTimerLogEntry* entry = nullptr);
356
357 // instance variables to support simple timing functionality,
358 // separate from timer table logging.
359 double StartTime;
360 double EndTime;
361
363
364 static void DumpEntry(ostream& os, int index, double time, double deltatime, int tick,
365 int deltatick, const char* event);
366
367private:
368 vtkTimerLog(const vtkTimerLog&) = delete;
369 void operator=(const vtkTimerLog&) = delete;
370};
371
376{
377public:
378 vtkTimerLogScope(const char* eventString)
379 {
380 if (eventString)
381 {
382 this->EventString = eventString;
383 }
384 vtkTimerLog::MarkStartEvent(eventString);
385 }
386
388
389protected:
391
392private:
393 vtkTimerLogScope(const vtkTimerLogScope&) = delete;
394 void operator=(const vtkTimerLogScope&) = delete;
395};
396
397//
398// Set built-in type. Creates member Set"name"() (e.g., SetVisibility());
399//
400#define vtkTimerLogMacro(string) \
401 { \
402 vtkTimerLog::FormatAndMarkEvent( \
403 "Mark: In %s, line %d, class %s: %s", __FILE__, __LINE__, this->GetClassName(), string); \
404 }
405
406// Implementation detail for Schwarz counter idiom.
407class VTKCOMMONSYSTEM_EXPORT vtkTimerLogCleanup
408{
409public:
412
413private:
414 vtkTimerLogCleanup(const vtkTimerLogCleanup&) = delete;
415 void operator=(const vtkTimerLogCleanup&) = delete;
416};
418
419#endif
a simple class to control print indentation
Definition: vtkIndent.h:119
abstract base class for most VTK objects
Definition: vtkObject.h:82
Helper class to log time within scope.
Definition: vtkTimerLog.h:376
vtkTimerLogScope(const char *eventString)
Definition: vtkTimerLog.h:378
std::string EventString
Definition: vtkTimerLog.h:390
Timer support and logging.
Definition: vtkTimerLog.h:205
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:348
static vtkTimerLogEntry::LogEntryType GetEventType(int i)
Programmatic access to events.
double StartTime
Definition: vtkTimerLog.h:359
static int Logging
Definition: vtkTimerLog.h:329
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:332
static timeval CurrentWallTime
Definition: vtkTimerLog.h:346
static int WrapFlag
Definition: vtkTimerLog.h:333
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:334
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:331
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:216
static int GetLogging()
Definition: vtkTimerLog.h:217
static timeval FirstWallTime
Definition: vtkTimerLog.h:345
static tms FirstCpuTicks
Definition: vtkTimerLog.h:347
static void LoggingOn()
Definition: vtkTimerLog.h:218
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:330
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:360
static void DumpEntry(ostream &os, int index, double time, double deltatime, int tick, int deltatick, const char *event)
static void LoggingOff()
Definition: vtkTimerLog.h:219
@ 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:194
LogEntryType Type
Definition: vtkTimerLog.h:193
std::string Event
Definition: vtkTimerLog.h:192
static vtkTimerLogCleanup vtkTimerLogCleanupInstance
Definition: vtkTimerLog.h:417
#define VTK_FILEPATH