Skip to content

Commit

Permalink
src,lib: add constrainedMemory API for process
Browse files Browse the repository at this point in the history
PR-URL: #46218
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
  • Loading branch information
theanarkh authored and juanarbol committed Mar 5, 2023
1 parent 116a336 commit f09b838
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
18 changes: 18 additions & 0 deletions doc/api/process.md
Expand Up @@ -1107,6 +1107,23 @@ and [Cluster][] documentation), the `process.connected` property will return
Once `process.connected` is `false`, it is no longer possible to send messages
over the IPC channel using `process.send()`.

## `process.constrainedMemory()`

<!-- YAML
added: REPLACEME
-->

> Stability: 1 - Experimental
* {number|undefined}

Gets the amount of memory available to the process (in bytes) based on
limits imposed by the OS. If there is no such constraint, or the constraint
is unknown, `undefined` is returned.

See [`uv_get_constrained_memory`][uv_get_constrained_memory] for more
information.

## `process.cpuUsage([previousValue])`

<!-- YAML
Expand Down Expand Up @@ -3893,6 +3910,7 @@ cases:
[process_warning]: #event-warning
[report documentation]: report.md
[terminal raw mode]: tty.md#readstreamsetrawmodemode
[uv_get_constrained_memory]: https://docs.libuv.org/en/v1.x/misc.html#c.uv_get_constrained_memory
[uv_rusage_t]: https://docs.libuv.org/en/v1.x/misc.html#c.uv_rusage_t
[wikipedia_major_fault]: https://en.wikipedia.org/wiki/Page_fault#Major
[wikipedia_minor_fault]: https://en.wikipedia.org/wiki/Page_fault#Minor
1 change: 1 addition & 0 deletions lib/internal/bootstrap/node.js
Expand Up @@ -200,6 +200,7 @@ const rawMethods = internalBinding('process_methods');
process.cpuUsage = wrapped.cpuUsage;
process.resourceUsage = wrapped.resourceUsage;
process.memoryUsage = wrapped.memoryUsage;
process.constrainedMemory = rawMethods.constrainedMemory;
process.kill = wrapped.kill;
process.exit = wrapped.exit;

Expand Down
9 changes: 9 additions & 0 deletions src/node_process_methods.cc
Expand Up @@ -206,6 +206,13 @@ static void MemoryUsage(const FunctionCallbackInfo<Value>& args) {
: static_cast<double>(array_buffer_allocator->total_mem_usage());
}

static void GetConstrainedMemory(const FunctionCallbackInfo<Value>& args) {
uint64_t value = uv_get_constrained_memory();
if (value != 0) {
args.GetReturnValue().Set(static_cast<double>(value));
}
}

void RawDebug(const FunctionCallbackInfo<Value>& args) {
CHECK(args.Length() == 1 && args[0]->IsString() &&
"must be called with a single string");
Expand Down Expand Up @@ -577,6 +584,7 @@ static void Initialize(Local<Object> target,

SetMethod(context, target, "umask", Umask);
SetMethod(context, target, "memoryUsage", MemoryUsage);
SetMethod(context, target, "constrainedMemory", GetConstrainedMemory);
SetMethod(context, target, "rss", Rss);
SetMethod(context, target, "cpuUsage", CPUUsage);
SetMethod(context, target, "resourceUsage", ResourceUsage);
Expand Down Expand Up @@ -607,6 +615,7 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(Umask);
registry->Register(RawDebug);
registry->Register(MemoryUsage);
registry->Register(GetConstrainedMemory);
registry->Register(Rss);
registry->Register(CPUUsage);
registry->Register(ResourceUsage);
Expand Down
12 changes: 12 additions & 0 deletions test/parallel/test-process-constrained-memory.js
@@ -0,0 +1,12 @@
'use strict';
require('../common');
const assert = require('assert');
const { Worker } = require('worker_threads');
const constrainedMemory = process.constrainedMemory();
if (constrainedMemory !== undefined) {
assert(constrainedMemory > 0);
}
if (!process.env.isWorker) {
process.env.isWorker = true;
new Worker(__filename);
}

0 comments on commit f09b838

Please sign in to comment.