Skip to content

Commit d52f600

Browse files
theanarkhruyadorno
authored andcommittedJan 31, 2023
src,lib: add constrainedMemory API for process
PR-URL: #46218 Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Darshan Sen <[email protected]>
1 parent 4c59b60 commit d52f600

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
@@ -1103,6 +1103,23 @@ and [Cluster][] documentation), the `process.connected` property will return
11031103
Once `process.connected` is `false`, it is no longer possible to send messages
11041104
over the IPC channel using `process.send()`.
11051105

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

11081125
<!-- YAML
@@ -3889,6 +3906,7 @@ cases:
38893906
[process_warning]: #event-warning
38903907
[report documentation]: report.md
38913908
[terminal raw mode]: tty.md#readstreamsetrawmodemode
3909+
[uv_get_constrained_memory]: https://docs.libuv.org/en/v1.x/misc.html#c.uv_get_constrained_memory
38923910
[uv_rusage_t]: https://docs.libuv.org/en/v1.x/misc.html#c.uv_rusage_t
38933911
[wikipedia_major_fault]: https://en.wikipedia.org/wiki/Page_fault#Major
38943912
[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
@@ -167,6 +167,7 @@ const rawMethods = internalBinding('process_methods');
167167
process.cpuUsage = wrapped.cpuUsage;
168168
process.resourceUsage = wrapped.resourceUsage;
169169
process.memoryUsage = wrapped.memoryUsage;
170+
process.constrainedMemory = rawMethods.constrainedMemory;
170171
process.kill = wrapped.kill;
171172
process.exit = wrapped.exit;
172173

‎src/node_process_methods.cc

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

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

583590
SetMethod(context, target, "umask", Umask);
584591
SetMethod(context, target, "memoryUsage", MemoryUsage);
592+
SetMethod(context, target, "constrainedMemory", GetConstrainedMemory);
585593
SetMethod(context, target, "rss", Rss);
586594
SetMethod(context, target, "cpuUsage", CPUUsage);
587595
SetMethod(context, target, "resourceUsage", ResourceUsage);
@@ -612,6 +620,7 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
612620
registry->Register(Umask);
613621
registry->Register(RawDebug);
614622
registry->Register(MemoryUsage);
623+
registry->Register(GetConstrainedMemory);
615624
registry->Register(Rss);
616625
registry->Register(CPUUsage);
617626
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)
Please sign in to comment.