-
-
Notifications
You must be signed in to change notification settings - Fork 33.1k
process: expose uv_rusage on process.resourceUsage() #28018
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -285,6 +285,38 @@ void GetActiveHandles(const FunctionCallbackInfo<Value>& args) { | |
Array::New(env->isolate(), handle_v.data(), handle_v.size())); | ||
} | ||
|
||
static void ResourceUsage(const FunctionCallbackInfo<Value>& args) { | ||
Environment* env = Environment::GetCurrent(args); | ||
|
||
uv_rusage_t rusage; | ||
int err = uv_getrusage(&rusage); | ||
if (err) | ||
return env->ThrowUVException(err, "uv_getrusage"); | ||
|
||
CHECK(args[0]->IsFloat64Array()); | ||
Local<Float64Array> array = args[0].As<Float64Array>(); | ||
CHECK_EQ(array->Length(), 16); | ||
Local<ArrayBuffer> ab = array->Buffer(); | ||
double* fields = static_cast<double*>(ab->GetContents().Data()); | ||
|
||
fields[0] = MICROS_PER_SEC * rusage.ru_utime.tv_sec + rusage.ru_utime.tv_usec; | ||
fields[1] = MICROS_PER_SEC * rusage.ru_stime.tv_sec + rusage.ru_stime.tv_usec; | ||
fields[2] = rusage.ru_maxrss; | ||
fields[3] = rusage.ru_ixrss; | ||
fields[4] = rusage.ru_idrss; | ||
fields[5] = rusage.ru_isrss; | ||
fields[6] = rusage.ru_minflt; | ||
fields[7] = rusage.ru_majflt; | ||
fields[8] = rusage.ru_nswap; | ||
fields[9] = rusage.ru_inblock; | ||
fields[10] = rusage.ru_oublock; | ||
fields[11] = rusage.ru_msgsnd; | ||
fields[12] = rusage.ru_msgrcv; | ||
fields[13] = rusage.ru_nsignals; | ||
fields[14] = rusage.ru_nvcsw; | ||
fields[15] = rusage.ru_nivcsw; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: this would be a bit more readable/maintainable if these were named constants instead of just literal index numbers There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure how to do that exactly, i never really coded in C++ |
||
} | ||
|
||
#ifdef __POSIX__ | ||
static void DebugProcess(const FunctionCallbackInfo<Value>& args) { | ||
Environment* env = Environment::GetCurrent(args); | ||
|
@@ -426,6 +458,7 @@ static void InitializeProcessMethods(Local<Object> target, | |
env->SetMethod(target, "cpuUsage", CPUUsage); | ||
env->SetMethod(target, "hrtime", Hrtime); | ||
env->SetMethod(target, "hrtimeBigInt", HrtimeBigInt); | ||
env->SetMethod(target, "resourceUsage", ResourceUsage); | ||
|
||
env->SetMethod(target, "_getActiveRequests", GetActiveRequests); | ||
env->SetMethod(target, "_getActiveHandles", GetActiveHandles); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
'use strict'; | ||
require('../common'); | ||
const assert = require('assert'); | ||
|
||
const rusage = process.resourceUsage(); | ||
|
||
[ | ||
'userCPUTime', | ||
'systemCPUTime', | ||
'maxRSS', | ||
'sharedMemorySize', | ||
'unsharedDataSize', | ||
'unsharedStackSize', | ||
'minorPageFault', | ||
'majorPageFault', | ||
'swappedOut', | ||
'fsRead', | ||
'fsWrite', | ||
'ipcSent', | ||
'ipcReceived', | ||
'signalsCount', | ||
'voluntaryContextSwitches', | ||
'involuntaryContextSwitches' | ||
].forEach((n) => { | ||
assert.strictEqual(typeof rusage[n], 'number', `${n} should be a number`); | ||
assert(rusage[n] >= 0, `${n} should be above or equal 0`); | ||
}); |
Uh oh!
There was an error while loading. Please reload this page.