Add process.uptime().
This commit is contained in:
parent
11a06fe1e4
commit
cf78ce59b3
@ -319,3 +319,7 @@ given, otherwise returns the current mask.
|
|||||||
console.log('Changed umask from: ' + oldmask.toString(8) +
|
console.log('Changed umask from: ' + oldmask.toString(8) +
|
||||||
' to ' + newmask.toString(8));
|
' to ' + newmask.toString(8));
|
||||||
|
|
||||||
|
|
||||||
|
### process.uptime()
|
||||||
|
|
||||||
|
Number of seconds Node has been running.
|
||||||
|
13
src/node.cc
13
src/node.cc
@ -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) {
|
v8::Handle<v8::Value> MemoryUsage(const v8::Arguments& args) {
|
||||||
HandleScope scope;
|
HandleScope scope;
|
||||||
@ -2023,6 +2035,7 @@ static void Load(int argc, char *argv[]) {
|
|||||||
NODE_SET_METHOD(process, "_kill", Kill);
|
NODE_SET_METHOD(process, "_kill", Kill);
|
||||||
#endif // __POSIX__
|
#endif // __POSIX__
|
||||||
|
|
||||||
|
NODE_SET_METHOD(process, "uptime", Uptime);
|
||||||
NODE_SET_METHOD(process, "memoryUsage", MemoryUsage);
|
NODE_SET_METHOD(process, "memoryUsage", MemoryUsage);
|
||||||
|
|
||||||
NODE_SET_METHOD(process, "binding", Binding);
|
NODE_SET_METHOD(process, "binding", Binding);
|
||||||
|
@ -16,8 +16,14 @@ class Platform {
|
|||||||
static int GetCPUInfo(v8::Local<v8::Array> *cpus);
|
static int GetCPUInfo(v8::Local<v8::Array> *cpus);
|
||||||
static double GetFreeMemory();
|
static double GetFreeMemory();
|
||||||
static double GetTotalMemory();
|
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);
|
static int GetLoadAvg(v8::Local<v8::Array> *loads);
|
||||||
|
private:
|
||||||
|
static double GetUptimeImpl();
|
||||||
|
static double prog_start_time;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -18,7 +18,7 @@ using namespace v8;
|
|||||||
|
|
||||||
static char buf[MAXPATHLEN + 1];
|
static char buf[MAXPATHLEN + 1];
|
||||||
static char *process_title = NULL;
|
static char *process_title = NULL;
|
||||||
|
double Platform::prog_start_time = Platform::GetUptime();
|
||||||
|
|
||||||
// Does the about the same as perror(), but for windows api functions
|
// Does the about the same as perror(), but for windows api functions
|
||||||
static void _winapi_perror(const char* prefix = NULL) {
|
static void _winapi_perror(const char* prefix = NULL) {
|
||||||
@ -338,7 +338,7 @@ double Platform::GetTotalMemory() {
|
|||||||
return pages * pagesize;
|
return pages * pagesize;
|
||||||
}
|
}
|
||||||
|
|
||||||
double Platform::GetUptime() {
|
double Platform::GetUptimeImpl() {
|
||||||
double amount;
|
double amount;
|
||||||
char line[512];
|
char line[512];
|
||||||
FILE *fpUptime = fopen("/proc/uptime", "r");
|
FILE *fpUptime = fopen("/proc/uptime", "r");
|
||||||
|
@ -19,6 +19,7 @@ namespace node {
|
|||||||
using namespace v8;
|
using namespace v8;
|
||||||
|
|
||||||
static char *process_title;
|
static char *process_title;
|
||||||
|
double Platform::prog_start_time = Platform::GetUptime();
|
||||||
|
|
||||||
char** Platform::SetupArgs(int argc, char *argv[]) {
|
char** Platform::SetupArgs(int argc, char *argv[]) {
|
||||||
process_title = argc ? strdup(argv[0]) : NULL;
|
process_title = argc ? strdup(argv[0]) : NULL;
|
||||||
@ -155,7 +156,7 @@ double Platform::GetTotalMemory() {
|
|||||||
return static_cast<double>(info);
|
return static_cast<double>(info);
|
||||||
}
|
}
|
||||||
|
|
||||||
double Platform::GetUptime() {
|
double Platform::GetUptimeImpl() {
|
||||||
time_t now;
|
time_t now;
|
||||||
struct timeval info;
|
struct timeval info;
|
||||||
size_t size = sizeof(info);
|
size_t size = sizeof(info);
|
||||||
|
@ -22,6 +22,7 @@ namespace node {
|
|||||||
using namespace v8;
|
using namespace v8;
|
||||||
|
|
||||||
static char *process_title;
|
static char *process_title;
|
||||||
|
double Platform::prog_start_time = Platform::GetUptime();
|
||||||
|
|
||||||
char** Platform::SetupArgs(int argc, char *argv[]) {
|
char** Platform::SetupArgs(int argc, char *argv[]) {
|
||||||
process_title = argc ? strdup(argv[0]) : NULL;
|
process_title = argc ? strdup(argv[0]) : NULL;
|
||||||
@ -175,7 +176,7 @@ double Platform::GetTotalMemory() {
|
|||||||
return static_cast<double>(info);
|
return static_cast<double>(info);
|
||||||
}
|
}
|
||||||
|
|
||||||
double Platform::GetUptime() {
|
double Platform::GetUptimeImpl() {
|
||||||
time_t now;
|
time_t now;
|
||||||
struct timeval info;
|
struct timeval info;
|
||||||
size_t size = sizeof(info);
|
size_t size = sizeof(info);
|
||||||
|
@ -21,6 +21,7 @@ using namespace v8;
|
|||||||
|
|
||||||
static char buf[MAXPATHLEN + 1];
|
static char buf[MAXPATHLEN + 1];
|
||||||
static char *process_title;
|
static char *process_title;
|
||||||
|
double Platform::prog_start_time = Platform::GetUptime();
|
||||||
|
|
||||||
|
|
||||||
char** Platform::SetupArgs(int argc, char *argv[]) {
|
char** Platform::SetupArgs(int argc, char *argv[]) {
|
||||||
@ -238,7 +239,7 @@ double Platform::GetTotalMemory() {
|
|||||||
return pages * pagesize;
|
return pages * pagesize;
|
||||||
}
|
}
|
||||||
|
|
||||||
double Platform::GetUptime() {
|
double Platform::GetUptimeImpl() {
|
||||||
struct sysinfo info;
|
struct sysinfo info;
|
||||||
|
|
||||||
if (sysinfo(&info) < 0) {
|
if (sysinfo(&info) < 0) {
|
||||||
|
@ -22,6 +22,7 @@ namespace node {
|
|||||||
using namespace v8;
|
using namespace v8;
|
||||||
|
|
||||||
static char *process_title;
|
static char *process_title;
|
||||||
|
double Platform::prog_start_time = Platform::GetUptime();
|
||||||
|
|
||||||
char** Platform::SetupArgs(int argc, char *argv[]) {
|
char** Platform::SetupArgs(int argc, char *argv[]) {
|
||||||
process_title = argc ? strdup(argv[0]) : NULL;
|
process_title = argc ? strdup(argv[0]) : NULL;
|
||||||
@ -166,7 +167,7 @@ double Platform::GetTotalMemory() {
|
|||||||
return static_cast<double>(info);
|
return static_cast<double>(info);
|
||||||
}
|
}
|
||||||
|
|
||||||
double Platform::GetUptime() {
|
double Platform::GetUptimeImpl() {
|
||||||
time_t now;
|
time_t now;
|
||||||
struct timeval info;
|
struct timeval info;
|
||||||
size_t size = sizeof(info);
|
size_t size = sizeof(info);
|
||||||
|
@ -25,6 +25,8 @@ namespace node {
|
|||||||
|
|
||||||
using namespace v8;
|
using namespace v8;
|
||||||
|
|
||||||
|
double Platform::prog_start_time = Platform::GetUptime();
|
||||||
|
|
||||||
char** Platform::SetupArgs(int argc, char *argv[]) {
|
char** Platform::SetupArgs(int argc, char *argv[]) {
|
||||||
return argv;
|
return argv;
|
||||||
}
|
}
|
||||||
@ -106,7 +108,7 @@ double Platform::GetTotalMemory() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
double Platform::GetUptime() {
|
double Platform::GetUptimeImpl() {
|
||||||
// http://munin-monitoring.org/attachment/ticket/419/uptime
|
// http://munin-monitoring.org/attachment/ticket/419/uptime
|
||||||
return 0.0;
|
return 0.0;
|
||||||
}
|
}
|
||||||
|
@ -18,6 +18,7 @@ namespace node {
|
|||||||
using namespace v8;
|
using namespace v8;
|
||||||
|
|
||||||
static char *process_title = NULL;
|
static char *process_title = NULL;
|
||||||
|
double Platform::prog_start_time = 0.0;
|
||||||
|
|
||||||
|
|
||||||
// Does the about the same as strerror(),
|
// 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;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
11
test/simple/test-process-uptime.js
Normal file
11
test/simple/test-process-uptime.js
Normal 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);
|
Loading…
x
Reference in New Issue
Block a user