source: src/Common/Stats/Counter.h@ 3c0d4cd

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr persistent-indexer pthread-emulation qualifiedEnum
Last change on this file since 3c0d4cd was 1cb7fab2, checked in by tdelisle <tdelisle@…>, 7 years ago

Added better support for enabling/disabling/compiling-out statistics

  • Property mode set to 100644
File size: 3.5 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2019 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// Counter.h --
8//
9// Author : Thierry Delisle
10// Created On : Thu Feb 28 12::05:10 2019
11// Last Modified By :
12// Last Modified On :
13// Update Count :
14//
15
16#pragma once
17
18#include <cstdint>
19#include <iostream>
20
21#include "Common/Stats/Base.h"
22
23#if defined( NO_STATISTICS )
24 #define NO_COUNTER_STATISTICS
25#endif
26
27namespace Stats {
28 namespace Counters {
29# if defined(NO_COUNTERS_STATISTICS)
30
31 static inline void print() {}
32
33 class CounterGroup {
34 public:
35 };
36
37 class SimpleCounter {
38 public:
39 inline void operator++(int) {}
40 inline void operator+=(size_t) {}
41 };
42
43 template<typename T>
44 class AverageCounter {
45 public:
46 inline void push(T value) {}
47 };
48
49 template<typename T>
50 class MaxCounter {
51 public:
52 inline void push(T value) {}
53 };
54
55 template<typename counter_t>
56 counter_t * build(const char * const name) {
57 return nullptr;
58 }
59
60 template<typename counter_t>
61 counter_t * build(const char * const name, Base::Tree<top> * parent) {
62 return nullptr;
63 }
64# else
65 extern bool enabled;
66
67 extern Base::TreeTop top;
68
69 class CounterGroup : public Base::Tree<top> {
70 public:
71 CounterGroup(const char * const name ) : Base::Tree<top>(name) {}
72 CounterGroup(const char * const name, Base::Tree<top> * parent) : Base::Tree<top>(name, parent) {}
73
74 virtual void print(std::ostream & os) override { os << ""; }
75 protected:
76 virtual ~CounterGroup() = default;
77 };
78
79 class SimpleCounter : public Base::Tree<top> {
80 public:
81 SimpleCounter(const char * const name ) : Base::Tree<top>(name) {}
82 SimpleCounter(const char * const name, Base::Tree<top> * parent) : Base::Tree<top>(name, parent) {}
83
84 virtual void print(std::ostream & os) override { os << count; }
85
86 inline void operator++(int) { if(!enabled) return; count++; }
87 inline void operator+=(size_t value) { if(!enabled) return; count += value; }
88
89 protected:
90 virtual ~SimpleCounter() = default;
91
92 private:
93 size_t count = 0;
94 };
95
96 template<typename T>
97 class AverageCounter : public Base::Tree<top> {
98 public:
99 AverageCounter(const char * const name ) : Base::Tree<top>(name), sum{} {}
100 AverageCounter(const char * const name, Base::Tree<top> * parent) : Base::Tree<top>(name, parent), sum{} {}
101
102 virtual void print(std::ostream & os) { os << sum / count; }
103
104 inline void push(T value) { if(!enabled) return; sum += value; count++; }
105
106 protected:
107 virtual ~AverageCounter() = default;
108
109 private:
110 T sum;
111 size_t count = 1;
112 };
113
114 template<typename T>
115 class MaxCounter : public Base::Tree<top> {
116 public:
117 MaxCounter(const char * const name ) : Base::Tree<top>(name), max{} {}
118 MaxCounter(const char * const name, Base::Tree<top> * parent) : Base::Tree<top>(name, parent), max{} {}
119
120 virtual void print(std::ostream & os) { os << max; }
121
122 inline void push(T value) { if(!enabled) return; max = std::max(max, value); }
123
124 protected:
125 virtual ~MaxCounter() = default;
126
127 private:
128 T max;
129 };
130
131 template<typename counter_t>
132 counter_t * build(const char * const name) {
133 if(!enabled) return nullptr;
134 return new counter_t(name);
135 }
136
137 template<typename counter_t>
138 counter_t * build(const char * const name, Base::Tree<top> * parent) {
139 if(!enabled) return nullptr;
140 return new counter_t(name, parent);
141 }
142# endif
143 }
144}
Note: See TracBrowser for help on using the repository browser.