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
37 changes: 37 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,43 @@ declare namespace Deno {
*/
export function osRelease(): string;

/** **Unstable** new API. yet to be vetted.
*
* Displays the total amount of free and used physical and swap memory in the
* system, as well as the buffers and caches used by the kernel.
*
* This is similar to the `free` command in Linux
*
* ```ts
* console.log(Deno.systemMemoryInfo());
* ```
*
* Requires `allow-env` permission.
*
*/
export function systemMemoryInfo(): SystemMemoryInfo;

export interface SystemMemoryInfo {
/** Total installed memory */
total: number;
/** Unused memory */
free: number;
/** Estimation of how much memory is available for starting new
* applications, without swapping. Unlike the data provided by the cache or
* free fields, this field takes into account page cache and also that not
* all reclaimable memory slabs will be reclaimed due to items being in use
*/
available: number;
humancalico marked this conversation as resolved.
Show resolved Hide resolved
/** Memory used by kernel buffers */
buffers: number;
/** Memory used by the page cache and slabs */
cached: number;
humancalico marked this conversation as resolved.
Show resolved Hide resolved
/** Total swap memory */
swapTotal: number;
/** Unused swap memory */
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_system_memory_info", op_system_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_system_memory_info(
state: &State,
_args: Value,
_zero_copy: &mut [ZeroCopyBuf],
) -> Result<Value, ErrBox> {
state.check_unstable("Deno.systemMemoryInfo");
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 systemMemoryInfo() {
return sendSync("op_system_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,
systemMemoryInfo,
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,
systemMemoryInfo: __bootstrap.os.systemMemoryInfo,
applySourceMap: __bootstrap.errorStack.opApplySourceMap,
formatDiagnostics: __bootstrap.errorStack.opFormatDiagnostics,
shutdown: __bootstrap.net.shutdown,
Expand Down
11 changes: 11 additions & 0 deletions cli/tests/unit/os_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,14 @@ unitTest({ perms: { env: false } }, function releasePerm(): void {
Deno.osRelease();
}, Deno.errors.PermissionDenied);
});

unitTest({ perms: { env: true } }, function systemMemoryInfo(): void {
const info = Deno.systemMemoryInfo();
assert(info.total >= 0);
assert(info.free >= 0);
assert(info.available >= 0);
assert(info.buffers >= 0);
assert(info.cached >= 0);
assert(info.swapTotal >= 0);
assert(info.swapFree >= 0);
});