Browse Source

use uv for memory and loadavg functions

v0.7.4-release
Fedor Indutny 14 years ago
committed by Ryan Dahl
parent
commit
698455384f
  1. 15
      src/node_os.cc
  2. 3
      src/platform.h
  3. 20
      src/platform_cygwin.cc
  4. 43
      src/platform_darwin.cc
  5. 43
      src/platform_freebsd.cc
  6. 29
      src/platform_linux.cc
  7. 48
      src/platform_openbsd.cc
  8. 46
      src/platform_sunos.cc
  9. 33
      src/platform_win32.cc

15
src/node_os.cc

@ -117,7 +117,7 @@ static Handle<Value> GetCPUInfo(const Arguments& args) {
static Handle<Value> GetFreeMemory(const Arguments& args) { static Handle<Value> GetFreeMemory(const Arguments& args) {
HandleScope scope; HandleScope scope;
double amount = Platform::GetFreeMemory(); double amount = uv_get_free_memory();
if (amount < 0) { if (amount < 0) {
return Undefined(); return Undefined();
@ -128,7 +128,7 @@ static Handle<Value> GetFreeMemory(const Arguments& args) {
static Handle<Value> GetTotalMemory(const Arguments& args) { static Handle<Value> GetTotalMemory(const Arguments& args) {
HandleScope scope; HandleScope scope;
double amount = Platform::GetTotalMemory(); double amount = uv_get_total_memory();
if (amount < 0) { if (amount < 0) {
return Undefined(); return Undefined();
@ -150,13 +150,18 @@ static Handle<Value> GetUptime(const Arguments& args) {
static Handle<Value> GetLoadAvg(const Arguments& args) { static Handle<Value> GetLoadAvg(const Arguments& args) {
HandleScope scope; HandleScope scope;
Local<Array> loads = Array::New(3); double loadavg[3];
int r = Platform::GetLoadAvg(&loads); uv_loadavg(loadavg);
if (r < 0) { if (loadavg[0] < 0) {
return Undefined(); return Undefined();
} }
Local<Array> loads = Array::New(3);
loads->Set(0, Number::New(loadavg[0]));
loads->Set(1, Number::New(loadavg[1]));
loads->Set(2, Number::New(loadavg[2]));
return scope.Close(loads); return scope.Close(loads);
} }

3
src/platform.h

@ -34,13 +34,10 @@ class Platform {
static int GetMemory(size_t *rss, size_t *vsize); static int GetMemory(size_t *rss, size_t *vsize);
static int GetCPUInfo(v8::Local<v8::Array> *cpus); static int GetCPUInfo(v8::Local<v8::Array> *cpus);
static double GetFreeMemory();
static double GetTotalMemory();
static double GetUptime(bool adjusted = false) static double GetUptime(bool adjusted = false)
{ {
return adjusted ? GetUptimeImpl() - prog_start_time : GetUptimeImpl(); return adjusted ? GetUptimeImpl() - prog_start_time : GetUptimeImpl();
} }
static int GetLoadAvg(v8::Local<v8::Array> *loads);
static v8::Handle<v8::Value> GetInterfaceAddresses(); static v8::Handle<v8::Value> GetInterfaceAddresses();
private: private:
static double GetUptimeImpl(); static double GetUptimeImpl();

20
src/platform_cygwin.cc

@ -321,20 +321,6 @@ int Platform::GetCPUInfo(Local<Array> *cpus) {
return 0; return 0;
} }
double Platform::GetFreeMemory() {
double pagesize = static_cast<double>(sysconf(_SC_PAGESIZE));
double pages = static_cast<double>(sysconf(_SC_AVPHYS_PAGES));
return static_cast<double>(pages * pagesize);
}
double Platform::GetTotalMemory() {
double pagesize = static_cast<double>(sysconf(_SC_PAGESIZE));
double pages = static_cast<double>(sysconf(_SC_PHYS_PAGES));
return pages * pagesize;
}
double Platform::GetUptimeImpl() { double Platform::GetUptimeImpl() {
double amount; double amount;
char line[512]; char line[512];
@ -352,12 +338,6 @@ double Platform::GetUptimeImpl() {
return amount; return amount;
} }
int Platform::GetLoadAvg(Local<Array> *loads) {
// Unsupported as of cygwin 1.7.7
return -1;
}
Handle<Value> Platform::GetInterfaceAddresses() { Handle<Value> Platform::GetInterfaceAddresses() {
HandleScope scope; HandleScope scope;
return scope.Close(Object::New()); return scope.Close(Object::New());

43
src/platform_darwin.cc

@ -140,31 +140,6 @@ int Platform::GetCPUInfo(Local<Array> *cpus) {
return 0; return 0;
} }
double Platform::GetFreeMemory() {
double pagesize = static_cast<double>(sysconf(_SC_PAGESIZE));
vm_statistics_data_t info;
mach_msg_type_number_t count = sizeof(info) / sizeof(integer_t);
if (host_statistics(mach_host_self(), HOST_VM_INFO,
(host_info_t)&info, &count) != KERN_SUCCESS) {
return -1;
}
return (static_cast<double>(info.free_count)) * pagesize;
}
double Platform::GetTotalMemory() {
uint64_t info;
static int which[] = {CTL_HW, HW_MEMSIZE};
size_t size = sizeof(info);
if (sysctl(which, 2, &info, &size, NULL, 0) < 0) {
return -1;
}
return static_cast<double>(info);
}
double Platform::GetUptimeImpl() { double Platform::GetUptimeImpl() {
time_t now; time_t now;
struct timeval info; struct timeval info;
@ -179,24 +154,6 @@ double Platform::GetUptimeImpl() {
return static_cast<double>(now - info.tv_sec); return static_cast<double>(now - info.tv_sec);
} }
int Platform::GetLoadAvg(Local<Array> *loads) {
struct loadavg info;
size_t size = sizeof(info);
static int which[] = {CTL_VM, VM_LOADAVG};
if (sysctl(which, 2, &info, &size, NULL, 0) < 0) {
return -1;
}
(*loads)->Set(0, Number::New(static_cast<double>(info.ldavg[0])
/ static_cast<double>(info.fscale)));
(*loads)->Set(1, Number::New(static_cast<double>(info.ldavg[1])
/ static_cast<double>(info.fscale)));
(*loads)->Set(2, Number::New(static_cast<double>(info.ldavg[2])
/ static_cast<double>(info.fscale)));
return 0;
}
v8::Handle<v8::Value> Platform::GetInterfaceAddresses() { v8::Handle<v8::Value> Platform::GetInterfaceAddresses() {
HandleScope scope; HandleScope scope;

43
src/platform_freebsd.cc

@ -154,31 +154,6 @@ int Platform::GetCPUInfo(Local<Array> *cpus) {
return 0; return 0;
} }
double Platform::GetFreeMemory() {
double pagesize = static_cast<double>(sysconf(_SC_PAGESIZE));
unsigned int info = 0;
size_t size = sizeof(info);
if (sysctlbyname("vm.stats.vm.v_free_count", &info, &size, NULL, 0) < 0) {
return -1;
}
return (static_cast<double>(info)) * pagesize;
}
double Platform::GetTotalMemory() {
unsigned long info;
static int which[] = {CTL_HW, HW_PHYSMEM};
size_t size = sizeof(info);
if (sysctl(which, 2, &info, &size, NULL, 0) < 0) {
return -1;
}
return static_cast<double>(info);
}
double Platform::GetUptimeImpl() { double Platform::GetUptimeImpl() {
time_t now; time_t now;
struct timeval info; struct timeval info;
@ -193,24 +168,6 @@ double Platform::GetUptimeImpl() {
return static_cast<double>(now - info.tv_sec); return static_cast<double>(now - info.tv_sec);
} }
int Platform::GetLoadAvg(Local<Array> *loads) {
struct loadavg info;
size_t size = sizeof(info);
static int which[] = {CTL_VM, VM_LOADAVG};
if (sysctl(which, 2, &info, &size, NULL, 0) < 0) {
return -1;
}
(*loads)->Set(0, Number::New(static_cast<double>(info.ldavg[0])
/ static_cast<double>(info.fscale)));
(*loads)->Set(1, Number::New(static_cast<double>(info.ldavg[1])
/ static_cast<double>(info.fscale)));
(*loads)->Set(2, Number::New(static_cast<double>(info.ldavg[2])
/ static_cast<double>(info.fscale)));
return 0;
}
Handle<Value> Platform::GetInterfaceAddresses() { Handle<Value> Platform::GetInterfaceAddresses() {
HandleScope scope; HandleScope scope;

29
src/platform_linux.cc

@ -25,8 +25,6 @@
#include <v8.h> #include <v8.h>
#include <sys/param.h> // for MAXPATHLEN #include <sys/param.h> // for MAXPATHLEN
#include <sys/sysctl.h>
#include <sys/sysinfo.h>
#include <unistd.h> // getpagesize, sysconf #include <unistd.h> // getpagesize, sysconf
#include <stdio.h> // sscanf, snprintf #include <stdio.h> // sscanf, snprintf
@ -257,20 +255,6 @@ int Platform::GetCPUInfo(Local<Array> *cpus) {
return 0; return 0;
} }
double Platform::GetFreeMemory() {
double pagesize = static_cast<double>(sysconf(_SC_PAGESIZE));
double pages = static_cast<double>(sysconf(_SC_AVPHYS_PAGES));
return static_cast<double>(pages * pagesize);
}
double Platform::GetTotalMemory() {
double pagesize = static_cast<double>(sysconf(_SC_PAGESIZE));
double pages = static_cast<double>(sysconf(_SC_PHYS_PAGES));
return pages * pagesize;
}
double Platform::GetUptimeImpl() { double Platform::GetUptimeImpl() {
#if HAVE_MONOTONIC_CLOCK #if HAVE_MONOTONIC_CLOCK
struct timespec now; struct timespec now;
@ -289,19 +273,6 @@ double Platform::GetUptimeImpl() {
#endif #endif
} }
int Platform::GetLoadAvg(Local<Array> *loads) {
struct sysinfo info;
if (sysinfo(&info) < 0) {
return -1;
}
(*loads)->Set(0, Number::New(static_cast<double>(info.loads[0]) / 65536.0));
(*loads)->Set(1, Number::New(static_cast<double>(info.loads[1]) / 65536.0));
(*loads)->Set(2, Number::New(static_cast<double>(info.loads[2]) / 65536.0));
return 0;
}
bool IsInternal(struct ifaddrs* addr) { bool IsInternal(struct ifaddrs* addr) {
return addr->ifa_flags & IFF_UP && return addr->ifa_flags & IFF_UP &&

48
src/platform_openbsd.cc

@ -153,36 +153,6 @@ int Platform::GetCPUInfo(Local<Array> *cpus) {
return 0; return 0;
} }
double Platform::GetFreeMemory() {
double pagesize = static_cast<double>(sysconf(_SC_PAGESIZE));
struct uvmexp info;
size_t size = sizeof(info);
static int which[] = {CTL_VM, VM_UVMEXP};
if (sysctl(which, 2, &info, &size, NULL, 0) < 0) {
return -1;
}
return static_cast<double>(info.free) * pagesize;
}
double Platform::GetTotalMemory() {
#if defined(HW_PHYSMEM64)
uint64_t info;
static int which[] = {CTL_HW, HW_PHYSMEM64};
#else
unsigned int info;
static int which[] = {CTL_HW, HW_PHYSMEM};
#endif
size_t size = sizeof(info);
if (sysctl(which, 2, &info, &size, NULL, 0) < 0) {
return -1;
}
return static_cast<double>(info);
}
double Platform::GetUptimeImpl() { double Platform::GetUptimeImpl() {
time_t now; time_t now;
struct timeval info; struct timeval info;
@ -197,24 +167,6 @@ double Platform::GetUptimeImpl() {
return static_cast<double>(now - info.tv_sec); return static_cast<double>(now - info.tv_sec);
} }
int Platform::GetLoadAvg(Local<Array> *loads) {
struct loadavg info;
size_t size = sizeof(info);
static int which[] = {CTL_VM, VM_LOADAVG};
if (sysctl(which, 2, &info, &size, NULL, 0) < 0) {
return -1;
}
(*loads)->Set(0, Number::New(static_cast<double>(info.ldavg[0])
/ static_cast<double>(info.fscale)));
(*loads)->Set(1, Number::New(static_cast<double>(info.ldavg[1])
/ static_cast<double>(info.fscale)));
(*loads)->Set(2, Number::New(static_cast<double>(info.ldavg[2])
/ static_cast<double>(info.fscale)));
return 0;
}
Handle<Value> Platform::GetInterfaceAddresses() { Handle<Value> Platform::GetInterfaceAddresses() {
HandleScope scope; HandleScope scope;

46
src/platform_sunos.cc

@ -207,40 +207,6 @@ int Platform::GetCPUInfo(Local<Array> *cpus) {
} }
double Platform::GetFreeMemory() {
kstat_ctl_t *kc;
kstat_t *ksp;
kstat_named_t *knp;
double pagesize = static_cast<double>(sysconf(_SC_PAGESIZE));
ulong_t freemem;
if((kc = kstat_open()) == NULL)
throw "could not open kstat";
ksp = kstat_lookup(kc, (char *)"unix", 0, (char *)"system_pages");
if(kstat_read(kc, ksp, NULL) == -1){
throw "could not read kstat";
}
else {
knp = (kstat_named_t *) kstat_data_lookup(ksp, (char *)"freemem");
freemem = knp->value.ul;
}
kstat_close(kc);
return static_cast<double>(freemem)*pagesize;
}
double Platform::GetTotalMemory() {
double pagesize = static_cast<double>(sysconf(_SC_PAGESIZE));
double pages = static_cast<double>(sysconf(_SC_PHYS_PAGES));
return pagesize*pages;
}
double Platform::GetUptimeImpl() { double Platform::GetUptimeImpl() {
kstat_ctl_t *kc; kstat_ctl_t *kc;
kstat_t *ksp; kstat_t *ksp;
@ -266,18 +232,6 @@ double Platform::GetUptimeImpl() {
return static_cast<double>( clk_intr / hz ); return static_cast<double>( clk_intr / hz );
} }
int Platform::GetLoadAvg(Local<Array> *loads) {
HandleScope scope;
double loadavg[3];
(void) getloadavg(loadavg, 3);
(*loads)->Set(0, Number::New(loadavg[LOADAVG_1MIN]));
(*loads)->Set(1, Number::New(loadavg[LOADAVG_5MIN]));
(*loads)->Set(2, Number::New(loadavg[LOADAVG_15MIN]));
return 0;
}
Handle<Value> Platform::GetInterfaceAddresses() { Handle<Value> Platform::GetInterfaceAddresses() {
HandleScope scope; HandleScope scope;

33
src/platform_win32.cc

@ -214,34 +214,6 @@ int Platform::GetMemory(size_t *rss, size_t *vsize) {
return 0; return 0;
} }
double Platform::GetFreeMemory() {
MEMORYSTATUSEX memory_status;
memory_status.dwLength = sizeof(memory_status);
if(!GlobalMemoryStatusEx(&memory_status))
{
winapi_perror("GlobalMemoryStatusEx");
}
return (double)memory_status.ullAvailPhys;
}
double Platform::GetTotalMemory() {
MEMORYSTATUSEX memory_status;
memory_status.dwLength = sizeof(memory_status);
if(!GlobalMemoryStatusEx(&memory_status))
{
winapi_perror("GlobalMemoryStatusEx");
}
return (double)memory_status.ullTotalPhys;
}
int Platform::GetCPUInfo(Local<Array> *cpus) { int Platform::GetCPUInfo(Local<Array> *cpus) {
return -1; return -1;
} }
@ -251,11 +223,6 @@ double Platform::GetUptimeImpl() {
return (double)GetTickCount()/1000.0; return (double)GetTickCount()/1000.0;
} }
int Platform::GetLoadAvg(Local<Array> *loads) {
return -1;
}
Handle<Value> Platform::GetInterfaceAddresses() { Handle<Value> Platform::GetInterfaceAddresses() {
HandleScope scope; HandleScope scope;
return scope.Close(Object::New()); return scope.Close(Object::New());

Loading…
Cancel
Save