-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathblock_profiler.hpp
64 lines (50 loc) · 1.62 KB
/
block_profiler.hpp
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
#pragma once
#include <iostream>
#include <atomic>
#include <mutex>
namespace ds2i {
class block_profiler {
public:
~block_profiler()
{
std::lock_guard<std::mutex> lock(m_mutex);
for (auto const& it: m_block_freqs) {
delete [] it.second.second;
}
}
typedef std::atomic_uint_fast32_t counter_type;
static block_profiler& get() {
static block_profiler instance;
return instance;
}
static counter_type* open_list(uint32_t term_id, uint32_t blocks)
{
block_profiler& instance = get();
std::lock_guard<std::mutex> lock(instance.m_mutex);
auto& v = instance.m_block_freqs[term_id];
if (v.second == nullptr) {
v.first = 2 * blocks;
v.second = new counter_type[v.first];
std::fill(v.second, v.second + v.first, 0);
}
return v.second;
}
static void dump(std::ostream& os)
{
block_profiler& instance = get();
std::lock_guard<std::mutex> lock(instance.m_mutex);
for (auto const& it: instance.m_block_freqs) {
os << it.first;
for (size_t i = 0; i < it.second.first; ++i) {
os << '\t' << it.second.second[i];
}
os << '\n';
}
}
private:
block_profiler() {}
// XXX can't do vector of atomics ARGHH
std::map<uint32_t, std::pair<size_t, counter_type*>> m_block_freqs;
std::mutex m_mutex;
};
}