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 
105  TimerTimePoint now();
106 
108  static const TimerClock m_clock;
109 
111  TimerTimePoint m_lastTime;
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
Definition: CompoundShapeToGraphics.cpp:29
Timer()
Instantiate a TimerClock and start a timing run.
Definition: Timer.cpp:24
size_t m_clockFails
Number of clock errors since last start.
Definition: Timer.h:123
double getCumulativeTime() const
Return the sum of the durations over all the stored frames.
Definition: Timer.cpp:66
size_t m_maxNumberOfFrames
Maximum number of frames to store.
Definition: Timer.h:114
std::deque< TimerDuration > m_frameDurations
Durations of the frames, i.e., the "stored frames".
Definition: Timer.h:117
double getAverageFrameRate() const
Return the inverse of the average duration across all stored frames.
Definition: Timer.cpp:98
boost::chrono::time_point< TimerClock, TimerDuration > TimerTimePoint
Time points used by the Timer class.
Definition: Timer.h:101
boost::chrono::duration< double > TimerDuration
Durations used by the Timer class.
Definition: Timer.h:98
Timer class, measures execution times.
Definition: Timer.h:30
size_t getCurrentNumberOfFrames() const
Definition: Timer.cpp:134
double getMaxFramePeriod() const
Definition: Timer.cpp:157
double getLastFrameRate() const
Return the inverse of the duration of the most-recent frame.
Definition: Timer.cpp:112
double getLastFramePeriod() const
Return the duration of the most-recent frame (time between last endFrame and the previous start...
Definition: Timer.cpp:103
void setMaxNumberOfFrames(size_t numberOfFrames)
Set the maximum number of frames to store.
Definition: Timer.cpp:117
boost::shared_mutex m_sharedMutex
Mutex to access the data structure m_frameDurations safely.
Definition: Timer.h:120
void start()
Begin a timing run by clearing out any stored frames and beginning a frame.
Definition: Timer.cpp:30
void endFrame()
End this frame by storing the duration since the current frame was begun.
Definition: Timer.cpp:45
TimerTimePoint m_lastTime
The time at last start, beginFrame, or markFrame.
Definition: Timer.h:111
double getMinFramePeriod() const
Definition: Timer.cpp:166
size_t getNumberOfClockFails() const
Definition: Timer.cpp:141
static const TimerClock m_clock
The clock used to get the time.
Definition: Timer.h:108
bool isBufferFull() const
Definition: Timer.cpp:175
boost::chrono::steady_clock TimerClock
The Clock used by the Timer class.
Definition: Timer.h:95
size_t getMaxNumberOfFrames()
Definition: Timer.cpp:129
TimerTimePoint now()
Get the current time.
Definition: Timer.cpp:146
double getAverageFramePeriod() const
Return the average duration across all stored frames.
Definition: Timer.cpp:80
void markFrame()
End the current frame and begin a new frame.
Definition: Timer.cpp:60
void beginFrame()
Begin a frame (storing the current time).
Definition: Timer.cpp:40