master
c 557 lines 18.4 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 #include "../libnetdata.h"
4
5 // defaults are for compatibility
6 // call clocks_init() once, to optimize these default settings
7 static clockid_t clock_boottime_to_use = CLOCK_MONOTONIC;
8 static clockid_t clock_monotonic_to_use = CLOCK_MONOTONIC;
9
10 // the default clock resolution is 1ms
11 #define DEFAULT_CLOCK_RESOLUTION_UT ((usec_t)0 * USEC_PER_SEC + (usec_t)1 * USEC_PER_MS)
12
13 // the max clock resolution is 10ms
14 #define MAX_CLOCK_RESOLUTION_UT ((usec_t)0 * USEC_PER_SEC + (usec_t)10 * USEC_PER_MS)
15
16 usec_t clock_monotonic_resolution = DEFAULT_CLOCK_RESOLUTION_UT;
17 usec_t clock_realtime_resolution = DEFAULT_CLOCK_RESOLUTION_UT;
18
19 #ifndef HAVE_CLOCK_GETTIME
20 inline int clock_gettime(clockid_t clk_id __maybe_unused, struct timespec *ts) {
21 struct timeval tv;
22 if(unlikely(gettimeofday(&tv, NULL) == -1)) {
23 netdata_log_error("gettimeofday() failed.");
24 return -1;
25 }
26 ts->tv_sec = tv.tv_sec;
27 ts->tv_nsec = (long)((tv.tv_usec % USEC_PER_SEC) * NSEC_PER_USEC);
28 return 0;
29 }
30 #endif
31
32 // Similar to CLOCK_MONOTONIC, but provides access to a raw hardware-based time that is not subject to NTP adjustments
33 // or the incremental adjustments performed by adjtime(3). This clock does not count time that the system is suspended
34
35 static void test_clock_monotonic_raw(void) {
36 #ifdef CLOCK_MONOTONIC_RAW
37 struct timespec ts;
38 if(clock_gettime(CLOCK_MONOTONIC_RAW, &ts) == -1 && errno == EINVAL)
39 clock_monotonic_to_use = CLOCK_MONOTONIC;
40 else
41 clock_monotonic_to_use = CLOCK_MONOTONIC_RAW;
42 #else
43 clock_monotonic_to_use = CLOCK_MONOTONIC;
44 #endif
45 }
46
47 // When running a binary with CLOCK_BOOTTIME defined on a system with a linux kernel older than Linux 2.6.39 the
48 // clock_gettime(2) system call fails with EINVAL. In that case it must fall-back to CLOCK_MONOTONIC.
49
50 static void test_clock_boottime(void) {
51 struct timespec ts;
52 if(clock_gettime(CLOCK_BOOTTIME, &ts) == -1 && errno == EINVAL)
53 clock_boottime_to_use = clock_monotonic_to_use;
54 else
55 clock_boottime_to_use = CLOCK_BOOTTIME;
56 }
57
58 static usec_t get_clock_resolution(clockid_t clock) {
59 struct timespec ts = { 0 };
60
61 if(clock_getres(clock, &ts) == 0) {
62 usec_t ret = (usec_t)ts.tv_sec * USEC_PER_SEC + (usec_t)ts.tv_nsec / NSEC_PER_USEC;
63 if(!ret && ts.tv_nsec > 0 && ts.tv_nsec < (long int)NSEC_PER_USEC)
64 return (usec_t)1;
65
66 else if(ret > MAX_CLOCK_RESOLUTION_UT) {
67 nd_log(NDLS_DAEMON, NDLP_ERR, "clock_getres(%d) returned %"PRIu64" usec is out of range, using defaults for clock resolution.", (int)clock, ret);
68 return DEFAULT_CLOCK_RESOLUTION_UT;
69 }
70
71 return ret;
72 }
73 else {
74 nd_log(NDLS_DAEMON, NDLP_ERR, "clock_getres(%d) failed, using defaults for clock resolution.", (int)clock);
75 return DEFAULT_CLOCK_RESOLUTION_UT;
76 }
77 }
78
79 // perform any initializations required for clocks
80
81 static __attribute__((constructor)) void clocks_init(void) {
82 os_get_system_HZ();
83
84 // monotonic raw has to be tested before boottime
85 test_clock_monotonic_raw();
86
87 // boottime has to be tested after monotonic coarse
88 test_clock_boottime();
89
90 clock_monotonic_resolution = get_clock_resolution(clock_monotonic_to_use);
91 clock_realtime_resolution = get_clock_resolution(CLOCK_REALTIME);
92
93 #if defined(OS_WINDOWS)
94 timeBeginPeriod(1);
95 clock_monotonic_resolution = 1 * USEC_PER_MS;
96 clock_realtime_resolution = 1 * USEC_PER_MS;
97 #endif
98 }
99
100 static __attribute__((destructor)) void clocks_fin(void) {
101 #if defined(OS_WINDOWS)
102 timeEndPeriod(1);
103 #endif
104 }
105
106 ALWAYS_INLINE time_t now_sec(clockid_t clk_id) {
107 struct timespec ts;
108 if(unlikely(clock_gettime(clk_id, &ts) == -1)) {
109 netdata_log_error("clock_gettime(%ld, &timespec) failed.", (long int)clk_id);
110 return 0;
111 }
112 return ts.tv_sec;
113 }
114
115 ALWAYS_INLINE usec_t now_usec(clockid_t clk_id) {
116 struct timespec ts;
117 if(unlikely(clock_gettime(clk_id, &ts) == -1)) {
118 netdata_log_error("clock_gettime(%ld, &timespec) failed.", (long int)clk_id);
119 return 0;
120 }
121 return (usec_t)ts.tv_sec * USEC_PER_SEC + (usec_t)(ts.tv_nsec % NSEC_PER_SEC) / NSEC_PER_USEC;
122 }
123
124 ALWAYS_INLINE int now_timeval(clockid_t clk_id, struct timeval *tv) {
125 struct timespec ts;
126
127 if(unlikely(clock_gettime(clk_id, &ts) == -1)) {
128 netdata_log_error("clock_gettime(%ld, &timespec) failed.", (long int)clk_id);
129 tv->tv_sec = 0;
130 tv->tv_usec = 0;
131 return -1;
132 }
133
134 tv->tv_sec = ts.tv_sec;
135 tv->tv_usec = (suseconds_t)((ts.tv_nsec % NSEC_PER_SEC) / NSEC_PER_USEC);
136 return 0;
137 }
138
139 ALWAYS_INLINE time_t now_realtime_sec(void) {
140 return now_sec(CLOCK_REALTIME);
141 }
142
143 ALWAYS_INLINE msec_t now_realtime_msec(void) {
144 return now_usec(CLOCK_REALTIME) / USEC_PER_MS;
145 }
146
147 ALWAYS_INLINE usec_t now_realtime_usec(void) {
148 return now_usec(CLOCK_REALTIME);
149 }
150
151 ALWAYS_INLINE int now_realtime_timeval(struct timeval *tv) {
152 return now_timeval(CLOCK_REALTIME, tv);
153 }
154
155 ALWAYS_INLINE time_t now_monotonic_sec(void) {
156 return now_sec(clock_monotonic_to_use);
157 }
158
159 ALWAYS_INLINE usec_t now_monotonic_usec(void) {
160 return now_usec(clock_monotonic_to_use);
161 }
162
163 ALWAYS_INLINE int now_monotonic_timeval(struct timeval *tv) {
164 return now_timeval(clock_monotonic_to_use, tv);
165 }
166
167 ALWAYS_INLINE time_t now_monotonic_high_precision_sec(void) {
168 return now_sec(CLOCK_MONOTONIC);
169 }
170
171 ALWAYS_INLINE usec_t now_monotonic_high_precision_usec(void) {
172 return now_usec(CLOCK_MONOTONIC);
173 }
174
175 ALWAYS_INLINE int now_monotonic_high_precision_timeval(struct timeval *tv) {
176 return now_timeval(CLOCK_MONOTONIC, tv);
177 }
178
179 ALWAYS_INLINE time_t now_boottime_sec(void) {
180 return now_sec(clock_boottime_to_use);
181 }
182
183 ALWAYS_INLINE usec_t now_boottime_usec(void) {
184 return now_usec(clock_boottime_to_use);
185 }
186
187 ALWAYS_INLINE int now_boottime_timeval(struct timeval *tv) {
188 return now_timeval(clock_boottime_to_use, tv);
189 }
190
191 ALWAYS_INLINE usec_t timeval_usec(struct timeval *tv) {
192 return (usec_t)tv->tv_sec * USEC_PER_SEC + (tv->tv_usec % USEC_PER_SEC);
193 }
194
195 ALWAYS_INLINE msec_t timeval_msec(struct timeval *tv) {
196 return (msec_t)tv->tv_sec * MSEC_PER_SEC + ((tv->tv_usec % USEC_PER_SEC) / MSEC_PER_SEC);
197 }
198
199 ALWAYS_INLINE susec_t dt_usec_signed(struct timeval *now, struct timeval *old) {
200 usec_t ts1 = timeval_usec(now);
201 usec_t ts2 = timeval_usec(old);
202
203 if(likely(ts1 >= ts2)) return (susec_t)(ts1 - ts2);
204 return -((susec_t)(ts2 - ts1));
205 }
206
207 ALWAYS_INLINE usec_t dt_usec(struct timeval *now, struct timeval *old) {
208 usec_t ts1 = timeval_usec(now);
209 usec_t ts2 = timeval_usec(old);
210 return (ts1 > ts2) ? (ts1 - ts2) : (ts2 - ts1);
211 }
212
213 #ifdef __linux__
214 void sleep_to_absolute_time(usec_t usec) {
215 static int einval_printed = 0, enotsup_printed = 0, eunknown_printed = 0;
216 clockid_t clock = CLOCK_REALTIME;
217
218 struct timespec req = {
219 .tv_sec = (time_t)(usec / USEC_PER_SEC),
220 .tv_nsec = (suseconds_t)((usec % USEC_PER_SEC) * NSEC_PER_USEC)
221 };
222
223 errno = 0;
224 int ret = 0;
225 while( (ret = clock_nanosleep(clock, TIMER_ABSTIME, &req, NULL)) != 0 ) {
226 if(ret == EINTR) {
227 errno = 0;
228 continue;
229 }
230 else {
231 if (ret == EINVAL) {
232 if (!einval_printed) {
233 einval_printed++;
234 netdata_log_error("Invalid time given to clock_nanosleep(): clockid = %d, tv_sec = %lld, tv_nsec = %ld",
235 clock,
236 (long long)req.tv_sec,
237 req.tv_nsec);
238 }
239 } else if (ret == ENOTSUP) {
240 if (!enotsup_printed) {
241 enotsup_printed++;
242 netdata_log_error("Invalid clock id given to clock_nanosleep(): clockid = %d, tv_sec = %lld, tv_nsec = %ld",
243 clock,
244 (long long)req.tv_sec,
245 req.tv_nsec);
246 }
247 } else {
248 if (!eunknown_printed) {
249 eunknown_printed++;
250 netdata_log_error("Unknown return value %d from clock_nanosleep(): clockid = %d, tv_sec = %lld, tv_nsec = %ld",
251 ret,
252 clock,
253 (long long)req.tv_sec,
254 req.tv_nsec);
255 }
256 }
257 sleep_usec(usec);
258 }
259 }
260 }
261 #endif
262
263 #define HEARTBEAT_MIN_OFFSET_UT (150 * USEC_PER_MS)
264 #define HEARTBEAT_RANDOM_OFFSET_UT (350 * USEC_PER_MS)
265
266 #define HEARTBEAT_ALIGNMENT_STATISTICS_SIZE 20
267 static SPINLOCK heartbeat_alignment_spinlock = SPINLOCK_INITIALIZER;
268 static size_t heartbeat_alignment_id = 0;
269
270 struct heartbeat_thread_statistics {
271 pid_t tid;
272 size_t sequence;
273 usec_t dt;
274 usec_t randomness;
275 };
276 static struct heartbeat_thread_statistics heartbeat_alignment_values[HEARTBEAT_ALIGNMENT_STATISTICS_SIZE] = { 0 };
277
278 void heartbeat_statistics(usec_t *min_ptr, usec_t *max_ptr, usec_t *average_ptr, size_t *count_ptr) {
279 struct heartbeat_thread_statistics current[HEARTBEAT_ALIGNMENT_STATISTICS_SIZE];
280 static struct heartbeat_thread_statistics old[HEARTBEAT_ALIGNMENT_STATISTICS_SIZE] = { 0 };
281
282 memcpy(current, heartbeat_alignment_values, sizeof(struct heartbeat_thread_statistics) * HEARTBEAT_ALIGNMENT_STATISTICS_SIZE);
283
284 usec_t min = 0, max = 0, total = 0, average = 0;
285 size_t i, count = 0;
286 for(i = 0; i < HEARTBEAT_ALIGNMENT_STATISTICS_SIZE ;i++) {
287 if(current[i].sequence == old[i].sequence) continue;
288 usec_t value = current[i].dt - old[i].dt;
289
290 if(!count) {
291 min = max = total = value;
292 count = 1;
293 }
294 else {
295 total += value;
296 if(value < min) min = value;
297 if(value > max) max = value;
298 count++;
299 }
300 }
301 if(count)
302 average = total / count;
303
304 if(min_ptr) *min_ptr = min;
305 if(max_ptr) *max_ptr = max;
306 if(average_ptr) *average_ptr = average;
307 if(count_ptr) *count_ptr = count;
308
309 memcpy(old, current, sizeof(struct heartbeat_thread_statistics) * HEARTBEAT_ALIGNMENT_STATISTICS_SIZE);
310 }
311
312 static XXH64_hash_t heartbeat_hash(usec_t step, size_t statistics_id) {
313 struct {
314 usec_t step;
315 pid_t pid;
316 pid_t tid;
317 usec_t now_ut;
318 size_t statistics_id;
319 char tag[ND_THREAD_TAG_MAX + 1];
320 } key = {
321 .step = step,
322 .pid = getpid(),
323 .tid = os_gettid(),
324 .now_ut = now_realtime_usec(),
325 .statistics_id = statistics_id,
326 };
327 strncpyz(key.tag, nd_thread_tag(), sizeof(key.tag) - 1);
328 return XXH3_64bits(&key, sizeof(key));
329 }
330
331 static usec_t heartbeat_randomness(XXH64_hash_t hash) {
332 usec_t offset_ut = HEARTBEAT_MIN_OFFSET_UT + (hash % HEARTBEAT_RANDOM_OFFSET_UT);
333
334 // A zero HZ value turns heartbeat initialization into SIGFPE.
335 usec_t hz = system_hz ? (usec_t)system_hz : 100;
336
337 // Calculate the scheduler tick interval in microseconds.
338 usec_t scheduler_step_ut = USEC_PER_SEC / hz;
339 if(scheduler_step_ut > 10 * USEC_PER_MS)
340 scheduler_step_ut = 10 * USEC_PER_MS;
341 else if(unlikely(!scheduler_step_ut))
342 scheduler_step_ut = 1;
343
344 // if the offset is close to the scheduler tick, move it away from it
345 if(offset_ut % scheduler_step_ut < scheduler_step_ut / 4)
346 offset_ut += scheduler_step_ut / 4;
347
348 return offset_ut;
349 }
350
351 inline void heartbeat_init(heartbeat_t *hb, usec_t step) {
352 if(!step) step = USEC_PER_SEC;
353
354 spinlock_lock(&heartbeat_alignment_spinlock);
355 hb->statistics_id = heartbeat_alignment_id;
356 heartbeat_alignment_id++;
357 spinlock_unlock(&heartbeat_alignment_spinlock);
358
359 hb->step = step;
360 hb->realtime = 0ULL;
361 hb->hash = heartbeat_hash(hb->step, hb->statistics_id);
362 hb->randomness = heartbeat_randomness(hb->hash);
363
364 if(hb->statistics_id < HEARTBEAT_ALIGNMENT_STATISTICS_SIZE) {
365 heartbeat_alignment_values[hb->statistics_id].dt = 0;
366 heartbeat_alignment_values[hb->statistics_id].sequence = 0;
367 heartbeat_alignment_values[hb->statistics_id].randomness = hb->randomness;
368 heartbeat_alignment_values[hb->statistics_id].tid = os_gettid();
369 }
370 }
371
372 // waits for the next heartbeat
373 // it waits using the monotonic clock
374 // it returns the dt using the realtime clock
375
376 usec_t heartbeat_next(heartbeat_t *hb) {
377 usec_t tick = hb->step;
378
379 usec_t dt;
380 usec_t now = now_realtime_usec();
381 usec_t next = now - (now % tick) + tick + hb->randomness;
382
383 // align the next time we want to the clock resolution
384 if(next % clock_realtime_resolution)
385 next = next - (next % clock_realtime_resolution) + clock_realtime_resolution;
386
387 // sleep_usec() has a loop to guarantee we will sleep for at least the requested time.
388 // According to the specs, when we sleep for a relative time, clock adjustments should
389 // not affect the duration we sleep.
390 sleep_usec_with_now(next - now, now);
391 spinlock_lock(&heartbeat_alignment_spinlock);
392 now = now_realtime_usec();
393 spinlock_unlock(&heartbeat_alignment_spinlock);
394
395 dt = now - hb->realtime;
396
397 if(hb->statistics_id < HEARTBEAT_ALIGNMENT_STATISTICS_SIZE) {
398 heartbeat_alignment_values[hb->statistics_id].dt += now - next;
399 heartbeat_alignment_values[hb->statistics_id].sequence++;
400 }
401
402 if(unlikely(now < next)) {
403 errno_clear();
404 nd_log_limit_static_global_var(erl, 10, 0);
405 nd_log_limit(&erl, NDLS_DAEMON, NDLP_NOTICE,
406 "heartbeat clock: woke up %"PRIu64" microseconds earlier than expected "
407 "(can be due to the CLOCK_REALTIME set to the past).",
408 next - now);
409 }
410 else if(unlikely(now - next > tick / 2)) {
411 errno_clear();
412 nd_log_limit_static_global_var(erl, 10, 0);
413 nd_log_limit(&erl, NDLS_DAEMON, NDLP_NOTICE,
414 "heartbeat clock: woke up %"PRIu64" microseconds later than expected "
415 "(can be due to system load or the CLOCK_REALTIME set to the future).",
416 now - next);
417 }
418
419 if(unlikely(!hb->realtime)) {
420 // the first time return zero
421 dt = 0;
422 }
423
424 hb->realtime = now;
425 return dt;
426 }
427
428 #if defined(OS_WINDOWS)
429 void sleep_usec_with_now(usec_t usec, usec_t started_ut __maybe_unused) {
430 if (usec == 0)
431 return;
432
433 // Honor Windows timer granularity by rounding the requested duration
434 // up to the next multiple of the effective clock resolution.
435 usec_t res_ut = clock_realtime_resolution ? clock_realtime_resolution : USEC_PER_MS;
436 usec_t to_sleep_ut = usec;
437 if (res_ut) {
438 to_sleep_ut = ((to_sleep_ut + res_ut - 1) / res_ut) * res_ut; // round up
439 }
440
441 // Convert microseconds to milliseconds for Sleep(), rounding up
442 DWORD sleep_ms = (DWORD)((to_sleep_ut + (USEC_PER_MS - 1)) / USEC_PER_MS);
443 if (sleep_ms == 0)
444 sleep_ms = 1; // safety: always sleep at least 1ms
445
446 Sleep(sleep_ms);
447 }
448 #else
449 void sleep_usec_with_now(usec_t usec, usec_t started_ut) {
450 // we expect microseconds (1.000.000 per second)
451 // but timespec is nanoseconds (1.000.000.000 per second)
452 struct timespec rem = { 0, 0 }, req = {
453 .tv_sec = (time_t) (usec / USEC_PER_SEC),
454 .tv_nsec = (suseconds_t) ((usec % USEC_PER_SEC) * NSEC_PER_USEC)
455 };
456
457 // make sure errno is not EINTR
458 errno_clear();
459
460 if(!started_ut)
461 started_ut = now_realtime_usec();
462
463 usec_t end_ut = started_ut + usec;
464
465 while (nanosleep(&req, &rem) != 0) {
466 if (likely(errno == EINTR && (rem.tv_sec || rem.tv_nsec))) {
467 req = rem;
468 rem = (struct timespec){ 0, 0 };
469
470 // break an infinite loop
471 errno_clear();
472
473 usec_t now_ut = now_realtime_usec();
474 if(now_ut >= end_ut)
475 break;
476
477 usec_t remaining_ut = (usec_t)req.tv_sec * USEC_PER_SEC + (usec_t)req.tv_nsec * NSEC_PER_USEC > usec;
478 usec_t check_ut = now_ut - started_ut;
479 if(remaining_ut > check_ut) {
480 req = (struct timespec){
481 .tv_sec = (time_t) ( check_ut / USEC_PER_SEC),
482 .tv_nsec = (suseconds_t) ((check_ut % USEC_PER_SEC) * NSEC_PER_USEC)
483 };
484 }
485 }
486 else {
487 netdata_log_error("Cannot nanosleep() for %"PRIu64" microseconds.", usec);
488 break;
489 }
490 }
491 }
492 #endif
493
494 static inline collected_number uptime_from_boottime(void) {
495 #ifdef CLOCK_BOOTTIME_IS_AVAILABLE
496 return (collected_number)(now_boottime_usec() / USEC_PER_MS);
497 #else
498 netdata_log_error("uptime cannot be read from CLOCK_BOOTTIME on this system.");
499 return 0;
500 #endif
501 }
502
503 static procfile *read_proc_uptime_ff = NULL;
504 static inline collected_number read_proc_uptime(const char *filename) {
505 if(unlikely(!read_proc_uptime_ff)) {
506 read_proc_uptime_ff = procfile_open(filename, " \t", PROCFILE_FLAG_DEFAULT);
507 if(unlikely(!read_proc_uptime_ff)) return 0;
508 }
509
510 read_proc_uptime_ff = procfile_readall(read_proc_uptime_ff);
511 if(unlikely(!read_proc_uptime_ff)) return 0;
512
513 if(unlikely(procfile_lines(read_proc_uptime_ff) < 1)) {
514 netdata_log_error("/proc/uptime has no lines.");
515 return 0;
516 }
517 if(unlikely(procfile_linewords(read_proc_uptime_ff, 0) < 1)) {
518 netdata_log_error("/proc/uptime has less than 1 word in it.");
519 return 0;
520 }
521
522 return (collected_number)(strtondd(procfile_lineword(read_proc_uptime_ff, 0, 0), NULL) * 1000.0);
523 }
524
525 inline collected_number uptime_msec(const char *filename){
526 static int use_boottime = -1;
527
528 if(unlikely(use_boottime == -1)) {
529 collected_number uptime_boottime = uptime_from_boottime();
530 collected_number uptime_proc = read_proc_uptime(filename);
531
532 long long delta = (long long)uptime_boottime - (long long)uptime_proc;
533 if(delta < 0) delta = -delta;
534
535 if(delta <= 1000 && uptime_boottime != 0) {
536 procfile_close(read_proc_uptime_ff);
537 netdata_log_info("Using now_boottime_usec() for uptime (dt is %lld ms)", delta);
538 use_boottime = 1;
539 }
540 else if(uptime_proc != 0) {
541 netdata_log_info("Using /proc/uptime for uptime (dt is %lld ms)", delta);
542 use_boottime = 0;
543 }
544 else {
545 netdata_log_error("Cannot find any way to read uptime on this system.");
546 return 1;
547 }
548 }
549
550 collected_number uptime;
551 if(use_boottime)
552 uptime = uptime_from_boottime();
553 else
554 uptime = read_proc_uptime(filename);
555
556 return uptime;
557 }