clocks.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "../libnetdata.h"
  3. // defaults are for compatibility
  4. // call clocks_init() once, to optimize these default settings
  5. static clockid_t clock_boottime_to_use = CLOCK_MONOTONIC;
  6. static clockid_t clock_monotonic_to_use = CLOCK_MONOTONIC;
  7. // the default clock resolution is 1ms
  8. #define DEFAULT_CLOCK_RESOLUTION_UT ((usec_t)0 * USEC_PER_SEC + (usec_t)1 * USEC_PER_MS)
  9. // the max clock resolution is 10ms
  10. #define MAX_CLOCK_RESOLUTION_UT ((usec_t)0 * USEC_PER_SEC + (usec_t)10 * USEC_PER_MS)
  11. usec_t clock_monotonic_resolution = DEFAULT_CLOCK_RESOLUTION_UT;
  12. usec_t clock_realtime_resolution = DEFAULT_CLOCK_RESOLUTION_UT;
  13. #ifndef HAVE_CLOCK_GETTIME
  14. inline int clock_gettime(clockid_t clk_id __maybe_unused, struct timespec *ts) {
  15. struct timeval tv;
  16. if(unlikely(gettimeofday(&tv, NULL) == -1)) {
  17. netdata_log_error("gettimeofday() failed.");
  18. return -1;
  19. }
  20. ts->tv_sec = tv.tv_sec;
  21. ts->tv_nsec = (long)((tv.tv_usec % USEC_PER_SEC) * NSEC_PER_USEC);
  22. return 0;
  23. }
  24. #endif
  25. // Similar to CLOCK_MONOTONIC, but provides access to a raw hardware-based time that is not subject to NTP adjustments
  26. // or the incremental adjustments performed by adjtime(3). This clock does not count time that the system is suspended
  27. static void test_clock_monotonic_raw(void) {
  28. #ifdef CLOCK_MONOTONIC_RAW
  29. struct timespec ts;
  30. if(clock_gettime(CLOCK_MONOTONIC_RAW, &ts) == -1 && errno == EINVAL)
  31. clock_monotonic_to_use = CLOCK_MONOTONIC;
  32. else
  33. clock_monotonic_to_use = CLOCK_MONOTONIC_RAW;
  34. #else
  35. clock_monotonic_to_use = CLOCK_MONOTONIC;
  36. #endif
  37. }
  38. // When running a binary with CLOCK_BOOTTIME defined on a system with a linux kernel older than Linux 2.6.39 the
  39. // clock_gettime(2) system call fails with EINVAL. In that case it must fall-back to CLOCK_MONOTONIC.
  40. static void test_clock_boottime(void) {
  41. struct timespec ts;
  42. if(clock_gettime(CLOCK_BOOTTIME, &ts) == -1 && errno == EINVAL)
  43. clock_boottime_to_use = clock_monotonic_to_use;
  44. else
  45. clock_boottime_to_use = CLOCK_BOOTTIME;
  46. }
  47. static usec_t get_clock_resolution(clockid_t clock) {
  48. struct timespec ts = { 0 };
  49. if(clock_getres(clock, &ts) == 0) {
  50. usec_t ret = (usec_t)ts.tv_sec * USEC_PER_SEC + (usec_t)ts.tv_nsec / NSEC_PER_USEC;
  51. if(!ret && ts.tv_nsec > 0 && ts.tv_nsec < NSEC_PER_USEC)
  52. return (usec_t)1;
  53. else if(ret > MAX_CLOCK_RESOLUTION_UT) {
  54. nd_log(NDLS_DAEMON, NDLP_ERR, "clock_getres(%d) returned %"PRIu64" usec is out of range, using defaults for clock resolution.", (int)clock, ret);
  55. return DEFAULT_CLOCK_RESOLUTION_UT;
  56. }
  57. return ret;
  58. }
  59. else {
  60. nd_log(NDLS_DAEMON, NDLP_ERR, "clock_getres(%d) failed, using defaults for clock resolution.", (int)clock);
  61. return DEFAULT_CLOCK_RESOLUTION_UT;
  62. }
  63. }
  64. // perform any initializations required for clocks
  65. void clocks_init(void) {
  66. // monotonic raw has to be tested before boottime
  67. test_clock_monotonic_raw();
  68. // boottime has to be tested after monotonic coarse
  69. test_clock_boottime();
  70. clock_monotonic_resolution = get_clock_resolution(clock_monotonic_to_use);
  71. clock_realtime_resolution = get_clock_resolution(CLOCK_REALTIME);
  72. }
  73. inline time_t now_sec(clockid_t clk_id) {
  74. struct timespec ts;
  75. if(unlikely(clock_gettime(clk_id, &ts) == -1)) {
  76. netdata_log_error("clock_gettime(%d, &timespec) failed.", clk_id);
  77. return 0;
  78. }
  79. return ts.tv_sec;
  80. }
  81. inline usec_t now_usec(clockid_t clk_id) {
  82. struct timespec ts;
  83. if(unlikely(clock_gettime(clk_id, &ts) == -1)) {
  84. netdata_log_error("clock_gettime(%d, &timespec) failed.", clk_id);
  85. return 0;
  86. }
  87. return (usec_t)ts.tv_sec * USEC_PER_SEC + (usec_t)(ts.tv_nsec % NSEC_PER_SEC) / NSEC_PER_USEC;
  88. }
  89. inline int now_timeval(clockid_t clk_id, struct timeval *tv) {
  90. struct timespec ts;
  91. if(unlikely(clock_gettime(clk_id, &ts) == -1)) {
  92. netdata_log_error("clock_gettime(%d, &timespec) failed.", clk_id);
  93. tv->tv_sec = 0;
  94. tv->tv_usec = 0;
  95. return -1;
  96. }
  97. tv->tv_sec = ts.tv_sec;
  98. tv->tv_usec = (suseconds_t)((ts.tv_nsec % NSEC_PER_SEC) / NSEC_PER_USEC);
  99. return 0;
  100. }
  101. inline time_t now_realtime_sec(void) {
  102. return now_sec(CLOCK_REALTIME);
  103. }
  104. inline msec_t now_realtime_msec(void) {
  105. return now_usec(CLOCK_REALTIME) / USEC_PER_MS;
  106. }
  107. inline usec_t now_realtime_usec(void) {
  108. return now_usec(CLOCK_REALTIME);
  109. }
  110. inline int now_realtime_timeval(struct timeval *tv) {
  111. return now_timeval(CLOCK_REALTIME, tv);
  112. }
  113. inline time_t now_monotonic_sec(void) {
  114. return now_sec(clock_monotonic_to_use);
  115. }
  116. inline usec_t now_monotonic_usec(void) {
  117. return now_usec(clock_monotonic_to_use);
  118. }
  119. inline int now_monotonic_timeval(struct timeval *tv) {
  120. return now_timeval(clock_monotonic_to_use, tv);
  121. }
  122. inline time_t now_monotonic_high_precision_sec(void) {
  123. return now_sec(CLOCK_MONOTONIC);
  124. }
  125. inline usec_t now_monotonic_high_precision_usec(void) {
  126. return now_usec(CLOCK_MONOTONIC);
  127. }
  128. inline int now_monotonic_high_precision_timeval(struct timeval *tv) {
  129. return now_timeval(CLOCK_MONOTONIC, tv);
  130. }
  131. inline time_t now_boottime_sec(void) {
  132. return now_sec(clock_boottime_to_use);
  133. }
  134. inline usec_t now_boottime_usec(void) {
  135. return now_usec(clock_boottime_to_use);
  136. }
  137. inline int now_boottime_timeval(struct timeval *tv) {
  138. return now_timeval(clock_boottime_to_use, tv);
  139. }
  140. inline usec_t timeval_usec(struct timeval *tv) {
  141. return (usec_t)tv->tv_sec * USEC_PER_SEC + (tv->tv_usec % USEC_PER_SEC);
  142. }
  143. inline msec_t timeval_msec(struct timeval *tv) {
  144. return (msec_t)tv->tv_sec * MSEC_PER_SEC + ((tv->tv_usec % USEC_PER_SEC) / MSEC_PER_SEC);
  145. }
  146. inline susec_t dt_usec_signed(struct timeval *now, struct timeval *old) {
  147. usec_t ts1 = timeval_usec(now);
  148. usec_t ts2 = timeval_usec(old);
  149. if(likely(ts1 >= ts2)) return (susec_t)(ts1 - ts2);
  150. return -((susec_t)(ts2 - ts1));
  151. }
  152. inline usec_t dt_usec(struct timeval *now, struct timeval *old) {
  153. usec_t ts1 = timeval_usec(now);
  154. usec_t ts2 = timeval_usec(old);
  155. return (ts1 > ts2) ? (ts1 - ts2) : (ts2 - ts1);
  156. }
  157. #ifdef __linux__
  158. void sleep_to_absolute_time(usec_t usec) {
  159. static int einval_printed = 0, enotsup_printed = 0, eunknown_printed = 0;
  160. clockid_t clock = CLOCK_REALTIME;
  161. struct timespec req = {
  162. .tv_sec = (time_t)(usec / USEC_PER_SEC),
  163. .tv_nsec = (suseconds_t)((usec % USEC_PER_SEC) * NSEC_PER_USEC)
  164. };
  165. errno = 0;
  166. int ret = 0;
  167. while( (ret = clock_nanosleep(clock, TIMER_ABSTIME, &req, NULL)) != 0 ) {
  168. if(ret == EINTR) {
  169. errno = 0;
  170. continue;
  171. }
  172. else {
  173. if (ret == EINVAL) {
  174. if (!einval_printed) {
  175. einval_printed++;
  176. netdata_log_error("Invalid time given to clock_nanosleep(): clockid = %d, tv_sec = %lld, tv_nsec = %ld",
  177. clock,
  178. (long long)req.tv_sec,
  179. req.tv_nsec);
  180. }
  181. } else if (ret == ENOTSUP) {
  182. if (!enotsup_printed) {
  183. enotsup_printed++;
  184. netdata_log_error("Invalid clock id given to clock_nanosleep(): clockid = %d, tv_sec = %lld, tv_nsec = %ld",
  185. clock,
  186. (long long)req.tv_sec,
  187. req.tv_nsec);
  188. }
  189. } else {
  190. if (!eunknown_printed) {
  191. eunknown_printed++;
  192. netdata_log_error("Unknown return value %d from clock_nanosleep(): clockid = %d, tv_sec = %lld, tv_nsec = %ld",
  193. ret,
  194. clock,
  195. (long long)req.tv_sec,
  196. req.tv_nsec);
  197. }
  198. }
  199. sleep_usec(usec);
  200. }
  201. }
  202. }
  203. #endif
  204. #define HEARTBEAT_ALIGNMENT_STATISTICS_SIZE 10
  205. netdata_mutex_t heartbeat_alignment_mutex = NETDATA_MUTEX_INITIALIZER;
  206. static size_t heartbeat_alignment_id = 0;
  207. struct heartbeat_thread_statistics {
  208. size_t sequence;
  209. usec_t dt;
  210. };
  211. static struct heartbeat_thread_statistics heartbeat_alignment_values[HEARTBEAT_ALIGNMENT_STATISTICS_SIZE] = { 0 };
  212. void heartbeat_statistics(usec_t *min_ptr, usec_t *max_ptr, usec_t *average_ptr, size_t *count_ptr) {
  213. struct heartbeat_thread_statistics current[HEARTBEAT_ALIGNMENT_STATISTICS_SIZE];
  214. static struct heartbeat_thread_statistics old[HEARTBEAT_ALIGNMENT_STATISTICS_SIZE] = { 0 };
  215. memcpy(current, heartbeat_alignment_values, sizeof(struct heartbeat_thread_statistics) * HEARTBEAT_ALIGNMENT_STATISTICS_SIZE);
  216. usec_t min = 0, max = 0, total = 0, average = 0;
  217. size_t i, count = 0;
  218. for(i = 0; i < HEARTBEAT_ALIGNMENT_STATISTICS_SIZE ;i++) {
  219. if(current[i].sequence == old[i].sequence) continue;
  220. usec_t value = current[i].dt - old[i].dt;
  221. if(!count) {
  222. min = max = total = value;
  223. count = 1;
  224. }
  225. else {
  226. total += value;
  227. if(value < min) min = value;
  228. if(value > max) max = value;
  229. count++;
  230. }
  231. }
  232. if(count)
  233. average = total / count;
  234. if(min_ptr) *min_ptr = min;
  235. if(max_ptr) *max_ptr = max;
  236. if(average_ptr) *average_ptr = average;
  237. if(count_ptr) *count_ptr = count;
  238. memcpy(old, current, sizeof(struct heartbeat_thread_statistics) * HEARTBEAT_ALIGNMENT_STATISTICS_SIZE);
  239. }
  240. inline void heartbeat_init(heartbeat_t *hb) {
  241. hb->realtime = 0ULL;
  242. hb->randomness = (usec_t)250 * USEC_PER_MS + ((usec_t)(now_realtime_usec() * clock_realtime_resolution) % (250 * USEC_PER_MS));
  243. hb->randomness -= (hb->randomness % clock_realtime_resolution);
  244. netdata_mutex_lock(&heartbeat_alignment_mutex);
  245. hb->statistics_id = heartbeat_alignment_id;
  246. heartbeat_alignment_id++;
  247. netdata_mutex_unlock(&heartbeat_alignment_mutex);
  248. if(hb->statistics_id < HEARTBEAT_ALIGNMENT_STATISTICS_SIZE) {
  249. heartbeat_alignment_values[hb->statistics_id].dt = 0;
  250. heartbeat_alignment_values[hb->statistics_id].sequence = 0;
  251. }
  252. }
  253. // waits for the next heartbeat
  254. // it waits using the monotonic clock
  255. // it returns the dt using the realtime clock
  256. usec_t heartbeat_next(heartbeat_t *hb, usec_t tick) {
  257. if(unlikely(hb->randomness > tick / 2)) {
  258. // TODO: The heartbeat tick should be specified at the heartbeat_init() function
  259. usec_t tmp = (now_realtime_usec() * clock_realtime_resolution) % (tick / 2);
  260. nd_log_limit_static_global_var(erl, 10, 0);
  261. nd_log_limit(&erl, NDLS_DAEMON, NDLP_NOTICE,
  262. "heartbeat randomness of %"PRIu64" is too big for a tick of %"PRIu64" - setting it to %"PRIu64"",
  263. hb->randomness, tick, tmp);
  264. hb->randomness = tmp;
  265. }
  266. usec_t dt;
  267. usec_t now = now_realtime_usec();
  268. usec_t next = now - (now % tick) + tick + hb->randomness;
  269. // align the next time we want to the clock resolution
  270. if(next % clock_realtime_resolution)
  271. next = next - (next % clock_realtime_resolution) + clock_realtime_resolution;
  272. // sleep_usec() has a loop to guarantee we will sleep for at least the requested time.
  273. // According the specs, when we sleep for a relative time, clock adjustments should not affect the duration
  274. // we sleep.
  275. sleep_usec_with_now(next - now, now);
  276. now = now_realtime_usec();
  277. dt = now - hb->realtime;
  278. if(hb->statistics_id < HEARTBEAT_ALIGNMENT_STATISTICS_SIZE) {
  279. heartbeat_alignment_values[hb->statistics_id].dt += now - next;
  280. heartbeat_alignment_values[hb->statistics_id].sequence++;
  281. }
  282. if(unlikely(now < next)) {
  283. errno = 0;
  284. nd_log_limit_static_global_var(erl, 10, 0);
  285. nd_log_limit(&erl, NDLS_DAEMON, NDLP_NOTICE,
  286. "heartbeat clock: woke up %"PRIu64" microseconds earlier than expected "
  287. "(can be due to the CLOCK_REALTIME set to the past).",
  288. next - now);
  289. }
  290. else if(unlikely(now - next > tick / 2)) {
  291. errno = 0;
  292. nd_log_limit_static_global_var(erl, 10, 0);
  293. nd_log_limit(&erl, NDLS_DAEMON, NDLP_NOTICE,
  294. "heartbeat clock: woke up %"PRIu64" microseconds later than expected "
  295. "(can be due to system load or the CLOCK_REALTIME set to the future).",
  296. now - next);
  297. }
  298. if(unlikely(!hb->realtime)) {
  299. // the first time return zero
  300. dt = 0;
  301. }
  302. hb->realtime = now;
  303. return dt;
  304. }
  305. void sleep_usec_with_now(usec_t usec, usec_t started_ut) {
  306. // we expect microseconds (1.000.000 per second)
  307. // but timespec is nanoseconds (1.000.000.000 per second)
  308. struct timespec rem = { 0, 0 }, req = {
  309. .tv_sec = (time_t) (usec / USEC_PER_SEC),
  310. .tv_nsec = (suseconds_t) ((usec % USEC_PER_SEC) * NSEC_PER_USEC)
  311. };
  312. // make sure errno is not EINTR
  313. errno = 0;
  314. if(!started_ut)
  315. started_ut = now_realtime_usec();
  316. usec_t end_ut = started_ut + usec;
  317. while (nanosleep(&req, &rem) != 0) {
  318. if (likely(errno == EINTR && (rem.tv_sec || rem.tv_nsec))) {
  319. req = rem;
  320. rem = (struct timespec){ 0, 0 };
  321. // break an infinite loop
  322. errno = 0;
  323. usec_t now_ut = now_realtime_usec();
  324. if(now_ut >= end_ut)
  325. break;
  326. usec_t remaining_ut = (usec_t)req.tv_sec * USEC_PER_SEC + (usec_t)req.tv_nsec * NSEC_PER_USEC > usec;
  327. usec_t check_ut = now_ut - started_ut;
  328. if(remaining_ut > check_ut) {
  329. req = (struct timespec){
  330. .tv_sec = (time_t) ( check_ut / USEC_PER_SEC),
  331. .tv_nsec = (suseconds_t) ((check_ut % USEC_PER_SEC) * NSEC_PER_USEC)
  332. };
  333. }
  334. }
  335. else {
  336. netdata_log_error("Cannot nanosleep() for %"PRIu64" microseconds.", usec);
  337. break;
  338. }
  339. }
  340. }
  341. static inline collected_number uptime_from_boottime(void) {
  342. #ifdef CLOCK_BOOTTIME_IS_AVAILABLE
  343. return (collected_number)(now_boottime_usec() / USEC_PER_MS);
  344. #else
  345. netdata_log_error("uptime cannot be read from CLOCK_BOOTTIME on this system.");
  346. return 0;
  347. #endif
  348. }
  349. static procfile *read_proc_uptime_ff = NULL;
  350. static inline collected_number read_proc_uptime(char *filename) {
  351. if(unlikely(!read_proc_uptime_ff)) {
  352. read_proc_uptime_ff = procfile_open(filename, " \t", PROCFILE_FLAG_DEFAULT);
  353. if(unlikely(!read_proc_uptime_ff)) return 0;
  354. }
  355. read_proc_uptime_ff = procfile_readall(read_proc_uptime_ff);
  356. if(unlikely(!read_proc_uptime_ff)) return 0;
  357. if(unlikely(procfile_lines(read_proc_uptime_ff) < 1)) {
  358. netdata_log_error("/proc/uptime has no lines.");
  359. return 0;
  360. }
  361. if(unlikely(procfile_linewords(read_proc_uptime_ff, 0) < 1)) {
  362. netdata_log_error("/proc/uptime has less than 1 word in it.");
  363. return 0;
  364. }
  365. return (collected_number)(strtondd(procfile_lineword(read_proc_uptime_ff, 0, 0), NULL) * 1000.0);
  366. }
  367. inline collected_number uptime_msec(char *filename){
  368. static int use_boottime = -1;
  369. if(unlikely(use_boottime == -1)) {
  370. collected_number uptime_boottime = uptime_from_boottime();
  371. collected_number uptime_proc = read_proc_uptime(filename);
  372. long long delta = (long long)uptime_boottime - (long long)uptime_proc;
  373. if(delta < 0) delta = -delta;
  374. if(delta <= 1000 && uptime_boottime != 0) {
  375. procfile_close(read_proc_uptime_ff);
  376. netdata_log_info("Using now_boottime_usec() for uptime (dt is %lld ms)", delta);
  377. use_boottime = 1;
  378. }
  379. else if(uptime_proc != 0) {
  380. netdata_log_info("Using /proc/uptime for uptime (dt is %lld ms)", delta);
  381. use_boottime = 0;
  382. }
  383. else {
  384. netdata_log_error("Cannot find any way to read uptime on this system.");
  385. return 1;
  386. }
  387. }
  388. collected_number uptime;
  389. if(use_boottime)
  390. uptime = uptime_from_boottime();
  391. else
  392. uptime = read_proc_uptime(filename);
  393. return uptime;
  394. }