mirror of
https://github.com/denoland/deno.git
synced 2024-10-29 08:58:01 -04:00
feat(unstable): Add Deno.systemMemoryInfo() (#7350)
Co-authored-by: marcopacini <pacinim88@gmail.com> Co-authored-by: Casper Beyer <caspervonb@pm.me>
This commit is contained in:
parent
dfd8794da4
commit
c1b4ff61c9
5 changed files with 76 additions and 0 deletions
37
cli/dts/lib.deno.unstable.d.ts
vendored
37
cli/dts/lib.deno.unstable.d.ts
vendored
|
@ -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;
|
||||
/** Memory used by kernel buffers */
|
||||
buffers: number;
|
||||
/** Memory used by the page cache and slabs */
|
||||
cached: number;
|
||||
/** Total swap memory */
|
||||
swapTotal: number;
|
||||
/** Unused swap memory */
|
||||
swapFree: number;
|
||||
}
|
||||
|
||||
/** **UNSTABLE**: new API, yet to be vetted.
|
||||
*
|
||||
* Open and initialize a plugin.
|
||||
|
|
|
@ -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(
|
||||
|
@ -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!({})),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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");
|
||||
|
@ -50,6 +54,7 @@
|
|||
execPath,
|
||||
exit,
|
||||
osRelease,
|
||||
systemMemoryInfo,
|
||||
hostname,
|
||||
loadavg,
|
||||
};
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue