Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli) add Deno.systemMemoryInfo() #7350

Merged
merged 16 commits into from
Sep 10, 2020
25 changes: 25 additions & 0 deletions cli/dts/lib.deno.unstable.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,31 @@ declare namespace Deno {
*/
export function osRelease(): string;

/** **Unstable** new API. yet to be vetted.
*
* Returns the release version of the Operating System.
akshatagarwl marked this conversation as resolved.
Show resolved Hide resolved
*
* ```ts
* console.log(Deno.memoryInfo());
* ```
*
* Requires `allow-env` permission.
*
*/
export function memoryInfo(): MemoryInfo;

export interface MemoryInfo {
/** Total physical memory. */
total: number;
free: number;
available: number;
akshatagarwl marked this conversation as resolved.
Show resolved Hide resolved
buffers: number;
cached: number;
akshatagarwl marked this conversation as resolved.
Show resolved Hide resolved
/** Total swap memory. */
swapTotal: number;
swapFree: number;
}

/** **UNSTABLE**: new API, yet to be vetted.
*
* Open and initialize a plugin.
Expand Down
22 changes: 22 additions & 0 deletions cli/ops/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub fn init(s: &Rc<State>) {
s.register_op_json_sync("op_hostname", op_hostname);
s.register_op_json_sync("op_loadavg", op_loadavg);
s.register_op_json_sync("op_os_release", op_os_release);
s.register_op_json_sync("op_memory_info", op_memory_info);
}

fn op_exec_path(
Expand Down Expand Up @@ -147,3 +148,24 @@ fn op_os_release(
let release = sys_info::os_release().unwrap_or_else(|_| "".to_string());
Ok(json!(release))
}

fn op_memory_info(
state: &State,
_args: Value,
_zero_copy: &mut [ZeroCopyBuf],
) -> Result<Value, ErrBox> {
state.check_unstable("Deno.memoryInfo");
state.check_env()?;
match sys_info::mem_info() {
Ok(info) => Ok(json!({
"total": info.total,
"free": info.free,
"available": info.avail,
"buffers": info.buffers,
"cached": info.cached,
"swapTotal": info.swap_total,
"swapFree": info.swap_free
})),
Err(_) => Ok(json!({})),
}
}
5 changes: 5 additions & 0 deletions cli/rt/30_os.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
return sendSync("op_os_release");
}

function memoryInfo() {
return sendSync("op_memory_info");
}

function exit(code = 0) {
sendSync("op_exit", { code });
throw new Error("Code not reachable");
Expand Down Expand Up @@ -50,6 +54,7 @@
execPath,
exit,
osRelease,
memoryInfo,
hostname,
loadavg,
};
Expand Down
1 change: 1 addition & 0 deletions cli/rt/90_deno_ns.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ __bootstrap.denoNsUnstable = {
loadavg: __bootstrap.os.loadavg,
hostname: __bootstrap.os.hostname,
osRelease: __bootstrap.os.osRelease,
memoryInfo: __bootstrap.os.memoryInfo,
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
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,9 @@ unitTest({ perms: { env: false } }, function releasePerm(): void {
Deno.osRelease();
}, Deno.errors.PermissionDenied);
});

unitTest({ perms: { env: true } }, function memoryInfo(): void {
/* const info = Deno.memoryInfo(); */
/* assert(info.total >= 0); */
bartlomieju marked this conversation as resolved.
Show resolved Hide resolved
assertNotEquals(Deno.memoryInfo(), {});
akshatagarwl marked this conversation as resolved.
Show resolved Hide resolved
});