Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: add uv_get_available_memory to report and process #52023

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -1843,25 +1843,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 @@ -1966,7 +1947,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 @@ -638,13 +638,8 @@ static void PrintResourceUsage(JSONWriter* writer) {
writer->json_keyvalue("constrained_memory", constrained_memory);
}

// See GuessMemoryAvailableToTheProcess
anonrig marked this conversation as resolved.
Show resolved Hide resolved
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');