Skip to content

Commit

Permalink
process: add direct access to rss without iterating pages
Browse files Browse the repository at this point in the history
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: #34291
Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
Adrien Maret authored and MylesBorins committed Aug 31, 2021
1 parent 577d228 commit 3982919
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
22 changes: 22 additions & 0 deletions doc/api/process.md
Original file line number Diff line number Diff line change
Expand Up @@ -1610,6 +1610,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
Expand Down
3 changes: 3 additions & 0 deletions lib/internal/process/per_thread.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ function wrapProcessMethods(binding) {
hrtimeBigInt: _hrtimeBigInt,
cpuUsage: _cpuUsage,
memoryUsage: _memoryUsage,
rss,
resourceUsage: _resourceUsage
} = binding;

Expand Down Expand Up @@ -160,6 +161,8 @@ function wrapProcessMethods(binding) {
};
}

memoryUsage.rss = rss;

function exit(code) {
if (code || code === 0)
process.exitCode = code;
Expand Down
14 changes: 13 additions & 1 deletion src/node_process_methods.cc
Original file line number Diff line number Diff line change
Expand Up @@ -193,14 +193,20 @@ 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;
int err = uv_resident_set_memory(&rss);
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;
Expand All @@ -213,6 +219,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();
Expand Down Expand Up @@ -450,6 +461,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, "hrtime", Hrtime);
env->SetMethod(target, "hrtimeBigInt", HrtimeBigInt);
Expand Down
2 changes: 2 additions & 0 deletions test/parallel/test-memory-usage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

0 comments on commit 3982919

Please sign in to comment.