-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathvarz.h
113 lines (83 loc) · 2.33 KB
/
varz.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
// Copyright 2021, Roman Gershman. All rights reserved.
// See LICENSE for licensing terms.
//
#pragma once
#include <absl/container/flat_hash_map.h>
#include <string_view>
#include "base/varz_node.h"
#include "util/sliding_counter.h"
#define DEFINE_VARZ(type, name) ::util::type name(#name)
namespace util {
class VarzQps : public base::VarzListNode {
public:
explicit VarzQps(const char* varname) : base::VarzListNode(varname) {
}
void Init(ProactorPool* pp) {
val_.Init(pp);
}
void Shutdown() {
val_.Shutdown();
}
void Inc() {
val_.Inc();
}
private:
virtual AnyValue GetData() const override;
using Counter = SlidingCounterDist<7>;
// 7-seconds window. We gather data based on the fully filled 6.
Counter val_;
};
class VarzMapAverage : public base::VarzListNode {
using Counter = SlidingCounter<7>;
using SumCnt = std::pair<SlidingCounter<7, uint64_t>, Counter>;
using Map = absl::flat_hash_map<std::string_view, SumCnt>;
public:
explicit VarzMapAverage(const char* varname) : base::VarzListNode(varname) {
}
~VarzMapAverage();
void Init(ProactorPool* pp);
void Shutdown();
void IncBy(std::string_view key, int32_t delta) {
auto& map = avg_map_[ProactorThreadIndex()];
auto it = map.find(key);
if (it == map.end()) {
it = FindSlow(key);
}
Inc(delta, &it->second);
}
private:
void Inc(int32_t delta, SumCnt* dest) {
dest->first.IncBy(delta);
dest->second.Inc();
}
virtual AnyValue GetData() const override;
unsigned ProactorThreadIndex() const;
Map::iterator FindSlow(std::string_view key);
ProactorPool* pp_ = nullptr;
std::unique_ptr<Map[]> avg_map_;
};
class VarzCount : public base::VarzListNode {
public:
explicit VarzCount(const char* varname) : base::VarzListNode(varname) {
}
~VarzCount();
void Init(ProactorPool* pp);
void Shutdown();
void IncBy(int64_t delta) {
count_.fetch_add(delta, std::memory_order_relaxed);
}
private:
AnyValue GetData() const override;
std::atomic_int64_t count_{0};
};
class VarzFunction : public base::VarzListNode {
public:
typedef AnyValue::Map KeyValMap;
typedef std::function<KeyValMap()> MapCb;
explicit VarzFunction(const char* varname, MapCb cb) : VarzListNode(varname), cb_(cb) {
}
private:
AnyValue GetData() const override;
MapCb cb_;
};
} // namespace util