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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
|
#include <library/cpp/testing/gtest/gtest.h>
#include <library/cpp/yt/rseq/per_cpu.h>
#include <library/cpp/yt/memory/public.h>
#include <util/system/types.h>
#include <atomic>
#include <iterator>
#include <thread>
#include <vector>
namespace NYT::NRseq {
namespace {
////////////////////////////////////////////////////////////////////////////////
// A per-CPU i64 accumulator built on the rseq primitives, mirroring how a profiling
// counter would use them.
class TPerCpuI64
{
public:
TPerCpuI64()
: Slots_(GetCpuCount())
{ }
void Add(i64 value)
{
AddPerCpu(Slots_.data(), &TSlot::Value, value);
}
i64 GetValue() const
{
i64 total = 0;
for (int index = 0; index < std::ssize(Slots_); ++index) {
total += LoadPerCpu(Slots_.data(), &TSlot::Value, index);
}
return total;
}
private:
struct alignas(CacheLineSize) TSlot
{
i64 Value = 0;
};
static_assert(sizeof(TSlot) == CacheLineSize);
std::vector<TSlot> Slots_;
};
////////////////////////////////////////////////////////////////////////////////
TEST(TPerCpuRseqTest, CpuCountIsSane)
{
EXPECT_GE(GetCpuCount(), 1);
EXPECT_LE(GetCpuCount(), 1 << 20);
}
TEST(TPerCpuRseqTest, FastPathSafetyIsStable)
{
// The probe spawns a thread on first use and caches its verdict, so repeated calls must
// agree. We avoid asserting a specific value: it depends on kernel rseq support.
bool safe = IsPerCpuFastPathSafe();
EXPECT_EQ(safe, IsPerCpuFastPathSafe());
}
TEST(TPerCpuRseqTest, ParsePossibleCpuCount)
{
using NDetail::ParsePossibleCpuCount;
EXPECT_EQ(ParsePossibleCpuCount("0"), 1);
EXPECT_EQ(ParsePossibleCpuCount("0-3"), 4);
EXPECT_EQ(ParsePossibleCpuCount("0-63"), 64);
// Sparse mask: the bound is the highest id + 1 (12), not the CPU popcount (8).
EXPECT_EQ(ParsePossibleCpuCount("0-3,8-11"), 12);
EXPECT_EQ(ParsePossibleCpuCount("0-3\n"), 4);
EXPECT_EQ(ParsePossibleCpuCount(""), -1);
EXPECT_EQ(ParsePossibleCpuCount("\n"), -1);
}
TEST(TPerCpuRseqTest, SingleThreadAccumulates)
{
TPerCpuI64 counter;
constexpr i64 Iterations = 1'000'000;
for (i64 i = 0; i < Iterations; ++i) {
counter.Add(1);
}
EXPECT_EQ(counter.GetValue(), Iterations);
}
TEST(TPerCpuRseqTest, SingleThreadHandlesNegativeAndLargeDeltas)
{
TPerCpuI64 counter;
counter.Add(1'000'000'000'000LL);
counter.Add(-7);
counter.Add(-1'000'000'000'000LL);
EXPECT_EQ(counter.GetValue(), -7);
}
// The core correctness guarantee: across many threads (which the scheduler migrates
// between CPUs, exercising rseq aborts/restarts), not a single increment is lost.
TEST(TPerCpuRseqTest, ConcurrentNoLostUpdates)
{
TPerCpuI64 counter;
const int threadCount = std::max<int>(4, std::thread::hardware_concurrency());
constexpr i64 PerThread = 2'000'000;
std::atomic<bool> start{false};
std::vector<std::thread> threads;
for (int t = 0; t < threadCount; ++t) {
threads.emplace_back([&] {
while (!start.load(std::memory_order::acquire)) {
}
for (i64 i = 0; i < PerThread; ++i) {
counter.Add(1);
}
});
}
start.store(true, std::memory_order::release);
for (auto& thread : threads) {
thread.join();
}
EXPECT_EQ(counter.GetValue(), static_cast<i64>(threadCount) * PerThread);
}
// Independent counters updated concurrently must not interfere with each other.
TEST(TPerCpuRseqTest, IndependentCountersDoNotInterfere)
{
TPerCpuI64 a;
TPerCpuI64 b;
const int threadCount = std::max<int>(4, std::thread::hardware_concurrency());
constexpr i64 PerThread = 1'000'000;
std::vector<std::thread> threads;
for (int t = 0; t < threadCount; ++t) {
threads.emplace_back([&, t] {
for (i64 i = 0; i < PerThread; ++i) {
a.Add(1);
if (t % 2 == 0) {
b.Add(2);
}
}
});
}
for (auto& thread : threads) {
thread.join();
}
EXPECT_EQ(a.GetValue(), static_cast<i64>(threadCount) * PerThread);
EXPECT_EQ(b.GetValue(), static_cast<i64>((threadCount + 1) / 2) * PerThread * 2);
}
////////////////////////////////////////////////////////////////////////////////
struct TPair
{
ui64 A;
ui64 B;
};
struct alignas(CacheLineSize) TPairSlot
{
TPair Value{};
};
static_assert(sizeof(TPairSlot) == CacheLineSize);
TEST(TPerCpuRseqTest, StorePerCpuPublishesValue)
{
std::vector<TPairSlot> slots(GetCpuCount());
constexpr ui64 Last = 100'000;
for (ui64 i = 1; i <= Last; ++i) {
StorePerCpu(slots.data(), &TPairSlot::Value, TPair{i, i});
}
bool foundLast = false;
for (const auto& slot : slots) {
// No store ever writes mismatched halves, so any populated slot must be consistent.
EXPECT_EQ(slot.Value.A, slot.Value.B);
if (slot.Value.A == Last) {
foundLast = true;
}
}
EXPECT_TRUE(foundLast);
}
////////////////////////////////////////////////////////////////////////////////
struct alignas(CacheLineSize) TWordSlot
{
ui64 Value = 0;
};
static_assert(sizeof(TWordSlot) == CacheLineSize);
TEST(TPerCpuRseqTest, StorePerCpu8PublishesValue)
{
std::vector<TWordSlot> slots(GetCpuCount());
constexpr ui64 Last = 100'000;
for (ui64 i = 1; i <= Last; ++i) {
StorePerCpu(slots.data(), &TWordSlot::Value, i);
}
bool foundLast = false;
for (const auto& slot : slots) {
if (slot.Value == Last) {
foundLast = true;
}
}
EXPECT_TRUE(foundLast);
}
////////////////////////////////////////////////////////////////////////////////
// LoadPerCpu must read exactly the requested slot (verifies the base + index * stride
// addressing), independent of the calling CPU.
TEST(TPerCpuRseqTest, LoadPerCpuReadsRequestedSlot)
{
std::vector<TWordSlot> slots(GetCpuCount());
for (int index = 0; index < std::ssize(slots); ++index) {
slots[index].Value = static_cast<ui64>(index) * 100 + 1;
}
for (int index = 0; index < std::ssize(slots); ++index) {
EXPECT_EQ(
LoadPerCpu(slots.data(), &TWordSlot::Value, index),
static_cast<ui64>(index) * 100 + 1);
}
}
////////////////////////////////////////////////////////////////////////////////
} // namespace
} // namespace NYT::NRseq
|