Skip to content

Commit fca8b25

Browse files
committed
src,lib: add constrainedMemory API for process
1 parent 5d50b84 commit fca8b25

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
lines changed

doc/api/process.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,6 +1103,27 @@ 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}
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+
It is not unusual for this value to be less than or greater than `os.totalmem()`.
1121+
This function currently only returns a non-zero value on Linux, based on cgroups
1122+
if it is present, and on z/OS based on `RLIMIT_MEMLIMIT`.
1123+
1124+
See [`uv_get_constrained_memory`][uv_get_constrained_memory] for more
1125+
information.
1126+
11061127
## `process.cpuUsage([previousValue])`
11071128

11081129
<!-- YAML
@@ -3901,6 +3922,7 @@ cases:
39013922
[process_warning]: #event-warning
39023923
[report documentation]: report.md
39033924
[terminal raw mode]: tty.md#readstreamsetrawmodemode
3925+
[uv_get_constrained_memory]: https://docs.libuv.org/en/v1.x/misc.html#c.uv_get_constrained_memory
39043926
[uv_rusage_t]: https://docs.libuv.org/en/v1.x/misc.html#c.uv_rusage_t
39053927
[wikipedia_major_fault]: https://en.wikipedia.org/wiki/Page_fault#Major
39063928
[wikipedia_minor_fault]: https://en.wikipedia.org/wiki/Page_fault#Minor

lib/internal/bootstrap/node.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ const rawMethods = internalBinding('process_methods');
179179
process.cpuUsage = wrapped.cpuUsage;
180180
process.resourceUsage = wrapped.resourceUsage;
181181
process.memoryUsage = wrapped.memoryUsage;
182+
process.constrainedMemory = rawMethods.constrainedMemory;
182183
process.kill = wrapped.kill;
183184
process.exit = wrapped.exit;
184185

src/node_process_methods.cc

Lines changed: 9 additions & 0 deletions
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+
double value = static_cast<double>(uv_get_constrained_memory());
212+
if (value) {
213+
args.GetReturnValue().Set(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);
Lines changed: 12 additions & 0 deletions
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) {
7+
assert(process.constrainedMemory() > 0);
8+
}
9+
if (!process.env.isWorker) {
10+
process.env.isWorker = true;
11+
new Worker(__filename);
12+
}

0 commit comments

Comments
 (0)