XROAD
xroad_tsc.h
Go to the documentation of this file.
1 
6 #pragma once
7 
9 #include <time.h>
10 #include <x86intrin.h>
11 
12 #ifdef __cplusplus
13 extern "C"
14 {
15 #endif
16 
18 static inline xroad_tsc_t xroad_tsc_now()
19 {
20 #ifndef XROAD_TSC_MOCKABLE
21  return (xroad_tsc_t){_rdtsc()};
22 #else
23  xroad_tsc_t xroad_tsc_mock_now(void);
24  return xroad_tsc_mock_now();
25 #endif
26 }
27 
29 typedef struct
30 {
31  uint64_t khz;
32  uint64_t tsc2ns_mult;
33  uint64_t ns2tsc_mult;
34  uint64_t tsc_1_sec;
36 
39 
42 
45 
55 xroad_tsc_t xroad_tsc_time_pair_now(clockid_t clockid, uint64_t* ns);
56 
57 enum
58 {
60  xroad_priv_tsc_sec_in_ns = 1000000000ul
61 };
62 
64 static inline xroad_tsc_t xroad_tsc_from_timespec_ex(struct timespec ts, const xroad_priv_tsc_conv_t* tsc_spec)
65 {
66  // This division is optimized by compiler
67  return (xroad_tsc_t){ts.tv_sec * tsc_spec->tsc_1_sec +
68  ((ts.tv_nsec * tsc_spec->ns2tsc_mult) >> xroad_priv_tsc_conv_shift)};
69 }
70 
72 static inline xroad_tsc_t xroad_tsc_from_timespec(struct timespec ts)
73 {
74  return xroad_tsc_from_timespec_ex(ts, &g_xroad_priv_cc);
75 }
76 
78 static inline xroad_tsc_t xroad_tsc_from_ns(uint64_t ns)
79 {
80  // This division is optimized by compiler
81  return xroad_tsc_from_timespec((struct timespec){(int64_t)(ns / xroad_priv_tsc_sec_in_ns),
82  (int64_t)(ns % xroad_priv_tsc_sec_in_ns)});
83 }
84 
86 static inline xroad_tsc_t xroad_tsc_from_ns_ex(uint64_t ns, const xroad_priv_tsc_conv_t* tsc_spec)
87 {
88  // This division is optimized by compiler
89  return xroad_tsc_from_timespec_ex((struct timespec){(int64_t)(ns / xroad_priv_tsc_sec_in_ns),
90  (int64_t)(ns % xroad_priv_tsc_sec_in_ns)}, tsc_spec);
91 }
92 
94 static inline struct timespec xroad_tsc_dur_to_timespec_ex(xroad_tsc_t t, const xroad_priv_tsc_conv_t* tsc_spec)
95 {
96  uint64_t sec = 0;
97  while(t.value >= tsc_spec->tsc_1_sec)
98  {
99  ++sec;
100  t.value -= tsc_spec->tsc_1_sec;
101  }
102  return (struct timespec){(int64_t)sec,
103  (int64_t)((t.value * tsc_spec->tsc2ns_mult) >> xroad_priv_tsc_conv_shift)};
104 }
105 
107 static inline struct timespec xroad_tsc_dur_to_timespec(xroad_tsc_t t)
108 {
109  return xroad_tsc_dur_to_timespec_ex(t, &g_xroad_priv_cc);
110 }
111 
113 static inline uint64_t xroad_tsc_dur_to_ns_ex(xroad_tsc_t t, const xroad_priv_tsc_conv_t* tsc_spec)
114 {
115  struct timespec ts = xroad_tsc_dur_to_timespec_ex(t, tsc_spec);
116  return ts.tv_sec * xroad_priv_tsc_sec_in_ns + ts.tv_nsec;
117 }
118 
120 static inline uint64_t xroad_tsc_dur_to_ns(xroad_tsc_t t)
121 {
122  return xroad_tsc_dur_to_ns_ex(t, &g_xroad_priv_cc);
123 }
124 
126 static inline uint64_t xroad_tsc_ts_to_ns_ex(xroad_tsc_t from, uint64_t ref_ns, xroad_tsc_t ref_tsc,
127  const xroad_priv_tsc_conv_t* tsc_spec)
128 {
129  int64_t tsc_delta = from.value - ref_tsc.value;
130  int64_t ns_delta;
131  if (tsc_delta > 0)
132  {
133  ns_delta = xroad_tsc_dur_to_ns_ex((xroad_tsc_t){(uint64_t)tsc_delta}, tsc_spec);
134  }
135  else
136  {
137  ns_delta = -xroad_tsc_dur_to_ns_ex((xroad_tsc_t){(uint64_t)-tsc_delta}, tsc_spec);
138  }
139  return (uint64_t)(ref_ns + ns_delta);
140 }
141 
143 static inline uint64_t xroad_tsc_ts_to_ns(xroad_tsc_t from, uint64_t ref_ns, xroad_tsc_t ref_tsc)
144 {
145  return xroad_tsc_ts_to_ns_ex(from, ref_ns, ref_tsc, &g_xroad_priv_cc);
146 }
147 
148 // Compare two TSC timestamps. Intel guarantees that wrap-around won't happen
149 // in 10 years after CPU Reset. However there's no overhead using the math below.
150 // Basically comparisons of `xroad_tsc_cmp(l, r)` with 0 (>, >=, ==, <=, <) dons't produce
151 // more code than required (well, sometimes gcc generates `sub` + `test` instead of `cmp`
152 // but I don't think it's countable).
153 static inline int xroad_tsc_cmp(xroad_tsc_t l, xroad_tsc_t r)
154 {
155  const int64_t signed_res = (int64_t)(l.value - r.value);
156  if (signed_res < 0)
157  {
158  return -1;
159  }
160  if (signed_res > 0)
161  {
162  return 1;
163  }
164  return 0;
165 }
166 
167 static inline xroad_tsc_t xroad_tsc_plus(xroad_tsc_t l, xroad_tsc_t r)
168 {
169  return (xroad_tsc_t){l.value + r.value};
170 }
171 
172 static inline xroad_tsc_t xroad_tsc_minus(xroad_tsc_t l, xroad_tsc_t r)
173 {
174  return (xroad_tsc_t){l.value - r.value};
175 }
176 
177 #ifdef __cplusplus
178 }
179 #endif
Internal structure with TSC <-> wallclock conversion constants.
Definition: xroad_tsc.h:30
uint64_t ns2tsc_mult
Multiplier fot nsec->TSC conversion.
Definition: xroad_tsc.h:33
uint64_t tsc2ns_mult
Multiplier fot TSC->nsec conversion.
Definition: xroad_tsc.h:32
uint64_t tsc_1_sec
Cached value of TSC cycles in 1 second.
Definition: xroad_tsc.h:34
uint64_t khz
TSC frequency in kilohertz.
Definition: xroad_tsc.h:31
Definition: xroad_common_types.h:21
xroad_priv_tsc_conv_t xroad_tsc_spec_from_khz(uint64_t khz)
Returns TSC/ns conversion spec from TSC frequency.
xroad_priv_tsc_conv_t g_xroad_priv_cc
Global instance of the xroad_priv_tsc_conv_t. Initialized at startup.
@ xroad_priv_tsc_conv_shift
Number of bit to shift-right in TSC<->nsec conversion.
Definition: xroad_tsc.h:59
void xroad_tsc_initialize(void)
Make sure the TSC sub-library is initialized.
xroad_tsc_t xroad_tsc_time_pair_now(clockid_t clockid, uint64_t *ns)
Obtain a relatively accurate time-pair, TSC and wallclock in nanoseconds since epoche.