WSL/SLF GitLab Repository

Commit 5a6ebd21 authored by Thomas Egger's avatar Thomas Egger
Browse files

The new UsageTimer as being used in the benchmarks for the MeteoIO paper -...

The new UsageTimer as being used in the benchmarks for the MeteoIO paper - comments and WIndows version pending.
parent 1131c5a9
......@@ -107,7 +107,7 @@ class StationData {
/**
* @brief Simple merge strategy.
* If some fields of the first argument are empty, they will be filled by the macthing field from the
* If some fields of the first argument are empty, they will be filled by the matching field from the
* second argument.
* @param sd1 first StationData to merge, highest priority
* @param sd2 second StationData to merge, lowest priority
......
......@@ -26,7 +26,6 @@
#undef min
#else
#include <sys/time.h>
#include <sys/resource.h>
#endif
#include "Timer.h"
......@@ -112,6 +111,85 @@ long double Timer::getCurrentTime() const {
const long double t = static_cast<long double>(tp.tv_sec) + static_cast<long double>(tp.tv_usec)*1.0e-6;
return t;
}
#endif
#ifndef WIN32
const int UsageTimer::who = RUSAGE_SELF;
UsageTimer::UsageTimer() : start_usage(), current_usage(), is_running(false), user_time(0.), sys_time(0.), elapsed(0.) {}
void UsageTimer::start()
{
if (!is_running) {
is_running = true;
getrusage(UsageTimer::who, &start_usage);
}
}
void UsageTimer::stop()
{
if (is_running) {
getElapsedTimes();
is_running = false;
}
}
void UsageTimer::restart()
{
reset();
is_running = true;
}
void UsageTimer::reset() {
user_time = sys_time = elapsed= 0.;
}
double UsageTimer::getElapsed()
{
if (is_running) {
getElapsedTimes();
}
return elapsed;
}
double UsageTimer::getElapsedUserTime()
{
if (is_running) {
getElapsedTimes();
}
return user_time;
}
double UsageTimer::getElapsedSystemTime()
{
if (is_running) {
getElapsedTimes();
}
return sys_time;
}
void UsageTimer::getElapsedTimes()
{
getrusage(UsageTimer::who, &current_usage);
//calculate start point
double start_user_time = static_cast<double>(start_usage.ru_utime.tv_sec) + static_cast<double>(start_usage.ru_utime.tv_usec) / 1000000.;
double start_sys_time = static_cast<double>(start_usage.ru_stime.tv_sec) + static_cast<double>(start_usage.ru_stime.tv_usec) / 1000000.;
//calculate end point
double end_user_time = static_cast<double>(current_usage.ru_utime.tv_sec) + static_cast<double>(current_usage.ru_utime.tv_usec) / 1000000;
double end_sys_time = static_cast<double>(current_usage.ru_stime.tv_sec) + static_cast<double>(current_usage.ru_stime.tv_usec) / 1000000;
//calculate different elapsed times
user_time = end_user_time - start_user_time;
sys_time = end_sys_time - start_sys_time;
elapsed = sys_time + user_time;
}
#endif
} //namespace
......
......@@ -31,6 +31,10 @@
#include <iostream>
#include <ctime>
#ifndef WIN32
#include <sys/resource.h>
#endif
namespace mio {
/**
......@@ -48,6 +52,7 @@ public:
void stop();
void reset();
double getElapsed() const;
protected:
long double getCurrentTime() const;
......@@ -56,5 +61,33 @@ protected:
bool isRunning;
};
#ifndef WIN32
/**
* @class UsageTimer
* @author Thomas Egger
*/
class UsageTimer {
public:
UsageTimer();
void start();
void restart();
void stop();
void reset();
double getElapsed();
double getElapsedUserTime();
double getElapsedSystemTime();
protected:
void getElapsedTimes();
static const int who;
struct rusage start_usage, current_usage;
bool is_running;
double user_time, sys_time, elapsed;
};
#endif
} //end namespace mio
#endif
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment