about summary refs log tree commit diff homepage
path: root/include/klee/System/Time.h
blob: 2ebebdfda1fcd1c4e871de5eb46ec077086f83db (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
//===-- Time.h --------------------------------------------------*- C++ -*-===//
//
//                     The KLEE Symbolic Virtual Machine
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#ifndef KLEE_TIME_H
#define KLEE_TIME_H

#include "llvm/Support/raw_ostream.h"

#include <chrono>
#include <string>
#include <sys/time.h>

namespace klee {
  namespace time {

    /// The klee::time namespace offers various functions to measure the time (`getWallTime`)
    /// and to get timing information for the current KLEE process (`getUserTime`).
    /// This implementation is based on `std::chrono` and uses time points and time spans.
    /// For KLEE statistics, spans are converted to µs and stored in `uint64_t`.

    struct Point;
    struct Span;

    /// Returns information about clock
    std::string getClockInfo();

    /// Returns time spent by this process in user mode
    Span getUserTime();

    /// Returns point in time using a monotonic steady clock
    Point getWallTime();

    struct Point {
      using SteadyTimePoint = std::chrono::steady_clock::time_point;

      SteadyTimePoint point;

      // ctors
      Point() = default;
      explicit Point(SteadyTimePoint p): point(p) {};

      // operators
      Point& operator+=(const Span&);
      Point& operator-=(const Span&);
    };

    // operators
    Point operator+(const Point&, const Span&);
    Point operator+(const Span&, const Point&);
    Point operator-(const Point&, const Span&);
    Span operator-(const Point&, const Point&);
    bool operator==(const Point&, const Point&);
    bool operator!=(const Point&, const Point&);
    bool operator<(const Point&, const Point&);
    bool operator<=(const Point&, const Point&);
    bool operator>(const Point&, const Point&);
    bool operator>=(const Point&, const Point&);

    namespace { using Duration = std::chrono::steady_clock::duration; }

    struct Span {
      Duration duration = Duration::zero();

      // ctors
      Span() = default;
      explicit Span(const Duration &d): duration(d) {}
      explicit Span(const std::string &s);

      // operators
      Span& operator=(const Duration&);
      Span& operator+=(const Span&);
      Span& operator-=(const Span&);
      Span& operator*=(unsigned);
      Span& operator*=(double);

      // conversions
      explicit operator Duration() const;
      explicit operator bool() const;
      explicit operator timeval() const;

      std::uint64_t toMicroseconds() const;
      double toSeconds() const;
      std::tuple<std::uint32_t, std::uint8_t, std::uint8_t> toHMS() const; // hours, minutes, seconds
    };

    Span operator+(const Span&, const Span&);
    Span operator-(const Span&, const Span&);
    Span operator*(const Span&, double);
    Span operator*(double, const Span&);
    Span operator*(const Span&, unsigned);
    Span operator*(unsigned, const Span&);
    Span operator/(const Span&, unsigned);
    bool operator==(const Span&, const Span&);
    bool operator<=(const Span&, const Span&);
    bool operator>=(const Span&, const Span&);
    bool operator<(const Span&, const Span&);
    bool operator>(const Span&, const Span&);

    /// Span -> "X.Ys"
    std::ostream& operator<<(std::ostream&, Span);
    llvm::raw_ostream& operator<<(llvm::raw_ostream&, Span);

    /// time spans
    Span hours(std::uint16_t);
    Span minutes(std::uint16_t);
    Span seconds(std::uint64_t);
    Span milliseconds(std::uint64_t);
    Span microseconds(std::uint64_t);
    Span nanoseconds(std::uint64_t);

  } // time
} // klee

#endif /* KLEE_TIME_H */