summaryrefslogtreecommitdiff
path: root/src/util/stats_base.h
blob: 9c3222d026451a3d11290265874935dcc7ec3758 (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/******************************************************************************
 * Top contributors (to current version):
 *   Gereon Kremer, Morgan Deters, Tim King
 *
 * 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.
 * ****************************************************************************
 *
 * Base statistic classes.
 */

#include "cvc5_private_library.h"

#ifndef CVC5__UTIL__STATS_BASE_H
#define CVC5__UTIL__STATS_BASE_H

#include <iomanip>
#include <sstream>
#include <string>

#include "cvc4_export.h"
#include "util/safe_print.h"
#include "util/sexpr.h"
#include "util/stats_utils.h"

#ifdef CVC5_STATISTICS_ON
#define CVC5_USE_STATISTICS true
#else
#define CVC5_USE_STATISTICS false
#endif

namespace cvc5 {

/**
 * The base class for all statistics.
 *
 * This base class keeps the name of the statistic and declares the (pure)
 * virtual functions flushInformation() and safeFlushInformation().
 * Derived classes must implement these function and pass their name to
 * the base class constructor.
 */
class CVC4_EXPORT Stat
{
 public:
  /**
   * Construct a statistic with the given name.  Debug builds of CVC4
   * will throw an assertion exception if the given name contains the
   * statistic delimiter string.
   */
  Stat(const std::string& name);

  /** Destruct a statistic.  This base-class version does nothing. */
  virtual ~Stat() = default;

  /**
   * Flush the value of this statistic to an output stream.  Should
   * finish the output with an end-of-line character.
   */
  virtual void flushInformation(std::ostream& out) const = 0;

  /**
   * Flush the value of this statistic to a file descriptor. Should finish the
   * output with an end-of-line character. Should be safe to use in a signal
   * handler.
   */
  virtual void safeFlushInformation(int fd) const = 0;

  /** Get the name of this statistic. */
  const std::string& getName() const { return d_name; }

  /** Get the value of this statistic as a string. */
  virtual SExpr getValue() const
  {
    std::stringstream ss;
    flushInformation(ss);
    return SExpr(ss.str());
  }

 protected:
  /** The name of this statistic */
  std::string d_name;
}; /* class Stat */

/**
 * A data statistic that keeps a T and sets it with setData().
 *
 * Template class T must have an operator=() and a copy constructor.
 */
template <class T>
class BackedStat : public Stat
{
 public:
  /** Construct a backed statistic with the given name and initial value. */
  BackedStat(const std::string& name, const T& init) : Stat(name), d_data(init)
  {
  }

  /** Set the underlying data value to the given value. */
  void set(const T& t)
  {
    if (CVC5_USE_STATISTICS)
    {
      d_data = t;
    }
  }

  const T& get() const { return d_data; }

  /** Flush the value of the statistic to the given output stream. */
  virtual void flushInformation(std::ostream& out) const override
  {
    out << d_data;
  }

  virtual void safeFlushInformation(int fd) const override
  {
    safe_print<T>(fd, d_data);
  }

 protected:
  /** The internally-kept statistic value */
  T d_data;
}; /* class BackedStat<T> */

/**
 * A data statistic that references a data cell of type T,
 * implementing get() by referencing that memory cell, and
 * setData() by reassigning the statistic to point to the new
 * data cell.  The referenced data cell is kept as a const
 * reference, meaning the referenced data is never actually
 * modified by this class (it must be externally modified for
 * a reference statistic to make sense).  A common use for
 * this type of statistic is to output a statistic that is kept
 * outside the statistics package (for example, one that's kept
 * by a theory implementation for internal heuristic purposes,
 * which is important to keep even if statistics are turned off).
 *
 * Template class T must have an assignment operator=().
 */
template <class T>
class ReferenceStat : public Stat
{
 public:
  /**
   * Construct a reference stat with the given name and a reference
   * to nullptr.
   */
  ReferenceStat(const std::string& name) : Stat(name) {}

  /**
   * Construct a reference stat with the given name and a reference to
   * the given data.
   */
  ReferenceStat(const std::string& name, const T& data) : Stat(name)
  {
    set(data);
  }

  /** Set this reference statistic to refer to the given data cell. */
  void set(const T& t)
  {
    if (CVC5_USE_STATISTICS)
    {
      d_data = &t;
    }
  }
  const T& get() const { return *d_data; }

  /** Flush the value of the statistic to the given output stream. */
  virtual void flushInformation(std::ostream& out) const override
  {
    out << *d_data;
  }

  virtual void safeFlushInformation(int fd) const override
  {
    safe_print<T>(fd, *d_data);
  }

 private:
  /** The referenced data cell */
  const T* d_data = nullptr;
}; /* class ReferenceStat<T> */

/**
 * A backed integer-valued (64-bit signed) statistic.
 * This doesn't functionally differ from its base class BackedStat<int64_t>,
 * except for adding convenience functions for dealing with integers.
 */
class IntStat : public BackedStat<int64_t>
{
 public:
  /**
   * Construct an integer-valued statistic with the given name and
   * initial value.
   */
  IntStat(const std::string& name, int64_t init);

  /** Increment the underlying integer statistic. */
  IntStat& operator++();
  /** Increment the underlying integer statistic. */
  IntStat& operator++(int);

  /** Increment the underlying integer statistic by the given amount. */
  IntStat& operator+=(int64_t val);

  /** Keep the maximum of the current statistic value and the given one. */
  void maxAssign(int64_t val);

  /** Keep the minimum of the current statistic value and the given one. */
  void minAssign(int64_t val);

  SExpr getValue() const override { return SExpr(Integer(d_data)); }

}; /* class IntStat */

/**
 * The value for an AverageStat is the running average of (e1, e_2, ..., e_n),
 *   (e1 + e_2 + ... + e_n)/n,
 * where e_i is an entry added by an addEntry(e_i) call.
 * The value is initially always 0.
 * (This is to avoid making parsers confused.)
 *
 * A call to setData() will change the running average but not reset the
 * running count, so should generally be avoided.  Call addEntry() to add
 * an entry to the average calculation.
 */
class AverageStat : public BackedStat<double>
{
 public:
  /** Construct an average statistic with the given name. */
  AverageStat(const std::string& name);

  /** Add an entry to the running-average calculation. */
  AverageStat& operator<<(double e);

  SExpr getValue() const override;

 private:
  /**
   * The number of accumulations of the running average that we
   * have seen so far.
   */
  uint32_t d_count = 0;
  double d_sum = 0;
}; /* class AverageStat */

template <class T>
class SizeStat : public Stat
{
 public:
  SizeStat(const std::string& name, const T& sized) : Stat(name), d_sized(sized)
  {
  }
  ~SizeStat() {}

  /** Flush the value of the statistic to the given output stream. */
  void flushInformation(std::ostream& out) const override
  {
    out << d_sized.size();
  }

  void safeFlushInformation(int fd) const override
  {
    safe_print(fd, d_sized.size());
  }

 private:
  const T& d_sized;
}; /* class SizeStat */

}  // namespace cvc5

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