forked from stephentu/silo
-
Notifications
You must be signed in to change notification settings - Fork 2
/
counter.h
163 lines (132 loc) · 3.41 KB
/
counter.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#ifndef _COUNTER_H_
#define _COUNTER_H_
// system event counters, for
#include <algorithm> // for std::max
#include <vector>
#include <map>
#include <string>
#include <stdint.h>
#include "macros.h"
#include "core.h"
#include "util.h"
#include "spinlock.h"
struct counter_data {
enum Type { TYPE_COUNT, TYPE_AGG };
counter_data()
: type_(TYPE_COUNT), count_(0), sum_(0), max_(0) {}
Type type_;
uint64_t count_;
uint64_t sum_;
uint64_t max_;
inline counter_data &
operator+=(const counter_data &that)
{
count_ += that.count_;
sum_ += that.sum_;
max_ = std::max(max_, that.max_);
return *this;
}
inline double
avg() const
{
INVARIANT(type_ == TYPE_AGG);
return double(sum_)/double(count_);
}
};
namespace private_ {
// these objects are *never* supposed to be destructed
// (this is a purposeful memory leak)
struct event_ctx {
static std::map<std::string, event_ctx *> &event_counters();
static spinlock &event_counters_lock();
// tag to avoid making event_ctx virtual
event_ctx(const std::string &name, bool avg_tag)
: name_(name), avg_tag_(avg_tag)
{}
~event_ctx()
{
ALWAYS_ASSERT(false);
}
event_ctx(const event_ctx &) = delete;
event_ctx &operator=(const event_ctx &) = delete;
event_ctx(event_ctx &&) = delete;
void stat(counter_data &d);
const std::string name_;
const bool avg_tag_;
// per-thread counts
percore<uint64_t, false, false> counts_;
};
// more expensive
struct event_ctx_avg : public event_ctx {
event_ctx_avg(const std::string &name) : event_ctx(name, true) {}
percore<uint64_t, false, false> sums_;
percore<uint64_t, false, false> highs_;
};
}
class event_counter {
public:
event_counter(const std::string &name);
event_counter(const event_counter &) = delete;
event_counter &operator=(const event_counter &) = delete;
event_counter(event_counter &&) = delete;
inline ALWAYS_INLINE void
inc(uint64_t i = 1)
{
#ifdef ENABLE_EVENT_COUNTERS
ctx_->counts_.my() += i;
#endif
}
inline ALWAYS_INLINE event_counter &
operator++()
{
inc();
return *this;
}
inline ALWAYS_INLINE event_counter &
operator+=(uint64_t i)
{
inc(i);
return *this;
}
// WARNING: an expensive operation!
static std::map<std::string, counter_data> get_all_counters();
// WARNING: an expensive operation!
static void reset_all_counters();
// WARNING: an expensive operation!
static bool
stat(const std::string &name, counter_data &d);
private:
#ifdef ENABLE_EVENT_COUNTERS
unmanaged<private_::event_ctx> ctx_;
#endif
};
class event_avg_counter {
public:
event_avg_counter(const std::string &name);
event_avg_counter(const event_avg_counter &) = delete;
event_avg_counter &operator=(const event_avg_counter &) = delete;
event_avg_counter(event_avg_counter &&) = delete;
inline ALWAYS_INLINE void
offer(uint64_t value)
{
#ifdef ENABLE_EVENT_COUNTERS
ctx_->counts_.my()++;
ctx_->sums_.my() += value;
ctx_->highs_.my() = std::max(ctx_->highs_.my(), value);
#endif
}
private:
#ifdef ENABLE_EVENT_COUNTERS
unmanaged<private_::event_ctx_avg> ctx_;
#endif
};
inline std::ostream &
operator<<(std::ostream &o, const counter_data &d)
{
if (d.type_ == counter_data::TYPE_COUNT)
o << "count=" << d.count_;
else
o << "count=" << d.count_ << ", max=" << d.max_ << ", avg=" << d.avg();
return o;
}
#endif /* _COUNTER_H_ */