summaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/system/cpu_id.cpp
blob: 9f6e46c9fc9cd67fb3d8a905f0228e9d948638e1 (plain) (blame)
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
#include "cpu_id.h"

#include <library/cpp/yt/misc/tls.h>

#ifdef __linux__
#include <library/cpp/yt/rseq/rseq.h>

#include <sched.h>
#endif

namespace NYT::NDetail {

////////////////////////////////////////////////////////////////////////////////

#ifdef __linux__

YT_PREVENT_TLS_CACHING int GetCurrentCpuIdSlow()
{
#ifdef YT_RSEQ_AVAILABLE
    if (NRseq::EnsureCurrentThreadRegistered()) {
        auto cpuId = NRseq::ReadField<int>(NRseq::CpuIdFieldOffset);
        if (cpuId >= 0) {
            return cpuId;
        }
    }
#endif
    // No rseq fast path (unsupported arch, or thread not registered): sched_getcpu.
    auto cpuId = ::sched_getcpu();
    return cpuId >= 0 ? cpuId : 0;
}

#else

// No sched_getcpu (darwin / windows): sharding degenerates to one shard.
int GetCurrentCpuIdSlow()
{
    return 0;
}

#endif

////////////////////////////////////////////////////////////////////////////////

} // namespace NYT::NDetail