Skip to content

Commit cc188ac

Browse files
committed
src: add uv_get_available_memory to report and process
1 parent 1263bb6 commit cc188ac

File tree

6 files changed

+40
-17
lines changed

6 files changed

+40
-17
lines changed

doc/api/process.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,6 +1127,21 @@ is unknown, `undefined` is returned.
11271127
See [`uv_get_constrained_memory`][uv_get_constrained_memory] for more
11281128
information.
11291129

1130+
## `process.availableMemory()`
1131+
1132+
<!-- YAML
1133+
added: REPLACEME
1134+
-->
1135+
1136+
> Stability: 1 - Experimental
1137+
1138+
* {number}
1139+
1140+
Gets the amount of free memory that is still available to the process.
1141+
1142+
See [`uv_get_available_memory`][uv_get_available_memory] for more
1143+
information.
1144+
11301145
## `process.cpuUsage([previousValue])`
11311146

11321147
<!-- YAML
@@ -4026,6 +4041,7 @@ cases:
40264041
[process_warning]: #event-warning
40274042
[report documentation]: report.md
40284043
[terminal raw mode]: tty.md#readstreamsetrawmodemode
4044+
[uv_get_available_memory]: https://docs.libuv.org/en/v1.x/misc.html#c.uv_get_available_memory
40294045
[uv_get_constrained_memory]: https://docs.libuv.org/en/v1.x/misc.html#c.uv_get_constrained_memory
40304046
[uv_rusage_t]: https://docs.libuv.org/en/v1.x/misc.html#c.uv_rusage_t
40314047
[wikipedia_major_fault]: https://en.wikipedia.org/wiki/Page_fault#Major

lib/internal/bootstrap/node.js

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

src/node_process_methods.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,11 @@ static void GetConstrainedMemory(const FunctionCallbackInfo<Value>& args) {
216216
}
217217
}
218218

219+
static void GetAvailableMemory(const FunctionCallbackInfo<Value>& args) {
220+
uint64_t value = uv_get_available_memory();
221+
args.GetReturnValue().Set(static_cast<double>(value));
222+
}
223+
219224
void RawDebug(const FunctionCallbackInfo<Value>& args) {
220225
CHECK(args.Length() == 1 && args[0]->IsString() &&
221226
"must be called with a single string");
@@ -633,6 +638,7 @@ static void CreatePerIsolateProperties(IsolateData* isolate_data,
633638
SetMethod(isolate, target, "umask", Umask);
634639
SetMethod(isolate, target, "memoryUsage", MemoryUsage);
635640
SetMethod(isolate, target, "constrainedMemory", GetConstrainedMemory);
641+
SetMethod(isolate, target, "availableMemory", GetAvailableMemory);
636642
SetMethod(isolate, target, "rss", Rss);
637643
SetMethod(isolate, target, "cpuUsage", CPUUsage);
638644
SetMethod(isolate, target, "resourceUsage", ResourceUsage);
@@ -674,6 +680,7 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
674680
registry->Register(RawDebug);
675681
registry->Register(MemoryUsage);
676682
registry->Register(GetConstrainedMemory);
683+
registry->Register(GetAvailableMemory);
677684
registry->Register(Rss);
678685
registry->Register(CPUUsage);
679686
registry->Register(ResourceUsage);

src/node_report.cc

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -634,17 +634,10 @@ static void PrintResourceUsage(JSONWriter* writer) {
634634
}
635635

636636
uint64_t constrained_memory = uv_get_constrained_memory();
637-
if (constrained_memory) {
638-
writer->json_keyvalue("constrained_memory", constrained_memory);
639-
}
637+
writer->json_keyvalue("constrained_memory", constrained_memory);
640638

641-
// See GuessMemoryAvailableToTheProcess
642-
if (!err && constrained_memory && constrained_memory >= rss) {
643-
uint64_t available_memory = constrained_memory - rss;
644-
writer->json_keyvalue("available_memory", available_memory);
645-
} else {
646-
writer->json_keyvalue("available_memory", free_memory);
647-
}
639+
uint64_t available_memory = uv_get_available_memory();
640+
writer->json_keyvalue("available_memory", available_memory);
648641

649642
if (uv_getrusage(&rusage) == 0) {
650643
double user_cpu =

test/common/report.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -233,13 +233,12 @@ function _validateContent(report, fields = []) {
233233

234234
// Verify the format of the resourceUsage section.
235235
const usage = { ...report.resourceUsage };
236-
// Delete it, otherwise checkForUnknownFields will throw error
237-
delete usage.constrained_memory;
238236
const resourceUsageFields = ['userCpuSeconds', 'kernelCpuSeconds',
239237
'cpuConsumptionPercent', 'userCpuConsumptionPercent',
240238
'kernelCpuConsumptionPercent',
241239
'maxRss', 'rss', 'free_memory', 'total_memory',
242-
'available_memory', 'pageFaults', 'fsActivity'];
240+
'available_memory', 'pageFaults', 'fsActivity',
241+
'constrained_memory'];
243242
checkForUnknownFields(usage, resourceUsageFields);
244243
assert.strictEqual(typeof usage.userCpuSeconds, 'number');
245244
assert.strictEqual(typeof usage.kernelCpuSeconds, 'number');
@@ -251,10 +250,7 @@ function _validateContent(report, fields = []) {
251250
assert(typeof usage.free_memory, 'string');
252251
assert(typeof usage.total_memory, 'string');
253252
assert(typeof usage.available_memory, 'string');
254-
// This field may not exsit
255-
if (report.resourceUsage.constrained_memory) {
256-
assert(typeof report.resourceUsage.constrained_memory, 'string');
257-
}
253+
assert(typeof usage.constrained_memory, 'string');
258254
assert(typeof usage.pageFaults === 'object' && usage.pageFaults !== null);
259255
checkForUnknownFields(usage.pageFaults, ['IORequired', 'IONotRequired']);
260256
assert(Number.isSafeInteger(usage.pageFaults.IORequired));
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict';
2+
require('../common');
3+
const assert = require('assert');
4+
const { Worker } = require('worker_threads');
5+
const availableMemory = process.availableMemory();
6+
assert(typeof availableMemory, 'number');
7+
if (!process.env.isWorker) {
8+
process.env.isWorker = true;
9+
new Worker(__filename);
10+
}

0 commit comments

Comments
 (0)