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

feat(std): add Deno.totalMems #8312

Closed
wants to merge 3 commits 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
1 change: 1 addition & 0 deletions cli/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ const UNSTABLE_DENO_PROPS: &[&str] = &[
"symlinkSync",
"systemMemoryInfo",
"systemCpuInfo",
"totalMems",
"transpileOnly",
"umask",
"utime",
Expand Down
12 changes: 12 additions & 0 deletions cli/dts/lib.deno.unstable.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,18 @@ declare namespace Deno {
speed: number | undefined;
}

/** **UNSTABLE**: new API, yet to be vetted.
*
* Returns the total physical memory amount
*
* ```ts
* console.log(Deno.totalMems());
* ```
*
* Requires `allow-env` permission.
*/
export function totalMems(): number;

/** **UNSTABLE**: new API, yet to be vetted.
*
* Open and initialize a plugin.
Expand Down
13 changes: 13 additions & 0 deletions cli/ops/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub fn init(rt: &mut deno_core::JsRuntime) {
super::reg_json_sync(rt, "op_os_release", op_os_release);
super::reg_json_sync(rt, "op_system_memory_info", op_system_memory_info);
super::reg_json_sync(rt, "op_system_cpu_info", op_system_cpu_info);
super::reg_json_sync(rt, "op_total_mems", op_total_mems);
}

fn op_exec_path(
Expand Down Expand Up @@ -190,3 +191,15 @@ fn op_system_cpu_info(
"speed": speed
}))
}

fn op_total_mems(
state: &mut OpState,
_args: Value,
_zero_copy: &mut [ZeroCopyBuf],
) -> Result<Value, AnyError> {
super::check_unstable(state, "Deno.totalMems");
state.borrow::<Permissions>().check_env()?;

let total_mems = sys_info::mem_info().unwrap().total;
Ok(json!({ "totalMems": total_mems }))
}
5 changes: 5 additions & 0 deletions cli/rt/30_os.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
return core.jsonOpSync("op_system_memory_info");
}

function totalMems() {
return core.jsonOpSync("op_total_mems");
}

function systemCpuInfo() {
return core.jsonOpSync("op_system_cpu_info");
}
Expand Down Expand Up @@ -61,6 +65,7 @@
systemMemoryInfo,
systemCpuInfo,
hostname,
totalMems,
loadavg,
};
})(this);
1 change: 1 addition & 0 deletions cli/rt/90_deno_ns.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
openPlugin: __bootstrap.plugins.openPlugin,
kill: __bootstrap.process.kill,
setRaw: __bootstrap.tty.setRaw,
totalMems: __bootstrap.os.totalMems,
consoleSize: __bootstrap.tty.consoleSize,
DiagnosticCategory: __bootstrap.diagnostics.DiagnosticCategory,
loadavg: __bootstrap.os.loadavg,
Expand Down
5 changes: 5 additions & 0 deletions cli/tests/unit/os_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,8 @@ unitTest({ perms: { env: true } }, function systemCpuInfo(): void {
assert(cores === undefined || cores > 0);
assert(speed === undefined || speed > 0);
});

unitTest({ perms: { env: true } }, function systemCpuInfo(): void {
const totalMems = Deno.totalMems();
assert(totalMems > 0);
});
4 changes: 2 additions & 2 deletions std/node/os.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,9 @@ export function tmpdir(): string | null {
notImplemented(SEE_GITHUB_ISSUE);
}

/** Not yet implemented */
/** Returns total physical memory. **/
export function totalmem(): number {
notImplemented(SEE_GITHUB_ISSUE);
return Deno.totalMems();
}

/** Not yet implemented */
Expand Down