| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 0d58353 commit d75c338
14 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,6 +2,101 @@ | |||
| 2 | 2 | ||
| 3 | 3 | Use `require('os')` to access this module. | |
| 4 | 4 | ||
| 5 | - ### os.getHostname() | ||
| 5 | + ### os.hostname() | ||
| 6 | 6 | ||
| 7 | 7 | Returns the hostname of the operating system. | |
| 8 | + | ||
| 9 | + ### os.type() | ||
| 10 | + | ||
| 11 | + Returns the operating system name. | ||
| 12 | + | ||
| 13 | + ### os.release() | ||
| 14 | + | ||
| 15 | + Returns the operating system release. | ||
| 16 | + | ||
| 17 | + ### os.uptime() | ||
| 18 | + | ||
| 19 | + Returns the system uptime in seconds. | ||
| 20 | + | ||
| 21 | + ### os.loadavg() | ||
| 22 | + | ||
| 23 | + Returns an array containing the 1, 5, and 15 minute load averages. | ||
| 24 | + | ||
| 25 | + ### os.totalmem() | ||
| 26 | + | ||
| 27 | + Returns the total amount of system memory in bytes. | ||
| 28 | + | ||
| 29 | + ### os.freemem() | ||
| 30 | + | ||
| 31 | + Returns the amount of free system memory in bytes. | ||
| 32 | + | ||
| 33 | + ### os.cpus() | ||
| 34 | + | ||
| 35 | + Returns an array of objects containing information about each CPU/core installed: model, speed (in MHz), and times (an object containing the number of CPU ticks spent in: user, nice, sys, idle, and irq). | ||
| 36 | + | ||
| 37 | + Example inspection of os.cpus: | ||
| 38 | + | ||
| 39 | + [ { model: 'Intel(R) Core(TM) i7 CPU 860 @ 2.80GHz', | ||
| 40 | + speed: 2926, | ||
| 41 | + times: | ||
| 42 | + { user: 252020, | ||
| 43 | + nice: 0, | ||
| 44 | + sys: 30340, | ||
| 45 | + idle: 1070356870, | ||
| 46 | + irq: 0 } }, | ||
| 47 | + { model: 'Intel(R) Core(TM) i7 CPU 860 @ 2.80GHz', | ||
| 48 | + speed: 2926, | ||
| 49 | + times: | ||
| 50 | + { user: 306960, | ||
| 51 | + nice: 0, | ||
| 52 | + sys: 26980, | ||
| 53 | + idle: 1071569080, | ||
| 54 | + irq: 0 } }, | ||
| 55 | + { model: 'Intel(R) Core(TM) i7 CPU 860 @ 2.80GHz', | ||
| 56 | + speed: 2926, | ||
| 57 | + times: | ||
| 58 | + { user: 248450, | ||
| 59 | + nice: 0, | ||
| 60 | + sys: 21750, | ||
| 61 | + idle: 1070919370, | ||
| 62 | + irq: 0 } }, | ||
| 63 | + { model: 'Intel(R) Core(TM) i7 CPU 860 @ 2.80GHz', | ||
| 64 | + speed: 2926, | ||
| 65 | + times: | ||
| 66 | + { user: 256880, | ||
| 67 | + nice: 0, | ||
| 68 | + sys: 19430, | ||
| 69 | + idle: 1070905480, | ||
| 70 | + irq: 20 } }, | ||
| 71 | + { model: 'Intel(R) Core(TM) i7 CPU 860 @ 2.80GHz', | ||
| 72 | + speed: 2926, | ||
| 73 | + times: | ||
| 74 | + { user: 511580, | ||
| 75 | + nice: 20, | ||
| 76 | + sys: 40900, | ||
| 77 | + idle: 1070842510, | ||
| 78 | + irq: 0 } }, | ||
| 79 | + { model: 'Intel(R) Core(TM) i7 CPU 860 @ 2.80GHz', | ||
| 80 | + speed: 2926, | ||
| 81 | + times: | ||
| 82 | + { user: 291660, | ||
| 83 | + nice: 0, | ||
| 84 | + sys: 34360, | ||
| 85 | + idle: 1070888000, | ||
| 86 | + irq: 10 } }, | ||
| 87 | + { model: 'Intel(R) Core(TM) i7 CPU 860 @ 2.80GHz', | ||
| 88 | + speed: 2926, | ||
| 89 | + times: | ||
| 90 | + { user: 308260, | ||
| 91 | + nice: 0, | ||
| 92 | + sys: 55410, | ||
| 93 | + idle: 1071129970, | ||
| 94 | + irq: 880 } }, | ||
| 95 | + { model: 'Intel(R) Core(TM) i7 CPU 860 @ 2.80GHz', | ||
| 96 | + speed: 2926, | ||
| 97 | + times: | ||
| 98 | + { user: 266450, | ||
| 99 | + nice: 1480, | ||
| 100 | + sys: 34920, | ||
| 101 | + idle: 1072572010, | ||
| 102 | + irq: 30 } } ] | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,3 +1,10 @@ | |||
| 1 | 1 | var binding = process.binding('os'); | |
| 2 | 2 | ||
| 3 | - exports.getHostname = binding.getHostname; | ||
| 3 | + exports.hostname = binding.getHostname; | ||
| 4 | + exports.loadavg = binding.getLoadAvg; | ||
| 5 | + exports.uptime = binding.getUptime; | ||
| 6 | + exports.freemem = binding.getFreeMem; | ||
| 7 | + exports.totalmem = binding.getTotalMem; | ||
| 8 | + exports.cpus = binding.getCPUs; | ||
| 9 | + exports.type = binding.getOSType; | ||
| 10 | + exports.release = binding.getOSRelease; | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1186,7 +1186,7 @@ static void CheckStatus(EV_P_ ev_timer *watcher, int revents) { | |||
| 1186 | 1186 | ||
| 1187 | 1187 | // check memory | |
| 1188 | 1188 | size_t rss, vsize; | |
| 1189 | - if (!ev_is_active(&gc_idle) && OS::GetMemory(&rss, &vsize) == 0) { | ||
| 1189 | + if (!ev_is_active(&gc_idle) && Platform::GetMemory(&rss, &vsize) == 0) { | ||
| 1190 | 1190 | if (rss > 1024*1024*128) { | |
| 1191 | 1191 | // larger than 128 megs, just start the idle watcher | |
| 1192 | 1192 | ev_idle_start(EV_A_ &gc_idle); | |
@@ -1211,7 +1211,7 @@ v8::Handle<v8::Value> MemoryUsage(const v8::Arguments& args) { | |||
| 1211 | 1211 | ||
| 1212 | 1212 | size_t rss, vsize; | |
| 1213 | 1213 | ||
| 1214 | - int r = OS::GetMemory(&rss, &vsize); | ||
| 1214 | + int r = Platform::GetMemory(&rss, &vsize); | ||
| 1215 | 1215 | ||
| 1216 | 1216 | if (r != 0) { | |
| 1217 | 1217 | return ThrowException(Exception::Error(String::New(strerror(errno)))); | |
@@ -1519,7 +1519,7 @@ static Handle<Value> ProcessTitleGetter(Local<String> property, | |||
| 1519 | 1519 | const AccessorInfo& info) { | |
| 1520 | 1520 | HandleScope scope; | |
| 1521 | 1521 | int len; | |
| 1522 | - const char *s = OS::GetProcessTitle(&len); | ||
| 1522 | + const char *s = Platform::GetProcessTitle(&len); | ||
| 1523 | 1523 | return scope.Close(s ? String::New(s, len) : String::Empty()); | |
| 1524 | 1524 | } | |
| 1525 | 1525 | ||
@@ -1529,7 +1529,7 @@ static void ProcessTitleSetter(Local<String> property, | |||
| 1529 | 1529 | const AccessorInfo& info) { | |
| 1530 | 1530 | HandleScope scope; | |
| 1531 | 1531 | String::Utf8Value title(value->ToString()); | |
| 1532 | - OS::SetProcessTitle(*title); | ||
| 1532 | + Platform::SetProcessTitle(*title); | ||
| 1533 | 1533 | } | |
| 1534 | 1534 | ||
| 1535 | 1535 | ||
@@ -1685,7 +1685,7 @@ static void Load(int argc, char *argv[]) { | |||
| 1685 | 1685 | ||
| 1686 | 1686 | size_t size = 2*PATH_MAX; | |
| 1687 | 1687 | char execPath[size]; | |
| 1688 | - if (OS::GetExecutablePath(execPath, &size) != 0) { | ||
| 1688 | + if (Platform::GetExecutablePath(execPath, &size) != 0) { | ||
| 1689 | 1689 | // as a last ditch effort, fallback on argv[0] ? | |
| 1690 | 1690 | process->Set(String::NewSymbol("execPath"), String::New(argv[0])); | |
| 1691 | 1691 | } else { | |
@@ -1874,7 +1874,7 @@ static int RegisterSignalHandler(int signal, void (*handler)(int)) { | |||
| 1874 | 1874 | ||
| 1875 | 1875 | int Start(int argc, char *argv[]) { | |
| 1876 | 1876 | // Hack aroung with the argv pointer. Used for process.title = "blah". | |
| 1877 | - argv = node::OS::SetupArgs(argc, argv); | ||
| 1877 | + argv = node::Platform::SetupArgs(argc, argv); | ||
| 1878 | 1878 | ||
| 1879 | 1879 | // Parse a few arguments which are specific to Node. | |
| 1880 | 1880 | node::ParseArgs(&argc, argv); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,8 +3,12 @@ | |||
| 3 | 3 | #include <node.h> | |
| 4 | 4 | #include <v8.h> | |
| 5 | 5 | ||
| 6 | + #include "platform.h" | ||
| 7 | + | ||
| 6 | 8 | #include <errno.h> | |
| 7 | - #include <unistd.h> // gethostname | ||
| 9 | + #include <unistd.h> // gethostname, sysconf | ||
| 10 | + #include <sys/param.h> // sysctl | ||
| 11 | + #include <sys/sysctl.h> // sysctl | ||
| 8 | 12 | ||
| 9 | 13 | namespace node { | |
| 10 | 14 | ||
@@ -13,19 +17,108 @@ using namespace v8; | |||
| 13 | 17 | static Handle<Value> GetHostname(const Arguments& args) { | |
| 14 | 18 | HandleScope scope; | |
| 15 | 19 | char s[255]; | |
| 16 | - int r = gethostname(s, 255); | ||
| 17 | 20 | ||
| 18 | - if (r < 0) { | ||
| 19 | - return ThrowException(ErrnoException(errno, "gethostname")); | ||
| 21 | + if (gethostname(s, 255) < 0) { | ||
| 22 | + return Undefined(); | ||
| 20 | 23 | } | |
| 21 | 24 | ||
| 22 | 25 | return scope.Close(String::New(s)); | |
| 23 | 26 | } | |
| 24 | 27 | ||
| 28 | + static Handle<Value> GetOSType(const Arguments& args) { | ||
| 29 | + HandleScope scope; | ||
| 30 | + char type[256]; | ||
| 31 | + static int which[] = {CTL_KERN, KERN_OSTYPE}; | ||
| 32 | + size_t size = sizeof(type); | ||
| 33 | + | ||
| 34 | + if (sysctl(which, 2, &type, &size, NULL, 0) < 0) { | ||
| 35 | + return Undefined(); | ||
| 36 | + } | ||
| 37 | + | ||
| 38 | + return scope.Close(String::New(type)); | ||
| 39 | + } | ||
| 40 | + | ||
| 41 | + static Handle<Value> GetOSRelease(const Arguments& args) { | ||
| 42 | + HandleScope scope; | ||
| 43 | + char release[256]; | ||
| 44 | + static int which[] = {CTL_KERN, KERN_OSRELEASE}; | ||
| 45 | + size_t size = sizeof(release); | ||
| 46 | + | ||
| 47 | + if (sysctl(which, 2, &release, &size, NULL, 0) < 0) { | ||
| 48 | + return Undefined(); | ||
| 49 | + } | ||
| 50 | + | ||
| 51 | + return scope.Close(String::New(release)); | ||
| 52 | + } | ||
| 53 | + | ||
| 54 | + static Handle<Value> GetCPUInfo(const Arguments& args) { | ||
| 55 | + HandleScope scope; | ||
| 56 | + Local<Array> cpus; | ||
| 57 | + int r = Platform::GetCPUInfo(&cpus); | ||
| 58 | + | ||
| 59 | + if (r < 0) { | ||
| 60 | + return Undefined(); | ||
| 61 | + } | ||
| 62 | + | ||
| 63 | + return scope.Close(cpus); | ||
| 64 | + } | ||
| 65 | + | ||
| 66 | + static Handle<Value> GetFreeMemory(const Arguments& args) { | ||
| 67 | + HandleScope scope; | ||
| 68 | + double amount = Platform::GetFreeMemory(); | ||
| 69 | + | ||
| 70 | + if (amount < 0) { | ||
| 71 | + return Undefined(); | ||
| 72 | + } | ||
| 73 | + | ||
| 74 | + return scope.Close(Number::New(amount)); | ||
| 75 | + } | ||
| 76 | + | ||
| 77 | + static Handle<Value> GetTotalMemory(const Arguments& args) { | ||
| 78 | + HandleScope scope; | ||
| 79 | + double amount = Platform::GetTotalMemory(); | ||
| 80 | + | ||
| 81 | + if (amount < 0) { | ||
| 82 | + return Undefined(); | ||
| 83 | + } | ||
| 84 | + | ||
| 85 | + return scope.Close(Number::New(amount)); | ||
| 86 | + } | ||
| 87 | + | ||
| 88 | + static Handle<Value> GetUptime(const Arguments& args) { | ||
| 89 | + HandleScope scope; | ||
| 90 | + double uptime = Platform::GetUptime(); | ||
| 91 | + | ||
| 92 | + if (uptime < 0) { | ||
| 93 | + return Undefined(); | ||
| 94 | + } | ||
| 95 | + | ||
| 96 | + return scope.Close(Number::New(uptime)); | ||
| 97 | + } | ||
| 98 | + | ||
| 99 | + static Handle<Value> GetLoadAvg(const Arguments& args) { | ||
| 100 | + HandleScope scope; | ||
| 101 | + Local<Array> loads = Array::New(3); | ||
| 102 | + int r = Platform::GetLoadAvg(&loads); | ||
| 103 | + | ||
| 104 | + if (r < 0) { | ||
| 105 | + return Undefined(); | ||
| 106 | + } | ||
| 107 | + | ||
| 108 | + return scope.Close(loads); | ||
| 109 | + } | ||
| 110 | + | ||
| 25 | 111 | void OS::Initialize(v8::Handle<v8::Object> target) { | |
| 26 | 112 | HandleScope scope; | |
| 27 | 113 | ||
| 28 | 114 | NODE_SET_METHOD(target, "getHostname", GetHostname); | |
| 115 | + NODE_SET_METHOD(target, "getLoadAvg", GetLoadAvg); | ||
| 116 | + NODE_SET_METHOD(target, "getUptime", GetUptime); | ||
| 117 | + NODE_SET_METHOD(target, "getTotalMem", GetTotalMemory); | ||
| 118 | + NODE_SET_METHOD(target, "getFreeMem", GetFreeMemory); | ||
| 119 | + NODE_SET_METHOD(target, "getCPUs", GetCPUInfo); | ||
| 120 | + NODE_SET_METHOD(target, "getOSType", GetOSType); | ||
| 121 | + NODE_SET_METHOD(target, "getOSRelease", GetOSRelease); | ||
| 29 | 122 | } | |
| 30 | 123 | ||
| 31 | 124 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,16 +1,23 @@ | |||
| 1 | 1 | #ifndef NODE_PLATFORM_H_ | |
| 2 | 2 | #define NODE_PLATFORM_H_ | |
| 3 | 3 | ||
| 4 | + #include <v8.h> | ||
| 5 | + | ||
| 4 | 6 | namespace node { | |
| 5 | 7 | ||
| 6 | - class OS { | ||
| 8 | + class Platform { | ||
| 7 | 9 | public: | |
| 8 | 10 | static char** SetupArgs(int argc, char *argv[]); | |
| 9 | 11 | static void SetProcessTitle(char *title); | |
| 10 | 12 | static const char* GetProcessTitle(int *len); | |
| 11 | 13 | ||
| 12 | 14 | static int GetMemory(size_t *rss, size_t *vsize); | |
| 13 | 15 | static int GetExecutablePath(char* buffer, size_t* size); | |
| 16 | + static int GetCPUInfo(v8::Local<v8::Array> *cpus); | ||
| 17 | + static double GetFreeMemory(); | ||
| 18 | + static double GetTotalMemory(); | ||
| 19 | + static double GetUptime(); | ||
| 20 | + static int GetLoadAvg(v8::Local<v8::Array> *loads); | ||
| 14 | 21 | }; | |
| 15 | 22 | ||
| 16 | 23 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments