]> arthur.barton.de Git - netdata.git/blobdiff - src/clocks.h
properly initialize the just allocated global structures
[netdata.git] / src / clocks.h
index 9634ac67e5007e16efe3ddc8dead592089373054..197b5431ffe81c9556c02ed9bbf81d1016adcf1d 100644 (file)
@@ -12,9 +12,12 @@ struct timespec {
 typedef int clockid_t;
 #endif
 
-#ifndef HAVE_CLOCK_GETTIME
-int clock_gettime(clockid_t clk_id, struct timespec *ts);
-#endif
+typedef unsigned long long nsec_t;
+typedef unsigned long long msec_t;
+typedef unsigned long long usec_t;
+typedef long long susec_t;
+
+typedef usec_t heartbeat_t;
 
 /* Linux value is as good as any other */
 #ifndef CLOCK_REALTIME
@@ -27,16 +30,30 @@ int clock_gettime(clockid_t clk_id, struct timespec *ts);
 #endif
 
 #ifndef CLOCK_BOOTTIME
-/* fallback to CLOCK_MONOTONIC if not available */
+
+#ifdef CLOCK_UPTIME
+/* CLOCK_BOOTTIME falls back to CLOCK_UPTIME on FreeBSD */
+#define CLOCK_BOOTTIME CLOCK_UPTIME
+#else // CLOCK_UPTIME
+/* CLOCK_BOOTTIME falls back to CLOCK_MONOTONIC */
 #define CLOCK_BOOTTIME  CLOCK_MONOTONIC
-#endif
+#endif // CLOCK_UPTIME
 
-typedef unsigned long long usec_t;
+#else // CLOCK_BOOTTIME
+
+#ifdef HAVE_CLOCK_GETTIME
+#define CLOCK_BOOTTIME_IS_AVAILABLE 1 // required for /proc/uptime
+#endif // HAVE_CLOCK_GETTIME
+
+#endif // CLOCK_BOOTTIME
 
-#define NSEC_PER_SEC    1000000000ULL
 #define NSEC_PER_MSEC   1000000ULL
+
+#define NSEC_PER_SEC    1000000000ULL
 #define NSEC_PER_USEC   1000ULL
+
 #define USEC_PER_SEC    1000000ULL
+#define MSEC_PER_SEC    1000ULL
 
 #ifndef HAVE_CLOCK_GETTIME
 /* Fallback function for POSIX.1-2001 clock_gettime() function.
@@ -48,41 +65,58 @@ typedef unsigned long long usec_t;
 extern int clock_gettime(clockid_t clk_id, struct timespec *ts);
 #endif
 
-/* Fills struct timeval with time since EPOCH from real-time clock (i.e. wall-clock).
- * - Hibernation/suspend time is included
- * - adjtime()/NTP adjustments affect this clock
- * Return 0 on succes, -1 else with errno set appropriately.
+/*
+ * Three clocks are available (cf. man 3 clock_gettime):
+ *
+ * REALTIME clock (i.e. wall-clock):
+ *  This clock is affected by discontinuous jumps in the system time
+ *  (e.g., if the system administrator manually changes the clock), and by the incremental adjustments performed by adjtime(3) and NTP.
+ *
+ * MONOTONIC clock
+ *  Clock that cannot be set and represents monotonic time since some unspecified starting point.
+ *  This clock is not affected by discontinuous jumps in the system time
+ *  (e.g., if the system administrator manually changes the clock), but is affected by the incremental adjustments performed by adjtime(3) and NTP.
+ *  If not available on the system, this clock falls back to REALTIME clock.
+ *
+ * BOOTTIME clock
+ *  Identical to  CLOCK_MONOTONIC, except it also includes any time that the system is suspended.
+ *  This allows applications to get a suspend-aware monotonic clock without having to deal with the complications of CLOCK_REALTIME,
+ *  which may have discontinuities if the time is changed using settimeofday(2).
+ *  If not available on the system, this clock falls back to MONOTONIC clock.
+ *
+ * All now_*_timeval() functions fill the `struct timeval` with the time from the appropriate clock.
+ * Those functions return 0 on success, -1 else with errno set appropriately.
+ *
+ * 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.
  */
 extern int now_realtime_timeval(struct timeval *tv);
-
-/* Returns time since EPOCH from real-time clock (i.e. wall-clock).
- * - Hibernation/suspend time is included
- * - adjtime()/NTP adjustments affect this clock
- */
 extern time_t now_realtime_sec(void);
 extern usec_t now_realtime_usec(void);
 
-/* Returns time from monotonic clock if available, real-time clock else.
- * If monotonic clock is available:
- * - hibernation/suspend time is not included
- * - adjtime()/NTP adjusments affect this clock
- * If monotonic clock is not available, this fallbacks to now_realtime().
- */
+extern int now_monotonic_timeval(struct timeval *tv);
 extern time_t now_monotonic_sec(void);
 extern usec_t now_monotonic_usec(void);
 
-/* Returns time from boottime clock if available,
- * monotonic clock else if available, real-time clock else.
- * If boottime clock is available:
- * - hibernation/suspend time is included
- * - adjtime()/NTP adjusments affect this clock
- * If boottime clock is not available, this fallbacks to now_monotonic().
- * If monotonic clock is not available, this fallbacks to now_realtime().
- */
+extern int now_boottime_timeval(struct timeval *tv);
 extern time_t now_boottime_sec(void);
 extern usec_t now_boottime_usec(void);
 
+
 extern usec_t timeval_usec(struct timeval *ts);
+extern msec_t timeval_msec(struct timeval *tv);
+
 extern usec_t dt_usec(struct timeval *now, struct timeval *old);
+extern susec_t dt_usec_signed(struct timeval *now, struct timeval *old);
+
+extern void heartbeat_init(heartbeat_t *hb);
+
+/* Sleeps until next multiple of tick using monotonic clock.
+ * Returns elapsed time in microseconds since previous heartbeat
+ */
+extern usec_t heartbeat_next(heartbeat_t *hb, usec_t tick);
+
+/* Returns elapsed time in microseconds since last heartbeat */
+extern usec_t heartbeat_dt_usec(heartbeat_t *hb);
 
 #endif /* NETDATA_CLOCKS_H */