-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbm_tlb_pressure.cpp
More file actions
84 lines (73 loc) · 2.73 KB
/
Copy pathbm_tlb_pressure.cpp
File metadata and controls
84 lines (73 loc) · 2.73 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
#include <benchmark/benchmark.h>
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <numeric>
#include <random>
#include <vector>
namespace {
constexpr std::size_t kStepsPerIteration = 4 * 1024 * 1024;
constexpr std::size_t kPageBytes = 4096;
constexpr std::size_t kPages = 32768;
std::vector<std::uint32_t> BuildPageCycle(bool randomize) {
std::vector<std::uint32_t> order(kPages);
std::iota(order.begin(), order.end(), 0U);
if (randomize) {
std::mt19937 rng(12345);
std::shuffle(order.begin(), order.end(), rng);
}
std::vector<std::uint32_t> next(kPages);
for (std::size_t i = 0; i + 1 < order.size(); ++i) {
next[order[i]] = order[i + 1];
}
next[order.back()] = order.front();
return next;
}
const std::vector<std::uint32_t>& GetPageCycle(bool randomize) {
static const std::vector<std::uint32_t> seq = BuildPageCycle(false);
static const std::vector<std::uint32_t> rnd = BuildPageCycle(true);
return randomize ? rnd : seq;
}
void BM_ContiguousPageWalk(benchmark::State& state) {
const std::size_t ints_per_page = kPageBytes / sizeof(std::uint32_t);
std::vector<std::uint32_t> data(kPages * ints_per_page);
std::uint64_t checksum = 0;
for (auto _ : state) {
for (std::size_t page = 0; page < kPages; ++page) {
checksum += data[page * ints_per_page];
}
benchmark::ClobberMemory();
}
benchmark::DoNotOptimize(checksum);
state.SetItemsProcessed(state.iterations() * static_cast<int64_t>(kPages));
}
void BM_PageStrideWalk(benchmark::State& state) {
const auto& next = GetPageCycle(false);
const std::size_t ints_per_page = kPageBytes / sizeof(std::uint32_t);
std::vector<std::uint32_t> data(kPages * ints_per_page);
std::uint32_t idx = 0;
for (auto _ : state) {
for (std::size_t step = 0; step < kStepsPerIteration; ++step) {
idx = next[idx];
benchmark::DoNotOptimize(data[static_cast<std::size_t>(idx) * ints_per_page]);
}
}
state.SetItemsProcessed(state.iterations() * static_cast<int64_t>(kStepsPerIteration));
}
void BM_RandomPageWalk(benchmark::State& state) {
const auto& next = GetPageCycle(true);
const std::size_t ints_per_page = kPageBytes / sizeof(std::uint32_t);
std::vector<std::uint32_t> data(kPages * ints_per_page);
std::uint32_t idx = 0;
for (auto _ : state) {
for (std::size_t step = 0; step < kStepsPerIteration; ++step) {
idx = next[idx];
benchmark::DoNotOptimize(data[static_cast<std::size_t>(idx) * ints_per_page]);
}
}
state.SetItemsProcessed(state.iterations() * static_cast<int64_t>(kStepsPerIteration));
}
} // namespace
BENCHMARK(BM_ContiguousPageWalk)->Unit(benchmark::kMicrosecond);
BENCHMARK(BM_PageStrideWalk)->Unit(benchmark::kMillisecond);
BENCHMARK(BM_RandomPageWalk)->Unit(benchmark::kMillisecond);