-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy paththread_specific_counters.cpp
More file actions
88 lines (73 loc) · 2.23 KB
/
thread_specific_counters.cpp
File metadata and controls
88 lines (73 loc) · 2.23 KB
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
#include "public/perfmon/thread_specific_counters.h"
#include "counters.h"
#include <algorithm>
#include <vector>
namespace perfmon {
namespace internal {
namespace {
class TssCountersHolder {
public:
static TssCounters Expand() {
const auto guard = GlobalLockGuard();
auto &instance = UnsafeGetInstance();
instance.counters_.resize((UnsafeNumberOfCounters() / 16 + 1) * 16,
TssCounter{0, 0});
return TssCounters{instance.counters_.size(), &instance.counters_[0]};
}
static void UnsafeFlushAll() // Must be protected by the global mutex!
{
for (auto *it = global_head; it; it = it->next_) {
it->UnsafeFlush();
}
}
~TssCountersHolder() {
const auto guard = GlobalLockGuard();
if (next_) {
next_->prev_ = prev_;
}
if (prev_) {
prev_->next_ = next_;
}
if (global_head == this) {
global_head = next_;
}
const size_t size = std::min(counters_.size(), UnsafeNumberOfCounters());
for (size_t i = 0; i < size; ++i) {
UnsafeCommit(i, counters_[i].calls, counters_[i].ticks);
}
}
private:
static TssCountersHolder &
UnsafeGetInstance(); // Must be protected by the global mutex!
TssCountersHolder() // Must be protected by the global mutex!
{
next_ = global_head;
prev_ = nullptr;
if (global_head) {
global_head->prev_ = this;
}
global_head = this;
}
void UnsafeFlush() // Must be protected by the global mutex!
{
const size_t size = std::min(counters_.size(), UnsafeNumberOfCounters());
for (size_t i = 0; i < size; ++i) {
UnsafeAccumulate(i, counters_[i].calls, counters_[i].ticks);
}
}
std::vector<TssCounter> counters_;
TssCountersHolder *next_;
TssCountersHolder *prev_;
static TssCountersHolder *global_head;
};
TssCountersHolder *TssCountersHolder::global_head;
TssCountersHolder &TssCountersHolder::UnsafeGetInstance() {
static thread_local TssCountersHolder instance;
return instance;
}
} // namespace
thread_local TssCounters global_tss_counters;
void ExpandTssCounters() { global_tss_counters = TssCountersHolder::Expand(); }
void UnsafeFlushTssCounters() { TssCountersHolder::UnsafeFlushAll(); }
} // namespace internal
} // namespace perfmon