use uv for memory and loadavg functions
authorFedor Indutny <fedor.indutny@gmail.com>
Fri, 7 Oct 2011 08:17:51 +0000 (15:17 +0700)
committerRyan Dahl <ry@tinyclouds.org>
Fri, 7 Oct 2011 08:31:35 +0000 (01:31 -0700)
src/node_os.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

index 35f558361154e31e44fbfb7df0ae228b80a8aabe..7e3e0896b7fe3c4f40722b237071ff72a9f34c92 100644 (file)
@@ -117,7 +117,7 @@ static Handle<Value> GetCPUInfo(const Arguments& args) {
 
 static Handle<Value> GetFreeMemory(const Arguments& args) {
   HandleScope scope;
-  double amount = Platform::GetFreeMemory();
+  double amount = uv_get_free_memory();
 
   if (amount < 0) {
     return Undefined();
@@ -128,7 +128,7 @@ static Handle<Value> GetFreeMemory(const Arguments& args) {
 
 static Handle<Value> GetTotalMemory(const Arguments& args) {
   HandleScope scope;
-  double amount = Platform::GetTotalMemory();
+  double amount = uv_get_total_memory();
 
   if (amount < 0) {
     return Undefined();
@@ -150,13 +150,18 @@ static Handle<Value> GetUptime(const Arguments& args) {
 
 static Handle<Value> GetLoadAvg(const Arguments& args) {
   HandleScope scope;
-  Local<Array> loads = Array::New(3);
-  int r = Platform::GetLoadAvg(&loads);
+  double loadavg[3];
+  uv_loadavg(loadavg);
 
-  if (r < 0) {
+  if (loadavg[0] < 0) {
     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);
 }
 
index 408d4781c56f99e256af1ba669127814ea446088..8284bc188dff0d58679bceec9c1ab80a864f5082 100644 (file)
@@ -34,13 +34,10 @@ class Platform {
 
   static int GetMemory(size_t *rss, size_t *vsize);
   static int GetCPUInfo(v8::Local<v8::Array> *cpus);
-  static double GetFreeMemory();
-  static double GetTotalMemory();
   static double GetUptime(bool adjusted = false)
   {
     return adjusted ? GetUptimeImpl() - prog_start_time : GetUptimeImpl();
   }
-  static int GetLoadAvg(v8::Local<v8::Array> *loads);
   static v8::Handle<v8::Value> GetInterfaceAddresses();
  private:
   static double GetUptimeImpl();
index 359fe920cd1c98ff91498b761f238eb32f2bfd35..d53e476a5066092970478e1be98330ccb2b616e4 100644 (file)
@@ -321,20 +321,6 @@ int Platform::GetCPUInfo(Local<Array> *cpus) {
   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 amount;
   char line[512];
@@ -352,12 +338,6 @@ double Platform::GetUptimeImpl() {
   return amount;
 }
 
-int Platform::GetLoadAvg(Local<Array> *loads) {
-  // Unsupported as of cygwin 1.7.7
-  return -1;
-}
-
-
 Handle<Value> Platform::GetInterfaceAddresses() {
   HandleScope scope;
   return scope.Close(Object::New());
index a64597c88edc2505789bfcc5f5e69f84d27fa503..49a34c41918da628e969fa5551afdcb06862e223 100644 (file)
@@ -140,31 +140,6 @@ int Platform::GetCPUInfo(Local<Array> *cpus) {
   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() {
   time_t now;
   struct timeval info;
@@ -179,24 +154,6 @@ double Platform::GetUptimeImpl() {
   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() {
   HandleScope scope;
index 7e60281418b55f25536e50fdb71fd00f1396d4aa..e5822729cae908b0b7cb5777ac09a63d0c8d669b 100644 (file)
@@ -154,31 +154,6 @@ int Platform::GetCPUInfo(Local<Array> *cpus) {
   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() {
   time_t now;
   struct timeval info;
@@ -193,24 +168,6 @@ double Platform::GetUptimeImpl() {
   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() {
   HandleScope scope;
index f8454eba72dc31b3c84860efd2be7fa98ee69671..da78bda139bbc499f3407472646ea3f6826cf45d 100644 (file)
@@ -25,8 +25,6 @@
 #include <v8.h>
 
 #include <sys/param.h> // for MAXPATHLEN
-#include <sys/sysctl.h>
-#include <sys/sysinfo.h>
 #include <unistd.h> // getpagesize, sysconf
 #include <stdio.h> // sscanf, snprintf
 
@@ -257,20 +255,6 @@ int Platform::GetCPUInfo(Local<Array> *cpus) {
   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() {
 #if HAVE_MONOTONIC_CLOCK
   struct timespec now;
@@ -289,19 +273,6 @@ double Platform::GetUptimeImpl() {
 #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) {
   return addr->ifa_flags & IFF_UP &&
index 50b4cac9beab5fd83ca19ebe42e9ee2430015173..13395585345426d04e80b46e66d862672cf8a322 100644 (file)
@@ -153,36 +153,6 @@ int Platform::GetCPUInfo(Local<Array> *cpus) {
   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() {
   time_t now;
   struct timeval info;
@@ -197,24 +167,6 @@ double Platform::GetUptimeImpl() {
   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() {
   HandleScope scope;
index 1897b0d7144492b4c7274fa178ebf99bf6c4f29a..03450a545fee63efcf6873e5908ed1358ef625b9 100644 (file)
@@ -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() {
   kstat_ctl_t   *kc;
   kstat_t       *ksp;
@@ -266,18 +232,6 @@ double Platform::GetUptimeImpl() {
   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() {
   HandleScope scope;
index dbc71355632692b5f163f1aed2a86591145b0824..e840bfee146b90392bd3d49b4b7dcf69743f07d9 100644 (file)
@@ -214,34 +214,6 @@ int Platform::GetMemory(size_t *rss, size_t *vsize) {
   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) {
   return -1;
 }
@@ -251,11 +223,6 @@ double Platform::GetUptimeImpl() {
   return (double)GetTickCount()/1000.0;
 }
 
-int Platform::GetLoadAvg(Local<Array> *loads) {
-  return -1;
-}
-
-
 Handle<Value> Platform::GetInterfaceAddresses() {
   HandleScope scope;
   return scope.Close(Object::New());