about summary refs log tree commit diff homepage
path: root/lib/Support/Time.cpp
blob: 93d77de1170bf7fa6068a871310f7e27c36b8b58 (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
//===-- Time.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/System/Time.h"


#include <cstdint>
#include <regex>
#include <sstream>
#include <tuple>
#include <sys/resource.h>


using namespace klee;


/* Why std::chrono:
 * - C++11
 * - separation between time points and durations
 * - good performance on Linux and macOS (similar to gettimeofday)
 * and not:
 * - clock_gettime(CLOCK_MONOTONIC_COARSE): software clock, Linux-specific
 * - clock_gettime(CLOCK_MONOTONIC): slowest on macOS, C-like API
 * - gettimeofday: C-like API, non-monotonic
 *
 * TODO: add time literals with C++14
 */

// === Point ===

// operators
time::Point& time::Point::operator+=(const time::Span &span) { point += span.duration; return *this; }
time::Point& time::Point::operator-=(const time::Span &span) { point -= span.duration; return *this; }

time::Point time::operator+(const time::Point &point, const time::Span &span) { return time::Point(point.point + span.duration); }
time::Point time::operator+(const time::Span &span, const time::Point &point) { return time::Point(point.point + span.duration); }
time::Point time::operator-(const time::Point &point, const time::Span &span) { return time::Point(point.point - span.duration); }
time::Span time::operator-(const time::Point &lhs, const time::Point &rhs) { return time::Span(lhs.point - rhs.point); }
bool time::operator==(const time::Point &lhs, const time::Point &rhs) { return lhs.point == rhs.point; }
bool time::operator!=(const time::Point &lhs, const time::Point &rhs) { return lhs.point != rhs.point; }
bool time::operator<(const time::Point &lhs, const time::Point &rhs) { return lhs.point < rhs.point; }
bool time::operator<=(const time::Point &lhs, const time::Point &rhs) { return lhs.point <= rhs.point; }
bool time::operator>(const time::Point &lhs, const time::Point &rhs) { return lhs.point > rhs.point; }
bool time::operator>=(const time::Point &lhs, const time::Point &rhs) { return lhs.point >= rhs.point; }


// === Span ===

// ctors
/// returns span from string in old (X.Y) and new (3h4min) format
time::Span::Span(const std::string &s) {
  if (s.empty()) return;

  std::regex re("^([0-9]*\\.?[0-9]+)|((([0-9]+)(h|min|s|ms|us|ns))+)$", std::regex::extended);
  std::regex nre("([0-9]+)(h|min|s|ms|us|ns)", std::regex::extended);
  std::smatch match;
  std::string submatch;

  // error
  if (!std::regex_match(s, match, re)) goto error;

  // old (double) format
  submatch = match[1].str();
  if (match[1].matched) {
    errno = 0;
    auto value = std::stod(submatch);
    if (errno) goto error;

    std::chrono::duration<double> d(value);
    duration = std::chrono::duration_cast<std::chrono::microseconds>(d);
  }

  // new (string) format
  submatch = match[2].str();
  for (std::smatch m; std::regex_search(submatch, m, nre); submatch = m.suffix()) {
    errno = 0;
    const auto value = std::stoull(m[1]);
    if (errno) goto error;

    Duration d;
    if (m[2] == "h") d = std::chrono::hours(value);
    else if (m[2] == "min") d = std::chrono::minutes(value);
    else if (m[2] ==   "s") d = std::chrono::seconds(value);
    else if (m[2] ==  "ms") d = std::chrono::milliseconds(value);
    else if (m[2] ==  "us") d = std::chrono::microseconds(value);
    else if (m[2] ==  "ns") d = std::chrono::nanoseconds(value);
    else goto error;

    duration += d;
  }

  return;

error:
  klee_error("Illegal number format: %s", s.c_str());
}

// operators
time::Span& time::Span::operator=(const time::Duration &d) { duration = d; return *this; };
time::Span& time::Span::operator+=(const time::Span &other) { duration += other.duration; return *this; }
time::Span& time::Span::operator-=(const time::Span &other) { duration -= other.duration; return *this; }
time::Span& time::Span::operator*=(unsigned factor) { duration *= factor; return *this; }
time::Span& time::Span::operator*=(double factor) {
  duration = std::chrono::microseconds((std::uint64_t)((double)toMicroseconds() * factor));
  return *this;
}

time::Span time::operator+(const time::Span &lhs, const time::Span &rhs) { return time::Span(lhs.duration + rhs.duration); }
time::Span time::operator-(const time::Span &lhs, const time::Span &rhs) { return time::Span(lhs.duration - rhs.duration); }
time::Span time::operator*(const time::Span &span, double factor) {
  return time::Span(std::chrono::microseconds((std::uint64_t)((double)span.toMicroseconds() * factor)));
};
time::Span time::operator*(double factor, const time::Span &span) {
  return time::Span(std::chrono::microseconds((std::uint64_t)((double)span.toMicroseconds() * factor)));
};
time::Span time::operator*(const time::Span &span, unsigned factor) { return time::Span(span.duration * factor); }
time::Span time::operator*(unsigned factor, const time::Span &span) { return time::Span(span.duration * factor); }
time::Span time::operator/(const time::Span &span, unsigned divisor) { return time::Span(span.duration / divisor); }
bool time::operator==(const time::Span &lhs, const time::Span &rhs) { return lhs.duration == rhs.duration; }
bool time::operator<=(const time::Span &lhs, const time::Span &rhs) { return lhs.duration <= rhs.duration; }
bool time::operator>=(const time::Span &lhs, const time::Span &rhs) { return lhs.duration >= rhs.duration; }
bool time::operator<(const time::Span &lhs, const time::Span &rhs) { return lhs.duration < rhs.duration; }
bool time::operator>(const time::Span &lhs, const time::Span &rhs) { return lhs.duration > rhs.duration; }

std::ostream& time::operator<<(std::ostream &stream, time::Span span) { return stream << span.toSeconds() << 's'; }
llvm::raw_ostream& time::operator<<(llvm::raw_ostream &stream, time::Span span) { return stream << span.toSeconds() << 's'; }


// units
time::Span time::hours(std::uint16_t ticks) { return time::Span(std::chrono::hours(ticks)); }
time::Span time::minutes(std::uint16_t ticks) { return time::Span(std::chrono::minutes(ticks)); }
time::Span time::seconds(std::uint64_t ticks) { return time::Span(std::chrono::seconds(ticks)); }
time::Span time::milliseconds(std::uint64_t ticks) { return time::Span(std::chrono::milliseconds(ticks)); }
time::Span time::microseconds(std::uint64_t ticks) { return time::Span(std::chrono::microseconds(ticks)); }
time::Span time::nanoseconds(std::uint64_t ticks) { return time::Span(std::chrono::nanoseconds(ticks)); }


// conversions
time::Span::operator time::Duration() const { return duration; }

time::Span::operator bool() const { return duration.count() != 0; }

time::Span::operator timeval() const {
  timeval tv{};
  const auto secs = std::chrono::duration_cast<std::chrono::seconds>(duration);
  const auto usecs = std::chrono::duration_cast<std::chrono::microseconds>(duration - secs);
  tv.tv_sec = secs.count();
  tv.tv_usec = usecs.count();
  return tv;
}

std::uint64_t time::Span::toMicroseconds() const {
  return (std::uint64_t)std::chrono::duration_cast<std::chrono::microseconds>(duration).count();
}

double time::Span::toSeconds() const {
  return std::chrono::duration_cast<std::chrono::nanoseconds>(duration).count() / (double)1000000000;
}

std::tuple<std::uint32_t, std::uint8_t, std::uint8_t> time::Span::toHMS() const {
  auto d = duration;
  const auto h = std::chrono::duration_cast<std::chrono::hours>(d);
  const auto m = std::chrono::duration_cast<std::chrono::minutes>(d -= h);
  const auto s = std::chrono::duration_cast<std::chrono::seconds>(d -= m);

  return std::make_tuple((std::uint32_t) h.count(), (std::uint8_t) m.count(), (std::uint8_t) s.count());
}


// methods
/// Returns information about clock
std::string time::getClockInfo() {
  std::stringstream buffer;
  buffer << "Using monotonic steady clock with "
         << std::chrono::steady_clock::period::num
         << '/'
         << std::chrono::steady_clock::period::den
         << "s resolution\n";
  return buffer.str();
}


/// Returns time spent by this process in user mode
time::Span time::getUserTime() {
  rusage usage{};
  auto ret = ::getrusage(RUSAGE_SELF, &usage);

  if (ret) {
    klee_warning("getrusage returned with error, return (0,0)");
    return {};
  } else {
    return time::seconds(static_cast<std::uint64_t>(usage.ru_utime.tv_sec)) +
           time::microseconds(static_cast<std::uint64_t>(usage.ru_utime.tv_usec));
  }
}


/// Returns point in time using a monotonic steady clock
time::Point time::getWallTime() {
  return time::Point(std::chrono::steady_clock::now());
}