src: add uv_get_available_memory to report and process

PR-URL: https://github.com/nodejs/node/pull/52023
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz.nizipli@sentry.io>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
This commit is contained in:
theanarkh 2024-03-13 12:06:49 +08:00 committed by GitHub
parent 4c3e9659ed
commit 78be0d0f1c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 33 additions and 27 deletions

View File

@ -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
@ -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

View File

@ -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;

View File

@ -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) {
@ -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;

View File

@ -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");
@ -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);
@ -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);

View File

@ -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 =

View File

@ -0,0 +1,5 @@
'use strict';
require('../common');
const assert = require('assert');
const availableMemory = process.availableMemory();
assert(typeof availableMemory, 'number');