GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/util/stats_timer.cpp Lines: 40 42 95.2 %
Date: 2021-03-22 Branches: 26 72 36.1 %

Line Exec Source
1
/*********************                                                        */
2
/*! \file stats_timer.cpp
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 "util/stats_timer.h"
18
19
#include <iostream>
20
21
#include "base/check.h"
22
#include "util/ostream_util.h"
23
24
namespace CVC4 {
25
26
template <>
27
2
void safe_print(int fd, const timer_stat_detail::duration& t)
28
{
29
2
  safe_print<uint64_t>(fd, t / std::chrono::seconds(1));
30
2
  safe_print(fd, ".");
31
2
  safe_print_right_aligned(fd, (t % std::chrono::seconds(1)).count(), 9);
32
2
}
33
34
13657813
void TimerStat::start()
35
{
36
  if (CVC4_USE_STATISTICS)
37
  {
38
13657813
    PrettyCheckArgument(!d_running, *this, "timer already running");
39
13657813
    d_start = timer_stat_detail::clock::now();
40
13657813
    d_running = true;
41
  }
42
13657813
}
43
44
13655343
void TimerStat::stop()
45
{
46
  if (CVC4_USE_STATISTICS)
47
  {
48
13655343
    AlwaysAssert(d_running) << "timer not running";
49
13655343
    d_data += timer_stat_detail::clock::now() - d_start;
50
13655343
    d_running = false;
51
  }
52
13655343
}
53
54
723373
bool TimerStat::running() const { return d_running; }
55
56
6
timer_stat_detail::duration TimerStat::get() const
57
{
58
6
  auto data = d_data;
59
6
  if (CVC4_USE_STATISTICS && d_running)
60
  {
61
    data += timer_stat_detail::clock::now() - d_start;
62
  }
63
6
  return data;
64
}
65
66
14
SExpr TimerStat::getValue() const
67
{
68
14
  auto data = d_data;
69
14
  if (CVC4_USE_STATISTICS && d_running)
70
  {
71
    data += timer_stat_detail::clock::now() - d_start;
72
  }
73
28
  std::stringstream ss;
74
14
  ss << std::fixed << std::setprecision(8) << data;
75
28
  return SExpr(Rational::fromDecimal(ss.str()));
76
}
77
78
2
void TimerStat::flushInformation(std::ostream& out) const { out << get(); }
79
80
2
void TimerStat::safeFlushInformation(int fd) const
81
{
82
  // Overwrite the implementation in the superclass because we cannot use
83
  // getDataRef(): it might return stale data if the timer is currently
84
  // running.
85
2
  safe_print<timer_stat_detail::duration>(fd, get());
86
2
}
87
88
13649909
CodeTimer::CodeTimer(TimerStat& timer, bool allow_reentrant)
89
13649909
    : d_timer(timer), d_reentrant(false)
90
{
91
13649909
  if (!allow_reentrant || !(d_reentrant = d_timer.running()))
92
  {
93
13649909
    d_timer.start();
94
  }
95
13649909
}
96
27299818
CodeTimer::~CodeTimer()
97
{
98
13649909
  if (!d_reentrant)
99
  {
100
13649909
    d_timer.stop();
101
  }
102
13649909
}
103
104
26676
}  // namespace CVC4