clocks.c 8.8 KB

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