Skip to content
Snippets Groups Projects
Commit f4221e99 authored by Alexis Girault's avatar Alexis Girault
Browse files

Merge branch 'addFrameCounterUtility' into 'master'

ENH: Adds UPS counter utility

See merge request !158
parents f2b6e613 50f48513
No related branches found
No related tags found
No related merge requests found
......@@ -53,9 +53,22 @@ Module::start()
}
else if (m_status == ModuleStatus::RUNNING)
{
if (m_UPSTrackerEnabled)
{
// Set the start point for update counter
m_UPSTracker->setStartPointOfUpdate();
}
if(m_loopDelay == 0)
{
this->runModule();
if (m_UPSTrackerEnabled)
{
// Set the end point for update counter
m_UPSTracker->setEndPointOfUpdate();
}
continue;
}
current_t = std::chrono::steady_clock::now();
......@@ -65,6 +78,12 @@ Module::start()
this->runModule();
previous_t = current_t;
}
if (m_UPSTrackerEnabled)
{
// Set the end point for update counter
m_UPSTracker->setEndPointOfUpdate();
}
}
}
......@@ -96,6 +115,7 @@ Module::pause()
return;
}
m_UPSTracker->reset();
m_status = ModuleStatus::PAUSING;
while (m_status != ModuleStatus::PAUSED) {}
......@@ -108,7 +128,7 @@ Module::end()
(m_status == ModuleStatus::TERMINATING))
{
LOG(WARNING) << "Can not end '" << m_name << "'.\n"
<< "Module alreading inactive or terminating.";
<< "Module already inactive or terminating.";
return;
}
......@@ -135,7 +155,7 @@ double Module::getLoopDelay() const
}
void
Module::setLoopDelay(double milliseconds)
Module::setLoopDelay(const double milliseconds)
{
if(milliseconds < 0)
{
......@@ -157,7 +177,7 @@ double Module::getFrequency() const
}
void
Module::setFrequency(double f)
Module::setFrequency(const double f)
{
if(f < 0)
{
......@@ -173,4 +193,20 @@ Module::setFrequency(double f)
m_loopDelay = 1000.0/f;
}
unsigned int
Module::getUPS() const
{
return m_UPSTracker->getUPS();
}
void
Module::setUPSTrackerEnabled(const bool enable)
{
m_UPSTrackerEnabled = enable;
if (!enable)
{
m_UPSTracker->reset();
}
}
}
......@@ -22,6 +22,8 @@
#ifndef imstkModule_h
#define imstkModule_h
#include "imstkTimer.h"
#include <iostream>
#include <atomic>
......@@ -55,7 +57,9 @@ public:
Module(std::string name, int loopDelay = 0) :
m_name(name),
m_loopDelay(loopDelay)
{}
{
m_UPSTracker = std::make_shared<UPSCounter>();
}
///
/// \brief Destructor
......@@ -100,7 +104,7 @@ public:
///
/// \brief Set the loop delay
///
void setLoopDelay(double milliseconds);
void setLoopDelay(const double milliseconds);
///
/// \brief Get loop delay
......@@ -110,7 +114,17 @@ public:
///
/// \brief Set the loop delay
///
void setFrequency(double f);
void setFrequency(const double f);
///
/// \brief Get the updates per second
///
unsigned int getUPS() const;
///
/// \brief Set/get UPS tracking status
///
inline void setUPSTrackerEnabled(const bool enable);
inline bool getUPSTrackingStatus() const { return m_UPSTrackerEnabled; }
protected:
......@@ -132,7 +146,10 @@ protected:
std::atomic<ModuleStatus> m_status{ModuleStatus::INACTIVE};///> Module status
std::string m_name; ///> Name of the module
double m_loopDelay; ///> Loop delay
double m_loopDelay; ///> Loop delay
bool m_UPSTrackerEnabled = false; ///> Track the ups
std::shared_ptr<UPSCounter> m_UPSTracker; ///> Keeps track of UPS
};
}
......
......@@ -148,7 +148,6 @@ StopWatch::printTimeElapsed(std::string const& name /* = std::string("noName")*/
double
StopWatch::getTimeElapsed(const TimeUnitType unitType /*= TimeUnitType::milliSeconds*/)
{
//return 0;
return std::chrono::duration<double, std::milli>
(std::chrono::high_resolution_clock::now() - wallClockTimeKeeper).count()*
wcTimerConstants[(int)unitType];
......@@ -160,4 +159,27 @@ CpuTimer::getTimeElapsed(const TimeUnitType unitType /*= TimeUnitType::milliSeco
return (std::clock() - cpuTimeKeeper)*cpuTimerConstants[(int)unitType];
}
void
UPSCounter::reset()
{
m_timer->reset();
m_accumulatedTimer = 0.;
m_ups = 0;
m_updateCount = 0;
}
void
UPSCounter::setEndPointOfUpdate()
{
m_accumulatedTimer += m_timer->getTimeElapsed(StopWatch::TimeUnitType::milliSeconds);
m_updateCount++;
if (m_accumulatedTimer > 1000.)
{
m_ups = m_updateCount;
m_updateCount = 0;
m_accumulatedTimer = 0.;
}
}
}
......@@ -19,8 +19,8 @@
=========================================================================*/
#ifndef imstkTimerUtility_h
#define imstkTimerUtility_h
#ifndef imstkTimer_h
#define imstkTimer_h
#include "g3log/g3log.hpp"
......@@ -150,6 +150,51 @@ private:
std::clock_t cpuTimeKeeper; ///> time keeper for cpu time
};
///
/// \class UPSCounter
///
/// \brief Utility class to count updates per second
///
class UPSCounter
{
public:
///
/// \brief Constructor/Destructor
///
UPSCounter() = default;
~UPSCounter() = default;
///
/// \brief Reset the variable that keep track of ups
///
void reset();
///
/// \brief Set the start point to the update
///
void setStartPointOfUpdate() { m_timer->start(); }
///
/// \brief Set the end point to the update
///
void setEndPointOfUpdate();
///
/// \brief Get the updates per second
///
unsigned int getUPS() const { return m_ups; }
protected:
std::shared_ptr<StopWatch> m_timer = std::make_shared<StopWatch>(); ///> Timer
double m_accumulatedTimer = 0.; ///> Accumulated time (always < 1 sec)
unsigned int m_ups = 0; ///> Most up-to-date ups
unsigned int m_updateCount = 0; ///> Current update count
};
}
#endif // ifndef imstkLogUtility_h
#endif // ifndef imstkTimer_h
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment