process: add direct access to rss without iterating pages

Accessing the rss value through memoryUsage() can be expensive
because this method will also generate  memory usage statistics
by iterating on each page.
This commit intend to offer a more direct access to rss value.

Refs: #33384

PR-URL: https://github.com/nodejs/node/pull/34291
Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Adrien Maret 2020-12-29 15:36:41 +01:00 committed by Node.js GitHub Bot
parent d548f4d116
commit 83ab5433ff
4 changed files with 41 additions and 1 deletions

View File

@ -1589,6 +1589,28 @@ Will generate:
When using [`Worker`][] threads, `rss` will be a value that is valid for the
entire process, while the other fields will only refer to the current thread.
The `process.memoryUsage()` method iterate over each page to gather
informations about memory usage which can be slow depending on the
program memory allocations.
## `process.memoryUsage.rss()`
* Returns: {integer}
The `process.memoryUsage.rss()` method returns an integer representing the
Resident Set Size (RSS) in bytes.
The Resident Set Size, is the amount of space occupied in the main
memory device (that is a subset of the total allocated memory) for the
process, including all C++ and JavaScript objects and code.
This is the same value as the one returned by `process.memoryUsage()`.
```js
console.log(process.memoryUsage.rss());
// 35655680
```
## `process.nextTick(callback[, ...args])`
<!-- YAML
added: v0.1.26

View File

@ -93,6 +93,7 @@ function wrapProcessMethods(binding) {
const {
cpuUsage: _cpuUsage,
memoryUsage: _memoryUsage,
rss,
resourceUsage: _resourceUsage
} = binding;
@ -168,6 +169,8 @@ function wrapProcessMethods(binding) {
};
}
memoryUsage.rss = rss;
function exit(code) {
if (code || code === 0)
process.exitCode = code;

View File

@ -172,7 +172,7 @@ static void Kill(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(err);
}
static void MemoryUsage(const FunctionCallbackInfo<Value>& args) {
static void Rss(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
size_t rss;
@ -180,6 +180,12 @@ static void MemoryUsage(const FunctionCallbackInfo<Value>& args) {
if (err)
return env->ThrowUVException(err, "uv_resident_set_memory");
args.GetReturnValue().Set(static_cast<double>(rss));
}
static void MemoryUsage(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
Isolate* isolate = env->isolate();
// V8 memory usage
HeapStatistics v8_heap_stats;
@ -192,6 +198,11 @@ static void MemoryUsage(const FunctionCallbackInfo<Value>& args) {
Local<ArrayBuffer> ab = get_fields_array_buffer(args, 0, 5);
double* fields = static_cast<double*>(ab->GetBackingStore()->Data());
size_t rss;
int err = uv_resident_set_memory(&rss);
if (err)
return env->ThrowUVException(err, "uv_resident_set_memory");
fields[0] = rss;
fields[1] = v8_heap_stats.total_heap_size();
fields[2] = v8_heap_stats.used_heap_size();
@ -542,6 +553,7 @@ static void InitializeProcessMethods(Local<Object> target,
env->SetMethod(target, "umask", Umask);
env->SetMethod(target, "_rawDebug", RawDebug);
env->SetMethod(target, "memoryUsage", MemoryUsage);
env->SetMethod(target, "rss", Rss);
env->SetMethod(target, "cpuUsage", CPUUsage);
env->SetMethod(target, "resourceUsage", ResourceUsage);
@ -568,6 +580,7 @@ void RegisterProcessMethodsExternalReferences(
registry->Register(Umask);
registry->Register(RawDebug);
registry->Register(MemoryUsage);
registry->Register(Rss);
registry->Register(CPUUsage);
registry->Register(ResourceUsage);

View File

@ -44,3 +44,5 @@ if (r.arrayBuffers > 0) {
assert.strictEqual(after.arrayBuffers - r.arrayBuffers, size,
`${after.arrayBuffers} - ${r.arrayBuffers} === ${size}`);
}
assert(process.memoryUsage.rss() > 0);