Skip to content

Commit 2c185a9

Browse files
thughesry
authored andcommitted
Use higher resolution clock for uptime on Linux (if available).
1 parent 502900c commit 2c185a9

3 files changed

Lines changed: 48 additions & 4 deletions

File tree

cmake/configure.cmake

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
# configure node for building
33
#
44
include(CheckFunctionExists)
5-
5+
include(CheckLibraryExists)
6+
include(CheckSymbolExists)
67

78
if(NOT "v${CMAKE_BUILD_TYPE}" MATCHES vDebug)
89
set(CMAKE_BUILD_TYPE "Release")
@@ -72,6 +73,20 @@ else()
7273
add_definitions(-DHAVE_FDATASYNC=0)
7374
endif()
7475

76+
# check first without rt and then with rt
77+
check_function_exists(clock_gettime HAVE_CLOCK_GETTIME)
78+
check_library_exists(rt clock_gettime "" HAVE_CLOCK_GETTIME_RT)
79+
80+
if(HAVE_CLOCK_GETTIME OR HAVE_CLOCK_GETTIME_RT)
81+
check_symbol_exists(CLOCK_MONOTONIC "time.h" HAVE_MONOTONIC_CLOCK)
82+
endif()
83+
84+
if(HAVE_MONOTONIC_CLOCK)
85+
add_definitions(-DHAVE_MONOTONIC_CLOCK=1)
86+
else()
87+
add_definitions(-DHAVE_MONOTONIC_CLOCK=0)
88+
endif()
89+
7590
if(DTRACE)
7691
if(NOT ${node_platform} MATCHES sunos)
7792
message(FATAL_ERROR "DTrace support only currently available on Solaris")

src/platform_linux.cc

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
#include <stdlib.h> // free
1616
#include <string.h> // strdup
1717

18+
#if HAVE_MONOTONIC_CLOCK
19+
#include <time.h>
20+
#endif
21+
1822
namespace node {
1923

2024
using namespace v8;
@@ -240,13 +244,21 @@ double Platform::GetTotalMemory() {
240244
}
241245

242246
double Platform::GetUptimeImpl() {
247+
#if HAVE_MONOTONIC_CLOCK
248+
struct timespec now;
249+
if (0 == clock_gettime(CLOCK_MONOTONIC, &now)) {
250+
double uptime = now.tv_sec;
251+
uptime += (double)now.tv_nsec / 1000000000.0;
252+
return uptime;
253+
}
254+
return -1;
255+
#else
243256
struct sysinfo info;
244-
245257
if (sysinfo(&info) < 0) {
246258
return -1;
247259
}
248-
249260
return static_cast<double>(info.uptime);
261+
#endif
250262
}
251263

252264
int Platform::GetLoadAvg(Local<Array> *loads) {

wscript

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,24 @@ def configure(conf):
328328
elif 'DEST_CPU' in conf.env and conf.env['DEST_CPU']:
329329
conf.env['DEST_CPU'] = canonical_cpu_type(conf.env['DEST_CPU'])
330330

331-
conf.check(lib='rt', uselib_store='RT')
331+
have_librt = conf.check(lib='rt', uselib_store='RT')
332+
333+
have_monotonic = False
334+
if have_librt:
335+
code = """
336+
#include <time.h>
337+
int main(void) {
338+
struct timespec now;
339+
clock_gettime(CLOCK_MONOTONIC, &now);
340+
return 0;
341+
}
342+
"""
343+
have_monotonic = conf.check_cc(lib="rt", msg="Checking for CLOCK_MONOTONIC", fragment=code)
344+
345+
if have_monotonic:
346+
conf.env.append_value('CPPFLAGS', '-DHAVE_MONOTONIC_CLOCK=1')
347+
else:
348+
conf.env.append_value('CPPFLAGS', '-DHAVE_MONOTONIC_CLOCK=0')
332349

333350
if sys.platform.startswith("sunos"):
334351
if not conf.check(lib='socket', uselib_store="SOCKET"):

0 commit comments

Comments
 (0)