opensurgsim
Timer.h
1 // This file is a part of the OpenSurgSim project.
2 // Copyright 2013, SimQuest Solutions Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #ifndef SURGSIM_FRAMEWORK_TIMER_H
17 #define SURGSIM_FRAMEWORK_TIMER_H
18 
19 #include <boost/chrono.hpp>
20 #include <boost/thread/shared_mutex.hpp>
21 #include <deque>
22 #include <atomic>
23 
24 namespace SurgSim
25 {
26 namespace Framework
27 {
28 
31 class Timer
32 {
33 public:
35  Timer();
36 
38  void start();
39 
41  void beginFrame();
42 
46  void endFrame();
47 
49  void markFrame();
50 
53  double getCumulativeTime() const;
54 
57  double getCurrentTime();
58 
61  double getAverageFramePeriod() const;
62 
65  double getAverageFrameRate() const;
66 
70  double getLastFramePeriod() const;
71 
74  double getLastFrameRate() const;
75 
77  void setMaxNumberOfFrames(size_t numberOfFrames);
78 
80  size_t getMaxNumberOfFrames();
81 
83  size_t getCurrentNumberOfFrames() const;
84 
87  size_t getNumberOfClockFails() const;
88 
90  double getMaxFramePeriod() const;
91 
93  double getMinFramePeriod() const;
94 
96  bool isBufferFull() const;
97 
98 private:
100  typedef boost::chrono::steady_clock TimerClock;
101 
103  typedef boost::chrono::duration<double> TimerDuration;
104 
106  typedef boost::chrono::time_point<TimerClock, TimerDuration> TimerTimePoint;
107 
110  TimerTimePoint now();
111 
113  static const TimerClock m_clock;
114 
116  TimerTimePoint m_lastTime;
117 
119  size_t m_maxNumberOfFrames;
120 
122  std::deque<TimerDuration> m_frameDurations;
123 
125  mutable boost::shared_mutex m_sharedMutex;
126 
128  std::atomic<size_t> m_clockFails;
129 };
130 
131 } // Framework
132 } // SurgSim
133 
134 #endif
Wraps glewInit() to separate the glew opengl definitions from the osg opengl definitions only imgui n...
Definition: AddRandomSphereBehavior.cpp:36
Timer()
Instantiate a TimerClock and start a timing run.
Definition: Timer.cpp:26
double getCumulativeTime() const
Return the sum of the durations over all the stored frames.
Definition: Timer.cpp:70
double getAverageFrameRate() const
Return the inverse of the average duration across all stored frames.
Definition: Timer.cpp:111
Timer class, measures execution times.
Definition: Timer.h:31
size_t getCurrentNumberOfFrames() const
Definition: Timer.cpp:147
double getMaxFramePeriod() const
Definition: Timer.cpp:170
double getLastFrameRate() const
Return the inverse of the duration of the most-recent frame.
Definition: Timer.cpp:125
double getLastFramePeriod() const
Return the duration of the most-recent frame (time between last endFrame and the previous start...
Definition: Timer.cpp:116
void setMaxNumberOfFrames(size_t numberOfFrames)
Set the maximum number of frames to store.
Definition: Timer.cpp:130
void start()
Begin a timing run by clearing out any stored frames and beginning a frame.
Definition: Timer.cpp:32
void endFrame()
End this frame by storing the duration since the current frame was begun.
Definition: Timer.cpp:48
double getMinFramePeriod() const
Definition: Timer.cpp:179
size_t getNumberOfClockFails() const
Definition: Timer.cpp:154
double getCurrentTime()
Return the amount of time spent in the current frame.
Definition: Timer.cpp:85
bool isBufferFull() const
Definition: Timer.cpp:188
size_t getMaxNumberOfFrames()
Definition: Timer.cpp:142
double getAverageFramePeriod() const
Return the average duration across all stored frames.
Definition: Timer.cpp:92
void markFrame()
End the current frame and begin a new frame.
Definition: Timer.cpp:64
void beginFrame()
Begin a frame (storing the current time).
Definition: Timer.cpp:43