Skip to content
This repository has been archived by the owner on May 4, 2018. It is now read-only.

Commit

Permalink
linux: use CLOCK_BOOTTIME if available
Browse files Browse the repository at this point in the history
Use CLOCK_BOOTTIME to calculate the system uptime. Fall back to CLOCK_MONOTONIC
if CLOCK_BOOTTIME is not available (all pre-2.6.39 kernels).

The problem with CLOCK_MONOTONIC is that it doesn't increase when the system is
suspended, making the uptime differ from what the uptime(1) tool reports.
  • Loading branch information
bnoordhuis committed Mar 19, 2012
1 parent 379ca42 commit 49d4e18
Showing 1 changed file with 26 additions and 12 deletions.
38 changes: 26 additions & 12 deletions src/unix/linux/core.c
Expand Up @@ -48,6 +48,14 @@
#undef NANOSEC
#define NANOSEC 1000000000

/* This is rather annoying: CLOCK_BOOTTIME lives in <linux/time.h> but we can't
* include that file because it conflicts with <time.h>. We'll just have to
* define it ourselves.
*/
#ifndef CLOCK_BOOTTIME
# define CLOCK_BOOTTIME 7
#endif

static char buf[MAXPATHLEN + 1];

static struct {
Expand Down Expand Up @@ -267,22 +275,28 @@ uv_err_t uv_resident_set_memory(size_t* rss) {


uv_err_t uv_uptime(double* uptime) {
#ifdef CLOCK_MONOTONIC
static volatile int no_clock_boottime;
struct timespec now;
if (0 == clock_gettime(CLOCK_MONOTONIC, &now)) {
*uptime = now.tv_sec;
*uptime += (double)now.tv_nsec / 1000000000.0;
return uv_ok_;
int r;

/* Try CLOCK_BOOTTIME first, fall back to CLOCK_MONOTONIC if not available
* (pre-2.6.39 kernels). CLOCK_MONOTONIC doesn't increase when the system
* is suspended.
*/
if (no_clock_boottime) {
retry: r = clock_gettime(CLOCK_MONOTONIC, &now);
}
return uv__new_sys_error(errno);
#else
struct sysinfo info;
if (sysinfo(&info) < 0) {
return uv__new_sys_error(errno);
else if ((r = clock_gettime(CLOCK_BOOTTIME, &now)) && errno == EINVAL) {
no_clock_boottime = 1;
goto retry;
}
*uptime = (double)info.uptime;

if (r)
return uv__new_sys_error(errno);

*uptime = now.tv_sec;
*uptime += (double)now.tv_nsec / 1000000000.0;
return uv_ok_;
#endif
}


Expand Down

0 comments on commit 49d4e18

Please sign in to comment.