Clean up build and remove generated files
[c11concurrency-benchmarks.git] / silo / counter.cc
1 #include "counter.h"
2 #include "util.h"
3 #include "lockguard.h"
4
5 using namespace std;
6 using namespace util;
7 using namespace private_;
8
9 map<string, event_ctx *> &
10 event_ctx::event_counters()
11 {
12   static map<string, event_ctx *> s_counters;
13   return s_counters;
14 }
15
16 spinlock &
17 event_ctx::event_counters_lock()
18 {
19   static spinlock s_lock;
20   return s_lock;
21 }
22
23 void
24 event_ctx::stat(counter_data &d)
25 {
26   for (size_t i = 0; i < coreid::NMaxCores; i++)
27     d.count_ += counts_[i];
28   if (avg_tag_) {
29     d.type_ = counter_data::TYPE_AGG;
30     uint64_t m = 0;
31     for (size_t i = 0; i < coreid::NMaxCores; i++) {
32       m = max(m, static_cast<event_ctx_avg *>(this)->highs_[i]);
33     }
34     uint64_t s = 0;
35     for (size_t i = 0; i < coreid::NMaxCores; i++)
36       s += static_cast<event_ctx_avg *>(this)->sums_[i];
37     d.sum_ = s;
38     d.max_ = m;
39   }
40 }
41
42 map<string, counter_data>
43 event_counter::get_all_counters()
44 {
45   map<string, counter_data> ret;
46   const map<string, event_ctx *> &evts = event_ctx::event_counters();
47   spinlock &l = event_ctx::event_counters_lock();
48   lock_guard<spinlock> sl(l);
49   for (auto &p : evts) {
50     counter_data d;
51     p.second->stat(d);
52     if (d.type_ == counter_data::TYPE_AGG)
53       ret[p.first].type_ = counter_data::TYPE_AGG;
54     ret[p.first] += d;
55   }
56   return ret;
57 }
58
59 void
60 event_counter::reset_all_counters()
61 {
62   const map<string, event_ctx *> &evts = event_ctx::event_counters();
63   spinlock &l = event_ctx::event_counters_lock();
64   lock_guard<spinlock> sl(l);
65   for (auto &p : evts)
66     for (size_t i = 0; i < coreid::NMaxCores; i++) {
67       p.second->counts_[i] = 0;
68       if (p.second->avg_tag_) {
69         static_cast<event_ctx_avg *>(p.second)->sums_[i] = 0;
70         static_cast<event_ctx_avg *>(p.second)->highs_[i] = 0;
71       }
72     }
73 }
74
75 bool
76 event_counter::stat(const string &name, counter_data &d)
77 {
78   const map<string, event_ctx *> &evts = event_ctx::event_counters();
79   spinlock &l = event_ctx::event_counters_lock();
80   event_ctx *ctx = nullptr;
81   {
82     lock_guard<spinlock> sl(l);
83     auto it = evts.find(name);
84     if (it != evts.end())
85       ctx = it->second;
86   }
87   if (!ctx)
88     return false;
89   ctx->stat(d);
90   return true;
91 }
92
93 #ifdef ENABLE_EVENT_COUNTERS
94 event_counter::event_counter(const string &name)
95   : ctx_(name, false)
96 {
97   spinlock &l = event_ctx::event_counters_lock();
98   map<string, event_ctx *> &evts = event_ctx::event_counters();
99   lock_guard<spinlock> sl(l);
100   evts[name] = ctx_.obj();
101 }
102
103 event_avg_counter::event_avg_counter(const string &name)
104   : ctx_(name)
105 {
106   spinlock &l = event_ctx::event_counters_lock();
107   map<string, event_ctx *> &evts = event_ctx::event_counters();
108   lock_guard<spinlock> sl(l);
109   evts[name] = ctx_.obj();
110 }
111 #else
112 event_counter::event_counter(const string &name)
113 {
114 }
115
116 event_avg_counter::event_avg_counter(const string &name)
117 {
118 }
119 #endif