Timer.h
Go to the documentation of this file.
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 
23 namespace SurgSim
24 {
25 namespace Framework
26 {
27 
30 class Timer
31 {
32 public:
34  Timer();
35 
37  void start();
38 
40  void beginFrame();
41 
45  void endFrame();
46 
48  void markFrame();
49 
52  double getCumulativeTime() const;
53 
56  double getAverageFramePeriod() const;
57 
60  double getAverageFrameRate() const;
61 
65  double getLastFramePeriod() const;
66 
69  double getLastFrameRate() const;
70 
72  void setMaxNumberOfFrames(size_t numberOfFrames);
73 
75  size_t getMaxNumberOfFrames();
76 
78  size_t getCurrentNumberOfFrames() const;
79 
82  size_t getNumberOfClockFails() const;
83 
85  double getMaxFramePeriod() const;
86 
88  double getMinFramePeriod() const;
89 
91  bool isBufferFull() const;
92 
93 private:
95  typedef boost::chrono::steady_clock TimerClock;
96 
98  typedef boost::chrono::duration<double> TimerDuration;
99 
101  typedef boost::chrono::time_point<TimerClock, TimerDuration> TimerTimePoint;
102 
106 
108  static const TimerClock m_clock;
109 
112 
115 
117  std::deque<TimerDuration> m_frameDurations;
118 
120  mutable boost::shared_mutex m_sharedMutex;
121 
123  size_t m_clockFails;
124 };
125 
126 } // Framework
127 } // SurgSim
128 
129 #endif
SurgSim::Framework::Timer::getMaxNumberOfFrames
size_t getMaxNumberOfFrames()
Definition: Timer.cpp:129
SurgSim::Framework::Timer::getNumberOfClockFails
size_t getNumberOfClockFails() const
Definition: Timer.cpp:141
SurgSim::Framework::Timer::Timer
Timer()
Instantiate a TimerClock and start a timing run.
Definition: Timer.cpp:24
SurgSim::Framework::Timer::getCurrentNumberOfFrames
size_t getCurrentNumberOfFrames() const
Definition: Timer.cpp:134
SurgSim::Framework::Timer::getMaxFramePeriod
double getMaxFramePeriod() const
Definition: Timer.cpp:157
SurgSim::Framework::Timer::getLastFrameRate
double getLastFrameRate() const
Return the inverse of the duration of the most-recent frame.
Definition: Timer.cpp:112
SurgSim::Framework::Timer
Timer class, measures execution times.
Definition: Timer.h:30
SurgSim::Framework::Timer::isBufferFull
bool isBufferFull() const
Definition: Timer.cpp:175
SurgSim::Framework::Timer::TimerTimePoint
boost::chrono::time_point< TimerClock, TimerDuration > TimerTimePoint
Time points used by the Timer class.
Definition: Timer.h:101
SurgSim::Framework::Timer::start
void start()
Begin a timing run by clearing out any stored frames and beginning a frame.
Definition: Timer.cpp:30
SurgSim::Framework::Timer::m_clockFails
size_t m_clockFails
Number of clock errors since last start.
Definition: Timer.h:123
SurgSim
Definition: CompoundShapeToGraphics.cpp:29
SurgSim::Framework::Timer::TimerDuration
boost::chrono::duration< double > TimerDuration
Durations used by the Timer class.
Definition: Timer.h:98
SurgSim::Framework::Timer::endFrame
void endFrame()
End this frame by storing the duration since the current frame was begun.
Definition: Timer.cpp:45
SurgSim::Framework::Timer::getCumulativeTime
double getCumulativeTime() const
Return the sum of the durations over all the stored frames.
Definition: Timer.cpp:66
SurgSim::Framework::Timer::m_maxNumberOfFrames
size_t m_maxNumberOfFrames
Maximum number of frames to store.
Definition: Timer.h:114
SurgSim::Framework::Timer::m_frameDurations
std::deque< TimerDuration > m_frameDurations
Durations of the frames, i.e., the "stored frames".
Definition: Timer.h:117
SurgSim::Framework::Timer::markFrame
void markFrame()
End the current frame and begin a new frame.
Definition: Timer.cpp:60
SurgSim::Framework::Timer::getAverageFramePeriod
double getAverageFramePeriod() const
Return the average duration across all stored frames.
Definition: Timer.cpp:80
SurgSim::Framework::Timer::beginFrame
void beginFrame()
Begin a frame (storing the current time).
Definition: Timer.cpp:40
SurgSim::Framework::Timer::setMaxNumberOfFrames
void setMaxNumberOfFrames(size_t numberOfFrames)
Set the maximum number of frames to store.
Definition: Timer.cpp:117
SurgSim::Framework::Timer::m_sharedMutex
boost::shared_mutex m_sharedMutex
Mutex to access the data structure m_frameDurations safely.
Definition: Timer.h:120
SurgSim::Framework::Timer::m_lastTime
TimerTimePoint m_lastTime
The time at last start, beginFrame, or markFrame.
Definition: Timer.h:111
SurgSim::Framework::Timer::getLastFramePeriod
double getLastFramePeriod() const
Return the duration of the most-recent frame (time between last endFrame and the previous start,...
Definition: Timer.cpp:103
SurgSim::Framework::Timer::getMinFramePeriod
double getMinFramePeriod() const
Definition: Timer.cpp:166
SurgSim::Framework::Timer::now
TimerTimePoint now()
Get the current time.
Definition: Timer.cpp:146
SurgSim::Framework::Timer::m_clock
static const TimerClock m_clock
The clock used to get the time.
Definition: Timer.h:108
SurgSim::Framework::Timer::TimerClock
boost::chrono::steady_clock TimerClock
The Clock used by the Timer class.
Definition: Timer.h:95
SurgSim::Framework::Timer::getAverageFrameRate
double getAverageFrameRate() const
Return the inverse of the average duration across all stored frames.
Definition: Timer.cpp:98