summaryrefslogtreecommitdiff
path: root/src/util/stats_timer.cpp
blob: 1ffbe28a85ead699f7d4f573ce545375c96eec17 (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
/******************************************************************************
 * Top contributors (to current version):
 *   Gereon Kremer, Morgan Deters, Andres Noetzli
 *
 * 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 "util/stats_timer.h"

#include <iostream>

#include "base/check.h"
#include "util/ostream_util.h"

namespace cvc5 {

template <>
void safe_print(int fd, const timer_stat_detail::duration& t)
{
  safe_print<uint64_t>(fd, t / std::chrono::seconds(1));
  safe_print(fd, ".");
  safe_print_right_aligned(fd, (t % std::chrono::seconds(1)).count(), 9);
}

void TimerStat::start()
{
  if (CVC5_USE_STATISTICS)
  {
    PrettyCheckArgument(!d_running, *this, "timer already running");
    d_start = timer_stat_detail::clock::now();
    d_running = true;
  }
}

void TimerStat::stop()
{
  if (CVC5_USE_STATISTICS)
  {
    AlwaysAssert(d_running) << "timer not running";
    d_data += timer_stat_detail::clock::now() - d_start;
    d_running = false;
  }
}

bool TimerStat::running() const { return d_running; }

timer_stat_detail::duration TimerStat::get() const
{
  auto data = d_data;
  if (CVC5_USE_STATISTICS && d_running)
  {
    data += timer_stat_detail::clock::now() - d_start;
  }
  return data;
}

SExpr TimerStat::getValue() const
{
  auto data = d_data;
  if (CVC5_USE_STATISTICS && d_running)
  {
    data += timer_stat_detail::clock::now() - d_start;
  }
  std::stringstream ss;
  ss << std::fixed << std::setprecision(8) << data;
  return SExpr(Rational::fromDecimal(ss.str()));
}

void TimerStat::flushInformation(std::ostream& out) const { out << get(); }

void TimerStat::safeFlushInformation(int fd) const
{
  // Overwrite the implementation in the superclass because we cannot use
  // getDataRef(): it might return stale data if the timer is currently
  // running.
  safe_print<timer_stat_detail::duration>(fd, get());
}

CodeTimer::CodeTimer(TimerStat& timer, bool allow_reentrant)
    : d_timer(timer), d_reentrant(false)
{
  if (!allow_reentrant || !(d_reentrant = d_timer.running()))
  {
    d_timer.start();
  }
}
CodeTimer::~CodeTimer()
{
  if (!d_reentrant)
  {
    d_timer.stop();
  }
}

}  // namespace cvc5
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback