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
|
#include <library/cpp/testing/gtest/gtest.h>
#include <library/cpp/yt/system/cpu_id.h>
#include <sched.h>
#include <vector>
namespace NYT {
namespace {
////////////////////////////////////////////////////////////////////////////////
std::vector<int> GetAllowedCpus()
{
cpu_set_t set;
CPU_ZERO(&set);
if (sched_getaffinity(0, sizeof(set), &set) != 0) {
return {};
}
std::vector<int> cpus;
for (int cpu = 0; cpu < CPU_SETSIZE; ++cpu) {
if (CPU_ISSET(cpu, &set)) {
cpus.push_back(cpu);
}
}
return cpus;
}
bool TryPinToCpu(int cpu)
{
cpu_set_t set;
CPU_ZERO(&set);
CPU_SET(cpu, &set);
// Pinning the current thread to a single allowed CPU: the kernel migrates us
// onto it before sched_setaffinity returns, and keeps us there (it is the only
// permitted CPU), so there is no migration race afterwards.
return sched_setaffinity(0, sizeof(set), &set) == 0;
}
////////////////////////////////////////////////////////////////////////////////
// When pinned to a CPU, GetCurrentCpuId must report exactly that CPU -- this checks
// the actual contract (the real running CPU), not just a range, and is independent
// of how many CPUs are online or how they are numbered.
TEST(TGetCurrentCpuIdTest, MatchesPinnedCpu)
{
auto allowedCpus = GetAllowedCpus();
if (allowedCpus.empty() || !TryPinToCpu(allowedCpus.front())) {
GTEST_SKIP() << "Cannot control CPU affinity in this environment";
}
for (int cpu : allowedCpus) {
ASSERT_TRUE(TryPinToCpu(cpu));
EXPECT_EQ(GetCurrentCpuId(), cpu);
}
}
////////////////////////////////////////////////////////////////////////////////
} // namespace
} // namespace NYT
|