Skip to content

Commit

Permalink
src: add uv_get_available_memory to report and process
Browse files Browse the repository at this point in the history
  • Loading branch information
theanarkh committed Mar 9, 2024
1 parent 1263bb6 commit cc188ac
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 17 deletions.
16 changes: 16 additions & 0 deletions doc/api/process.md
Original file line number Diff line number Diff line change
Expand Up @@ -1127,6 +1127,21 @@ is unknown, `undefined` is returned.
See [`uv_get_constrained_memory`][uv_get_constrained_memory] for more
information.

## `process.availableMemory()`

<!-- YAML
added: REPLACEME
-->

> Stability: 1 - Experimental
* {number}

Gets the amount of free memory that is still available to the process.

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

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

<!-- YAML
Expand Down Expand Up @@ -4026,6 +4041,7 @@ cases:
[process_warning]: #event-warning
[report documentation]: report.md
[terminal raw mode]: tty.md#readstreamsetrawmodemode
[uv_get_available_memory]: https://docs.libuv.org/en/v1.x/misc.html#c.uv_get_available_memory
[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
Expand Down
1 change: 1 addition & 0 deletions lib/internal/bootstrap/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ const rawMethods = internalBinding('process_methods');
process.resourceUsage = wrapped.resourceUsage;
process.memoryUsage = wrapped.memoryUsage;
process.constrainedMemory = rawMethods.constrainedMemory;
process.availableMemory = rawMethods.availableMemory;
process.kill = wrapped.kill;
process.exit = wrapped.exit;

Expand Down
7 changes: 7 additions & 0 deletions src/node_process_methods.cc
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,11 @@ static void GetConstrainedMemory(const FunctionCallbackInfo<Value>& args) {
}
}

static void GetAvailableMemory(const FunctionCallbackInfo<Value>& args) {
uint64_t value = uv_get_available_memory();
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 @@ -633,6 +638,7 @@ static void CreatePerIsolateProperties(IsolateData* isolate_data,
SetMethod(isolate, target, "umask", Umask);
SetMethod(isolate, target, "memoryUsage", MemoryUsage);
SetMethod(isolate, target, "constrainedMemory", GetConstrainedMemory);
SetMethod(isolate, target, "availableMemory", GetAvailableMemory);
SetMethod(isolate, target, "rss", Rss);
SetMethod(isolate, target, "cpuUsage", CPUUsage);
SetMethod(isolate, target, "resourceUsage", ResourceUsage);
Expand Down Expand Up @@ -674,6 +680,7 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(RawDebug);
registry->Register(MemoryUsage);
registry->Register(GetConstrainedMemory);
registry->Register(GetAvailableMemory);
registry->Register(Rss);
registry->Register(CPUUsage);
registry->Register(ResourceUsage);
Expand Down
13 changes: 3 additions & 10 deletions src/node_report.cc
Original file line number Diff line number Diff line change
Expand Up @@ -634,17 +634,10 @@ static void PrintResourceUsage(JSONWriter* writer) {
}

uint64_t constrained_memory = uv_get_constrained_memory();
if (constrained_memory) {
writer->json_keyvalue("constrained_memory", constrained_memory);
}
writer->json_keyvalue("constrained_memory", constrained_memory);

// See GuessMemoryAvailableToTheProcess
if (!err && constrained_memory && constrained_memory >= rss) {
uint64_t available_memory = constrained_memory - rss;
writer->json_keyvalue("available_memory", available_memory);
} else {
writer->json_keyvalue("available_memory", free_memory);
}
uint64_t available_memory = uv_get_available_memory();
writer->json_keyvalue("available_memory", available_memory);

if (uv_getrusage(&rusage) == 0) {
double user_cpu =
Expand Down
10 changes: 3 additions & 7 deletions test/common/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,13 +233,12 @@ function _validateContent(report, fields = []) {

// Verify the format of the resourceUsage section.
const usage = { ...report.resourceUsage };
// Delete it, otherwise checkForUnknownFields will throw error
delete usage.constrained_memory;
const resourceUsageFields = ['userCpuSeconds', 'kernelCpuSeconds',
'cpuConsumptionPercent', 'userCpuConsumptionPercent',
'kernelCpuConsumptionPercent',
'maxRss', 'rss', 'free_memory', 'total_memory',
'available_memory', 'pageFaults', 'fsActivity'];
'available_memory', 'pageFaults', 'fsActivity',
'constrained_memory'];
checkForUnknownFields(usage, resourceUsageFields);
assert.strictEqual(typeof usage.userCpuSeconds, 'number');
assert.strictEqual(typeof usage.kernelCpuSeconds, 'number');
Expand All @@ -251,10 +250,7 @@ function _validateContent(report, fields = []) {
assert(typeof usage.free_memory, 'string');
assert(typeof usage.total_memory, 'string');
assert(typeof usage.available_memory, 'string');
// This field may not exsit
if (report.resourceUsage.constrained_memory) {
assert(typeof report.resourceUsage.constrained_memory, 'string');
}
assert(typeof usage.constrained_memory, 'string');
assert(typeof usage.pageFaults === 'object' && usage.pageFaults !== null);
checkForUnknownFields(usage.pageFaults, ['IORequired', 'IONotRequired']);
assert(Number.isSafeInteger(usage.pageFaults.IORequired));
Expand Down
10 changes: 10 additions & 0 deletions test/parallel/test-process-available-memory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';
require('../common');
const assert = require('assert');
const { Worker } = require('worker_threads');
const availableMemory = process.availableMemory();
assert(typeof availableMemory, 'number');
if (!process.env.isWorker) {
process.env.isWorker = true;
new Worker(__filename);
}

0 comments on commit cc188ac

Please sign in to comment.