From 2b13bb694532904704c16bec4e8a47c386e681e2 Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Fri, 30 Jul 2021 01:15:11 +0530 Subject: feat(runtime): implement navigator.hardwareConcurrency (#11448) This commit implements "navigator.hardwareConcurrency" API, which supersedes "Deno.systemCpuInfo()" API (which was removed in this commit). --- 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 +++ 7 files changed, 35 insertions(+), 35 deletions(-) (limited to 'runtime') 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, - 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 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, + 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, + 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) -- cgit v1.2.3