9#include <sys/resource.h>
13#include "dlinear/util/logging.h"
22 DLINEAR_TRACE(
"TimerBase::Start");
24 elapsed_ = duration{0};
32 elapsed_ += (now() - last_start_);
51 DLINEAR_TRACE(
"TimerBase::duration");
52 return running_ ? elapsed_ + (now() - last_start_) : elapsed_;
57 DLINEAR_TRACE(
"TimerBase::seconds");
58 return std::chrono::duration_cast<std::chrono::duration<double>>(elapsed()).count();
61user_clock::time_point user_clock::now() {
62 DLINEAR_TRACE(
"user_clock::now");
63 struct rusage usage{};
64 if (0 != getrusage(RUSAGE_SELF, &usage))
throw std::runtime_error(
"Failed to get current resource usage (getrusage)");
65 return time_point(duration(uint64_t(usage.ru_utime.tv_sec) * std::micro::den + uint64_t(usage.ru_utime.tv_usec)));
73 : timer_{timer}, enabled_{enabled && timer_ != nullptr} {
Simple timer class to evaluate the performance of the software.
TimerBase()
Construct a new TimerBase object.
duration elapsed() const
Get read-only access to the duration of elapsed time of the timer.
bool is_running() const
Check whether the timer is running.
void Resume()
Resume the timer.
void Start()
Start the timer.
void Pause()
Pause the timer.
std::chrono::duration< double >::rep seconds() const
Get read-only access to the number elapsed seconds of the timer.
const bool enabled_
Whether the timer is enabled.
~TimerGuard()
When the timer guard object is destructed, it pauses the embedded timer object.
void Resume()
Resume the guarded timer object.
Timer *const timer_
The timer to be guarded.
TimerGuard(Timer *timer, bool enabled, bool start_timer=true)
Construct a new TimeGuard object.
void Pause()
Pause the guarded timer object.
Timer class using the a steady clock.
Global namespace for the dlinear library.