summaryrefslogtreecommitdiffstats
path: root/libnetdata/clocks/clocks.h
diff options
context:
space:
mode:
Diffstat (limited to 'libnetdata/clocks/clocks.h')
-rw-r--r--libnetdata/clocks/clocks.h30
1 files changed, 29 insertions, 1 deletions
diff --git a/libnetdata/clocks/clocks.h b/libnetdata/clocks/clocks.h
index 4af451d60..cfe99f5e7 100644
--- a/libnetdata/clocks/clocks.h
+++ b/libnetdata/clocks/clocks.h
@@ -36,6 +36,12 @@ typedef struct heartbeat {
#define CLOCK_MONOTONIC CLOCK_REALTIME
#endif
+/* Prefer CLOCK_MONOTONIC_COARSE where available to reduce overhead. It has the same semantics as CLOCK_MONOTONIC */
+#ifndef CLOCK_MONOTONIC_COARSE
+/* fallback to CLOCK_MONOTONIC if not available */
+#define CLOCK_MONOTONIC_COARSE CLOCK_MONOTONIC
+#endif
+
#ifndef CLOCK_BOOTTIME
#ifdef CLOCK_UPTIME
@@ -43,7 +49,7 @@ typedef struct heartbeat {
#define CLOCK_BOOTTIME CLOCK_UPTIME
#else // CLOCK_UPTIME
/* CLOCK_BOOTTIME falls back to CLOCK_MONOTONIC */
-#define CLOCK_BOOTTIME CLOCK_MONOTONIC
+#define CLOCK_BOOTTIME CLOCK_MONOTONIC_COARSE
#endif // CLOCK_UPTIME
#else // CLOCK_BOOTTIME
@@ -54,13 +60,23 @@ typedef struct heartbeat {
#endif // CLOCK_BOOTTIME
+#ifndef NSEC_PER_MSEC
#define NSEC_PER_MSEC 1000000ULL
+#endif
+#ifndef NSEC_PER_SEC
#define NSEC_PER_SEC 1000000000ULL
+#endif
+#ifndef NSEC_PER_USEC
#define NSEC_PER_USEC 1000ULL
+#endif
+#ifndef USEC_PER_SEC
#define USEC_PER_SEC 1000000ULL
+#endif
+#ifndef MSEC_PER_SEC
#define MSEC_PER_SEC 1000ULL
+#endif
#define USEC_PER_MS 1000ULL
@@ -98,6 +114,9 @@ extern int clock_gettime(clockid_t clk_id, struct timespec *ts);
*
* All now_*_sec() functions return the time in seconds from the approriate clock, or 0 on error.
* All now_*_usec() functions return the time in microseconds from the approriate clock, or 0 on error.
+ *
+ * Most functions will attempt to use CLOCK_MONOTONIC_COARSE if available to reduce contention overhead and improve
+ * performance scaling. If high precision is required please use one of the available now_*_high_precision_* functions.
*/
extern int now_realtime_timeval(struct timeval *tv);
extern time_t now_realtime_sec(void);
@@ -106,6 +125,9 @@ extern usec_t now_realtime_usec(void);
extern int now_monotonic_timeval(struct timeval *tv);
extern time_t now_monotonic_sec(void);
extern usec_t now_monotonic_usec(void);
+extern int now_monotonic_high_precision_timeval(struct timeval *tv);
+extern time_t now_monotonic_high_precision_sec(void);
+extern usec_t now_monotonic_high_precision_usec(void);
extern int now_boottime_timeval(struct timeval *tv);
extern time_t now_boottime_sec(void);
@@ -136,6 +158,12 @@ extern int sleep_usec(usec_t usec);
*/
void test_clock_boottime(void);
+/*
+ * When running a binary with CLOCK_MONOTONIC_COARSE defined on a system with a linux kernel older than Linux 2.6.32 the
+ * clock_gettime(2) system call fails with EINVAL. In that case it must fall-back to CLOCK_MONOTONIC.
+ */
+void test_clock_monotonic_coarse(void);
+
extern collected_number uptime_msec(char *filename);
#endif /* NETDATA_CLOCKS_H */