summaryrefslogtreecommitdiff
path: root/src/util/statistics_registry.cpp
blob: 815c61059beee8fcd37f34bd08ee1ee17ad21565 (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/******************************************************************************
 * Top contributors (to current version):
 *   Gereon Kremer
 *
 * 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.
 * ****************************************************************************
 *
 * Central statistics registry.
 *
 * The StatisticsRegistry that issues statistic proxy objects.
 */

#include "util/statistics_registry.h"

#include "options/base_options.h"
#include "util/statistics_public.h"

namespace cvc5 {

StatisticsRegistry::StatisticsRegistry(Env& env, bool registerPublic)
    : EnvObj(env)
{
  if (registerPublic)
  {
    registerPublicStatistics(*this);
  }
}

AverageStat StatisticsRegistry::registerAverage(const std::string& name,
                                                bool expert)
{
  return registerStat<AverageStat>(name, expert);
}
IntStat StatisticsRegistry::registerInt(const std::string& name, bool expert)
{
  return registerStat<IntStat>(name, expert);
}
TimerStat StatisticsRegistry::registerTimer(const std::string& name,
                                            bool expert)
{
  return registerStat<TimerStat>(name, expert);
}

void StatisticsRegistry::storeSnapshot()
{
  if constexpr (Configuration::isStatisticsBuild())
  {
    d_lastSnapshot = std::make_unique<Snapshot>();
    for (const auto& s : d_stats)
    {
      if (!options().base.statisticsExpert && s.second->d_expert) continue;
      if (!options().base.statisticsAll && s.second->isDefault()) continue;
      d_lastSnapshot->emplace(
          s.first,
          s.second->getViewer());
    }
  }
}

StatisticBaseValue* StatisticsRegistry::get(const std::string& name) const
{
  if constexpr (Configuration::isStatisticsBuild())
  {
    auto it = d_stats.find(name);
    if (it == d_stats.end()) return nullptr;
    return it->second.get();
  }
  return nullptr;
}

void StatisticsRegistry::print(std::ostream& os) const
{
  if constexpr (Configuration::isStatisticsBuild())
  {
    for (const auto& s : d_stats)
    {
      if (!options().base.statisticsExpert && s.second->d_expert) continue;
      if (!options().base.statisticsAll && s.second->isDefault()) continue;
      os << s.first << " = " << *s.second << std::endl;
    }
  }
}

void StatisticsRegistry::printSafe(int fd) const
{
  if constexpr (Configuration::isStatisticsBuild())
  {
    for (const auto& s : d_stats)
    {
      if (!options().base.statisticsExpert && s.second->d_expert) continue;
      if (!options().base.statisticsAll && s.second->isDefault()) continue;

      safe_print(fd, s.first);
      safe_print(fd, " = ");
      s.second->printSafe(fd);
      safe_print(fd, "\n");
    }
  }
}
void StatisticsRegistry::printDiff(std::ostream& os) const
{
  if constexpr (Configuration::isStatisticsBuild())
  {
    if (!d_lastSnapshot)
    {
      // we have no snapshot, print as usual
      print(os);
      return;
    }
    for (const auto& s : d_stats)
    {
      if (!options().base.statisticsExpert && s.second->d_expert) continue;
      if (!options().base.statisticsAll && s.second->isDefault())
      {
        auto oldit = d_lastSnapshot->find(s.first);
        if (oldit != d_lastSnapshot->end() && oldit->second != s.second->getViewer())
        {
          // present in the snapshot, now defaulted
          os << s.first << " = " << *s.second << " (was ";
          detail::print(os, oldit->second);
          os << ")" << std::endl;
        }
      }
      else
      {
        auto oldit = d_lastSnapshot->find(s.first);
        if (oldit == d_lastSnapshot->end())
        {
          // not present in the snapshot
          os << s.first << " = " << *s.second << " (was <default>)"
             << std::endl;
        }
        else if (oldit->second != s.second->getViewer())
        {
          // present in the snapshot, print old value
          os << s.first << " = " << *s.second << " (was ";
          detail::print(os, oldit->second);
          os << ")" << std::endl;
        }
      }
    }
  }
}

std::ostream& operator<<(std::ostream& os, const StatisticsRegistry& sr)
{
  sr.print(os);
  return os;
}

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