mirror of
https://github.com/denoland/deno.git
synced 2025-01-11 16:42:21 -05:00
feat(runtime): implement navigator.hardwareConcurrency (#11448)
This commit implements "navigator.hardwareConcurrency" API, which supersedes "Deno.systemCpuInfo()" API (which was removed in this commit).
This commit is contained in:
parent
eece46f0d8
commit
2b13bb6945
15 changed files with 46 additions and 66 deletions
|
@ -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",
|
||||
|
|
23
cli/dts/lib.deno.unstable.d.ts
vendored
23
cli/dts/lib.deno.unstable.d.ts
vendored
|
@ -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.
|
||||
|
|
1
cli/dts/lib.deno.window.d.ts
vendored
1
cli/dts/lib.deno.window.d.ts
vendored
|
@ -37,6 +37,7 @@ declare var sessionStorage: Storage;
|
|||
declare class Navigator {
|
||||
constructor();
|
||||
readonly gpu: GPU;
|
||||
readonly hardwareConcurrency: number;
|
||||
}
|
||||
|
||||
declare var navigator: Navigator;
|
||||
|
|
1
cli/dts/lib.deno.worker.d.ts
vendored
1
cli/dts/lib.deno.worker.d.ts
vendored
|
@ -50,6 +50,7 @@ declare class WorkerGlobalScope extends EventTarget {
|
|||
declare class WorkerNavigator {
|
||||
constructor();
|
||||
readonly gpu: GPU;
|
||||
readonly hardwareConcurrency: number;
|
||||
}
|
||||
|
||||
declare var navigator: WorkerNavigator;
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
|
6
cli/tests/unit/navigator_test.ts
Normal file
6
cli/tests/unit/navigator_test.ts
Normal file
|
@ -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);
|
||||
});
|
|
@ -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);
|
||||
});
|
||||
|
|
|
@ -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 =
|
||||
|
|
|
@ -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,
|
||||
};
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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 })
|
||||
}
|
||||
|
|
|
@ -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 =
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue