Add process.uptime().
authorTom Hughes <tom.hughes@palm.com>
Fri, 4 Mar 2011 23:57:54 +0000 (17:57 -0600)
committerRyan Dahl <ry@tinyclouds.org>
Mon, 7 Mar 2011 18:45:25 +0000 (10:45 -0800)
doc/api/process.markdown
src/node.cc
src/platform.h
src/platform_cygwin.cc
src/platform_darwin.cc
src/platform_freebsd.cc
src/platform_linux.cc
src/platform_openbsd.cc
src/platform_sunos.cc
src/platform_win32.cc
test/simple/test-process-uptime.js [new file with mode: 0644]

index ca65f3e..d1d396a 100644 (file)
@@ -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.
index f2f3670..ddca07b 100644 (file)
@@ -1486,6 +1486,18 @@ static void CheckStatus(EV_P_ ev_timer *watcher, int revents) {
   }
 }
 
+static Handle<Value> 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<v8::Value> 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);
index bf7cd52..a2fbe30 100644 (file)
@@ -16,8 +16,14 @@ class Platform {
   static int GetCPUInfo(v8::Local<v8::Array> *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<v8::Array> *loads);
+ private:
+  static double GetUptimeImpl();
+  static double prog_start_time;
 };
 
 
index 9338d4f..3d65425 100644 (file)
@@ -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");
index 828d511..f60d119 100644 (file)
@@ -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<double>(info);
 }
 
-double Platform::GetUptime() {
+double Platform::GetUptimeImpl() {
   time_t now;
   struct timeval info;
   size_t size = sizeof(info);
index 6eb5ca2..0b48d45 100644 (file)
@@ -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<double>(info);
 }
 
-double Platform::GetUptime() {
+double Platform::GetUptimeImpl() {
   time_t now;
   struct timeval info;
   size_t size = sizeof(info);
index 4739977..2b90833 100644 (file)
@@ -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) {
index b8a3209..e195fdb 100644 (file)
@@ -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<double>(info);
 }
 
-double Platform::GetUptime() {
+double Platform::GetUptimeImpl() {
   time_t now;
   struct timeval info;
   size_t size = sizeof(info);
index 6d70d01..9613325 100644 (file)
@@ -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;
 }
index c019857..f963a94 100644 (file)
@@ -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<Array> *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 (file)
index 0000000..d8b4cff
--- /dev/null
@@ -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);