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

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 a1099278 was 8f74a6a, checked in by tdelisle <tdelisle@…>, 7 years ago

Added code to support generic statistic counters in the compiler

  • Property mode set to 100644
File size: 2.9 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// Heap.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
21namespace Stats {
22 namespace Counters {
23 void print();
24
25 class BaseCounter {
26 public:
27 BaseCounter(const char * const name) : name(name) {
28 top.append(this);
29 }
30
31 BaseCounter(const char * const name, BaseCounter * parent) : name(name) {
32 parent->children.append(this);
33 }
34 protected:
35 virtual ~BaseCounter() = default;
36
37 struct list_t {
38 BaseCounter * head = nullptr;
39 BaseCounter * tail = nullptr;
40
41 void append(BaseCounter * node) {
42 if(!head) { head = node; }
43 else { tail->next = node;}
44 tail = node;
45 }
46 };
47
48 private:
49 virtual void print(std::ostream &) = 0;
50 template<typename T>
51 friend void ForAllCounters(BaseCounter::list_t &, size_t, T );
52 friend void print();
53
54 private:
55 const char * const name;
56
57 BaseCounter * next = nullptr;
58 list_t children;
59
60 static list_t top;
61 };
62
63 class CounterGroup : public BaseCounter {
64 public:
65 CounterGroup(const char * const name ) : BaseCounter(name) {}
66 CounterGroup(const char * const name, BaseCounter * parent) : BaseCounter(name, parent) {}
67
68 protected:
69 virtual ~CounterGroup() = default;
70
71 private:
72 virtual void print(std::ostream & os) {
73 os << "";
74 }
75 template<typename T>
76 friend void ForAllCounters(BaseCounter::list_t &, size_t, T );
77 friend void print();
78 };
79
80 class SimpleCounter : public BaseCounter {
81 public:
82 SimpleCounter(const char * const name ) : BaseCounter(name) {}
83 SimpleCounter(const char * const name, BaseCounter * parent) : BaseCounter(name, parent) {}
84
85 inline void operator++(int) { count++; }
86 inline void operator+=(size_t value) { count += value; }
87 protected:
88 virtual ~SimpleCounter() = default;
89
90 private:
91 virtual void print(std::ostream & os) {
92 os << count;
93 }
94 template<typename T>
95 friend void ForAllCounters(BaseCounter::list_t &, size_t, T );
96 friend void print();
97
98 size_t count = 0;
99
100 };
101
102 template<typename T>
103 class AverageCounter : public BaseCounter {
104 public:
105 AverageCounter(const char * const name ) : BaseCounter(name), sum{} {}
106 AverageCounter(const char * const name, BaseCounter * parent) : BaseCounter(name, parent), sum{} {}
107
108 inline void push(T value) {
109 sum += value;
110 count++;
111 }
112
113 protected:
114 virtual ~AverageCounter() = default;
115
116 private:
117 virtual void print(std::ostream & os) {
118 os << sum / count;
119 }
120 template<typename F>
121 friend void ForAllCounters(BaseCounter::list_t &, size_t, F );
122 friend void print();
123
124 T sum;
125 size_t count = 1;
126 };
127 }
128}
Note: See TracBrowser for help on using the repository browser.