Ignore:
Timestamp:
Mar 4, 2019, 2:53:55 PM (6 years ago)
Author:
tdelisle <tdelisle@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, persistent-indexer, pthread-emulation, qualifiedEnum
Children:
874ffa4
Parents:
675716e
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Common/Stats/Counter.h

    r675716e r1cb7fab2  
    1818#include <cstdint>
    1919#include <iostream>
    20 #include <string>
    2120
    2221#include "Common/Stats/Base.h"
    2322
     23#if defined( NO_STATISTICS )
     24        #define NO_COUNTER_STATISTICS
     25#endif
     26
    2427namespace Stats {
    2528        namespace Counters {
    26                 void print();
     29#               if defined(NO_COUNTERS_STATISTICS)
    2730
    28                 extern Base::TreeImpl::Top top;
     31                        static inline void print() {}
    2932
    30                 class CounterGroup : public Base::Tree<top> {
    31                 public:
    32                         CounterGroup(const std::string & name ) : Base::Tree<top>(name) {}
    33                         CounterGroup(const std::string & name, Base::Tree<top> * parent) : Base::Tree<top>(name, parent) {}
     33                        class CounterGroup {
     34                        public:
     35                        };
    3436
    35                         virtual void print(std::ostream & os) override { os << ""; }
    36                 protected:
    37                         virtual ~CounterGroup() = default;
    38                 };
     37                        class SimpleCounter {
     38                        public:
     39                                inline void operator++(int) {}
     40                                inline void operator+=(size_t) {}
     41                        };
    3942
    40                 class SimpleCounter : public Base::Tree<top> {
    41                 public:
    42                         SimpleCounter(const std::string & name ) : Base::Tree<top>(name) {}
    43                         SimpleCounter(const std::string & name, Base::Tree<top> * parent) : Base::Tree<top>(name, parent) {}
     43                        template<typename T>
     44                        class AverageCounter {
     45                        public:
     46                                inline void push(T value) {}
     47                        };
    4448
    45                         virtual void print(std::ostream & os) override { os << count; }
     49                        template<typename T>
     50                        class MaxCounter {
     51                        public:
     52                                inline void push(T value) {}
     53                        };
    4654
    47                         inline void operator++(int)          { count++;        }
    48                         inline void operator+=(size_t value) { count += value; }
    49                 protected:
    50                         virtual ~SimpleCounter() = default;
    51 
    52                 private:
    53                         size_t count = 0;
    54                 };
    55 
    56                 template<typename T>
    57                 class AverageCounter : public Base::Tree<top> {
    58                 public:
    59                         AverageCounter(const std::string & name ) : Base::Tree<top>(name), sum{} {}
    60                         AverageCounter(const std::string & name, Base::Tree<top> * parent) : Base::Tree<top>(name, parent), sum{} {}
    61 
    62                         virtual void print(std::ostream & os) { os << sum / count; }
    63 
    64                         inline void push(T value) {
    65                                 sum += value;
    66                                 count++;
     55                        template<typename counter_t>
     56                        counter_t * build(const char * const name) {
     57                                return nullptr;
    6758                        }
    6859
    69                 protected:
    70                         virtual ~AverageCounter() = default;
     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;
    7166
    72                 private:
    73                         T sum;
    74                         size_t count = 1;
    75                 };
     67                        extern Base::TreeTop top;
    7668
    77                 template<typename T>
    78                 class MaxCounter : public Base::Tree<top> {
    79                 public:
    80                         MaxCounter(const std::string & name ) : Base::Tree<top>(name), max{} {}
    81                         MaxCounter(const std::string & name, Base::Tree<top> * parent) : Base::Tree<top>(name, parent), max{} {}
     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) {}
    8273
    83                         virtual void print(std::ostream & os) { os << max; }
     74                                virtual void print(std::ostream & os) override { os << ""; }
     75                        protected:
     76                                virtual ~CounterGroup() = default;
     77                        };
    8478
    85                         inline void push(T value) { max = std::max(max, value); }
    86                 protected:
    87                         virtual ~MaxCounter() = default;
     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) {}
    8883
    89                 private:
    90                         T max;
    91                 };
     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
    92143        }
    93144}
Note: See TracChangeset for help on using the changeset viewer.