summaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/system/tscp-inl.h
blob: 9a068166b6c978bf8f174351d0eb19272888ab16 (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
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
#ifndef TSCP_INL_H_
#error "Direct inclusion of this file is not allowed, include tscp.h"
// For the sake of sane code completion.
#include "tscp.h"
#endif

#include "cpu_id.h"

#include <util/system/cpu_id.h>

namespace NYT::NProfiling {

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

namespace {

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

#if defined(__x86_64__)

const bool SupportsRdtscp = NX86::HaveRDTSCP();

#endif

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

} // namespace

inline TTscp TTscp::Get()
{
#if defined(__x86_64__)
    ui64 t;
    ui64 c;
    if (SupportsRdtscp) {
        ui64 rax, rcx, rdx;
        asm volatile ( "rdtscp\n" : "=a" (rax), "=c" (rcx), "=d" (rdx) : : );
        t = (rdx << 32) + rax;
        c = rcx;
    } else {
        ui64 rax, rdx;
        asm volatile ( "rdtsc\n" : "=a" (rax), "=d" (rdx) : : );
        t = (rdx << 32) + rax;

        // cpuId[1] >> 24 is an APIC id.
        ui32 cpuId[4];
        NX86::CpuId(1, 0, cpuId);
        c = cpuId[1] >> 24;
    }
#elif defined(__arm64__) || defined(__aarch64__)
    ui64 c;
    __asm__ volatile("mrs %x0, tpidrro_el0" : "=r"(c));
    c = c & 0x07u;
    TCpuInstant t = GetCpuInstant();
#endif
    return TTscp{
        .Instant = static_cast<TCpuInstant>(t),
        .ProcessorId = static_cast<int>(c) & (MaxProcessorId - 1)
    };
}

inline TTscp TTscp::GetApproximate()
{
    return TTscp{
        .Instant = GetApproximateCpuInstant(),
        .ProcessorId = NYT::GetCurrentCpuId() & (MaxProcessorId - 1),
    };
}

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

} // namespace NYT::NProfiling