1 //===-- Time.h --------------------------------------------------*- C++ -*-===//
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 
10 #ifndef KLEE_TIME_H
11 #define KLEE_TIME_H
12 
13 #include "llvm/Support/raw_ostream.h"
14 
15 #include <chrono>
16 #include <string>
17 #include <sys/time.h>
18 
19 namespace klee {
20   namespace time {
21 
22     /// The klee::time namespace offers various functions to measure the time (`getWallTime`)
23     /// and to get timing information for the current KLEE process (`getUserTime`).
24     /// This implementation is based on `std::chrono` and uses time points and time spans.
25     /// For KLEE statistics, spans are converted to µs and stored in `uint64_t`.
26 
27     struct Point;
28     struct Span;
29 
30     /// Returns information about clock
31     std::string getClockInfo();
32 
33     /// Returns time spent by this process in user mode
34     Span getUserTime();
35 
36     /// Returns point in time using a monotonic steady clock
37     Point getWallTime();
38 
39     struct Point {
40       using SteadyTimePoint = std::chrono::steady_clock::time_point;
41 
42       SteadyTimePoint point;
43 
44       // ctors
45       Point() = default;
PointPoint46       explicit Point(SteadyTimePoint p): point(p) {};
47 
48       // operators
49       Point& operator+=(const Span&);
50       Point& operator-=(const Span&);
51     };
52 
53     // operators
54     Point operator+(const Point&, const Span&);
55     Point operator+(const Span&, const Point&);
56     Point operator-(const Point&, const Span&);
57     Span operator-(const Point&, const Point&);
58     bool operator==(const Point&, const Point&);
59     bool operator!=(const Point&, const Point&);
60     bool operator<(const Point&, const Point&);
61     bool operator<=(const Point&, const Point&);
62     bool operator>(const Point&, const Point&);
63     bool operator>=(const Point&, const Point&);
64 
65     namespace { using Duration = std::chrono::steady_clock::duration; }
66 
67     struct Span {
68       Duration duration = Duration::zero();
69 
70       // ctors
71       Span() = default;
SpanSpan72       explicit Span(const Duration &d): duration(d) {}
73       explicit Span(const std::string &s);
74 
75       // operators
76       Span& operator=(const Duration&);
77       Span& operator+=(const Span&);
78       Span& operator-=(const Span&);
79       Span& operator*=(unsigned);
80       Span& operator*=(double);
81 
82       // conversions
83       explicit operator Duration() const;
84       explicit operator bool() const;
85       explicit operator timeval() const;
86 
87       std::uint64_t toMicroseconds() const;
88       double toSeconds() const;
89       std::tuple<std::uint32_t, std::uint8_t, std::uint8_t> toHMS() const; // hours, minutes, seconds
90     };
91 
92     Span operator+(const Span&, const Span&);
93     Span operator-(const Span&, const Span&);
94     Span operator*(const Span&, double);
95     Span operator*(double, const Span&);
96     Span operator*(const Span&, unsigned);
97     Span operator*(unsigned, const Span&);
98     Span operator/(const Span&, unsigned);
99     bool operator==(const Span&, const Span&);
100     bool operator<=(const Span&, const Span&);
101     bool operator>=(const Span&, const Span&);
102     bool operator<(const Span&, const Span&);
103     bool operator>(const Span&, const Span&);
104 
105     /// Span -> "X.Ys"
106     std::ostream& operator<<(std::ostream&, Span);
107     llvm::raw_ostream& operator<<(llvm::raw_ostream&, Span);
108 
109     /// time spans
110     Span hours(std::uint16_t);
111     Span minutes(std::uint16_t);
112     Span seconds(std::uint64_t);
113     Span milliseconds(std::uint64_t);
114     Span microseconds(std::uint64_t);
115     Span nanoseconds(std::uint64_t);
116 
117   } // time
118 } // klee
119 
120 #endif /* KLEE_TIME_H */
121