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
PR-URL: nodejs#52023
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Yagiz Nizipli <[email protected]>
Reviewed-By: Chengzhong Wu <[email protected]>
  • Loading branch information
theanarkh authored Mar 13, 2024
1 parent 4c3e965 commit 78be0d0
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 27 deletions.
17 changes: 17 additions & 0 deletions doc/api/process.md
Original file line number Diff line number Diff line change
Expand Up @@ -1127,6 +1127,22 @@ 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
(in bytes).

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

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

<!-- YAML
Expand Down Expand Up @@ -4026,6 +4042,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
21 changes: 1 addition & 20 deletions src/env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1883,25 +1883,6 @@ void Environment::DeserializeProperties(const EnvSerializeInfo* info) {
should_abort_on_uncaught_toggle_.Deserialize(ctx);
}

uint64_t GuessMemoryAvailableToTheProcess() {
uint64_t free_in_system = uv_get_free_memory();
size_t allowed = uv_get_constrained_memory();
if (allowed == 0) {
return free_in_system;
}
size_t rss;
int err = uv_resident_set_memory(&rss);
if (err) {
return free_in_system;
}
if (allowed < rss) {
// Something is probably wrong. Fallback to the free memory.
return free_in_system;
}
// There may still be room for swap, but we will just leave it here.
return allowed - rss;
}

void Environment::BuildEmbedderGraph(Isolate* isolate,
EmbedderGraph* graph,
void* data) {
Expand Down Expand Up @@ -2006,7 +1987,7 @@ size_t Environment::NearHeapLimitCallback(void* data,
static_cast<uint64_t>(old_gen_size),
static_cast<uint64_t>(young_gen_size + old_gen_size));

uint64_t available = GuessMemoryAvailableToTheProcess();
uint64_t available = uv_get_available_memory();
// TODO(joyeecheung): get a better estimate about the native memory
// usage into the overhead, e.g. based on the count of objects.
uint64_t estimated_overhead = max_young_gen_size;
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
9 changes: 2 additions & 7 deletions src/node_report.cc
Original file line number Diff line number Diff line change
Expand Up @@ -641,13 +641,8 @@ static void PrintResourceUsage(JSONWriter* writer) {
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
5 changes: 5 additions & 0 deletions test/parallel/test-process-available-memory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';
require('../common');
const assert = require('assert');
const availableMemory = process.availableMemory();
assert(typeof availableMemory, 'number');

0 comments on commit 78be0d0

Please sign in to comment.