Skip to content
Permalink
Browse files

process: expose uv_rusage on process.resourcesUsage()

As discussed in nodejs/diagnostics#161,
the core should expose important metrics about the runtime, this PR's
goal is to let user get the number of io request made, and lower level
mertrics like the page faults and context switches.
  • Loading branch information...
vmarchaud committed Jun 2, 2019
1 parent 47ee383 commit 54750e8455a21875fbe60cf51b2c0707d6dc83e5
@@ -714,6 +714,11 @@ A constructor for a class was called without `new`.

The native call from `process.cpuUsage` could not be processed.

<a id="ERR_RESOURCES_USAGE"></a>
### ERR_RESOURCES_USAGE

The native call from `process.resourcesUsage` could not be processed.

<a id="ERR_CRYPTO_CUSTOM_ENGINE_NOT_SUPPORTED"></a>
### ERR_CRYPTO_CUSTOM_ENGINE_NOT_SUPPORTED

@@ -1819,6 +1819,46 @@ process.report.writeReport();

Additional documentation is available in the [report documentation][].

## process.resourcesUsage()
<!-- YAML
added: v12.x.x
-->

* Returns: {Object}
* `minflt` {integer}
* `majflt` {integer}
* `inblock` {integer}
* `oublock` {integer}
* `nvcsw` {integer}
* `nivcsw` {integer}

The `process.resourcesUsage()` method return the current resources usage
for the current process.
To understand what these value mean, please refer to
[getrusage(2)](http://man7.org/linux/man-pages/man2/getrusage.2.html)
On Windows platform, only `inblock` and `oublock` are supported,
all other fields will report zero as value.

For example, the code:

```js
console.log(process.resourcesUsage());
```

Will generate:

<!-- eslint-skip -->
```js
{
minflt: 2854,
majflt: 0,
inblock: 0,
oublock: 8,
nvcsw: 238,
nivcsw: 0
}
```

## process.send(message[, sendHandle[, options]][, callback])
<!-- YAML
added: v0.5.9
@@ -110,6 +110,7 @@ if (isMainThread) {
process.hrtime = wrapped.hrtime;
process.hrtime.bigint = wrapped.hrtimeBigInt;
process.cpuUsage = wrapped.cpuUsage;
process.resourcesUsage = wrapped.resourcesUsage;
process.memoryUsage = wrapped.memoryUsage;
process.kill = wrapped.kill;
process.exit = wrapped.exit;
@@ -1053,6 +1053,7 @@ E('ERR_OUT_OF_RANGE',
return msg;
}, RangeError);
E('ERR_REQUIRE_ESM', 'Must use import to load ES Module: %s', Error);
E('ERR_RESOURCES_USAGE', 'Unable to obtain resources usage %s', Error);
E('ERR_SCRIPT_EXECUTION_INTERRUPTED',
'Script execution was interrupted by `SIGINT`', Error);
E('ERR_SERVER_ALREADY_LISTEN',
@@ -19,7 +19,8 @@ const {
ERR_INVALID_ARG_TYPE,
ERR_INVALID_OPT_VALUE,
ERR_OUT_OF_RANGE,
ERR_UNKNOWN_SIGNAL
ERR_UNKNOWN_SIGNAL,
ERR_RESOURCES_USAGE
}
} = require('internal/errors');
const format = require('internal/util/inspect').format;
@@ -35,7 +36,8 @@ function wrapProcessMethods(binding) {
hrtime: _hrtime,
hrtimeBigInt: _hrtimeBigInt,
cpuUsage: _cpuUsage,
memoryUsage: _memoryUsage
memoryUsage: _memoryUsage,
resourcesUsage: _resourcesUsage
} = binding;

function _rawDebug(...args) {
@@ -190,12 +192,29 @@ function wrapProcessMethods(binding) {
return true;
}

const resourcesValues = new Float64Array(6);
function resourcesUsage() {
const errmsg = _resourcesUsage(resourcesValues);
if (errmsg) {
throw new ERR_RESOURCES_USAGE(errmsg);
}
return {
minflt: resourcesValues[0],
majflt: resourcesValues[1],
inblock: resourcesValues[2],
oublock: resourcesValues[3],
nvcsw: resourcesValues[4],
nivcsw: resourcesValues[5]
};
}


return {
_rawDebug,
hrtime,
hrtimeBigInt,
cpuUsage,
resourcesUsage,
memoryUsage,
kill,
exit
@@ -278,6 +278,30 @@ void GetActiveHandles(const FunctionCallbackInfo<Value>& args) {
Array::New(env->isolate(), handle_v.data(), handle_v.size()));
}

static void ResourcesUsage(const FunctionCallbackInfo<Value>& args) {
uv_rusage_t rusage;
int err = uv_getrusage(&rusage);
if (err) {
// On error, return the strerror version of the error code.
Local<String> errmsg = OneByteString(args.GetIsolate(), uv_strerror(err));
return args.GetReturnValue().Set(errmsg);
}

// Get the double array pointer from the Float64Array argument.
CHECK(args[0]->IsFloat64Array());
Local<Float64Array> array = args[0].As<Float64Array>();
CHECK_EQ(array->Length(), 6);
Local<ArrayBuffer> ab = array->Buffer();
double* fields = static_cast<double*>(ab->GetContents().Data());

fields[0] = rusage.ru_minflt;
fields[1] = rusage.ru_majflt;
fields[2] = rusage.ru_inblock;
fields[3] = rusage.ru_oublock;
fields[4] = rusage.ru_nvcsw;
fields[5] = rusage.ru_nivcsw;
}

#ifdef __POSIX__
static void DebugProcess(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
@@ -418,6 +442,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, "resourcesUsage", ResourcesUsage);

env->SetMethod(target, "_getActiveRequests", GetActiveRequests);
env->SetMethod(target, "_getActiveHandles", GetActiveHandles);
@@ -0,0 +1,32 @@
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.

'use strict';
require('../common');
const assert = require('assert');

const r = process.resourcesUsage();
assert.ok(typeof r.minflt === 'number');
assert.ok(typeof r.majflt === 'number');
assert.ok(typeof r.inblock === 'number');
assert.ok(typeof r.oublock === 'number');
assert.ok(typeof r.nvcsw === 'number');
assert.ok(typeof r.nivcsw === 'number');

0 comments on commit 54750e8

Please sign in to comment.
You can’t perform that action at this time.