From cf78ce59b3c0a9e7cdcdbd84d71300b04150b6df Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Fri, 4 Mar 2011 17:57:54 -0600 Subject: [PATCH] Add process.uptime(). --- doc/api/process.markdown | 4 ++++ src/node.cc | 13 +++++++++++++ src/platform.h | 8 +++++++- src/platform_cygwin.cc | 4 ++-- src/platform_darwin.cc | 3 ++- src/platform_freebsd.cc | 3 ++- src/platform_linux.cc | 3 ++- src/platform_openbsd.cc | 3 ++- src/platform_sunos.cc | 4 +++- src/platform_win32.cc | 3 ++- test/simple/test-process-uptime.js | 11 +++++++++++ 11 files changed, 50 insertions(+), 9 deletions(-) create mode 100644 test/simple/test-process-uptime.js diff --git a/doc/api/process.markdown b/doc/api/process.markdown index ca65f3e5d4df99..d1d396a1e9ad89 100644 --- a/doc/api/process.markdown +++ b/doc/api/process.markdown @@ -319,3 +319,7 @@ given, otherwise returns the current mask. console.log('Changed umask from: ' + oldmask.toString(8) + ' to ' + newmask.toString(8)); + +### process.uptime() + +Number of seconds Node has been running. diff --git a/src/node.cc b/src/node.cc index f2f367095f335b..ddca07b8cbfcf0 100644 --- a/src/node.cc +++ b/src/node.cc @@ -1486,6 +1486,18 @@ static void CheckStatus(EV_P_ ev_timer *watcher, int revents) { } } +static Handle Uptime(const Arguments& args) { + HandleScope scope; + assert(args.Length() == 0); + + double uptime = Platform::GetUptime(true); + + if (uptime < 0) { + return Undefined(); + } + + return scope.Close(Number::New(uptime)); +} v8::Handle MemoryUsage(const v8::Arguments& args) { HandleScope scope; @@ -2023,6 +2035,7 @@ static void Load(int argc, char *argv[]) { NODE_SET_METHOD(process, "_kill", Kill); #endif // __POSIX__ + NODE_SET_METHOD(process, "uptime", Uptime); NODE_SET_METHOD(process, "memoryUsage", MemoryUsage); NODE_SET_METHOD(process, "binding", Binding); diff --git a/src/platform.h b/src/platform.h index bf7cd52fa3ae4b..a2fbe3019edf61 100644 --- a/src/platform.h +++ b/src/platform.h @@ -16,8 +16,14 @@ class Platform { static int GetCPUInfo(v8::Local *cpus); static double GetFreeMemory(); static double GetTotalMemory(); - static double GetUptime(); + static double GetUptime(bool adjusted = false) + { + return adjusted ? GetUptimeImpl() - prog_start_time : GetUptimeImpl(); + } static int GetLoadAvg(v8::Local *loads); + private: + static double GetUptimeImpl(); + static double prog_start_time; }; diff --git a/src/platform_cygwin.cc b/src/platform_cygwin.cc index 9338d4f4b85006..3d65425266b3b9 100644 --- a/src/platform_cygwin.cc +++ b/src/platform_cygwin.cc @@ -18,7 +18,7 @@ using namespace v8; static char buf[MAXPATHLEN + 1]; static char *process_title = NULL; - +double Platform::prog_start_time = Platform::GetUptime(); // Does the about the same as perror(), but for windows api functions static void _winapi_perror(const char* prefix = NULL) { @@ -338,7 +338,7 @@ double Platform::GetTotalMemory() { return pages * pagesize; } -double Platform::GetUptime() { +double Platform::GetUptimeImpl() { double amount; char line[512]; FILE *fpUptime = fopen("/proc/uptime", "r"); diff --git a/src/platform_darwin.cc b/src/platform_darwin.cc index 828d511056482d..f60d119b99e4f5 100644 --- a/src/platform_darwin.cc +++ b/src/platform_darwin.cc @@ -19,6 +19,7 @@ namespace node { using namespace v8; static char *process_title; +double Platform::prog_start_time = Platform::GetUptime(); char** Platform::SetupArgs(int argc, char *argv[]) { process_title = argc ? strdup(argv[0]) : NULL; @@ -155,7 +156,7 @@ double Platform::GetTotalMemory() { return static_cast(info); } -double Platform::GetUptime() { +double Platform::GetUptimeImpl() { time_t now; struct timeval info; size_t size = sizeof(info); diff --git a/src/platform_freebsd.cc b/src/platform_freebsd.cc index 6eb5ca2903a9be..0b48d45ea30a6b 100644 --- a/src/platform_freebsd.cc +++ b/src/platform_freebsd.cc @@ -22,6 +22,7 @@ namespace node { using namespace v8; static char *process_title; +double Platform::prog_start_time = Platform::GetUptime(); char** Platform::SetupArgs(int argc, char *argv[]) { process_title = argc ? strdup(argv[0]) : NULL; @@ -175,7 +176,7 @@ double Platform::GetTotalMemory() { return static_cast(info); } -double Platform::GetUptime() { +double Platform::GetUptimeImpl() { time_t now; struct timeval info; size_t size = sizeof(info); diff --git a/src/platform_linux.cc b/src/platform_linux.cc index 473997727b2319..2b90833918c9da 100644 --- a/src/platform_linux.cc +++ b/src/platform_linux.cc @@ -21,6 +21,7 @@ using namespace v8; static char buf[MAXPATHLEN + 1]; static char *process_title; +double Platform::prog_start_time = Platform::GetUptime(); char** Platform::SetupArgs(int argc, char *argv[]) { @@ -238,7 +239,7 @@ double Platform::GetTotalMemory() { return pages * pagesize; } -double Platform::GetUptime() { +double Platform::GetUptimeImpl() { struct sysinfo info; if (sysinfo(&info) < 0) { diff --git a/src/platform_openbsd.cc b/src/platform_openbsd.cc index b8a3209bcd75ec..e195fdbe95efb7 100644 --- a/src/platform_openbsd.cc +++ b/src/platform_openbsd.cc @@ -22,6 +22,7 @@ namespace node { using namespace v8; static char *process_title; +double Platform::prog_start_time = Platform::GetUptime(); char** Platform::SetupArgs(int argc, char *argv[]) { process_title = argc ? strdup(argv[0]) : NULL; @@ -166,7 +167,7 @@ double Platform::GetTotalMemory() { return static_cast(info); } -double Platform::GetUptime() { +double Platform::GetUptimeImpl() { time_t now; struct timeval info; size_t size = sizeof(info); diff --git a/src/platform_sunos.cc b/src/platform_sunos.cc index 6d70d01680902a..96133259bb5d22 100644 --- a/src/platform_sunos.cc +++ b/src/platform_sunos.cc @@ -25,6 +25,8 @@ namespace node { using namespace v8; +double Platform::prog_start_time = Platform::GetUptime(); + char** Platform::SetupArgs(int argc, char *argv[]) { return argv; } @@ -106,7 +108,7 @@ double Platform::GetTotalMemory() { } -double Platform::GetUptime() { +double Platform::GetUptimeImpl() { // http://munin-monitoring.org/attachment/ticket/419/uptime return 0.0; } diff --git a/src/platform_win32.cc b/src/platform_win32.cc index c019857f299a55..f963a94f2ff089 100644 --- a/src/platform_win32.cc +++ b/src/platform_win32.cc @@ -18,6 +18,7 @@ namespace node { using namespace v8; static char *process_title = NULL; +double Platform::prog_start_time = 0.0; // Does the about the same as strerror(), @@ -220,7 +221,7 @@ int Platform::GetCPUInfo(Local *cpus) { } -double Platform::GetUptime() { +double Platform::GetUptimeImpl() { return -1; } diff --git a/test/simple/test-process-uptime.js b/test/simple/test-process-uptime.js new file mode 100644 index 00000000000000..d8b4cffc4f00ed --- /dev/null +++ b/test/simple/test-process-uptime.js @@ -0,0 +1,11 @@ +var assert = require('assert'); + +assert.equal(process.uptime(), 0); + +setTimeout(function() { + var uptime = process.uptime(); + // some wiggle room to account for timer + // granularity, processor speed, and scheduling + assert.ok(uptime >= 2); + assert.ok(uptime <= 3); +}, 2000);