Timer.h
Go to the documentation of this file.
// This file is a part of the OpenSurgSim project.
// Copyright 2013, SimQuest Solutions Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef SURGSIM_FRAMEWORK_TIMER_H
#define SURGSIM_FRAMEWORK_TIMER_H
#include <boost/chrono.hpp>
#include <boost/thread/shared_mutex.hpp>
#include <deque>
namespace SurgSim
{
namespace Framework
{
class Timer
{
public:
Timer();
void start();
void beginFrame();
void endFrame();
void markFrame();
double getCumulativeTime() const;
double getAverageFramePeriod() const;
double getAverageFrameRate() const;
double getLastFramePeriod() const;
double getLastFrameRate() const;
void setMaxNumberOfFrames(size_t numberOfFrames);
size_t getMaxNumberOfFrames();
size_t getCurrentNumberOfFrames() const;
size_t getNumberOfClockFails() const;
double getMaxFramePeriod() const;
double getMinFramePeriod() const;
bool isBufferFull() const;
private:
typedef boost::chrono::steady_clock TimerClock;
typedef boost::chrono::duration<double> TimerDuration;
typedef boost::chrono::time_point<TimerClock, TimerDuration> TimerTimePoint;
TimerTimePoint now();
static const TimerClock m_clock;
TimerTimePoint m_lastTime;
size_t m_maxNumberOfFrames;
std::deque<TimerDuration> m_frameDurations;
mutable boost::shared_mutex m_sharedMutex;
size_t m_clockFails;
};
} // Framework
} // SurgSim
#endif
SurgSim
Definition: CompoundShapeToGraphics.cpp:29
SurgSim::Framework::Timer::Timer
Timer()
Instantiate a TimerClock and start a timing run.
Definition: Timer.cpp:24
SurgSim::Framework::Timer::m_clockFails
size_t m_clockFails
Number of clock errors since last start.
Definition: Timer.h:123
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::getAverageFrameRate
double getAverageFrameRate() const
Return the inverse of the average duration across all stored frames.
Definition: Timer.cpp:98
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::TimerDuration
boost::chrono::duration< double > TimerDuration
Durations used by the Timer class.
Definition: Timer.h:98
SurgSim::Framework::Timer
Timer class, measures execution times.
Definition: Timer.h:30
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::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::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::start
void start()
Begin a timing run by clearing out any stored frames and beginning a frame.
Definition: Timer.cpp:30
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::m_lastTime
TimerTimePoint m_lastTime
The time at last start, beginFrame, or markFrame.
Definition: Timer.h:111
SurgSim::Framework::Timer::getMinFramePeriod
double getMinFramePeriod() const
Definition: Timer.cpp:166
SurgSim::Framework::Timer::getNumberOfClockFails
size_t getNumberOfClockFails() const
Definition: Timer.cpp:141
SurgSim::Framework::Timer::m_clock
static const TimerClock m_clock
The clock used to get the time.
Definition: Timer.h:108
SurgSim::Framework::Timer::isBufferFull
bool isBufferFull() const
Definition: Timer.cpp:175
SurgSim::Framework::Timer::TimerClock
boost::chrono::steady_clock TimerClock
The Clock used by the Timer class.
Definition: Timer.h:95
SurgSim::Framework::Timer::getMaxNumberOfFrames
size_t getMaxNumberOfFrames()
Definition: Timer.cpp:129
SurgSim::Framework::Timer::now
TimerTimePoint now()
Get the current time.
Definition: Timer.cpp:146
SurgSim::Framework::Timer::getAverageFramePeriod
double getAverageFramePeriod() const
Return the average duration across all stored frames.
Definition: Timer.cpp:80
SurgSim::Framework::Timer::markFrame
void markFrame()
End the current frame and begin a new frame.
Definition: Timer.cpp:60
SurgSim::Framework::Timer::beginFrame
void beginFrame()
Begin a frame (storing the current time).
Definition: Timer.cpp:40