about summary refs log tree commit diff homepage
path: root/lib/Support/Timer.cpp
blob: 1948449c3791d421b0d8d5cdd858c8581ab5e889 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//===-- Timer.cpp ---------------------------------------------------------===//
//
//                     The KLEE Symbolic Virtual Machine
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "klee/Support/ErrorHandling.h"
#include "klee/Support/Timer.h"
#include "klee/System/Time.h"


using namespace klee;


// WallTimer

WallTimer::WallTimer() : start{time::getWallTime()} {}

time::Span WallTimer::delta() const {
  return {time::getWallTime() - start};
}


// Timer

Timer::Timer(const time::Span &interval, std::function<void()> &&callback) :
  interval{interval}, nextInvocationTime{time::getWallTime() + interval}, run{std::move(callback)} {};

time::Span Timer::getInterval() const {
  return interval;
};

void Timer::invoke(const time::Point &currentTime) {
  if (currentTime < nextInvocationTime) return;

  run();
  nextInvocationTime = currentTime + interval;
};

void Timer::reset(const time::Point &currentTime) {
  nextInvocationTime = currentTime + interval;
};


// TimerGroup

TimerGroup::TimerGroup(const time::Span &minInterval) :
  invocationTimer{
    minInterval,
    [&]{
      // invoke timers
      for (auto &timer : timers)
        timer->invoke(currentTime);
    }
  } {};

void TimerGroup::add(std::unique_ptr<klee::Timer> timer) {
  const auto &interval = timer->getInterval();
  const auto &minInterval = invocationTimer.getInterval();
  if (interval < minInterval)
    klee_warning("Timer interval below minimum timer interval (-timer-interval)");
  if (interval.toMicroseconds() % minInterval.toMicroseconds())
    klee_warning("Timer interval not a multiple of timer interval (-timer-interval)");

  timers.emplace_back(std::move(timer));
}

void TimerGroup::invoke() {
  currentTime = time::getWallTime();
  invocationTimer.invoke(currentTime);
}

void TimerGroup::reset() {
  currentTime = time::getWallTime();
  invocationTimer.reset(currentTime);
  for (auto &timer : timers)
    timer->reset(currentTime);
}