Add process.uptime().

This commit is contained in:
Tom Hughes 2011-03-04 17:57:54 -06:00 committed by Ryan Dahl
parent 11a06fe1e4
commit cf78ce59b3
11 changed files with 50 additions and 9 deletions

View 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.

View 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);

View 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;
};

View 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");

View 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);

View 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);

View 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) {

View 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);

View 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;
}

View 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;
}

View File

@ -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);