delpi  0.0.1
DElta-complete LP solver
Loading...
Searching...
No Matches
Timer.cpp
1
6
7#include "delpi/util/Timer.h"
8
9#include <sys/resource.h>
10
11#include <stdexcept>
12
13#include "delpi/util/exception.h"
14#include "delpi/util/logging.h"
15
16namespace delpi {
17
18template <class T>
19TimerBase<T>::TimerBase() : last_start_{now()} {}
20
21template <class T>
23 DELPI_TRACE("TimerBase::Start");
24 last_start_ = now();
25 elapsed_ = duration{0};
26 running_ = true;
27}
28
29template <class T>
31 if (running_) {
32 running_ = false;
33 elapsed_ += (now() - last_start_);
34 }
35}
36
37template <class T>
39 if (!running_) {
40 last_start_ = now();
41 running_ = true;
42 }
43}
44
45template <class T>
46bool TimerBase<T>::is_running() const {
47 return running_;
48}
49
50template <class T>
51typename TimerBase<T>::duration TimerBase<T>::elapsed() const {
52 DELPI_TRACE("TimerBase::duration");
53 return running_ ? elapsed_ + (now() - last_start_) : elapsed_;
55
56template <class T>
57std::chrono::duration<double>::rep TimerBase<T>::seconds() const {
58 DELPI_TRACE("TimerBase::seconds");
59 return std::chrono::duration_cast<std::chrono::duration<double>>(elapsed()).count();
60}
61
62user_clock::time_point user_clock::now() {
63 DELPI_TRACE("user_clock::now");
64 struct rusage usage{};
65 if (0 != getrusage(RUSAGE_SELF, &usage)) throw DelpiException("Failed to get current resource usage (getrusage)");
66 return time_point(duration(static_cast<uint64_t>(usage.ru_utime.tv_sec) * std::micro::den +
67 static_cast<uint64_t>(usage.ru_utime.tv_usec)));
68}
69
70// Explicit instantiations
71template class TimerBase<chosen_steady_clock>;
72template class TimerBase<user_clock>;
73
74TimerGuard::TimerGuard(Timer *const timer, const bool enabled, const bool start_timer)
75 : timer_{timer}, enabled_{enabled && timer_ != nullptr} {
76 if (enabled_ && start_timer) timer_->Resume();
77}
78
80 if (enabled_) timer_->Pause();
81}
82
84 if (enabled_) timer_->Pause();
85}
86
88 if (enabled_) timer_->Resume();
89}
90
91} // namespace delpi
void Resume()
Resume the timer.
Definition Timer.cpp:38
duration elapsed_
Elapsed time so far. This doesn't include the current fragment if it is running.
Definition Timer.h:73
void Pause()
Pause the timer.
Definition Timer.cpp:30
void Start()
Start the timer.
Definition Timer.cpp:22
time_point last_start_
Last time_point when the timer is started or resumed.
Definition Timer.h:72
bool running_
Whether the timer is running or not.
Definition Timer.h:71
void Pause()
Pause the guarded timer object.
Definition Timer.cpp:83
const bool enabled_
Whether the timer is enabled.
Definition Timer.h:165
TimerGuard(Timer *timer, bool enabled, bool start_timer=true)
Construct a new TimeGuard object.
Definition Timer.cpp:74
void Resume()
Resume the guarded timer object.
Definition Timer.cpp:87
Timer *const timer_
The timer to be guarded.
Definition Timer.h:164
~TimerGuard()
When the timer guard object is destructed, it pauses the embedded timer object.
Definition Timer.cpp:79
Timer class using the a steady clock.
Definition Timer.h:95
Global namespace for the delpi library.