source: src/Common/Stats/Counter.h @ fca6ca6

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpersistent-indexerpthread-emulationqualifiedEnum
Last change on this file since fca6ca6 was 1cb7fab2, checked in by tdelisle <tdelisle@…>, 5 years ago

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

  • Property mode set to 100644
File size: 3.5 KB
RevLine 
[8f74a6a]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//
[675716e]7// Counter.h --
[8f74a6a]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>
[8e70823]20
21#include "Common/Stats/Base.h"
[8f74a6a]22
[1cb7fab2]23#if defined( NO_STATISTICS )
24        #define NO_COUNTER_STATISTICS
25#endif
26
[8f74a6a]27namespace Stats {
28        namespace Counters {
[1cb7fab2]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;
[8f74a6a]66
[1cb7fab2]67                        extern Base::TreeTop top;
[8f74a6a]68
[1cb7fab2]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) {}
[8f74a6a]73
[1cb7fab2]74                                virtual void print(std::ostream & os) override { os << ""; }
75                        protected:
76                                virtual ~CounterGroup() = default;
77                        };
[8f74a6a]78
[1cb7fab2]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) {}
[8e70823]83
[1cb7fab2]84                                virtual void print(std::ostream & os) override { os << count; }
[8f74a6a]85
[1cb7fab2]86                                inline void operator++(int)          { if(!enabled) return; count++;        }
87                                inline void operator+=(size_t value) { if(!enabled) return; count += value; }
[8f74a6a]88
[1cb7fab2]89                        protected:
90                                virtual ~SimpleCounter() = default;
[8f74a6a]91
[1cb7fab2]92                        private:
93                                size_t count = 0;
94                        };
[8e70823]95
[1cb7fab2]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{} {}
[8f74a6a]101
[1cb7fab2]102                                virtual void print(std::ostream & os) { os << sum / count; }
103
104                                inline void push(T value) { if(!enabled) return; sum += value; count++; }
[8f74a6a]105
[1cb7fab2]106                        protected:
107                                virtual ~AverageCounter() = default;
[8f74a6a]108
[1cb7fab2]109                        private:
110                                T sum;
111                                size_t count = 1;
112                        };
[351c519]113
[1cb7fab2]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{} {}
[351c519]119
[1cb7fab2]120                                virtual void print(std::ostream & os) { os << max; }
[351c519]121
[1cb7fab2]122                                inline void push(T value) { if(!enabled) return; max = std::max(max, value); }
[351c519]123
[1cb7fab2]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
[8f74a6a]143        }
144}
Note: See TracBrowser for help on using the repository browser.