diff options
author | Divy Srivastava <dj.srivastava23@gmail.com> | 2021-07-30 01:15:11 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-07-29 21:45:11 +0200 |
commit | 2b13bb694532904704c16bec4e8a47c386e681e2 (patch) | |
tree | 0021131ce86873a5aa965d79b99185b3ca5c5aff | |
parent | eece46f0d85faa90c97841a4f409be39272e809b (diff) |
feat(runtime): implement navigator.hardwareConcurrency (#11448)
This commit implements "navigator.hardwareConcurrency" API, which
supersedes "Deno.systemCpuInfo()" API (which was removed in this commit).
-rw-r--r-- | cli/diagnostics.rs | 2 | ||||
-rw-r--r-- | cli/dts/lib.deno.unstable.d.ts | 23 | ||||
-rw-r--r-- | cli/dts/lib.deno.window.d.ts | 1 | ||||
-rw-r--r-- | cli/dts/lib.deno.worker.d.ts | 1 | ||||
-rw-r--r-- | cli/main.rs | 2 | ||||
-rw-r--r-- | cli/standalone.rs | 1 | ||||
-rw-r--r-- | cli/tests/unit/navigator_test.ts | 6 | ||||
-rw-r--r-- | cli/tests/unit/os_test.ts | 6 | ||||
-rw-r--r-- | runtime/examples/hello_runtime.rs | 1 | ||||
-rw-r--r-- | runtime/js/30_os.js | 10 | ||||
-rw-r--r-- | runtime/js/90_deno_ns.js | 1 | ||||
-rw-r--r-- | runtime/js/99_main.js | 32 | ||||
-rw-r--r-- | runtime/ops/os.rs | 21 | ||||
-rw-r--r-- | runtime/web_worker.rs | 2 | ||||
-rw-r--r-- | runtime/worker.rs | 3 |
15 files changed, 46 insertions, 66 deletions
diff --git a/cli/diagnostics.rs b/cli/diagnostics.rs index da9a090c8..9d1faa6b1 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 199f05631..442da1e53 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 00100768b..e515c19b7 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 7d8f6078b..d35828135 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 bb7e03041..52f2c55af 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 c8918563f..65ca58ec3 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 000000000..10475d973 --- /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 9b0f71352..daa7a1ca2 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 707809037..4883ee7c7 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 afb5aa4e0..f026940ca 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 e4d0b00f2..27900431f 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 a48105559..16a444098 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 80e4995e6..c9567a7d7 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<u32>, - speed: Option<u64>, -} - -fn op_system_cpu_info( - state: &mut OpState, - _args: (), - _: (), -) -> Result<CpuInfo, AnyError> { - super::check_unstable(state, "Deno.systemCpuInfo"); - state.borrow_mut::<Permissions>().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 5724517a0..344bb73c0 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<SharedArrayBufferStore>, + 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 c8c93d2f0..f7287cfbb 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<SharedArrayBufferStore>, + 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) |