mirror of
https://github.com/denoland/deno.git
synced 2025-01-12 00:54:02 -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",
|
"Signal",
|
||||||
"SignalStream",
|
"SignalStream",
|
||||||
"StartTlsOptions",
|
"StartTlsOptions",
|
||||||
"SystemCpuInfo",
|
|
||||||
"SystemMemoryInfo",
|
"SystemMemoryInfo",
|
||||||
"UnixConnectOptions",
|
"UnixConnectOptions",
|
||||||
"UnixListenOptions",
|
"UnixListenOptions",
|
||||||
|
@ -68,7 +67,6 @@ const UNSTABLE_DENO_PROPS: &[&str] = &[
|
||||||
"signals",
|
"signals",
|
||||||
"sleepSync",
|
"sleepSync",
|
||||||
"startTls",
|
"startTls",
|
||||||
"systemCpuInfo",
|
|
||||||
"systemMemoryInfo",
|
"systemMemoryInfo",
|
||||||
"umask",
|
"umask",
|
||||||
"utime",
|
"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;
|
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.
|
/** **UNSTABLE**: new API, yet to be vetted.
|
||||||
*
|
*
|
||||||
* Open and initialize a plugin.
|
* 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 {
|
declare class Navigator {
|
||||||
constructor();
|
constructor();
|
||||||
readonly gpu: GPU;
|
readonly gpu: GPU;
|
||||||
|
readonly hardwareConcurrency: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
declare var navigator: Navigator;
|
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 {
|
declare class WorkerNavigator {
|
||||||
constructor();
|
constructor();
|
||||||
readonly gpu: GPU;
|
readonly gpu: GPU;
|
||||||
|
readonly hardwareConcurrency: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
declare var navigator: WorkerNavigator;
|
declare var navigator: WorkerNavigator;
|
||||||
|
|
|
@ -126,6 +126,7 @@ fn create_web_worker_callback(
|
||||||
shared_array_buffer_store: Some(
|
shared_array_buffer_store: Some(
|
||||||
program_state.shared_array_buffer_store.clone(),
|
program_state.shared_array_buffer_store.clone(),
|
||||||
),
|
),
|
||||||
|
cpu_count: num_cpus::get(),
|
||||||
};
|
};
|
||||||
|
|
||||||
let (mut worker, external_handle) = WebWorker::from_options(
|
let (mut worker, external_handle) = WebWorker::from_options(
|
||||||
|
@ -215,6 +216,7 @@ pub fn create_main_worker(
|
||||||
shared_array_buffer_store: Some(
|
shared_array_buffer_store: Some(
|
||||||
program_state.shared_array_buffer_store.clone(),
|
program_state.shared_array_buffer_store.clone(),
|
||||||
),
|
),
|
||||||
|
cpu_count: num_cpus::get(),
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut worker = MainWorker::from_options(main_module, permissions, &options);
|
let mut worker = MainWorker::from_options(main_module, permissions, &options);
|
||||||
|
|
|
@ -249,6 +249,7 @@ pub async fn run(
|
||||||
blob_store,
|
blob_store,
|
||||||
broadcast_channel,
|
broadcast_channel,
|
||||||
shared_array_buffer_store: None,
|
shared_array_buffer_store: None,
|
||||||
|
cpu_count: num_cpus::get(),
|
||||||
};
|
};
|
||||||
let mut worker =
|
let mut worker =
|
||||||
MainWorker::from_options(main_module.clone(), permissions, &options);
|
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.swapTotal >= 0);
|
||||||
assert(info.swapFree >= 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(),
|
blob_store: BlobStore::default(),
|
||||||
broadcast_channel: InMemoryBroadcastChannel::default(),
|
broadcast_channel: InMemoryBroadcastChannel::default(),
|
||||||
shared_array_buffer_store: None,
|
shared_array_buffer_store: None,
|
||||||
|
cpu_count: 1,
|
||||||
};
|
};
|
||||||
|
|
||||||
let js_path =
|
let js_path =
|
||||||
|
|
|
@ -24,15 +24,6 @@
|
||||||
return core.opSync("op_system_memory_info");
|
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
|
// This is an internal only method used by the test harness to override the
|
||||||
// behavior of exit when the exit sanitizer is enabled.
|
// behavior of exit when the exit sanitizer is enabled.
|
||||||
let exitHandler = null;
|
let exitHandler = null;
|
||||||
|
@ -89,7 +80,6 @@
|
||||||
exit,
|
exit,
|
||||||
osRelease,
|
osRelease,
|
||||||
systemMemoryInfo,
|
systemMemoryInfo,
|
||||||
systemCpuInfo,
|
|
||||||
hostname,
|
hostname,
|
||||||
loadavg,
|
loadavg,
|
||||||
};
|
};
|
||||||
|
|
|
@ -118,7 +118,6 @@
|
||||||
hostname: __bootstrap.os.hostname,
|
hostname: __bootstrap.os.hostname,
|
||||||
osRelease: __bootstrap.os.osRelease,
|
osRelease: __bootstrap.os.osRelease,
|
||||||
systemMemoryInfo: __bootstrap.os.systemMemoryInfo,
|
systemMemoryInfo: __bootstrap.os.systemMemoryInfo,
|
||||||
systemCpuInfo: __bootstrap.os.systemCpuInfo,
|
|
||||||
applySourceMap: __bootstrap.errorStack.opApplySourceMap,
|
applySourceMap: __bootstrap.errorStack.opApplySourceMap,
|
||||||
formatDiagnostics: __bootstrap.errorStack.opFormatDiagnostics,
|
formatDiagnostics: __bootstrap.errorStack.opFormatDiagnostics,
|
||||||
sleepSync: __bootstrap.timers.sleepSync,
|
sleepSync: __bootstrap.timers.sleepSync,
|
||||||
|
|
|
@ -247,6 +247,8 @@ delete Object.prototype.__proto__;
|
||||||
|
|
||||||
const navigator = webidl.createBranded(Navigator);
|
const navigator = webidl.createBranded(Navigator);
|
||||||
|
|
||||||
|
let numCpus;
|
||||||
|
|
||||||
ObjectDefineProperties(Navigator.prototype, {
|
ObjectDefineProperties(Navigator.prototype, {
|
||||||
gpu: {
|
gpu: {
|
||||||
configurable: true,
|
configurable: true,
|
||||||
|
@ -256,6 +258,14 @@ delete Object.prototype.__proto__;
|
||||||
return webgpu.gpu;
|
return webgpu.gpu;
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
hardwareConcurrency: {
|
||||||
|
configurable: true,
|
||||||
|
enumerable: true,
|
||||||
|
get() {
|
||||||
|
webidl.assertBranded(this, Navigator);
|
||||||
|
return numCpus;
|
||||||
|
},
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
class WorkerNavigator {
|
class WorkerNavigator {
|
||||||
|
@ -279,6 +289,14 @@ delete Object.prototype.__proto__;
|
||||||
return webgpu.gpu;
|
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
|
// https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope
|
||||||
|
@ -491,12 +509,13 @@ delete Object.prototype.__proto__;
|
||||||
pid,
|
pid,
|
||||||
ppid,
|
ppid,
|
||||||
unstableFlag,
|
unstableFlag,
|
||||||
|
cpuCount,
|
||||||
} = runtimeOptions;
|
} = runtimeOptions;
|
||||||
|
|
||||||
if (locationHref != null) {
|
if (locationHref != null) {
|
||||||
location.setLocationHref(locationHref);
|
location.setLocationHref(locationHref);
|
||||||
}
|
}
|
||||||
|
numCpus = cpuCount;
|
||||||
registerErrors();
|
registerErrors();
|
||||||
|
|
||||||
const internalSymbol = Symbol("Deno.internal");
|
const internalSymbol = Symbol("Deno.internal");
|
||||||
|
@ -566,10 +585,17 @@ delete Object.prototype.__proto__;
|
||||||
runtimeOptions,
|
runtimeOptions,
|
||||||
internalName ?? name,
|
internalName ?? name,
|
||||||
);
|
);
|
||||||
const { unstableFlag, pid, noColor, args, location: locationHref } =
|
const {
|
||||||
runtimeOptions;
|
unstableFlag,
|
||||||
|
pid,
|
||||||
|
noColor,
|
||||||
|
args,
|
||||||
|
location: locationHref,
|
||||||
|
cpuCount,
|
||||||
|
} = runtimeOptions;
|
||||||
|
|
||||||
location.setLocationHref(locationHref);
|
location.setLocationHref(locationHref);
|
||||||
|
numCpus = cpuCount;
|
||||||
registerErrors();
|
registerErrors();
|
||||||
|
|
||||||
pollForMessages();
|
pollForMessages();
|
||||||
|
|
|
@ -25,7 +25,6 @@ pub fn init() -> Extension {
|
||||||
("op_loadavg", op_sync(op_loadavg)),
|
("op_loadavg", op_sync(op_loadavg)),
|
||||||
("op_os_release", op_sync(op_os_release)),
|
("op_os_release", op_sync(op_os_release)),
|
||||||
("op_system_memory_info", op_sync(op_system_memory_info)),
|
("op_system_memory_info", op_sync(op_system_memory_info)),
|
||||||
("op_system_cpu_info", op_sync(op_system_cpu_info)),
|
|
||||||
])
|
])
|
||||||
.build()
|
.build()
|
||||||
}
|
}
|
||||||
|
@ -180,23 +179,3 @@ fn op_system_memory_info(
|
||||||
Err(_) => Ok(None),
|
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 blob_store: BlobStore,
|
||||||
pub broadcast_channel: InMemoryBroadcastChannel,
|
pub broadcast_channel: InMemoryBroadcastChannel,
|
||||||
pub shared_array_buffer_store: Option<SharedArrayBufferStore>,
|
pub shared_array_buffer_store: Option<SharedArrayBufferStore>,
|
||||||
|
pub cpu_count: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl WebWorker {
|
impl WebWorker {
|
||||||
|
@ -412,6 +413,7 @@ impl WebWorker {
|
||||||
"unstableFlag": options.unstable,
|
"unstableFlag": options.unstable,
|
||||||
"v8Version": deno_core::v8_version(),
|
"v8Version": deno_core::v8_version(),
|
||||||
"location": self.main_module,
|
"location": self.main_module,
|
||||||
|
"cpuCount": options.cpu_count,
|
||||||
});
|
});
|
||||||
|
|
||||||
let runtime_options_str =
|
let runtime_options_str =
|
||||||
|
|
|
@ -72,6 +72,7 @@ pub struct WorkerOptions {
|
||||||
pub blob_store: BlobStore,
|
pub blob_store: BlobStore,
|
||||||
pub broadcast_channel: InMemoryBroadcastChannel,
|
pub broadcast_channel: InMemoryBroadcastChannel,
|
||||||
pub shared_array_buffer_store: Option<SharedArrayBufferStore>,
|
pub shared_array_buffer_store: Option<SharedArrayBufferStore>,
|
||||||
|
pub cpu_count: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MainWorker {
|
impl MainWorker {
|
||||||
|
@ -178,6 +179,7 @@ impl MainWorker {
|
||||||
"unstableFlag": options.unstable,
|
"unstableFlag": options.unstable,
|
||||||
"v8Version": deno_core::v8_version(),
|
"v8Version": deno_core::v8_version(),
|
||||||
"location": options.location,
|
"location": options.location,
|
||||||
|
"cpuCount": options.cpu_count,
|
||||||
});
|
});
|
||||||
|
|
||||||
let script = format!(
|
let script = format!(
|
||||||
|
@ -309,6 +311,7 @@ mod tests {
|
||||||
blob_store: BlobStore::default(),
|
blob_store: BlobStore::default(),
|
||||||
broadcast_channel: InMemoryBroadcastChannel::default(),
|
broadcast_channel: InMemoryBroadcastChannel::default(),
|
||||||
shared_array_buffer_store: None,
|
shared_array_buffer_store: None,
|
||||||
|
cpu_count: 1,
|
||||||
};
|
};
|
||||||
|
|
||||||
MainWorker::from_options(main_module, permissions, &options)
|
MainWorker::from_options(main_module, permissions, &options)
|
||||||
|
|
Loading…
Reference in a new issue