VTK  9.0.20200927
vtkRenderTimerLog.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkRenderTimerLog.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 =========================================================================*/
15 
49 #ifndef vtkRenderTimerLog_h
50 #define vtkRenderTimerLog_h
51 
52 #include "vtkObject.h"
53 #include "vtkRenderingCoreModule.h" // For export macro
54 #include "vtkType.h" // For vtkTypeUint64, etc
55 #include <sstream> // for std::ostringstream
56 #include <string> // for std::string
57 #include <vector> // for std::vector
58 
63 #define VTK_SCOPED_RENDER_EVENT(eventName, timer) VTK_SCOPED_RENDER_EVENT2(eventName, timer, _event)
64 
70 #define VTK_SCOPED_RENDER_EVENT2(eventName, timer, identifier) \
71  vtkRenderTimerLog::ScopedEventLogger identifier; \
72  do \
73  { \
74  std::ostringstream _eventNameStream; \
75  _eventNameStream << eventName; \
76  identifier = timer->StartScopedEvent(_eventNameStream.str()); \
77  (void)identifier; /* Prevent set-but-not-used var warnings */ \
78  } while (false) /* Do-while loop prevents duplicate semicolon warnings */
79 
80 class VTKRENDERINGCORE_EXPORT vtkRenderTimerLog : public vtkObject
81 {
82 public:
83  struct Frame;
84 
86  struct VTKRENDERINGCORE_EXPORT Event
87  {
90 
92  vtkTypeUInt64 StartTime;
93  vtkTypeUInt64 EndTime;
97  float ElapsedTimeSeconds() const { return this->ElapsedTimeNanoseconds() * 1e-9f; }
98  float ElapsedTimeMilliseconds() const { return this->ElapsedTimeNanoseconds() * 1e-6f; }
99  vtkTypeUInt64 ElapsedTimeNanoseconds() const { return this->EndTime - this->StartTime; }
100 
102  std::vector<Event> Events;
103 
109  void Print(std::ostream& os, float threshMs = 0.f, vtkIndent indent = vtkIndent())
110  {
111  this->Print(os, 0.f, threshMs, indent);
112  }
113 
115 
116  protected:
117  void Print(std::ostream& os, float parentTime, float threshMs, vtkIndent indent);
118  };
119 
121  struct VTKRENDERINGCORE_EXPORT Frame
122  {
123  std::vector<Event> Events;
124 
129  void Print(std::ostream& os, float threshMs = 0.f);
130  };
131 
137  struct VTKRENDERINGCORE_EXPORT ScopedEventLogger
138  {
140  : Log(nullptr)
141  {
142  }
144  ScopedEventLogger& operator=(ScopedEventLogger&& other);
145  ~ScopedEventLogger() { this->Stop(); }
146  void Stop();
147  friend class vtkRenderTimerLog;
148 
149  protected:
151  : Log(log)
152  {
153  }
154 
155  private:
156  void operator=(const ScopedEventLogger&) = delete;
157  ScopedEventLogger(const ScopedEventLogger& other) = delete;
158  vtkRenderTimerLog* Log;
159  };
160 
161  static vtkRenderTimerLog* New();
162  vtkTypeMacro(vtkRenderTimerLog, vtkObject);
163  void PrintSelf(ostream& os, vtkIndent indent) override;
164 
169  virtual bool IsSupported();
170 
175  virtual void MarkFrame();
176 
181 
185  virtual void MarkStartEvent(const std::string& name);
186  virtual void MarkEndEvent();
192  virtual bool FrameReady();
193 
198  virtual Frame PopFirstReadyFrame();
199 
201  vtkSetMacro(LoggingEnabled, bool);
202  vtkGetMacro(LoggingEnabled, bool);
203  vtkBooleanMacro(LoggingEnabled, bool);
211  vtkSetMacro(FrameLimit, unsigned int);
212  vtkGetMacro(FrameLimit, unsigned int);
218  virtual void ReleaseGraphicsResources();
219 
220 protected:
222  ~vtkRenderTimerLog() override;
223 
225  unsigned int FrameLimit;
226 
227 private:
228  vtkRenderTimerLog(const vtkRenderTimerLog&) = delete;
229  void operator=(const vtkRenderTimerLog&) = delete;
230 };
231 
232 #endif // vtkRenderTimerLog_h
vtkRenderTimerLog::Event::StartTime
vtkTypeUInt64 StartTime
Times are in nanoseconds.
Definition: vtkRenderTimerLog.h:92
vtkRenderTimerLog::Frame::Events
std::vector< Event > Events
Definition: vtkRenderTimerLog.h:123
vtkRenderTimerLog::Event::Name
std::string Name
Event name.
Definition: vtkRenderTimerLog.h:89
vtkRenderTimerLog::Event::ElapsedTimeSeconds
float ElapsedTimeSeconds() const
Convenience methods to compute times.
Definition: vtkRenderTimerLog.h:97
vtkRenderTimerLog::Event::ElapsedTimeNanoseconds
vtkTypeUInt64 ElapsedTimeNanoseconds() const
Definition: vtkRenderTimerLog.h:99
vtkRenderTimerLog::MarkFrame
virtual void MarkFrame()
Call to mark the start of a new frame, or the end of an old one.
vtkRenderTimerLog::Event::ElapsedTimeMilliseconds
float ElapsedTimeMilliseconds() const
Definition: vtkRenderTimerLog.h:98
vtkRenderTimerLog::ScopedEventLogger::~ScopedEventLogger
~ScopedEventLogger()
Definition: vtkRenderTimerLog.h:145
vtkRenderTimerLog::StartScopedEvent
ScopedEventLogger StartScopedEvent(const std::string &name)
Create a RAII scoped event.
vtkObject
abstract base class for most VTK objects
Definition: vtkObject.h:62
vtkRenderTimerLog::Event::Print
void Print(std::ostream &os, float threshMs=0.f, vtkIndent indent=vtkIndent())
Print details of the event to a stream.
Definition: vtkRenderTimerLog.h:109
vtkRenderTimerLog::LoggingEnabled
bool LoggingEnabled
Definition: vtkRenderTimerLog.h:224
vtkType.h
vtx::helper::Print
void Print(const std::vector< T > &input, const std::string &name)
Print a vector with an associated name.
vtkRenderTimerLog::New
static vtkRenderTimerLog * New()
vtkRenderTimerLog::vtkRenderTimerLog
vtkRenderTimerLog()
vtkRenderTimerLog::IsSupported
virtual bool IsSupported()
Returns true if stream timings are implemented for the current graphics backend.
vtkIndent
a simple class to control print indentation
Definition: vtkIndent.h:33
vtkRenderTimerLog::MarkEndEvent
virtual void MarkEndEvent()
Mark the beginning or end of an event.
vtkRenderTimerLog::ScopedEventLogger::ScopedEventLogger
ScopedEventLogger(vtkRenderTimerLog *log)
Definition: vtkRenderTimerLog.h:150
vtkRenderTimerLog::ReleaseGraphicsResources
virtual void ReleaseGraphicsResources()
Releases any resources allocated on the graphics device.
vtkRenderTimerLog::PrintSelf
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
vtkRenderTimerLog::MarkStartEvent
virtual void MarkStartEvent(const std::string &name)
Mark the beginning or end of an event.
vtkX3D::name
Definition: vtkX3D.h:225
vtkObject.h
vtkRenderTimerLog::Frame
Container for a frame's events.
Definition: vtkRenderTimerLog.h:121
vtkRenderTimerLog::Event::EndTime
vtkTypeUInt64 EndTime
Times are in nanoseconds.
Definition: vtkRenderTimerLog.h:93
vtkRenderTimerLog
Asynchronously measures GPU execution times for a series of events.
Definition: vtkRenderTimerLog.h:80
vtkRenderTimerLog::PopFirstReadyFrame
virtual Frame PopFirstReadyFrame()
Retrieve the first available frame's timing info.
vtkRenderTimerLog::ScopedEventLogger::ScopedEventLogger
ScopedEventLogger()
Definition: vtkRenderTimerLog.h:139
vtkX3D::string
Definition: vtkX3D.h:496
vtkRenderTimerLog::FrameReady
virtual bool FrameReady()
Returns true if there are any frames ready with complete timing info.
vtkRenderTimerLog::ScopedEventLogger
RAII struct for logging events.
Definition: vtkRenderTimerLog.h:137
vtkRenderTimerLog::Event
Container for a single timed event.
Definition: vtkRenderTimerLog.h:86
vtkRenderTimerLog::Event::Events
std::vector< Event > Events
Child events that occurred while this event was running.
Definition: vtkRenderTimerLog.h:102
vtkRenderTimerLog::FrameLimit
unsigned int FrameLimit
Definition: vtkRenderTimerLog.h:225
vtkRenderTimerLog::~vtkRenderTimerLog
~vtkRenderTimerLog() override