summaryrefslogtreecommitdiff
path: root/src/util/statistics_stats.h
blob: 982190b7970c5d963793a843a6eaf77e520552f3 (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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/******************************************************************************
 * 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.
 * ****************************************************************************
 *
 * Statistic proxy objects
 *
 * Conceptually, every statistic consists of a data object and a proxy
 * object. The proxy objects are issued by the `StatisticsRegistry` and
 * maintained by the user. They only hold a pointer to a matching data
 * object. The purpose of proxy objects is to implement methods to easily
 * change the statistic data, but shield the regular user from the internals.
 */

#include "cvc5_private_library.h"

#ifndef CVC5__UTIL__STATISTICS_STATS_H
#define CVC5__UTIL__STATISTICS_STATS_H

#include <optional>

#include "base/configuration.h"

namespace cvc5 {

// forward declare all values to avoid inclusion
struct StatisticAverageValue;
template <typename T>
struct StatisticBackedValue;
template <typename T>
struct StatisticHistogramValue;
template <typename T>
struct StatisticReferenceValue;
template <typename T>
struct StatisticSizeValue;
struct StatisticTimerValue;

class StatisticsRegistry;

/**
 * Collects the average of a series of double values.
 * New values are added by
 *   AverageStat stat;
 *   stat << 1.0 << 2.0;
 */
class AverageStat
{
 public:
  /** Allow access to private constructor */
  friend class StatisticsRegistry;
  /** Value stored for this statistic */
  using stat_type = StatisticAverageValue;
  /** Add the value `v` to the running average */
  AverageStat& operator<<(double v);

 private:
  /** Construct from a pointer to the internal data */
  AverageStat(stat_type* data) : d_data(data) {}
  /** The actual data that lives in the registry */
  stat_type* d_data;
};

/**
 * Collects a histogram over some type.
 * The type needs to be (convertible to) integral and support streaming to
 * an `std::ostream`.
 * New values are added by
 *    HistogramStat<Kind> stat;
 *    stat << Kind::PLUS << Kind::AND;
 */
template <typename Integral>
class HistogramStat
{
 public:
  /** Allow access to private constructor */
  friend class StatisticsRegistry;
  /** Value stored for this statistic */
  using stat_type = StatisticHistogramValue<Integral>;
  /** Add the value `val` to the histogram */
  HistogramStat& operator<<(Integral val)
  {
    if constexpr (Configuration::isStatisticsBuild())
    {
      d_data->add(val);
    }
    return *this;
  }

 private:
  /** Construct from a pointer to the internal data */
  HistogramStat(stat_type* data) : d_data(data) {}
  /** The actual data that lives in the registry */
  stat_type* d_data;
};

/**
 * Stores the reference to some value that exists outside of this statistic.
 * Despite being called `ReferenceStat`, the reference is held as a pointer
 * and can thus be reset using `set`.
 * Note that the referenced object must have a lifetime that is longer than
 * the lifetime of the `ReferenceStat` object. Upon destruction of the
 * `ReferenceStat` the current value of the referenced object is copied into
 * the `StatisticsRegistry`.
 *
 * To convert to the API representation in `api::Stat`, `T` can only be one
 * of the types accepted by the `api::Stat` constructors (or be implicitly
 * converted to one of them).
 */
template <typename T>
class ReferenceStat
{
 public:
  /** Allow access to private constructor */
  friend class StatisticsRegistry;
  /** Value stored for this statistic */
  using stat_type = StatisticReferenceValue<T>;
  /** Reset the reference to point to `t`. */
  template <typename TT>
  void set(const TT& t)
  {
    static_assert(std::is_same_v<T, TT>, "Incorrect type for ReferenceStat");
    if constexpr (Configuration::isStatisticsBuild())
    {
      d_data->d_value = &t;
    }
  }
  /** Commit the value currently pointed to and release it. */
  void reset()
  {
    if constexpr (Configuration::isStatisticsBuild())
    {
      d_data->commit();
      d_data->d_value = nullptr;
    }
  }
  /** Copy the current value of the referenced object. */
  ~ReferenceStat()
  {
    if constexpr (Configuration::isStatisticsBuild())
    {
      d_data->commit();
    }
  }

 private:
  /** Construct from a pointer to the internal data */
  ReferenceStat(StatisticReferenceValue<T>* data) : d_data(data) {}
  /** The actual data that lives in the registry */
  StatisticReferenceValue<T>* d_data;
};

/**
 * Stores the size of some container that exists outside of this statistic.
 * Note that the referenced container must have a lifetime that is longer than
 * the lifetime of the `SizeStat` object. Upon destruction of the `SizeStat`
 * the current size of the referenced container is copied into the
 * `StatisticsRegistry`.
 */
template <typename T>
class SizeStat
{
 public:
  /** Allow access to private constructor */
  friend class StatisticsRegistry;
  /** Value stored for this statistic */
  using stat_type = StatisticSizeValue<T>;
  /** Reset the reference to point to `t`. */
  void set(const T& t)
  {
    if constexpr (Configuration::isStatisticsBuild())
    {
      d_data->d_value = &t;
    }
  }
  /** Copy the current size of the referenced container. */
  ~SizeStat()
  {
    if constexpr (Configuration::isStatisticsBuild())
    {
      d_data->commit();
    }
  }

 private:
  /** Construct from a pointer to the internal data */
  SizeStat(stat_type* data) : d_data(data) {}
  /** The actual data that lives in the registry */
  stat_type* d_data;
};

class CodeTimer;
/**
 * Collects cumulative runtimes. The timer can be started and stopped
 * arbitrarily like a stopwatch. The value of the statistic is the
 * accumulated time over all (start,stop) pairs.
 * While the runtimes are stored in nanosecond precision internally,
 * the API exports runtimes as integral numbers in millisecond
 * precision.
 *
 * Note that it is recommended to use it in an RAII fashion using the
 * `CodeTimer` class.
 */
class TimerStat
{
 public:
  /** Utility for RAII-style timing of code blocks */
  using CodeTimer = cvc5::CodeTimer;
  /** Allow access to private constructor */
  friend class StatisticsRegistry;
  /** Value stored for this statistic */
  using stat_type = StatisticTimerValue;

  /** Start the timer. Assumes it is not already running. */
  void start();
  /** Stop the timer. Assumes it is running. */
  void stop();
  /** Checks whether the timer is running. */
  bool running() const;

 private:
  /** Construct from a pointer to the internal data */
  TimerStat(stat_type* data) : d_data(data) {}
  /** The actual data that lives in the registry */
  stat_type* d_data;
};

/**
 * 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.
 *
 * Allows for reentrant usage. If `allow_reentrant` is true, we check
 * whether the timer is already running. If so, this particular instance
 * of `CodeTimer` neither starts nor stops the actual timer, but leaves
 * this to the first (or outermost) `CodeTimer`.
 */
class CodeTimer
{
 public:
  /** Disallow copying */
  CodeTimer(const CodeTimer& timer) = delete;
  /** Disallow assignment */
  CodeTimer& operator=(const CodeTimer& timer) = delete;
  /**
   * Start the timer.
   * If `allow_reentrant` is true we check whether the timer is already
   * running. If so, this particular instance of `CodeTimer` neither starts
   * nor stops the actual timer, but leaves this to the first (or outermost)
   * `CodeTimer`.
   */
  CodeTimer(TimerStat& timer, bool allow_reentrant = false);
  /** Stop the timer */
  ~CodeTimer();

 private:
  /** Reference to the timer this utility works on */
  TimerStat& d_timer;
  /** Whether this timer is reentrant (i.e. does not do anything) */
  bool d_reentrant;
};

/**
 * Stores a simple value that can be set manually using regular assignment
 * or the `set` method.
 *
 * To convert to the API representation in `api::Stat`, `T` can only be one
 * of the types accepted by the `api::Stat` constructors (or be implicitly
 * converted to one of them).
 */
template <typename T>
class ValueStat
{
 public:
  /** Allow access to private constructor */
  friend class StatisticsRegistry;
  friend class IntStat;
  /** Value stored for this statistic */
  using stat_type = StatisticBackedValue<T>;
  /** Set to `t` */
  void set(const T& t)
  {
    if constexpr (Configuration::isStatisticsBuild())
    {
      d_data->d_value = t;
    }
  }
  /** Set to `t` */
  ValueStat<T>& operator=(const T& t)
  {
    if constexpr (Configuration::isStatisticsBuild())
    {
      set(t);
    }
    return *this;
  }
  T get() const
  {
    if constexpr (Configuration::isStatisticsBuild())
    {
      return d_data->d_value;
    }
    return T();
  }

 private:
  /** Construct from a pointer to the internal data */
  ValueStat(StatisticBackedValue<T>* data) : d_data(data) {}
  /** The actual data that lives in the registry */
  StatisticBackedValue<T>* d_data;
};

/**
 * Stores an integer value as int64_t.
 * Supports the most useful standard operators (assignment, pre- and
 * post-increment, addition assignment) and some custom ones (maximum
 * assignment, minimum assignment).
 */
class IntStat : public ValueStat<int64_t>
{
 public:
  /** Allow access to private constructor */
  friend class StatisticsRegistry;
  /** Value stored for this statistic */
  using stat_type = StatisticBackedValue<int64_t>;
  /** Set to given value */
  IntStat& operator=(int64_t val);
  /** Pre-increment for the integer */
  IntStat& operator++();
  /** Post-increment for the integer */
  IntStat& operator++(int);
  /** Add `val` to the integer */
  IntStat& operator+=(int64_t val);
  /** Assign the maximum of the current value and `val` */
  void maxAssign(int64_t val);
  /** Assign the minimum of the current value and `val` */
  void minAssign(int64_t val);

 private:
  /** Construct from a pointer to the internal data */
  IntStat(stat_type* data) : ValueStat(data) {}
};

}  // namespace cvc5

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