From 2b13bb694532904704c16bec4e8a47c386e681e2 Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Fri, 30 Jul 2021 01:15:11 +0530 Subject: [PATCH] feat(runtime): implement navigator.hardwareConcurrency (#11448) This commit implements "navigator.hardwareConcurrency" API, which supersedes "Deno.systemCpuInfo()" API (which was removed in this commit). --- cli/diagnostics.rs | 2 -- cli/dts/lib.deno.unstable.d.ts | 23 ---------------------- cli/dts/lib.deno.window.d.ts | 1 + cli/dts/lib.deno.worker.d.ts | 1 + cli/main.rs | 2 ++ cli/standalone.rs | 1 + cli/tests/unit/navigator_test.ts | 6 ++++++ cli/tests/unit/os_test.ts | 6 ------ runtime/examples/hello_runtime.rs | 1 + runtime/js/30_os.js | 10 ---------- runtime/js/90_deno_ns.js | 1 - runtime/js/99_main.js | 32 ++++++++++++++++++++++++++++--- runtime/ops/os.rs | 21 -------------------- runtime/web_worker.rs | 2 ++ runtime/worker.rs | 3 +++ 15 files changed, 46 insertions(+), 66 deletions(-) create mode 100644 cli/tests/unit/navigator_test.ts diff --git a/cli/diagnostics.rs b/cli/diagnostics.rs index da9a090c89..9d1faa6b18 100644 --- a/cli/diagnostics.rs +++ b/cli/diagnostics.rs @@ -40,7 +40,6 @@ const UNSTABLE_DENO_PROPS: &[&str] = &[ "Signal", "SignalStream", "StartTlsOptions", - "SystemCpuInfo", "SystemMemoryInfo", "UnixConnectOptions", "UnixListenOptions", @@ -68,7 +67,6 @@ const UNSTABLE_DENO_PROPS: &[&str] = &[ "signals", "sleepSync", "startTls", - "systemCpuInfo", "systemMemoryInfo", "umask", "utime", diff --git a/cli/dts/lib.deno.unstable.d.ts b/cli/dts/lib.deno.unstable.d.ts index 199f05631c..442da1e539 100644 --- a/cli/dts/lib.deno.unstable.d.ts +++ b/cli/dts/lib.deno.unstable.d.ts @@ -107,29 +107,6 @@ 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/dts/lib.deno.window.d.ts b/cli/dts/lib.deno.window.d.ts index 00100768b5..e515c19b71 100644 --- a/cli/dts/lib.deno.window.d.ts +++ b/cli/dts/lib.deno.window.d.ts @@ -37,6 +37,7 @@ declare var sessionStorage: Storage; declare class Navigator { constructor(); readonly gpu: GPU; + readonly hardwareConcurrency: number; } declare var navigator: Navigator; diff --git a/cli/dts/lib.deno.worker.d.ts b/cli/dts/lib.deno.worker.d.ts index 7d8f6078b1..d35828135c 100644 --- a/cli/dts/lib.deno.worker.d.ts +++ b/cli/dts/lib.deno.worker.d.ts @@ -50,6 +50,7 @@ declare class WorkerGlobalScope extends EventTarget { declare class WorkerNavigator { constructor(); readonly gpu: GPU; + readonly hardwareConcurrency: number; } declare var navigator: WorkerNavigator; diff --git a/cli/main.rs b/cli/main.rs index bb7e03041e..52f2c55af8 100644 --- a/cli/main.rs +++ b/cli/main.rs @@ -126,6 +126,7 @@ fn create_web_worker_callback( shared_array_buffer_store: Some( program_state.shared_array_buffer_store.clone(), ), + cpu_count: num_cpus::get(), }; let (mut worker, external_handle) = WebWorker::from_options( @@ -215,6 +216,7 @@ pub fn create_main_worker( shared_array_buffer_store: Some( program_state.shared_array_buffer_store.clone(), ), + cpu_count: num_cpus::get(), }; let mut worker = MainWorker::from_options(main_module, permissions, &options); diff --git a/cli/standalone.rs b/cli/standalone.rs index c8918563f9..65ca58ec39 100644 --- a/cli/standalone.rs +++ b/cli/standalone.rs @@ -249,6 +249,7 @@ pub async fn run( blob_store, broadcast_channel, shared_array_buffer_store: None, + cpu_count: num_cpus::get(), }; let mut worker = MainWorker::from_options(main_module.clone(), permissions, &options); diff --git a/cli/tests/unit/navigator_test.ts b/cli/tests/unit/navigator_test.ts new file mode 100644 index 0000000000..10475d9733 --- /dev/null +++ b/cli/tests/unit/navigator_test.ts @@ -0,0 +1,6 @@ +// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. +import { assert, unitTest } from "./test_util.ts"; + +unitTest(function navigatorNumCpus(): void { + assert(navigator.hardwareConcurrency > 0); +}); diff --git a/cli/tests/unit/os_test.ts b/cli/tests/unit/os_test.ts index 9b0f71352b..daa7a1ca27 100644 --- a/cli/tests/unit/os_test.ts +++ b/cli/tests/unit/os_test.ts @@ -203,9 +203,3 @@ 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); -}); diff --git a/runtime/examples/hello_runtime.rs b/runtime/examples/hello_runtime.rs index 7078090373..4883ee7c72 100644 --- a/runtime/examples/hello_runtime.rs +++ b/runtime/examples/hello_runtime.rs @@ -44,6 +44,7 @@ async fn main() -> Result<(), AnyError> { blob_store: BlobStore::default(), broadcast_channel: InMemoryBroadcastChannel::default(), shared_array_buffer_store: None, + cpu_count: 1, }; let js_path = diff --git a/runtime/js/30_os.js b/runtime/js/30_os.js index afb5aa4e0e..f026940ca5 100644 --- a/runtime/js/30_os.js +++ b/runtime/js/30_os.js @@ -24,15 +24,6 @@ return core.opSync("op_system_memory_info"); } - function systemCpuInfo() { - const { cores, speed } = core.opSync("op_system_cpu_info"); - // Map nulls to undefined for compatibility - return { - cores: cores ?? undefined, - speed: speed ?? undefined, - }; - } - // This is an internal only method used by the test harness to override the // behavior of exit when the exit sanitizer is enabled. let exitHandler = null; @@ -89,7 +80,6 @@ exit, osRelease, systemMemoryInfo, - systemCpuInfo, hostname, loadavg, }; diff --git a/runtime/js/90_deno_ns.js b/runtime/js/90_deno_ns.js index e4d0b00f25..27900431f1 100644 --- a/runtime/js/90_deno_ns.js +++ b/runtime/js/90_deno_ns.js @@ -118,7 +118,6 @@ hostname: __bootstrap.os.hostname, osRelease: __bootstrap.os.osRelease, systemMemoryInfo: __bootstrap.os.systemMemoryInfo, - systemCpuInfo: __bootstrap.os.systemCpuInfo, applySourceMap: __bootstrap.errorStack.opApplySourceMap, formatDiagnostics: __bootstrap.errorStack.opFormatDiagnostics, sleepSync: __bootstrap.timers.sleepSync, diff --git a/runtime/js/99_main.js b/runtime/js/99_main.js index a48105559d..16a444098a 100644 --- a/runtime/js/99_main.js +++ b/runtime/js/99_main.js @@ -247,6 +247,8 @@ delete Object.prototype.__proto__; const navigator = webidl.createBranded(Navigator); + let numCpus; + ObjectDefineProperties(Navigator.prototype, { gpu: { configurable: true, @@ -256,6 +258,14 @@ delete Object.prototype.__proto__; return webgpu.gpu; }, }, + hardwareConcurrency: { + configurable: true, + enumerable: true, + get() { + webidl.assertBranded(this, Navigator); + return numCpus; + }, + }, }); class WorkerNavigator { @@ -279,6 +289,14 @@ delete Object.prototype.__proto__; return webgpu.gpu; }, }, + hardwareConcurrency: { + configurable: true, + enumerable: true, + get() { + webidl.assertBranded(this, Navigator); + return numCpus; + }, + }, }); // https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope @@ -491,12 +509,13 @@ delete Object.prototype.__proto__; pid, ppid, unstableFlag, + cpuCount, } = runtimeOptions; if (locationHref != null) { location.setLocationHref(locationHref); } - + numCpus = cpuCount; registerErrors(); const internalSymbol = Symbol("Deno.internal"); @@ -566,10 +585,17 @@ delete Object.prototype.__proto__; runtimeOptions, internalName ?? name, ); - const { unstableFlag, pid, noColor, args, location: locationHref } = - runtimeOptions; + const { + unstableFlag, + pid, + noColor, + args, + location: locationHref, + cpuCount, + } = runtimeOptions; location.setLocationHref(locationHref); + numCpus = cpuCount; registerErrors(); pollForMessages(); diff --git a/runtime/ops/os.rs b/runtime/ops/os.rs index 80e4995e6b..c9567a7d7d 100644 --- a/runtime/ops/os.rs +++ b/runtime/ops/os.rs @@ -25,7 +25,6 @@ pub fn init() -> Extension { ("op_loadavg", op_sync(op_loadavg)), ("op_os_release", op_sync(op_os_release)), ("op_system_memory_info", op_sync(op_system_memory_info)), - ("op_system_cpu_info", op_sync(op_system_cpu_info)), ]) .build() } @@ -180,23 +179,3 @@ fn op_system_memory_info( Err(_) => Ok(None), } } - -#[derive(Serialize)] -struct CpuInfo { - cores: Option, - speed: Option, -} - -fn op_system_cpu_info( - state: &mut OpState, - _args: (), - _: (), -) -> Result { - super::check_unstable(state, "Deno.systemCpuInfo"); - state.borrow_mut::().env.check_all()?; - - let cores = sys_info::cpu_num().ok(); - let speed = sys_info::cpu_speed().ok(); - - Ok(CpuInfo { cores, speed }) -} diff --git a/runtime/web_worker.rs b/runtime/web_worker.rs index 5724517a00..344bb73c04 100644 --- a/runtime/web_worker.rs +++ b/runtime/web_worker.rs @@ -271,6 +271,7 @@ pub struct WebWorkerOptions { pub blob_store: BlobStore, pub broadcast_channel: InMemoryBroadcastChannel, pub shared_array_buffer_store: Option, + pub cpu_count: usize, } impl WebWorker { @@ -412,6 +413,7 @@ impl WebWorker { "unstableFlag": options.unstable, "v8Version": deno_core::v8_version(), "location": self.main_module, + "cpuCount": options.cpu_count, }); let runtime_options_str = diff --git a/runtime/worker.rs b/runtime/worker.rs index c8c93d2f01..f7287cfbb8 100644 --- a/runtime/worker.rs +++ b/runtime/worker.rs @@ -72,6 +72,7 @@ pub struct WorkerOptions { pub blob_store: BlobStore, pub broadcast_channel: InMemoryBroadcastChannel, pub shared_array_buffer_store: Option, + pub cpu_count: usize, } impl MainWorker { @@ -178,6 +179,7 @@ impl MainWorker { "unstableFlag": options.unstable, "v8Version": deno_core::v8_version(), "location": options.location, + "cpuCount": options.cpu_count, }); let script = format!( @@ -309,6 +311,7 @@ mod tests { blob_store: BlobStore::default(), broadcast_channel: InMemoryBroadcastChannel::default(), shared_array_buffer_store: None, + cpu_count: 1, }; MainWorker::from_options(main_module, permissions, &options)