diff --git a/src/core/util/CMakeLists.txt b/src/core/util/CMakeLists.txt index 9473fd9c..78ecbf26 100644 --- a/src/core/util/CMakeLists.txt +++ b/src/core/util/CMakeLists.txt @@ -12,6 +12,8 @@ set(SRCS SynchronizedCounter.hpp SynchronizedMap.hpp SynchronizedQueue.hpp + WallclockTimer.cc + WallclockTimer.hpp ) # required by ProtoStream.cc: diff --git a/src/core/util/WallclockTimer.cc b/src/core/util/WallclockTimer.cc new file mode 100644 index 00000000..e854bd73 --- /dev/null +++ b/src/core/util/WallclockTimer.cc @@ -0,0 +1,77 @@ +#include +#include +#include + +#include "WallclockTimer.hpp" + +namespace fail { + +WallclockTimer::WallclockTimer() { + m_log.setDescription("WallclockTimer"); + m_log.showTime(false); + isRunning = false; +} + +void WallclockTimer::startTimer() { + if (isRunning) { + m_log << "WallclockTimer is already running." << std::endl; + } else { + isRunning = true; + gettimeofday(&start, NULL); + m_log << "WallclockTimer started." << std::endl; + } +} + +std::string WallclockTimer::getRuntime() { + + int length; + long t1,t2, duration; + std::stringstream lengthinfo, resultstring; + + if (isRunning) { + gettimeofday(¤t, NULL); + t1 = (start.tv_sec*1000000)+start.tv_usec; + t2 = (current.tv_sec*1000000)+current.tv_usec; + } else { + t1 = (start.tv_sec*1000000)+start.tv_usec; + t2 = (end.tv_sec*1000000)+end.tv_usec; + } + + duration = t2-t1; + lengthinfo << duration-((duration/1000000)*1000000); + length = lengthinfo.str().length(); + resultstring << (int) duration/1000000 << "."; + + if (length < 6) { + int i; + for (i = 0 ; i< 6-length ; i++){ + resultstring << "0"; + } + } + + resultstring << (int) duration-((duration/1000000)*1000000); + return resultstring.str(); +} + +void WallclockTimer::stopTimer() { + if (isRunning) { + isRunning = false; + gettimeofday(&end, NULL); + m_log << "WallclockTimer stopped." << std::endl; + } else { + m_log << "WallclockTimer is already stopped." << std::endl; + } +} + +void WallclockTimer::reset() { + isRunning = false; + start.tv_sec = 0; + start.tv_usec = 0; + current.tv_sec = 0; + current.tv_usec = 0; + end.tv_sec = 0; + end.tv_usec = 0; + m_log << "WallclockTimer reseted." << std::endl; +} + +} // end-of-namespace: fail diff --git a/src/core/util/WallclockTimer.hpp b/src/core/util/WallclockTimer.hpp new file mode 100644 index 00000000..2e9f743c --- /dev/null +++ b/src/core/util/WallclockTimer.hpp @@ -0,0 +1,55 @@ +/** + * \brief The WallclockTimer measures the elapsed time + * + * The WallclockTimer measures the time which is elapsed between start and stop of the timer. + */ + +#ifndef __WALLCLOCKTIMER_HPP__ + #define __WALLCLOCKTIMER_HPP__ + +#include +#include + +#include "Logger.hpp" + +namespace fail { + +/** + * \class WallclockTimer + * + * The class WallclockTimer contains all functions for start, stop, reset and to get the elapsed + * time. + */ +class WallclockTimer { +private: + + bool isRunning; + struct timeval start,end,current; + Logger m_log; + +public: + WallclockTimer(); + virtual ~WallclockTimer() { } + /** + * Starts the timer. + */ + void startTimer(); + /** + * Returns the elapsed time. This works while the timer is running, and if it is stopped. + */ + std::string getRuntime(); + /** + * Stops the timer. + */ + void stopTimer(); + /** + * Resets the timer. The timer is after a call of reset stopped. + */ + void reset(); + + +}; + +} // end-of-namespace: fail + +#endif // __WALLCLOCKTIMER_HPP__