diff --git a/cli/diagnostics.rs b/cli/diagnostics.rs index ba21e5aa95ac09..57c140bda0484f 100644 --- a/cli/diagnostics.rs +++ b/cli/diagnostics.rs @@ -77,6 +77,7 @@ const UNSTABLE_DENO_PROPS: &[&str] = &[ "symlinkSync", "systemMemoryInfo", "systemCpuInfo", + "totalMems", "transpileOnly", "umask", "utime", diff --git a/cli/dts/lib.deno.unstable.d.ts b/cli/dts/lib.deno.unstable.d.ts index 6eb39c21c4a20b..fa8a74ed5fd7c6 100644 --- a/cli/dts/lib.deno.unstable.d.ts +++ b/cli/dts/lib.deno.unstable.d.ts @@ -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. diff --git a/cli/ops/os.rs b/cli/ops/os.rs index 6fd404a2365323..1d4f83f3d51484 100644 --- a/cli/ops/os.rs +++ b/cli/ops/os.rs @@ -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( @@ -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 { + super::check_unstable(state, "Deno.totalMems"); + state.borrow::().check_env()?; + + let total_mems = sys_info::mem_info().unwrap().total; + Ok(json!({ "totalMems": total_mems })) +} diff --git a/cli/rt/30_os.js b/cli/rt/30_os.js index ebc4e89164743b..d03f32f9754fad 100644 --- a/cli/rt/30_os.js +++ b/cli/rt/30_os.js @@ -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"); } @@ -61,6 +65,7 @@ systemMemoryInfo, systemCpuInfo, hostname, + totalMems, loadavg, }; })(this); diff --git a/cli/rt/90_deno_ns.js b/cli/rt/90_deno_ns.js index 9188788ec75f16..76f9f8e4e8137f 100644 --- a/cli/rt/90_deno_ns.js +++ b/cli/rt/90_deno_ns.js @@ -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, diff --git a/cli/tests/unit/os_test.ts b/cli/tests/unit/os_test.ts index 4e12ddca3c7aad..4927e7faa27330 100644 --- a/cli/tests/unit/os_test.ts +++ b/cli/tests/unit/os_test.ts @@ -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); +}); diff --git a/std/node/os.ts b/std/node/os.ts index 773cca8bdcbe8c..8f478c2467e102 100644 --- a/std/node/os.ts +++ b/std/node/os.ts @@ -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 */