Skip to content

Commit f09b838

Browse files
theanarkhjuanarbol
authored andcommitted
src,lib: add constrainedMemory API for process
PR-URL: #46218 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Darshan Sen <raisinten@gmail.com>
1 parent 116a336 commit f09b838

File tree

4 files changed

+40
-0
lines changed

4 files changed

+40
-0
lines changed

doc/api/process.md

+18
Original file line numberDiff line numberDiff line change
@@ -1107,6 +1107,23 @@ and [Cluster][] documentation), the `process.connected` property will return
11071107
Once `process.connected` is `false`, it is no longer possible to send messages
11081108
over the IPC channel using `process.send()`.
11091109

1110+
## `process.constrainedMemory()`
1111+
1112+
<!-- YAML
1113+
added: REPLACEME
1114+
-->
1115+
1116+
> Stability: 1 - Experimental
1117+
1118+
* {number|undefined}
1119+
1120+
Gets the amount of memory available to the process (in bytes) based on
1121+
limits imposed by the OS. If there is no such constraint, or the constraint
1122+
is unknown, `undefined` is returned.
1123+
1124+
See [`uv_get_constrained_memory`][uv_get_constrained_memory] for more
1125+
information.
1126+
11101127
## `process.cpuUsage([previousValue])`
11111128

11121129
<!-- YAML
@@ -3893,6 +3910,7 @@ cases:
38933910
[process_warning]: #event-warning
38943911
[report documentation]: report.md
38953912
[terminal raw mode]: tty.md#readstreamsetrawmodemode
3913+
[uv_get_constrained_memory]: https://docs.libuv.org/en/v1.x/misc.html#c.uv_get_constrained_memory
38963914
[uv_rusage_t]: https://docs.libuv.org/en/v1.x/misc.html#c.uv_rusage_t
38973915
[wikipedia_major_fault]: https://en.wikipedia.org/wiki/Page_fault#Major
38983916
[wikipedia_minor_fault]: https://en.wikipedia.org/wiki/Page_fault#Minor

lib/internal/bootstrap/node.js

+1
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ const rawMethods = internalBinding('process_methods');
200200
process.cpuUsage = wrapped.cpuUsage;
201201
process.resourceUsage = wrapped.resourceUsage;
202202
process.memoryUsage = wrapped.memoryUsage;
203+
process.constrainedMemory = rawMethods.constrainedMemory;
203204
process.kill = wrapped.kill;
204205
process.exit = wrapped.exit;
205206

src/node_process_methods.cc

+9
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,13 @@ static void MemoryUsage(const FunctionCallbackInfo<Value>& args) {
206206
: static_cast<double>(array_buffer_allocator->total_mem_usage());
207207
}
208208

209+
static void GetConstrainedMemory(const FunctionCallbackInfo<Value>& args) {
210+
uint64_t value = uv_get_constrained_memory();
211+
if (value != 0) {
212+
args.GetReturnValue().Set(static_cast<double>(value));
213+
}
214+
}
215+
209216
void RawDebug(const FunctionCallbackInfo<Value>& args) {
210217
CHECK(args.Length() == 1 && args[0]->IsString() &&
211218
"must be called with a single string");
@@ -577,6 +584,7 @@ static void Initialize(Local<Object> target,
577584

578585
SetMethod(context, target, "umask", Umask);
579586
SetMethod(context, target, "memoryUsage", MemoryUsage);
587+
SetMethod(context, target, "constrainedMemory", GetConstrainedMemory);
580588
SetMethod(context, target, "rss", Rss);
581589
SetMethod(context, target, "cpuUsage", CPUUsage);
582590
SetMethod(context, target, "resourceUsage", ResourceUsage);
@@ -607,6 +615,7 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
607615
registry->Register(Umask);
608616
registry->Register(RawDebug);
609617
registry->Register(MemoryUsage);
618+
registry->Register(GetConstrainedMemory);
610619
registry->Register(Rss);
611620
registry->Register(CPUUsage);
612621
registry->Register(ResourceUsage);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict';
2+
require('../common');
3+
const assert = require('assert');
4+
const { Worker } = require('worker_threads');
5+
const constrainedMemory = process.constrainedMemory();
6+
if (constrainedMemory !== undefined) {
7+
assert(constrainedMemory > 0);
8+
}
9+
if (!process.env.isWorker) {
10+
process.env.isWorker = true;
11+
new Worker(__filename);
12+
}

0 commit comments

Comments
 (0)