aboutsummaryrefslogtreecommitdiffstats
path: root/yt/yt/core/misc/hr_timer.h
blob: a39223f2c4ad045fc6f5815caf5e489b66d46b61 (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
#pragma once

#include "common.h"

#include <util/system/datetime.h>

namespace NYT::NHRTimer {

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

// Returns CPU internal cycle counter.
// On modern systems, cycle counters are consistent across cores and cycle rate
// can be considered constant for practical purposes.
Y_FORCE_INLINE ui64 GetRdtsc()
{
    return GetCycleCount();
}

// Represents an offset from an arbitrary point in the past;
// it should be used only for relative measurements.
struct THRInstant
{
    i64 Seconds;
    i64 Nanoseconds;
};

// Represents a duration in nano-seconds.
using THRDuration = ui64;

#ifdef _linux_
static_assert(
    sizeof(THRInstant) == sizeof(struct timespec),
    "THRInstant should be ABI-compatible with struct timespec");
static_assert(
    offsetof(THRInstant, Seconds) == offsetof(struct timespec, tv_sec),
    "THRInstant should be ABI-compatible with struct timespec");
static_assert(
    offsetof(THRInstant, Nanoseconds) == offsetof(struct timespec, tv_nsec),
    "THRInstant should be ABI-compatible with struct timespec");
#endif

// Returns instant.
void GetHRInstant(THRInstant* instant);

// Returns time difference in nanoseconds.
THRDuration GetHRDuration(const THRInstant& begin, const THRInstant& end);

// Returns instant resolution.
THRDuration GetHRResolution();

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

} // namespace NYT::NHRTimer