linux time lib improvements

stop watch other function used (more precise for linux)
This commit is contained in:
Robin Müller 2020-05-29 02:23:14 +02:00
parent ccf79ab5b6
commit 3d2935ac69
4 changed files with 31 additions and 27 deletions

View File

@ -1,11 +1,13 @@
#include <sys/time.h>
#include <time.h>
#include <framework/serviceinterface/ServiceInterfaceStream.h>
#include <framework/timemanager/Clock.h>
extern "C" {
#include <sys/time.h>
#include <sys/sysinfo.h>
#include <linux/sysinfo.h>
#include <time.h>
#include <unistd.h>
}
//#include <fstream>
uint16_t Clock::leapSeconds = 0;
@ -65,6 +67,15 @@ ReturnValue_t Clock::getClock_usecs(uint64_t* time) {
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) {
//TODO This is not posix compatible and delivers only seconds precision
struct sysinfo sysInfo;

View File

@ -2,11 +2,12 @@
#define FRAMEWORK_TIMEMANAGER_CLOCK_H_
#include <framework/returnvalues/HasReturnvaluesIF.h>
#include <stdint.h>
#include <sys/time.h>
#include <framework/ipc/MutexFactory.h>
#include <framework/globalfunctions/timevalOperations.h>
#include <cstdint>
#include <sys/time.h>
typedef uint32_t millis_t;
typedef float seconds_t;
@ -22,7 +23,7 @@ public:
uint32_t usecond; //!< Microseconds, 0 .. 999999
} TimeOfDay_t;
/**static Clock* TimeOfDay_t();
/**
* This method returns the number of clock ticks per second.
* In RTEMS, this is typically 1000.
* @return The number of ticks.
@ -34,22 +35,23 @@ public:
* This system call sets the system time.
* To set the time, it uses a TimeOfDay_t struct.
* @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);
/**
* This system call sets the system time.
* To set the time, it uses a timeval struct.
* @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);
/**
* 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.
* @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);
@ -57,7 +59,7 @@ public:
* Get the time since boot in a timeval struct
*
* @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()
*/

View File

@ -1,9 +1,3 @@
/**
* @file Stopwatch.cpp
*
* @date 08.04.2020
*/
#include <framework/timemanager/Stopwatch.h>
#include <framework/serviceinterface/ServiceInterfaceStream.h>
#include <iomanip>
@ -12,11 +6,11 @@ Stopwatch::Stopwatch(bool displayOnDestruction,
StopwatchDisplayMode displayMode): displayOnDestruction(
displayOnDestruction), displayMode(displayMode) {
// Measures start time on initialization.
startTime = Clock::getUptime();
Clock::getClock_timeval(&startTime);
}
void Stopwatch::start() {
startTime = Clock::getUptime();
Clock::getClock_timeval(&startTime);
}
millis_t Stopwatch::stop() {
@ -57,5 +51,7 @@ StopwatchDisplayMode Stopwatch::getDisplayMode() const {
}
void Stopwatch::stopInternal() {
elapsedTime = Clock::getUptime() - startTime;
timeval endTime;
Clock::getClock_timeval(&endTime);
elapsedTime = endTime - startTime;
}

View File

@ -1,9 +1,3 @@
/**
* @file Stopwatch.h
*
* @date 08.04.2020
*/
#ifndef FRAMEWORK_TIMEMANAGER_STOPWATCH_H_
#define FRAMEWORK_TIMEMANAGER_STOPWATCH_H_
#include <framework/timemanager/Clock.h>
@ -14,12 +8,13 @@ enum class StopwatchDisplayMode {
};
/**
* @brief Simple Stopwatch implementation to measure elapsed time
* @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
* as a float.
* @author R. Mueller
*/
class Stopwatch {
public: