diff --git a/cli/diagnostics.rs b/cli/diagnostics.rs index b92388322f..ba53ce6233 100644 --- a/cli/diagnostics.rs +++ b/cli/diagnostics.rs @@ -74,6 +74,7 @@ const UNSTABLE_DENO_PROPS: &[&str] = &[ "symlink", "symlinkSync", "systemMemoryInfo", + "systemCpuInfo", "transpileOnly", "umask", "utime", diff --git a/cli/dts/lib.deno.unstable.d.ts b/cli/dts/lib.deno.unstable.d.ts index 0bd4c7474e..78a7e79c2e 100644 --- a/cli/dts/lib.deno.unstable.d.ts +++ b/cli/dts/lib.deno.unstable.d.ts @@ -167,6 +167,29 @@ declare namespace Deno { swapFree: number; } + /** **Unstable** new API. yet to be vetted. + * + * Returns the total number of logical cpus in the system along with + * the speed measured in MHz. If either the syscall to get the core + * count or speed of the cpu is unsuccessful the value of the it + * is undefined. + * + * ```ts + * console.log(Deno.systemCpuInfo()); + * ``` + * + * Requires `allow-env` permission. + * + */ + export function systemCpuInfo(): SystemCpuInfo; + + export interface SystemCpuInfo { + /** Total number of logical cpus in the system */ + cores: number | undefined; + /** The speed of the cpu measured in MHz */ + speed: number | undefined; + } + /** **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 5265d4d086..6fd404a236 100644 --- a/cli/ops/os.rs +++ b/cli/ops/os.rs @@ -23,6 +23,7 @@ pub fn init(rt: &mut deno_core::JsRuntime) { super::reg_json_sync(rt, "op_loadavg", op_loadavg); 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); } fn op_exec_path( @@ -172,3 +173,20 @@ fn op_system_memory_info( Err(_) => Ok(json!({})), } } + +fn op_system_cpu_info( + state: &mut OpState, + _args: Value, + _zero_copy: &mut [ZeroCopyBuf], +) -> Result { + super::check_unstable(state, "Deno.systemCpuInfo"); + state.borrow::().check_env()?; + + let cores = sys_info::cpu_num().ok(); + let speed = sys_info::cpu_speed().ok(); + + Ok(json!({ + "cores": cores, + "speed": speed + })) +} diff --git a/cli/rt/30_os.js b/cli/rt/30_os.js index 892d3bf8bb..ebc4e89164 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 systemCpuInfo() { + return core.jsonOpSync("op_system_cpu_info"); + } + function exit(code = 0) { core.jsonOpSync("op_exit", { code }); throw new Error("Code not reachable"); @@ -55,6 +59,7 @@ exit, osRelease, systemMemoryInfo, + systemCpuInfo, hostname, loadavg, }; diff --git a/cli/rt/90_deno_ns.js b/cli/rt/90_deno_ns.js index 7e8598923a..9667b47c8a 100644 --- a/cli/rt/90_deno_ns.js +++ b/cli/rt/90_deno_ns.js @@ -103,6 +103,7 @@ __bootstrap.denoNsUnstable = { hostname: __bootstrap.os.hostname, osRelease: __bootstrap.os.osRelease, systemMemoryInfo: __bootstrap.os.systemMemoryInfo, + systemCpuInfo: __bootstrap.os.systemCpuInfo, applySourceMap: __bootstrap.errorStack.opApplySourceMap, formatDiagnostics: __bootstrap.errorStack.opFormatDiagnostics, shutdown: __bootstrap.net.shutdown, diff --git a/cli/tests/unit/os_test.ts b/cli/tests/unit/os_test.ts index a6eb3a760c..4e12ddca3c 100644 --- a/cli/tests/unit/os_test.ts +++ b/cli/tests/unit/os_test.ts @@ -188,3 +188,9 @@ unitTest({ perms: { env: true } }, function systemMemoryInfo(): void { assert(info.swapTotal >= 0); assert(info.swapFree >= 0); }); + +unitTest({ perms: { env: true } }, function systemCpuInfo(): void { + const { cores, speed } = Deno.systemCpuInfo(); + assert(cores === undefined || cores > 0); + assert(speed === undefined || speed > 0); +});