Skip to content

Commit

Permalink
feat(unstable): add Deno.systemCpuInfo() (denoland#7774)
Browse files Browse the repository at this point in the history
  • Loading branch information
eliassjogreen committed Oct 26, 2020
1 parent d52fb90 commit 305a9c0
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions cli/diagnostics.rs
Expand Up @@ -74,6 +74,7 @@ const UNSTABLE_DENO_PROPS: &[&str] = &[
"symlink",
"symlinkSync",
"systemMemoryInfo",
"systemCpuInfo",
"transpileOnly",
"umask",
"utime",
Expand Down
23 changes: 23 additions & 0 deletions cli/dts/lib.deno.unstable.d.ts
Expand Up @@ -167,6 +167,29 @@ 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.
Expand Down
18 changes: 18 additions & 0 deletions cli/ops/os.rs
Expand Up @@ -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_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_cpu_info", op_system_cpu_info);
}

fn op_exec_path(
Expand Down Expand Up @@ -172,3 +173,20 @@ fn op_system_memory_info(
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
}))
}
5 changes: 5 additions & 0 deletions cli/rt/30_os.js
Expand Up @@ -19,6 +19,10 @@
return core.jsonOpSync("op_system_memory_info");
}

function systemCpuInfo() {
return core.jsonOpSync("op_system_cpu_info");
}

function exit(code = 0) {
core.jsonOpSync("op_exit", { code });
throw new Error("Code not reachable");
Expand Down Expand Up @@ -55,6 +59,7 @@
exit,
osRelease,
systemMemoryInfo,
systemCpuInfo,
hostname,
loadavg,
};
Expand Down
1 change: 1 addition & 0 deletions cli/rt/90_deno_ns.js
Expand Up @@ -103,6 +103,7 @@ __bootstrap.denoNsUnstable = {
hostname: __bootstrap.os.hostname,
osRelease: __bootstrap.os.osRelease,
systemMemoryInfo: __bootstrap.os.systemMemoryInfo,
systemCpuInfo: __bootstrap.os.systemCpuInfo,
applySourceMap: __bootstrap.errorStack.opApplySourceMap,
formatDiagnostics: __bootstrap.errorStack.opFormatDiagnostics,
shutdown: __bootstrap.net.shutdown,
Expand Down
6 changes: 6 additions & 0 deletions cli/tests/unit/os_test.ts
Expand Up @@ -188,3 +188,9 @@ 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);
});

0 comments on commit 305a9c0

Please sign in to comment.