Set incomplete if not applying ho extensionality (#6281)
[cvc5.git] / src / util / stats_timer.h
1 /********************* */
2 /*! \file stats_timer.h
3 ** \verbatim
4 ** Top contributors (to current version):
5 ** Gereon Kremer
6 ** This file is part of the CVC4 project.
7 ** Copyright (c) 2009-2021 by the authors listed in the file AUTHORS
8 ** in the top-level source directory and their institutional affiliations.
9 ** All rights reserved. See the file COPYING in the top-level source
10 ** directory for licensing information.\endverbatim
11 **
12 ** \brief Timer statistics
13 **
14 ** Stat classes that hold timers
15 **/
16
17 #include "cvc4_private_library.h"
18
19 #ifndef CVC4__UTIL__STATS_TIMER_H
20 #define CVC4__UTIL__STATS_TIMER_H
21
22 #include <chrono>
23
24 #include "cvc4_export.h"
25 #include "util/stats_base.h"
26
27 namespace cvc5 {
28 namespace timer_stat_detail {
29 using clock = std::chrono::steady_clock;
30 using time_point = clock::time_point;
31 struct duration : public std::chrono::nanoseconds
32 {
33 };
34 } // namespace timer_stat_detail
35
36 template <>
37 void CVC4_EXPORT safe_print(int fd, const timer_stat_detail::duration& t);
38
39 class CodeTimer;
40
41 /**
42 * A timer statistic. The timer can be started and stopped
43 * arbitrarily, like a stopwatch; the value of the statistic at the
44 * end is the accumulated time over all (start,stop) pairs.
45 */
46 class CVC4_EXPORT TimerStat : public BackedStat<timer_stat_detail::duration>
47 {
48 public:
49 typedef cvc5::CodeTimer CodeTimer;
50
51 /**
52 * Construct a timer statistic with the given name. Newly-constructed
53 * timers have a 0.0 value and are not running.
54 */
55 TimerStat(const std::string& name)
56 : BackedStat<timer_stat_detail::duration>(name,
57 timer_stat_detail::duration()),
58 d_start(),
59 d_running(false)
60 {
61 }
62
63 /** Start the timer. */
64 void start();
65
66 /**
67 * Stop the timer and update the statistic value with the
68 * accumulated time.
69 */
70 void stop();
71
72 /** If the timer is currently running */
73 bool running() const;
74
75 timer_stat_detail::duration get() const;
76
77 void flushInformation(std::ostream& out) const override;
78 void safeFlushInformation(int fd) const override;
79
80 SExpr getValue() const override;
81
82 private:
83 /** The last start time of this timer */
84 timer_stat_detail::time_point d_start;
85
86 /** Whether this timer is currently running */
87 bool d_running;
88 }; /* class TimerStat */
89
90 /**
91 * Utility class to make it easier to call stop() at the end of a
92 * code block. When constructed, it starts the timer. When
93 * destructed, it stops the timer.
94 */
95 class CodeTimer
96 {
97 public:
98 CodeTimer(TimerStat& timer, bool allow_reentrant = false);
99 ~CodeTimer();
100
101 private:
102 TimerStat& d_timer;
103 bool d_reentrant;
104
105 /** Private copy constructor undefined (no copy permitted). */
106 CodeTimer(const CodeTimer& timer) = delete;
107 /** Private assignment operator undefined (no copy permitted). */
108 CodeTimer& operator=(const CodeTimer& timer) = delete;
109 }; /* class CodeTimer */
110
111 } // namespace cvc5
112
113 #endif