VTK  9.3.20240328
vtkRenderTimerLog.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2 // SPDX-License-Identifier: BSD-3-Clause
3 
40 #ifndef vtkRenderTimerLog_h
41 #define vtkRenderTimerLog_h
42 
43 #include "vtkObject.h"
44 #include "vtkRenderingCoreModule.h" // For export macro
45 #include "vtkType.h" // For vtkTypeUint64, etc
46 #include <sstream> // for std::ostringstream
47 #include <string> // for std::string
48 #include <vector> // for std::vector
49 
54 #define VTK_SCOPED_RENDER_EVENT(eventName, timer) VTK_SCOPED_RENDER_EVENT2(eventName, timer, _event)
55 
61 #define VTK_SCOPED_RENDER_EVENT2(eventName, timer, identifier) \
62  vtkRenderTimerLog::ScopedEventLogger identifier; \
63  do \
64  { \
65  std::ostringstream _eventNameStream; \
66  _eventNameStream << eventName; \
67  identifier = timer->StartScopedEvent(_eventNameStream.str()); \
68  (void)identifier; /* Prevent set-but-not-used var warnings */ \
69  } while (false) /* Do-while loop prevents duplicate semicolon warnings */
70 
71 VTK_ABI_NAMESPACE_BEGIN
72 class VTKRENDERINGCORE_EXPORT vtkRenderTimerLog : public vtkObject
73 {
74 public:
75  struct Frame;
76 
78  struct VTKRENDERINGCORE_EXPORT Event
79  {
82 
84  vtkTypeUInt64 StartTime;
85  vtkTypeUInt64 EndTime;
89  float ElapsedTimeSeconds() const { return this->ElapsedTimeNanoseconds() * 1e-9f; }
90  float ElapsedTimeMilliseconds() const { return this->ElapsedTimeNanoseconds() * 1e-6f; }
91  vtkTypeUInt64 ElapsedTimeNanoseconds() const { return this->EndTime - this->StartTime; }
92 
94  std::vector<Event> Events;
95 
101  void Print(std::ostream& os, float threshMs = 0.f, vtkIndent indent = vtkIndent())
102  {
103  this->Print(os, 0.f, threshMs, indent);
104  }
105 
107 
108  protected:
109  void Print(std::ostream& os, float parentTime, float threshMs, vtkIndent indent);
110  };
111 
113  struct VTKRENDERINGCORE_EXPORT Frame
114  {
115  std::vector<Event> Events;
116 
121  void Print(std::ostream& os, float threshMs = 0.f);
122  };
123 
129  struct VTKRENDERINGCORE_EXPORT ScopedEventLogger
130  {
132  : Log(nullptr)
133  {
134  }
137  ~ScopedEventLogger() { this->Stop(); }
138  void Stop();
139  friend class vtkRenderTimerLog;
140 
141  protected:
143  : Log(log)
144  {
145  }
146 
147  private:
148  void operator=(const ScopedEventLogger&) = delete;
149  ScopedEventLogger(const ScopedEventLogger& other) = delete;
150  vtkRenderTimerLog* Log;
151  };
152 
154  vtkTypeMacro(vtkRenderTimerLog, vtkObject);
155  void PrintSelf(ostream& os, vtkIndent indent) override;
156 
161  virtual bool IsSupported() VTK_FUTURE_CONST;
162 
167  virtual void MarkFrame();
168 
173 
177  virtual void MarkStartEvent(const std::string& name);
178  virtual void MarkEndEvent();
184  virtual bool FrameReady();
185 
191 
193  vtkSetMacro(LoggingEnabled, bool);
194  vtkGetMacro(LoggingEnabled, bool);
195  vtkBooleanMacro(LoggingEnabled, bool);
203  vtkSetMacro(FrameLimit, unsigned int);
204  vtkGetMacro(FrameLimit, unsigned int);
210  virtual void ReleaseGraphicsResources();
211 
212 protected:
214  ~vtkRenderTimerLog() override;
215 
216  mutable bool LoggingEnabled;
217  unsigned int FrameLimit;
218 
219 private:
220  vtkRenderTimerLog(const vtkRenderTimerLog&) = delete;
221  void operator=(const vtkRenderTimerLog&) = delete;
222 };
223 
224 VTK_ABI_NAMESPACE_END
225 #endif // vtkRenderTimerLog_h
a simple class to control print indentation
Definition: vtkIndent.h:108
void Print(ostream &os)
Print an object to an ostream.
abstract base class for most VTK objects
Definition: vtkObject.h:161
Asynchronously measures GPU execution times for a series of events.
virtual void MarkStartEvent(const std::string &name)
Mark the beginning or end of an event.
static vtkRenderTimerLog * New()
virtual void MarkEndEvent()
Mark the beginning or end of an event.
virtual void MarkFrame()
Call to mark the start of a new frame, or the end of an old one.
ScopedEventLogger StartScopedEvent(const std::string &name)
Create a RAII scoped event.
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
virtual void ReleaseGraphicsResources()
Releases any resources allocated on the graphics device.
unsigned int FrameLimit
virtual bool IsSupported() VTK_FUTURE_CONST
Returns true if stream timings are implemented for the current graphics backend.
virtual Frame PopFirstReadyFrame()
Retrieve the first available frame's timing info.
virtual bool FrameReady()
Returns true if there are any frames ready with complete timing info.
@ name
Definition: vtkX3D.h:219
@ string
Definition: vtkX3D.h:490
Container for a single timed event.
vtkTypeUInt64 EndTime
Times are in nanoseconds.
void Print(std::ostream &os, float parentTime, float threshMs, vtkIndent indent)
std::vector< Event > Events
Child events that occurred while this event was running.
float ElapsedTimeMilliseconds() const
void Print(std::ostream &os, float threshMs=0.f, vtkIndent indent=vtkIndent())
Print details of the event to a stream.
vtkTypeUInt64 StartTime
Times are in nanoseconds.
vtkTypeUInt64 ElapsedTimeNanoseconds() const
std::string Name
Event name.
float ElapsedTimeSeconds() const
Convenience methods to compute times.
Container for a frame's events.
std::vector< Event > Events
void Print(std::ostream &os, float threshMs=0.f)
Print details of all events in this frame to a stream.
RAII struct for logging events.
ScopedEventLogger(vtkRenderTimerLog *log)
ScopedEventLogger(ScopedEventLogger &&other) noexcept
ScopedEventLogger & operator=(ScopedEventLogger &&other) noexcept