klee
Timer.cpp
Go to the documentation of this file.
1//===-- Timer.cpp ---------------------------------------------------------===//
2//
3// The KLEE Symbolic Virtual Machine
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
11#include "klee/Support/Timer.h"
12#include "klee/System/Time.h"
13
14
15using namespace klee;
16
17
18// WallTimer
19
20WallTimer::WallTimer() : start{time::getWallTime()} {}
21
23 return {time::getWallTime() - start};
24}
25
26
27// Timer
28
29Timer::Timer(const time::Span &interval, std::function<void()> &&callback) :
30 interval{interval}, nextInvocationTime{time::getWallTime() + interval}, run{std::move(callback)} {};
31
33 return interval;
34};
35
36void Timer::invoke(const time::Point &currentTime) {
37 if (currentTime < nextInvocationTime) return;
38
39 run();
40 nextInvocationTime = currentTime + interval;
41};
42
43void Timer::reset(const time::Point &currentTime) {
44 nextInvocationTime = currentTime + interval;
45};
46
47
48// TimerGroup
49
51 invocationTimer{
52 minInterval,
53 [&]{
54 // invoke timers
55 for (auto &timer : timers)
56 timer->invoke(currentTime);
57 }
58 } {};
59
60void TimerGroup::add(std::unique_ptr<klee::Timer> timer) {
61 const auto &interval = timer->getInterval();
62 const auto &minInterval = invocationTimer.getInterval();
63 if (interval < minInterval)
64 klee_warning("Timer interval below minimum timer interval (-timer-interval)");
65 if (interval.toMicroseconds() % minInterval.toMicroseconds())
66 klee_warning("Timer interval not a multiple of timer interval (-timer-interval)");
67
68 timers.emplace_back(std::move(timer));
69}
70
74}
75
79 for (auto &timer : timers)
80 timer->reset(currentTime);
81}
TimerGroup(const time::Span &minInterval)
Definition: Timer.cpp:50
void reset()
Reset all timers.
Definition: Timer.cpp:76
void add(std::unique_ptr< Timer > timer)
Definition: Timer.cpp:60
time::Point currentTime
Time of last invoke call.
Definition: Timer.h:80
void invoke()
Invoke registered timers with current time only if minimum interval exceeded.
Definition: Timer.cpp:71
Timer invocationTimer
Timer that invokes all registered timers after minimum interval.
Definition: Timer.h:78
llvm::SmallVector< std::unique_ptr< Timer >, 4 > timers
Registered timers.
Definition: Timer.h:76
time::Span interval
Approximate interval between callback invocations.
Definition: Timer.h:45
time::Point nextInvocationTime
Wall time for next invocation.
Definition: Timer.h:47
Timer(const time::Span &interval, std::function< void()> &&callback)
Definition: Timer.cpp:29
std::function< void()> run
The event callback.
Definition: Timer.h:49
void invoke(const time::Point &currentTime)
Execute callback if invocation time exceeded.
Definition: Timer.cpp:36
time::Span getInterval() const
Return specified interval between invocations.
Definition: Timer.cpp:32
void reset(const time::Point &currentTime)
Set new invocation time to currentTime + interval.
Definition: Timer.cpp:43
const time::Point start
Definition: Timer.h:26
time::Span delta() const
Return the delta since the timer was created.
Definition: Timer.cpp:22
Point getWallTime()
Returns point in time using a monotonic steady clock.
Definition: Time.cpp:206
Definition: main.cpp:291
void void void klee_warning(const char *msg,...) __attribute__((format(printf