1
// Copyright Joyent, Inc. and other Node contributors.
3
// Permission is hereby granted, free of charge, to any person obtaining a
4
// copy of this software and associated documentation files (the
5
// "Software"), to deal in the Software without restriction, including
6
// without limitation the rights to use, copy, modify, merge, publish,
7
// distribute, sublicense, and/or sell copies of the Software, and to permit
8
// persons to whom the Software is furnished to do so, subject to the
9
// following conditions:
11
// The above copyright notice and this permission notice shall be included
12
// in all copies or substantial portions of the Software.
14
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20
// USE OR OTHER DEALINGS IN THE SOFTWARE.
29
#include <sys/param.h>
30
#include <sys/sysctl.h>
32
#include <sys/dkstat.h>
33
#include <uvm/uvm_param.h>
45
static char *process_title;
46
double Platform::prog_start_time = Platform::GetUptime();
48
char** Platform::SetupArgs(int argc, char *argv[]) {
49
process_title = argc ? strdup(argv[0]) : NULL;
54
void Platform::SetProcessTitle(char *title) {
55
if (process_title) free(process_title);
56
process_title = strdup(title);
60
const char* Platform::GetProcessTitle(int *len) {
62
*len = strlen(process_title);
69
int Platform::GetMemory(size_t *rss) {
71
struct kinfo_proc2 *kinfo = NULL;
73
int nprocs, max_size = sizeof(struct kinfo_proc2);
74
size_t page_size = getpagesize();
78
kd = kvm_open(NULL, _PATH_MEM, NULL, O_RDONLY, "kvm_open");
79
if (kd == NULL) goto error;
81
kinfo = kvm_getproc2(kd, KERN_PROC_PID, pid, max_size, &nprocs);
82
if (kinfo == NULL) goto error;
84
*rss = kinfo->p_vm_rssize * page_size;
91
if (kd) kvm_close(kd);
96
int Platform::GetCPUInfo(Local<Array> *cpus) {
97
Local<Object> cpuinfo;
98
Local<Object> cputimes;
99
unsigned int ticks = (unsigned int)sysconf(_SC_CLK_TCK),
100
multiplier = ((uint64_t)1000L / ticks), cpuspeed;
101
uint64_t info[CPUSTATES];
104
static int which[] = {CTL_HW, HW_MODEL, NULL};
107
size = sizeof(model);
108
if (sysctl(which, 2, &model, &size, NULL, 0) < 0) {
112
size = sizeof(numcpus);
113
if (sysctl(which, 2, &numcpus, &size, NULL, 0) < 0) {
117
*cpus = Array::New(numcpus);
119
which[1] = HW_CPUSPEED;
120
size = sizeof(cpuspeed);
121
if (sysctl(which, 2, &cpuspeed, &size, NULL, 0) < 0) {
126
which[1] = KERN_CPTIME2;
127
for (int i = 0; i < numcpus; i++) {
130
if (sysctl(which, 3, &info, &size, NULL, 0) < 0) {
133
cpuinfo = Object::New();
134
cputimes = Object::New();
135
cputimes->Set(String::New("user"),
136
Number::New((uint64_t)(info[CP_USER]) * multiplier));
137
cputimes->Set(String::New("nice"),
138
Number::New((uint64_t)(info[CP_NICE]) * multiplier));
139
cputimes->Set(String::New("sys"),
140
Number::New((uint64_t)(info[CP_SYS]) * multiplier));
141
cputimes->Set(String::New("idle"),
142
Number::New((uint64_t)(info[CP_IDLE]) * multiplier));
143
cputimes->Set(String::New("irq"),
144
Number::New((uint64_t)(info[CP_INTR]) * multiplier));
146
cpuinfo->Set(String::New("model"), String::New(model));
147
cpuinfo->Set(String::New("speed"), Number::New(cpuspeed));
149
cpuinfo->Set(String::New("times"), cputimes);
150
(*cpus)->Set(i, cpuinfo);
155
double Platform::GetUptimeImpl() {
158
size_t size = sizeof(info);
159
static int which[] = {CTL_KERN, KERN_BOOTTIME};
161
if (sysctl(which, 2, &info, &size, NULL, 0) < 0) {
166
return static_cast<double>(now - info.tv_sec);
170
Handle<Value> Platform::GetInterfaceAddresses() {
172
return scope.Close(Object::New());