1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-22 15:24:46 -05:00

feat(unstable): add Deno.systemCpuInfo() (#7774)

This commit is contained in:
Elias Sjögreen 2020-10-26 15:54:27 +01:00 committed by GitHub
parent d52fb903cd
commit 305a9c04ba
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 54 additions and 0 deletions

View file

@ -74,6 +74,7 @@ const UNSTABLE_DENO_PROPS: &[&str] = &[
"symlink", "symlink",
"symlinkSync", "symlinkSync",
"systemMemoryInfo", "systemMemoryInfo",
"systemCpuInfo",
"transpileOnly", "transpileOnly",
"umask", "umask",
"utime", "utime",

View file

@ -167,6 +167,29 @@ 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.

View file

@ -23,6 +23,7 @@ pub fn init(rt: &mut deno_core::JsRuntime) {
super::reg_json_sync(rt, "op_loadavg", op_loadavg); super::reg_json_sync(rt, "op_loadavg", op_loadavg);
super::reg_json_sync(rt, "op_os_release", op_os_release); super::reg_json_sync(rt, "op_os_release", op_os_release);
super::reg_json_sync(rt, "op_system_memory_info", op_system_memory_info); super::reg_json_sync(rt, "op_system_memory_info", op_system_memory_info);
super::reg_json_sync(rt, "op_system_cpu_info", op_system_cpu_info);
} }
fn op_exec_path( fn op_exec_path(
@ -172,3 +173,20 @@ fn op_system_memory_info(
Err(_) => Ok(json!({})), Err(_) => Ok(json!({})),
} }
} }
fn op_system_cpu_info(
state: &mut OpState,
_args: Value,
_zero_copy: &mut [ZeroCopyBuf],
) -> Result<Value, AnyError> {
super::check_unstable(state, "Deno.systemCpuInfo");
state.borrow::<Permissions>().check_env()?;
let cores = sys_info::cpu_num().ok();
let speed = sys_info::cpu_speed().ok();
Ok(json!({
"cores": cores,
"speed": speed
}))
}

View file

@ -19,6 +19,10 @@
return core.jsonOpSync("op_system_memory_info"); return core.jsonOpSync("op_system_memory_info");
} }
function systemCpuInfo() {
return core.jsonOpSync("op_system_cpu_info");
}
function exit(code = 0) { function exit(code = 0) {
core.jsonOpSync("op_exit", { code }); core.jsonOpSync("op_exit", { code });
throw new Error("Code not reachable"); throw new Error("Code not reachable");
@ -55,6 +59,7 @@
exit, exit,
osRelease, osRelease,
systemMemoryInfo, systemMemoryInfo,
systemCpuInfo,
hostname, hostname,
loadavg, loadavg,
}; };

View file

@ -103,6 +103,7 @@ __bootstrap.denoNsUnstable = {
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,
shutdown: __bootstrap.net.shutdown, shutdown: __bootstrap.net.shutdown,

View file

@ -188,3 +188,9 @@ 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);
});