summaryrefslogtreecommitdiff
path: root/src/util/stats_timer.h
blob: b11944152ca7984078df78eb03d130a90064dcf4 (plain)
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
/******************************************************************************
 * Top contributors (to current version):
 *   Morgan Deters, Gereon Kremer, Mathias Preiner
 *
 * This file is part of the cvc5 project.
 *
 * Copyright (c) 2009-2021 by the authors listed in the file AUTHORS
 * in the top-level source directory and their institutional affiliations.
 * All rights reserved.  See the file COPYING in the top-level source
 * directory for licensing information.
 * ****************************************************************************
 *
 * Timer statistics.
 *
 * Stat classes that hold timers.
 */

#include "cvc4_private_library.h"

#ifndef CVC5__UTIL__STATS_TIMER_H
#define CVC5__UTIL__STATS_TIMER_H

#include <chrono>

#include "cvc4_export.h"
#include "util/stats_base.h"

namespace cvc5 {
namespace timer_stat_detail {
using clock = std::chrono::steady_clock;
using time_point = clock::time_point;
struct duration : public std::chrono::nanoseconds
{
};
}  // namespace timer_stat_detail

template <>
void CVC4_EXPORT safe_print(int fd, const timer_stat_detail::duration& t);

class CodeTimer;

/**
 * A timer statistic.  The timer can be started and stopped
 * arbitrarily, like a stopwatch; the value of the statistic at the
 * end is the accumulated time over all (start,stop) pairs.
 */
class CVC4_EXPORT TimerStat : public BackedStat<timer_stat_detail::duration>
{
 public:
  typedef cvc5::CodeTimer CodeTimer;

  /**
   * Construct a timer statistic with the given name.  Newly-constructed
   * timers have a 0.0 value and are not running.
   */
  TimerStat(const std::string& name)
      : BackedStat<timer_stat_detail::duration>(name,
                                                timer_stat_detail::duration()),
        d_start(),
        d_running(false)
  {
  }

  /** Start the timer. */
  void start();

  /**
   * Stop the timer and update the statistic value with the
   * accumulated time.
   */
  void stop();

  /** If the timer is currently running */
  bool running() const;

  timer_stat_detail::duration get() const;

  void flushInformation(std::ostream& out) const override;
  void safeFlushInformation(int fd) const override;

  SExpr getValue() const override;

 private:
  /** The last start time of this timer */
  timer_stat_detail::time_point d_start;

  /** Whether this timer is currently running */
  bool d_running;
}; /* class TimerStat */

/**
 * Utility class to make it easier to call stop() at the end of a
 * code block.  When constructed, it starts the timer.  When
 * destructed, it stops the timer.
 */
class CodeTimer
{
 public:
  CodeTimer(TimerStat& timer, bool allow_reentrant = false);
  ~CodeTimer();

private:
  TimerStat& d_timer;
  bool d_reentrant;

  /** Private copy constructor undefined (no copy permitted). */
  CodeTimer(const CodeTimer& timer) = delete;
  /** Private assignment operator undefined (no copy permitted). */
  CodeTimer& operator=(const CodeTimer& timer) = delete;
}; /* class CodeTimer */

}  // namespace cvc5

#endif
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback