FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Add more functionality to the os module · pandodev/node@d75c338 · GitHub

Repository navigation

Commit d75c338

Browse files
authored andcommitted
Add more functionality to the os module
1 parent 0d58353 commit d75c338

14 files changed

Lines changed: 932 additions & 44 deletions

‎doc/api/os.markdown‎

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,101 @@
22

33
Use `require('os')` to access this module.
44

5-
### os.getHostname()
5+
### os.hostname()
66

77
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 } } ]

‎lib/os.js‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
var binding = process.binding('os');
22

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;

‎src/node.cc‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,7 +1186,7 @@ static void CheckStatus(EV_P_ ev_timer *watcher, int revents) {
11861186

11871187
// check memory
11881188
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) {
11901190
if (rss > 1024*1024*128) {
11911191
// larger than 128 megs, just start the idle watcher
11921192
ev_idle_start(EV_A_ &gc_idle);
@@ -1211,7 +1211,7 @@ v8::Handle<v8::Value> MemoryUsage(const v8::Arguments& args) {
12111211

12121212
size_t rss, vsize;
12131213

1214-
int r = OS::GetMemory(&rss, &vsize);
1214+
int r = Platform::GetMemory(&rss, &vsize);
12151215

12161216
if (r != 0) {
12171217
return ThrowException(Exception::Error(String::New(strerror(errno))));
@@ -1519,7 +1519,7 @@ static Handle<Value> ProcessTitleGetter(Local<String> property,
15191519
const AccessorInfo& info) {
15201520
HandleScope scope;
15211521
int len;
1522-
const char *s = OS::GetProcessTitle(&len);
1522+
const char *s = Platform::GetProcessTitle(&len);
15231523
return scope.Close(s ? String::New(s, len) : String::Empty());
15241524
}
15251525

@@ -1529,7 +1529,7 @@ static void ProcessTitleSetter(Local<String> property,
15291529
const AccessorInfo& info) {
15301530
HandleScope scope;
15311531
String::Utf8Value title(value->ToString());
1532-
OS::SetProcessTitle(*title);
1532+
Platform::SetProcessTitle(*title);
15331533
}
15341534

15351535

@@ -1685,7 +1685,7 @@ static void Load(int argc, char *argv[]) {
16851685

16861686
size_t size = 2*PATH_MAX;
16871687
char execPath[size];
1688-
if (OS::GetExecutablePath(execPath, &size) != 0) {
1688+
if (Platform::GetExecutablePath(execPath, &size) != 0) {
16891689
// as a last ditch effort, fallback on argv[0] ?
16901690
process->Set(String::NewSymbol("execPath"), String::New(argv[0]));
16911691
} else {
@@ -1874,7 +1874,7 @@ static int RegisterSignalHandler(int signal, void (*handler)(int)) {
18741874

18751875
int Start(int argc, char *argv[]) {
18761876
// 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);
18781878

18791879
// Parse a few arguments which are specific to Node.
18801880
node::ParseArgs(&argc, argv);

‎src/node_os.cc‎

Lines changed: 97 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
33
#include <node.h>
44
#include <v8.h>
55

6+
#include "platform.h"
7+
68
#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
812

913
namespace node {
1014

@@ -13,19 +17,108 @@ using namespace v8;
1317
static Handle<Value> GetHostname(const Arguments& args) {
1418
HandleScope scope;
1519
char s[255];
16-
int r = gethostname(s, 255);
1720

18-
if (r < 0) {
19-
return ThrowException(ErrnoException(errno, "gethostname"));
21+
if (gethostname(s, 255) < 0) {
22+
return Undefined();
2023
}
2124

2225
return scope.Close(String::New(s));
2326
}
2427

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+
25111
void OS::Initialize(v8::Handle<v8::Object> target) {
26112
HandleScope scope;
27113

28114
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);
29122
}
30123

31124

‎src/platform.h‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
#ifndef NODE_PLATFORM_H_
22
#define NODE_PLATFORM_H_
33

4+
#include <v8.h>
5+
46
namespace node {
57

6-
class OS {
8+
class Platform {
79
public:
810
static char** SetupArgs(int argc, char *argv[]);
911
static void SetProcessTitle(char *title);
1012
static const char* GetProcessTitle(int *len);
1113

1214
static int GetMemory(size_t *rss, size_t *vsize);
1315
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);
1421
};
1522

1623

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL