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

lib, src: add maxmem prop in os module #51102

Closed
wants to merge 1 commit into from
Closed
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
13 changes: 13 additions & 0 deletions doc/api/os.md
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,19 @@ added: v0.3.3

Returns the total amount of system memory in bytes as an integer.

## `os.maxmem()`

<!-- YAML
added:
- REPLACEME
-->

* Returns: {integer}

Returns the total amount of memory available to the process (in bytes) based on
limits imposed by the OS.
If there is no such constraint, or the constraint is unknown, 0 is returned.

## `os.type()`

<!-- YAML
Expand Down
3 changes: 3 additions & 0 deletions lib/os.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const {
getPriority: _getPriority,
getOSInformation: _getOSInformation,
getTotalMem,
getConstrainedMem,
getUserInfo,
getUptime: _getUptime,
isBigEndian,
Expand Down Expand Up @@ -109,6 +110,7 @@ getOSRelease[SymbolToPrimitive] = () => getOSRelease();
getMachine[SymbolToPrimitive] = () => getMachine();
getHomeDirectory[SymbolToPrimitive] = () => getHomeDirectory();
getTotalMem[SymbolToPrimitive] = () => getTotalMem();
getConstrainedMem[SymbolToPrimitive] = () => getConstrainedMem();
getUptime[SymbolToPrimitive] = () => getUptime();

const kEndianness = isBigEndian ? 'BE' : 'LE';
Expand Down Expand Up @@ -388,6 +390,7 @@ module.exports = {
setPriority,
tmpdir,
totalmem: getTotalMem,
maxmem: getConstrainedMem,
type: getOSType,
userInfo,
uptime: getUptime,
Expand Down
6 changes: 6 additions & 0 deletions src/node_os.cc
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ static void GetTotalMemory(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(amount);
}

static void GetConstrainedMemory(const FunctionCallbackInfo<Value>& args) {
double amount = static_cast<double>(uv_get_constrained_memory());
args.GetReturnValue().Set(amount);
}

static void GetUptime(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
Expand Down Expand Up @@ -394,6 +398,7 @@ void Initialize(Local<Object> target,
SetMethod(context, target, "getLoadAvg", GetLoadAvg);
SetMethod(context, target, "getUptime", GetUptime);
SetMethod(context, target, "getTotalMem", GetTotalMemory);
SetMethod(context, target, "getConstrainedMem", GetConstrainedMemory);
SetMethod(context, target, "getFreeMem", GetFreeMemory);
SetMethod(context, target, "getCPUs", GetCPUInfo);
SetMethod(context, target, "getInterfaceAddresses", GetInterfaceAddresses);
Expand All @@ -416,6 +421,7 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(GetLoadAvg);
registry->Register(GetUptime);
registry->Register(GetTotalMemory);
registry->Register(GetConstrainedMemory);
registry->Register(GetFreeMemory);
registry->Register(GetCPUInfo);
registry->Register(GetInterfaceAddresses);
Expand Down
1 change: 1 addition & 0 deletions typings/internalBinding/os.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface OSBinding {
getLoadAvg(array: Float64Array): void;
getUptime(): number;
getTotalMem(): number;
getConstrainedMem(): number;
getFreeMem(): number;
getCPUs(): Array<string | number>;
getInterfaceAddresses(ctx: InternalOSBinding.OSContext): Array<string | number | boolean> | undefined;
Expand Down