Merge pull request 'mueller_stopwatch' (#30) from KSat/fsfw:mueller_stopwatch into master

This commit is contained in:
Robin Müller 2020-07-07 12:05:04 +02:00
commit 6f40a8c622
7 changed files with 179 additions and 31 deletions

View File

@ -1,16 +1,18 @@
#include <framework/timemanager/Clock.h> #include <framework/timemanager/Clock.h>
#include <framework/globalfunctions/timevalOperations.h> #include <framework/globalfunctions/timevalOperations.h>
#include <stdlib.h> #include <framework/osal/FreeRTOS/Timekeeper.h>
#include "Timekeeper.h"
#include <FreeRTOS.h> #include <freertos/FreeRTOS.h>
#include <task.h> #include <freertos/task.h>
#include <stdlib.h>
#include <time.h>
//TODO sanitize input? //TODO sanitize input?
//TODO much of this code can be reused for tick-only systems //TODO much of this code can be reused for tick-only systems
uint16_t Clock::leapSeconds = 0; uint16_t Clock::leapSeconds = 0;
MutexIF* Clock::timeMutex = NULL; MutexIF* Clock::timeMutex = nullptr;
uint32_t Clock::getTicksPerSecond(void) { uint32_t Clock::getTicksPerSecond(void) {
return 1000; return 1000;
@ -56,7 +58,6 @@ ReturnValue_t Clock::getUptime(timeval* uptime) {
timeval Clock::getUptime() { timeval Clock::getUptime() {
TickType_t ticksSinceStart = xTaskGetTickCount(); TickType_t ticksSinceStart = xTaskGetTickCount();
return Timekeeper::ticksToTimeval(ticksSinceStart); return Timekeeper::ticksToTimeval(ticksSinceStart);
} }
@ -128,7 +129,7 @@ ReturnValue_t Clock::convertTimevalToJD2000(timeval time, double* JD2000) {
ReturnValue_t Clock::convertUTCToTT(timeval utc, timeval* tt) { ReturnValue_t Clock::convertUTCToTT(timeval utc, timeval* tt) {
//SHOULDDO: works not for dates in the past (might have less leap seconds) //SHOULDDO: works not for dates in the past (might have less leap seconds)
if (timeMutex == NULL) { if (timeMutex == nullptr) {
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
} }

View File

@ -1,20 +1,19 @@
#include "Timekeeper.h" #include <framework/osal/FreeRTOS/Timekeeper.h>
#include <FreeRTOSConfig.h>
Timekeeper::Timekeeper() : #include "FreeRTOSConfig.h"
offset( { 0, 0 }) {
// TODO Auto-generated constructor stub
} Timekeeper * Timekeeper::myinstance = nullptr;
Timekeeper * Timekeeper::myinstance = NULL; Timekeeper::Timekeeper() : offset( { 0, 0 } ) {}
Timekeeper::~Timekeeper() {}
const timeval& Timekeeper::getOffset() const { const timeval& Timekeeper::getOffset() const {
return offset; return offset;
} }
Timekeeper* Timekeeper::instance() { Timekeeper* Timekeeper::instance() {
if (myinstance == NULL) { if (myinstance == nullptr) {
myinstance = new Timekeeper(); myinstance = new Timekeeper();
} }
return myinstance; return myinstance;
@ -24,10 +23,6 @@ void Timekeeper::setOffset(const timeval& offset) {
this->offset = offset; this->offset = offset;
} }
Timekeeper::~Timekeeper() {
// TODO Auto-generated destructor stub
}
timeval Timekeeper::ticksToTimeval(TickType_t ticks) { timeval Timekeeper::ticksToTimeval(TickType_t ticks) {
timeval uptime; timeval uptime;
uptime.tv_sec = ticks / configTICK_RATE_HZ; uptime.tv_sec = ticks / configTICK_RATE_HZ;
@ -40,3 +35,7 @@ timeval Timekeeper::ticksToTimeval(TickType_t ticks) {
return uptime; return uptime;
} }
TickType_t Timekeeper::getTicks() {
return xTaskGetTickCount();
}

View File

@ -3,7 +3,9 @@
#include <framework/timemanager/Clock.h> #include <framework/timemanager/Clock.h>
#include <FreeRTOS.h> #include <freertos/FreeRTOS.h>
#include <freertos/task.h>
/** /**
* A Class to basically store the time difference between uptime and UTC * A Class to basically store the time difference between uptime and UTC
@ -25,6 +27,11 @@ public:
virtual ~Timekeeper(); virtual ~Timekeeper();
static timeval ticksToTimeval(TickType_t ticks); static timeval ticksToTimeval(TickType_t ticks);
/**
* Get elapsed time in system ticks.
* @return
*/
static TickType_t getTicks();
const timeval& getOffset() const; const timeval& getOffset() const;
void setOffset(const timeval& offset); void setOffset(const timeval& offset);

View File

@ -1,10 +1,10 @@
#include <sys/time.h> #include <framework/serviceinterface/ServiceInterfaceStream.h>
#include <time.h>
#include <framework/timemanager/Clock.h> #include <framework/timemanager/Clock.h>
#include <sys/time.h>
#include <sys/sysinfo.h> #include <sys/sysinfo.h>
#include <linux/sysinfo.h> #include <linux/sysinfo.h>
#include <time.h>
#include <unistd.h> #include <unistd.h>
//#include <fstream> //#include <fstream>
@ -65,6 +65,15 @@ ReturnValue_t Clock::getClock_usecs(uint64_t* time) {
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} }
timeval Clock::getUptime() {
timeval uptime;
auto result = getUptime(&uptime);
if(result != HasReturnvaluesIF::RETURN_OK) {
sif::error << "Clock::getUptime: Error getting uptime" << std::endl;
}
return uptime;
}
ReturnValue_t Clock::getUptime(timeval* uptime) { ReturnValue_t Clock::getUptime(timeval* uptime) {
//TODO This is not posix compatible and delivers only seconds precision //TODO This is not posix compatible and delivers only seconds precision
struct sysinfo sysInfo; struct sysinfo sysInfo;

View File

@ -2,12 +2,15 @@
#define FRAMEWORK_TIMEMANAGER_CLOCK_H_ #define FRAMEWORK_TIMEMANAGER_CLOCK_H_
#include <framework/returnvalues/HasReturnvaluesIF.h> #include <framework/returnvalues/HasReturnvaluesIF.h>
#include <stdint.h>
#include <sys/time.h>
#include <framework/ipc/MutexFactory.h> #include <framework/ipc/MutexFactory.h>
#include <framework/globalfunctions/timevalOperations.h> #include <framework/globalfunctions/timevalOperations.h>
#include <cstdint>
#include <sys/time.h>
//! Don't use these for time points, type is not large enough for UNIX epoch.
typedef uint32_t dur_millis_t;
typedef double dur_seconds_t;
class Clock { class Clock {
public: public:
@ -21,7 +24,7 @@ public:
uint32_t usecond; //!< Microseconds, 0 .. 999999 uint32_t usecond; //!< Microseconds, 0 .. 999999
} TimeOfDay_t; } TimeOfDay_t;
/**static Clock* TimeOfDay_t(); /**
* This method returns the number of clock ticks per second. * This method returns the number of clock ticks per second.
* In RTEMS, this is typically 1000. * In RTEMS, this is typically 1000.
* @return The number of ticks. * @return The number of ticks.
@ -33,22 +36,23 @@ public:
* This system call sets the system time. * This system call sets the system time.
* To set the time, it uses a TimeOfDay_t struct. * To set the time, it uses a TimeOfDay_t struct.
* @param time The struct with the time settings to set. * @param time The struct with the time settings to set.
* @return \c RETURN_OK on success. Otherwise, the OS failure code is returned. * @return -@c RETURN_OK on success. Otherwise, the OS failure code
* is returned.
*/ */
static ReturnValue_t setClock(const TimeOfDay_t* time); static ReturnValue_t setClock(const TimeOfDay_t* time);
/** /**
* This system call sets the system time. * This system call sets the system time.
* To set the time, it uses a timeval struct. * To set the time, it uses a timeval struct.
* @param time The struct with the time settings to set. * @param time The struct with the time settings to set.
* @return \c RETURN_OK on success. Otherwise, the OS failure code is returned. * @return -@c RETURN_OK on success. Otherwise, the OS failure code is returned.
*/ */
static ReturnValue_t setClock(const timeval* time); static ReturnValue_t setClock(const timeval* time);
/** /**
* This system call returns the current system clock in timeval format. * This system call returns the current system clock in timeval format.
* The timval format has the fields \c tv_sec with seconds and \c tv_usec with * The timval format has the fields @c tv_sec with seconds and @c tv_usec with
* microseconds since an OS-defined epoch. * microseconds since an OS-defined epoch.
* @param time A pointer to a timeval struct where the current time is stored. * @param time A pointer to a timeval struct where the current time is stored.
* @return \c RETURN_OK on success. Otherwise, the OS failure code is returned. * @return @c RETURN_OK on success. Otherwise, the OS failure code is returned.
*/ */
static ReturnValue_t getClock_timeval(timeval* time); static ReturnValue_t getClock_timeval(timeval* time);
@ -56,7 +60,7 @@ public:
* Get the time since boot in a timeval struct * Get the time since boot in a timeval struct
* *
* @param[out] time A pointer to a timeval struct where the uptime is stored. * @param[out] time A pointer to a timeval struct where the uptime is stored.
* @return\c RETURN_OK on success. Otherwise, the OS failure code is returned. * @return @c RETURN_OK on success. Otherwise, the OS failure code is returned.
* *
* @deprecated, I do not think this should be able to fail, use timeval getUptime() * @deprecated, I do not think this should be able to fail, use timeval getUptime()
*/ */

57
timemanager/Stopwatch.cpp Normal file
View File

@ -0,0 +1,57 @@
#include <framework/timemanager/Stopwatch.h>
#include <framework/serviceinterface/ServiceInterfaceStream.h>
#include <iomanip>
Stopwatch::Stopwatch(bool displayOnDestruction,
StopwatchDisplayMode displayMode): displayOnDestruction(
displayOnDestruction), displayMode(displayMode) {
// Measures start time on initialization.
Clock::getClock_timeval(&startTime);
}
void Stopwatch::start() {
Clock::getClock_timeval(&startTime);
}
dur_millis_t Stopwatch::stop() {
stopInternal();
return elapsedTime.tv_sec * 1000 + elapsedTime.tv_usec / 1000;
}
dur_seconds_t Stopwatch::stopSeconds() {
stopInternal();
return timevalOperations::toDouble(elapsedTime);
}
void Stopwatch::display() {
if(displayMode == StopwatchDisplayMode::MILLIS) {
sif::info << "Stopwatch: Operation took " << (elapsedTime.tv_sec * 1000 +
elapsedTime.tv_usec / 1000) << " milliseconds" << std::endl;
}
else if(displayMode == StopwatchDisplayMode::SECONDS) {
sif::info <<"Stopwatch: Operation took " << std::setprecision(3)
<< std::fixed << timevalOperations::toDouble(elapsedTime)
<< " seconds" << std::endl;
}
}
Stopwatch::~Stopwatch() {
if(displayOnDestruction) {
stopInternal();
display();
}
}
void Stopwatch::setDisplayMode(StopwatchDisplayMode displayMode) {
this->displayMode = displayMode;
}
StopwatchDisplayMode Stopwatch::getDisplayMode() const {
return displayMode;
}
void Stopwatch::stopInternal() {
timeval endTime;
Clock::getClock_timeval(&endTime);
elapsedTime = endTime - startTime;
}

71
timemanager/Stopwatch.h Normal file
View File

@ -0,0 +1,71 @@
#ifndef FRAMEWORK_TIMEMANAGER_STOPWATCH_H_
#define FRAMEWORK_TIMEMANAGER_STOPWATCH_H_
#include <framework/timemanager/Clock.h>
enum class StopwatchDisplayMode {
MILLIS,
SECONDS
};
/**
* @brief Simple Stopwatch implementation to measure elapsed time
* @details
* This class can be used to measure elapsed times. It also displays elapsed
* times automatically on destruction if not explicitely deactivated in the
* constructor. The default time format is the elapsed time in miliseconds
* in seconds as a double.
* @author R. Mueller
*/
class Stopwatch {
public:
/**
* Default constructor. Call "Stopwatch stopwatch" without brackets if
* no parameters are required!
* @param displayOnDestruction If set to true, displays measured time on
* object destruction
* @param displayMode Display format is either MS rounded or MS as double
* format
* @param outputPrecision If using double format, specify precision here.
*/
Stopwatch(bool displayOnDestruction = true, StopwatchDisplayMode displayMode
= StopwatchDisplayMode::MILLIS);
virtual~ Stopwatch();
/**
* Caches the start time
*/
void start();
/**
* Calculates the elapsed time since start and returns it
* @return elapsed time in milliseconds (rounded)
*/
dur_millis_t stop();
/**
* Calculates the elapsed time since start and returns it
* @return elapsed time in seconds (double precision)
*/
dur_seconds_t stopSeconds();
/**
* Displays the elapsed times on the osstream, depending on internal display
* mode.
*/
void display();
StopwatchDisplayMode getDisplayMode() const;
void setDisplayMode(StopwatchDisplayMode displayMode);
bool displayOnDestruction = true;
private:
timeval startTime {0, 0};
timeval elapsedTime {0, 0};
StopwatchDisplayMode displayMode = StopwatchDisplayMode::MILLIS;
void stopInternal();
};
#endif /* FRAMEWORK_TIMEMANAGER_STOPWATCH_H_ */