Skip to content

Commit 5d9416c

Browse files
committed
src: add uv_get_available_memory to report and process
1 parent 1263bb6 commit 5d9416c

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
@@ -1843,25 +1843,6 @@ void Environment::DeserializeProperties(const EnvSerializeInfo* info) {
18431843
should_abort_on_uncaught_toggle_.Deserialize(ctx);
18441844
}
18451845

1846-
uint64_t GuessMemoryAvailableToTheProcess() {
1847-
uint64_t free_in_system = uv_get_free_memory();
1848-
size_t allowed = uv_get_constrained_memory();
1849-
if (allowed == 0) {
1850-
return free_in_system;
1851-
}
1852-
size_t rss;
1853-
int err = uv_resident_set_memory(&rss);
1854-
if (err) {
1855-
return free_in_system;
1856-
}
1857-
if (allowed < rss) {
1858-
// Something is probably wrong. Fallback to the free memory.
1859-
return free_in_system;
1860-
}
1861-
// There may still be room for swap, but we will just leave it here.
1862-
return allowed - rss;
1863-
}
1864-
18651846
void Environment::BuildEmbedderGraph(Isolate* isolate,
18661847
EmbedderGraph* graph,
18671848
void* data) {
@@ -1966,7 +1947,7 @@ size_t Environment::NearHeapLimitCallback(void* data,
19661947
static_cast<uint64_t>(old_gen_size),
19671948
static_cast<uint64_t>(young_gen_size + old_gen_size));
19681949

1969-
uint64_t available = GuessMemoryAvailableToTheProcess();
1950+
uint64_t available = uv_get_available_memory();
19701951
// TODO(joyeecheung): get a better estimate about the native memory
19711952
// usage into the overhead, e.g. based on the count of objects.
19721953
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
@@ -638,13 +638,8 @@ static void PrintResourceUsage(JSONWriter* writer) {
638638
writer->json_keyvalue("constrained_memory", constrained_memory);
639639
}
640640

641-
// See GuessMemoryAvailableToTheProcess
642-
if (!err && constrained_memory && constrained_memory >= rss) {
643-
uint64_t available_memory = constrained_memory - rss;
644-
writer->json_keyvalue("available_memory", available_memory);
645-
} else {
646-
writer->json_keyvalue("available_memory", free_memory);
647-
}
641+
uint64_t available_memory = uv_get_available_memory();
642+
writer->json_keyvalue("available_memory", available_memory);
648643

649644
if (uv_getrusage(&rusage) == 0) {
650645
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)