Skip to content

Commit 78be0d0

Browse files
authored
src: add uv_get_available_memory to report and process
PR-URL: #52023 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz.nizipli@sentry.io> Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
1 parent 4c3e965 commit 78be0d0

File tree

6 files changed

+33
-27
lines changed

6 files changed

+33
-27
lines changed

doc/api/process.md

+17
Original file line numberDiff line numberDiff line change
@@ -1127,6 +1127,22 @@ 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+
(in bytes).
1142+
1143+
See [`uv_get_available_memory`][uv_get_available_memory] for more
1144+
information.
1145+
11301146
## `process.cpuUsage([previousValue])`
11311147

11321148
<!-- YAML
@@ -4026,6 +4042,7 @@ cases:
40264042
[process_warning]: #event-warning
40274043
[report documentation]: report.md
40284044
[terminal raw mode]: tty.md#readstreamsetrawmodemode
4045+
[uv_get_available_memory]: https://docs.libuv.org/en/v1.x/misc.html#c.uv_get_available_memory
40294046
[uv_get_constrained_memory]: https://docs.libuv.org/en/v1.x/misc.html#c.uv_get_constrained_memory
40304047
[uv_rusage_t]: https://docs.libuv.org/en/v1.x/misc.html#c.uv_rusage_t
40314048
[wikipedia_major_fault]: https://en.wikipedia.org/wiki/Page_fault#Major

lib/internal/bootstrap/node.js

+1
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/env.cc

+1-20
Original file line numberDiff line numberDiff line change
@@ -1883,25 +1883,6 @@ void Environment::DeserializeProperties(const EnvSerializeInfo* info) {
18831883
should_abort_on_uncaught_toggle_.Deserialize(ctx);
18841884
}
18851885

1886-
uint64_t GuessMemoryAvailableToTheProcess() {
1887-
uint64_t free_in_system = uv_get_free_memory();
1888-
size_t allowed = uv_get_constrained_memory();
1889-
if (allowed == 0) {
1890-
return free_in_system;
1891-
}
1892-
size_t rss;
1893-
int err = uv_resident_set_memory(&rss);
1894-
if (err) {
1895-
return free_in_system;
1896-
}
1897-
if (allowed < rss) {
1898-
// Something is probably wrong. Fallback to the free memory.
1899-
return free_in_system;
1900-
}
1901-
// There may still be room for swap, but we will just leave it here.
1902-
return allowed - rss;
1903-
}
1904-
19051886
void Environment::BuildEmbedderGraph(Isolate* isolate,
19061887
EmbedderGraph* graph,
19071888
void* data) {
@@ -2006,7 +1987,7 @@ size_t Environment::NearHeapLimitCallback(void* data,
20061987
static_cast<uint64_t>(old_gen_size),
20071988
static_cast<uint64_t>(young_gen_size + old_gen_size));
20081989

2009-
uint64_t available = GuessMemoryAvailableToTheProcess();
1990+
uint64_t available = uv_get_available_memory();
20101991
// TODO(joyeecheung): get a better estimate about the native memory
20111992
// usage into the overhead, e.g. based on the count of objects.
20121993
uint64_t estimated_overhead = max_young_gen_size;

src/node_process_methods.cc

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

+2-7
Original file line numberDiff line numberDiff line change
@@ -641,13 +641,8 @@ static void PrintResourceUsage(JSONWriter* writer) {
641641
writer->json_keyvalue("constrained_memory", constrained_memory);
642642
}
643643

644-
// See GuessMemoryAvailableToTheProcess
645-
if (!err && constrained_memory && constrained_memory >= rss) {
646-
uint64_t available_memory = constrained_memory - rss;
647-
writer->json_keyvalue("available_memory", available_memory);
648-
} else {
649-
writer->json_keyvalue("available_memory", free_memory);
650-
}
644+
uint64_t available_memory = uv_get_available_memory();
645+
writer->json_keyvalue("available_memory", available_memory);
651646

652647
if (uv_getrusage(&rusage) == 0) {
653648
double user_cpu =
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use strict';
2+
require('../common');
3+
const assert = require('assert');
4+
const availableMemory = process.availableMemory();
5+
assert(typeof availableMemory, 'number');

0 commit comments

Comments
 (0)