forked from andrasq/node-qrusage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qrusage.cc
190 lines (160 loc) · 7.25 KB
/
qrusage.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/**
* Copyright (C) 2014-2019 Andras Radics
* Licensed under the Apache License, Version 2.0
*/
#include <nan.h>
using v8::FunctionTemplate;
#include <sys/time.h>
#include <sys/resource.h>
//#include <unistd.h>
#ifndef RUSAGE_THREAD
# define RUSAGE_THREAD RUSAGE_SELF
#endif
// get the address to the float64 data array in the argument
double * getFloat64ArrayPointer( unsigned int length, v8::Local<v8::Value> arg ) {
#if NODE_MAJOR_VERSION >= 4
if (arg->IsArrayBufferView()) {
v8::Local<v8::Float64Array> array = arg.As<v8::Float64Array>();
if (array->Length() >= length) {
v8::Local<v8::ArrayBuffer> ab = array->Buffer();
double* fields = static_cast<double*>(ab->GetContents().Data());
return fields;
}
}
#endif
return NULL;
}
// extract the integer passed as the first argument
static int getIntArg( Nan::NAN_METHOD_ARGS_TYPE info, int index ) {
#if NODE_MAJOR_VERSION >= 10
int defaultValue = RUSAGE_SELF;
return Nan::To<int32_t>(info[index]).FromMaybe(defaultValue);
#else
return info[index]->Int32Value();
#endif
}
NAN_METHOD(zero) {
double* fields = getFloat64ArrayPointer(1, info[0]);
if (fields) {
fields[0] = 0;
return;
}
else {
info.GetReturnValue().Set(Nan::New(0));
}
}
NAN_METHOD(cputime) {
struct rusage ru;
int who = (info[0]->IsNumber()) ? getIntArg(info, 0) : RUSAGE_SELF;
getrusage(who, &ru);
double cpuUsed =
ru.ru_utime.tv_sec + ru.ru_utime.tv_usec * 1e-6 +
ru.ru_stime.tv_sec + ru.ru_stime.tv_usec * 1e-6;
info.GetReturnValue().Set(Nan::New(cpuUsed));
}
NAN_METHOD( cpuusage ) {
struct rusage ru;
int who = (info[0]->IsNumber()) ? getIntArg(info, 0) : RUSAGE_SELF;
// faster to poke the return values into an existing array
double* fields = getFloat64ArrayPointer(2, info[0]);
getrusage(who, &ru);
if (fields) {
// fastest is to poke the results into an existing array
fields[0] = (double)ru.ru_utime.tv_sec * 1e6 + (double)ru.ru_utime.tv_usec;
fields[1] = (double)ru.ru_stime.tv_sec * 1e6 + (double)ru.ru_stime.tv_usec;;
}
else {
// faster to populate an array of 2 with 2 doubles than to build one string
v8::Local<v8::Array> usage_array = Nan::New<v8::Array>(2);
Nan::Set(usage_array, 0, Nan::New((double)ru.ru_utime.tv_sec * 1e6 + (double)ru.ru_utime.tv_usec));
Nan::Set(usage_array, 1, Nan::New((double)ru.ru_stime.tv_sec * 1e6 + (double)ru.ru_stime.tv_usec));
info.GetReturnValue().Set(usage_array);
}
return;
// faster to build a string than an object
char timebuf[1000];
sprintf(timebuf, "%llu %llu",
((unsigned long long)ru.ru_utime.tv_sec * 1000000 + ru.ru_utime.tv_usec),
((unsigned long long)ru.ru_stime.tv_sec * 1000000 + ru.ru_stime.tv_usec));
info.GetReturnValue().Set(Nan::New(timebuf).ToLocalChecked());
return;
v8::Local<v8::Object> usage_object = Nan::New<v8::Object>();
Nan::Set(usage_object, Nan::New("user").ToLocalChecked(), Nan::New((double)ru.ru_utime.tv_sec * 1e6 + (double)ru.ru_utime.tv_usec));
Nan::Set(usage_object, Nan::New("system").ToLocalChecked(), Nan::New((double)ru.ru_stime.tv_sec * 1e6 + (double)ru.ru_stime.tv_usec));
info.GetReturnValue().Set(usage_object);
}
NAN_METHOD( gettimeofday ) {
struct timeval tv;
gettimeofday(&tv, NULL);
double time = tv.tv_sec + tv.tv_usec * 1e-6;
info.GetReturnValue().Set(Nan::New(time));
}
NAN_METHOD( microtime ) {
struct timeval tv;
double* fields = getFloat64ArrayPointer(1, info[0]);
gettimeofday(&tv, NULL);
double time = tv.tv_sec * 1e6 + tv.tv_usec;
if (fields) fields[0] = time;
else info.GetReturnValue().Set(Nan::New(time));
}
NAN_METHOD( getrusage_array ) {
struct rusage ru;
int who = (info[0]->IsNumber()) ? getIntArg(info, 0) : RUSAGE_SELF;
double* fields = getFloat64ArrayPointer(16, info[1]);
getrusage(who, &ru);
if (fields) {
fields[0] = ru.ru_utime.tv_sec + ru.ru_utime.tv_usec * 1e-6;
fields[1] = ru.ru_stime.tv_sec + ru.ru_stime.tv_usec * 1e-6;
fields[2] = ru.ru_maxrss;
fields[4] = ru.ru_ixrss;
fields[3] = ru.ru_idrss;
fields[5] = ru.ru_isrss;
fields[6] = ru.ru_minflt;
fields[7] = ru.ru_majflt;
fields[8] = ru.ru_nswap;
fields[9] = ru.ru_inblock;
fields[10] = ru.ru_oublock;
fields[11] = ru.ru_msgsnd;
fields[12] = ru.ru_msgrcv;
fields[13] = ru.ru_nsignals;
fields[14] = ru.ru_nvcsw;
fields[15] = ru.ru_nivcsw;
}
else {
// faster to zero-detect than to create new Number every time
v8::Local<v8::Number> zero = Nan::New(0);
#define _number(v) (((v) > 0) ? Nan::New((double)v) : zero)
// note: nan-2.2.0 is unable to Nan::New() a long (ambiguous), cast to double
v8::Local<v8::Array> usage_array = Nan::New<v8::Array>(16);
Nan::Set(usage_array, 0, Nan::New((double)ru.ru_utime.tv_sec + (double)ru.ru_utime.tv_usec * 1e-6));
Nan::Set(usage_array, 1, Nan::New((double)ru.ru_stime.tv_sec + (double)ru.ru_stime.tv_usec * 1e-6));
Nan::Set(usage_array, 2, Nan::New((double)ru.ru_maxrss));
Nan::Set(usage_array, 4, _number(ru.ru_ixrss));
Nan::Set(usage_array, 3, _number(ru.ru_idrss));
Nan::Set(usage_array, 5, _number(ru.ru_isrss));
Nan::Set(usage_array, 6, Nan::New((double)ru.ru_minflt));
Nan::Set(usage_array, 7, _number(ru.ru_majflt));
Nan::Set(usage_array, 8, _number(ru.ru_nswap));
Nan::Set(usage_array, 9, _number(ru.ru_inblock));
Nan::Set(usage_array, 10, _number(ru.ru_oublock));
Nan::Set(usage_array, 11, _number(ru.ru_msgsnd));
Nan::Set(usage_array, 12, _number(ru.ru_msgrcv));
Nan::Set(usage_array, 13, _number(ru.ru_nsignals));
Nan::Set(usage_array, 14, Nan::New((double)ru.ru_nvcsw));
Nan::Set(usage_array, 15, Nan::New((double)ru.ru_nivcsw));
info.GetReturnValue().Set(usage_array);
}
}
NAN_MODULE_INIT(InitAll) {
Nan::Set(target, Nan::New("RUSAGE_SELF").ToLocalChecked(), Nan::New(RUSAGE_SELF)),
Nan::Set(target, Nan::New("RUSAGE_CHILDREN").ToLocalChecked(), Nan::New(RUSAGE_CHILDREN)),
Nan::Set(target, Nan::New("RUSAGE_THREAD").ToLocalChecked(), Nan::New(RUSAGE_THREAD)),
Nan::Set(target, Nan::New("zero").ToLocalChecked(), Nan::GetFunction(Nan::New<FunctionTemplate>(zero)).ToLocalChecked()),
Nan::Set(target, Nan::New("cputime").ToLocalChecked(), Nan::GetFunction(Nan::New<FunctionTemplate>(cputime)).ToLocalChecked()),
Nan::Set(target, Nan::New("cpuusage").ToLocalChecked(), Nan::GetFunction(Nan::New<FunctionTemplate>(cpuusage)).ToLocalChecked()),
Nan::Set(target, Nan::New("gettimeofday").ToLocalChecked(), Nan::GetFunction(Nan::New<FunctionTemplate>(gettimeofday)).ToLocalChecked()),
Nan::Set(target, Nan::New("microtime").ToLocalChecked(), Nan::GetFunction(Nan::New<FunctionTemplate>(microtime)).ToLocalChecked()),
Nan::Set(target, Nan::New("getrusage_array").ToLocalChecked(), Nan::GetFunction(Nan::New<FunctionTemplate>(getrusage_array)).ToLocalChecked()),
((void)0) ;
}
NODE_MODULE(qrusage, InitAll)