clocks.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. #ifndef HAVE_CLOCK_GETTIME
  8. inline int clock_gettime(clockid_t clk_id, struct timespec *ts) {
  9. struct timeval tv;
  10. if(unlikely(gettimeofday(&tv, NULL) == -1)) {
  11. error("gettimeofday() failed.");
  12. return -1;
  13. }
  14. ts->tv_sec = tv.tv_sec;
  15. ts->tv_nsec = (tv.tv_usec % USEC_PER_SEC) * NSEC_PER_USEC;
  16. return 0;
  17. }
  18. #endif
  19. // When running a binary with CLOCK_MONOTONIC_COARSE defined on a system with a linux kernel older than Linux 2.6.32 the
  20. // clock_gettime(2) system call fails with EINVAL. In that case it must fall-back to CLOCK_MONOTONIC.
  21. static void test_clock_monotonic_coarse(void) {
  22. struct timespec ts;
  23. if(clock_gettime(CLOCK_MONOTONIC_COARSE, &ts) == -1 && errno == EINVAL)
  24. clock_monotonic_to_use = CLOCK_MONOTONIC;
  25. else
  26. clock_monotonic_to_use = CLOCK_MONOTONIC_COARSE;
  27. }
  28. // When running a binary with CLOCK_BOOTTIME defined on a system with a linux kernel older than Linux 2.6.39 the
  29. // clock_gettime(2) system call fails with EINVAL. In that case it must fall-back to CLOCK_MONOTONIC.
  30. static void test_clock_boottime(void) {
  31. struct timespec ts;
  32. if(clock_gettime(CLOCK_BOOTTIME, &ts) == -1 && errno == EINVAL)
  33. clock_boottime_to_use = clock_monotonic_to_use;
  34. else
  35. clock_boottime_to_use = CLOCK_BOOTTIME;
  36. }
  37. // perform any initializations required for clocks
  38. void clocks_init(void) {
  39. // monotonic coarse has to be tested before boottime
  40. test_clock_monotonic_coarse();
  41. // boottime has to be tested after monotonic coarse
  42. test_clock_boottime();
  43. }
  44. inline time_t now_sec(clockid_t clk_id) {
  45. struct timespec ts;
  46. if(unlikely(clock_gettime(clk_id, &ts) == -1)) {
  47. error("clock_gettime(%d, &timespec) failed.", clk_id);
  48. return 0;
  49. }
  50. return ts.tv_sec;
  51. }
  52. inline usec_t now_usec(clockid_t clk_id) {
  53. struct timespec ts;
  54. if(unlikely(clock_gettime(clk_id, &ts) == -1)) {
  55. error("clock_gettime(%d, &timespec) failed.", clk_id);
  56. return 0;
  57. }
  58. return (usec_t)ts.tv_sec * USEC_PER_SEC + (ts.tv_nsec % NSEC_PER_SEC) / NSEC_PER_USEC;
  59. }
  60. inline int now_timeval(clockid_t clk_id, struct timeval *tv) {
  61. struct timespec ts;
  62. if(unlikely(clock_gettime(clk_id, &ts) == -1)) {
  63. error("clock_gettime(%d, &timespec) failed.", clk_id);
  64. tv->tv_sec = 0;
  65. tv->tv_usec = 0;
  66. return -1;
  67. }
  68. tv->tv_sec = ts.tv_sec;
  69. tv->tv_usec = (suseconds_t)((ts.tv_nsec % NSEC_PER_SEC) / NSEC_PER_USEC);
  70. return 0;
  71. }
  72. inline time_t now_realtime_sec(void) {
  73. return now_sec(CLOCK_REALTIME);
  74. }
  75. inline usec_t now_realtime_usec(void) {
  76. return now_usec(CLOCK_REALTIME);
  77. }
  78. inline int now_realtime_timeval(struct timeval *tv) {
  79. return now_timeval(CLOCK_REALTIME, tv);
  80. }
  81. inline time_t now_monotonic_sec(void) {
  82. return now_sec(clock_monotonic_to_use);
  83. }
  84. inline usec_t now_monotonic_usec(void) {
  85. return now_usec(clock_monotonic_to_use);
  86. }
  87. inline int now_monotonic_timeval(struct timeval *tv) {
  88. return now_timeval(clock_monotonic_to_use, tv);
  89. }
  90. inline time_t now_monotonic_high_precision_sec(void) {
  91. return now_sec(CLOCK_MONOTONIC);
  92. }
  93. inline usec_t now_monotonic_high_precision_usec(void) {
  94. return now_usec(CLOCK_MONOTONIC);
  95. }
  96. inline int now_monotonic_high_precision_timeval(struct timeval *tv) {
  97. return now_timeval(CLOCK_MONOTONIC, tv);
  98. }
  99. inline time_t now_boottime_sec(void) {
  100. return now_sec(clock_boottime_to_use);
  101. }
  102. inline usec_t now_boottime_usec(void) {
  103. return now_usec(clock_boottime_to_use);
  104. }
  105. inline int now_boottime_timeval(struct timeval *tv) {
  106. return now_timeval(clock_boottime_to_use, tv);
  107. }
  108. inline usec_t timeval_usec(struct timeval *tv) {
  109. return (usec_t)tv->tv_sec * USEC_PER_SEC + (tv->tv_usec % USEC_PER_SEC);
  110. }
  111. inline msec_t timeval_msec(struct timeval *tv) {
  112. return (msec_t)tv->tv_sec * MSEC_PER_SEC + ((tv->tv_usec % USEC_PER_SEC) / MSEC_PER_SEC);
  113. }
  114. inline susec_t dt_usec_signed(struct timeval *now, struct timeval *old) {
  115. usec_t ts1 = timeval_usec(now);
  116. usec_t ts2 = timeval_usec(old);
  117. if(likely(ts1 >= ts2)) return (susec_t)(ts1 - ts2);
  118. return -((susec_t)(ts2 - ts1));
  119. }
  120. inline usec_t dt_usec(struct timeval *now, struct timeval *old) {
  121. usec_t ts1 = timeval_usec(now);
  122. usec_t ts2 = timeval_usec(old);
  123. return (ts1 > ts2) ? (ts1 - ts2) : (ts2 - ts1);
  124. }
  125. inline void heartbeat_init(heartbeat_t *hb) {
  126. hb->monotonic = hb->realtime = 0ULL;
  127. }
  128. // waits for the next heartbeat
  129. // it waits using the monotonic clock
  130. // it returns the dt using the realtime clock
  131. usec_t heartbeat_next(heartbeat_t *hb, usec_t tick) {
  132. heartbeat_t now;
  133. now.monotonic = now_monotonic_usec();
  134. now.realtime = now_realtime_usec();
  135. usec_t next_monotonic = now.monotonic - (now.monotonic % tick) + tick;
  136. while(now.monotonic < next_monotonic) {
  137. sleep_usec(next_monotonic - now.monotonic);
  138. now.monotonic = now_monotonic_usec();
  139. now.realtime = now_realtime_usec();
  140. }
  141. if(likely(hb->realtime != 0ULL)) {
  142. usec_t dt_monotonic = now.monotonic - hb->monotonic;
  143. usec_t dt_realtime = now.realtime - hb->realtime;
  144. hb->monotonic = now.monotonic;
  145. hb->realtime = now.realtime;
  146. if(unlikely(dt_monotonic >= tick + tick / 2)) {
  147. errno = 0;
  148. error("heartbeat missed %llu monotonic microseconds", dt_monotonic - tick);
  149. }
  150. return dt_realtime;
  151. }
  152. else {
  153. hb->monotonic = now.monotonic;
  154. hb->realtime = now.realtime;
  155. return 0ULL;
  156. }
  157. }
  158. // returned the elapsed time, since the last heartbeat
  159. // using the monotonic clock
  160. inline usec_t heartbeat_monotonic_dt_to_now_usec(heartbeat_t *hb) {
  161. if(!hb || !hb->monotonic) return 0ULL;
  162. return now_monotonic_usec() - hb->monotonic;
  163. }
  164. int sleep_usec(usec_t usec) {
  165. #ifndef NETDATA_WITH_USLEEP
  166. // we expect microseconds (1.000.000 per second)
  167. // but timespec is nanoseconds (1.000.000.000 per second)
  168. struct timespec rem, req = {
  169. .tv_sec = (time_t) (usec / 1000000),
  170. .tv_nsec = (suseconds_t) ((usec % 1000000) * 1000)
  171. };
  172. while (nanosleep(&req, &rem) == -1) {
  173. if (likely(errno == EINTR)) {
  174. debug(D_SYSTEM, "nanosleep() interrupted (while sleeping for %llu microseconds).", usec);
  175. req.tv_sec = rem.tv_sec;
  176. req.tv_nsec = rem.tv_nsec;
  177. } else {
  178. error("Cannot nanosleep() for %llu microseconds.", usec);
  179. break;
  180. }
  181. }
  182. return 0;
  183. #else
  184. int ret = usleep(usec);
  185. if(unlikely(ret == -1 && errno == EINVAL)) {
  186. // on certain systems, usec has to be up to 999999
  187. if(usec > 999999) {
  188. int counter = usec / 999999;
  189. while(counter--)
  190. usleep(999999);
  191. usleep(usec % 999999);
  192. }
  193. else {
  194. error("Cannot usleep() for %llu microseconds.", usec);
  195. return ret;
  196. }
  197. }
  198. if(ret != 0)
  199. error("usleep() failed for %llu microseconds.", usec);
  200. return ret;
  201. #endif
  202. }
  203. static inline collected_number uptime_from_boottime(void) {
  204. #ifdef CLOCK_BOOTTIME_IS_AVAILABLE
  205. return now_boottime_usec() / 1000;
  206. #else
  207. error("uptime cannot be read from CLOCK_BOOTTIME on this system.");
  208. return 0;
  209. #endif
  210. }
  211. static procfile *read_proc_uptime_ff = NULL;
  212. static inline collected_number read_proc_uptime(char *filename) {
  213. if(unlikely(!read_proc_uptime_ff)) {
  214. read_proc_uptime_ff = procfile_open(filename, " \t", PROCFILE_FLAG_DEFAULT);
  215. if(unlikely(!read_proc_uptime_ff)) return 0;
  216. }
  217. read_proc_uptime_ff = procfile_readall(read_proc_uptime_ff);
  218. if(unlikely(!read_proc_uptime_ff)) return 0;
  219. if(unlikely(procfile_lines(read_proc_uptime_ff) < 1)) {
  220. error("/proc/uptime has no lines.");
  221. return 0;
  222. }
  223. if(unlikely(procfile_linewords(read_proc_uptime_ff, 0) < 1)) {
  224. error("/proc/uptime has less than 1 word in it.");
  225. return 0;
  226. }
  227. return (collected_number)(strtold(procfile_lineword(read_proc_uptime_ff, 0, 0), NULL) * 1000.0);
  228. }
  229. inline collected_number uptime_msec(char *filename){
  230. static int use_boottime = -1;
  231. if(unlikely(use_boottime == -1)) {
  232. collected_number uptime_boottime = uptime_from_boottime();
  233. collected_number uptime_proc = read_proc_uptime(filename);
  234. long long delta = (long long)uptime_boottime - (long long)uptime_proc;
  235. if(delta < 0) delta = -delta;
  236. if(delta <= 1000 && uptime_boottime != 0) {
  237. procfile_close(read_proc_uptime_ff);
  238. info("Using now_boottime_usec() for uptime (dt is %lld ms)", delta);
  239. use_boottime = 1;
  240. }
  241. else if(uptime_proc != 0) {
  242. info("Using /proc/uptime for uptime (dt is %lld ms)", delta);
  243. use_boottime = 0;
  244. }
  245. else {
  246. error("Cannot find any way to read uptime on this system.");
  247. return 1;
  248. }
  249. }
  250. collected_number uptime;
  251. if(use_boottime)
  252. uptime = uptime_from_boottime();
  253. else
  254. uptime = read_proc_uptime(filename);
  255. return uptime;
  256. }