Skip to content

Commit

Permalink
Add process.uptime().
Browse files Browse the repository at this point in the history
  • Loading branch information
thughes authored and ry committed Mar 7, 2011
1 parent 11a06fe commit cf78ce5
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 9 deletions.
4 changes: 4 additions & 0 deletions doc/api/process.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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.
13 changes: 13 additions & 0 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 7 additions & 1 deletion src/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};


Expand Down
4 changes: 2 additions & 2 deletions src/platform_cygwin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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");
Expand Down
3 changes: 2 additions & 1 deletion src/platform_darwin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion src/platform_freebsd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion src/platform_linux.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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[]) {
Expand Down Expand Up @@ -238,7 +239,7 @@ double Platform::GetTotalMemory() {
return pages * pagesize;
}

double Platform::GetUptime() {
double Platform::GetUptimeImpl() {
struct sysinfo info;

if (sysinfo(&info) < 0) {
Expand Down
3 changes: 2 additions & 1 deletion src/platform_openbsd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion src/platform_sunos.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -106,7 +108,7 @@ double Platform::GetTotalMemory() {
}


double Platform::GetUptime() {
double Platform::GetUptimeImpl() {
// http://munin-monitoring.org/attachment/ticket/419/uptime
return 0.0;
}
Expand Down
3 changes: 2 additions & 1 deletion src/platform_win32.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -220,7 +221,7 @@ int Platform::GetCPUInfo(Local<Array> *cpus) {
}


double Platform::GetUptime() {
double Platform::GetUptimeImpl() {
return -1;
}

Expand Down
11 changes: 11 additions & 0 deletions test/simple/test-process-uptime.js
Original file line number Diff line number Diff line change
@@ -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);

0 comments on commit cf78ce5

Please sign in to comment.