summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cli/dts/lib.deno.unstable.d.ts37
-rw-r--r--cli/ops/os.rs22
-rw-r--r--cli/rt/30_os.js5
-rw-r--r--cli/rt/90_deno_ns.js1
-rw-r--r--cli/tests/unit/os_test.ts11
5 files changed, 76 insertions, 0 deletions
diff --git a/cli/dts/lib.deno.unstable.d.ts b/cli/dts/lib.deno.unstable.d.ts
index 9c5590f97..fad31a3f2 100644
--- a/cli/dts/lib.deno.unstable.d.ts
+++ b/cli/dts/lib.deno.unstable.d.ts
@@ -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.
diff --git a/cli/ops/os.rs b/cli/ops/os.rs
index 4778d49a4..a38b5b08a 100644
--- a/cli/ops/os.rs
+++ b/cli/ops/os.rs
@@ -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!({})),
+ }
+}
diff --git a/cli/rt/30_os.js b/cli/rt/30_os.js
index 743ecd585..adf256fc6 100644
--- a/cli/rt/30_os.js
+++ b/cli/rt/30_os.js
@@ -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,
};
diff --git a/cli/rt/90_deno_ns.js b/cli/rt/90_deno_ns.js
index 82acdef3c..23ab12924 100644
--- a/cli/rt/90_deno_ns.js
+++ b/cli/rt/90_deno_ns.js
@@ -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,
diff --git a/cli/tests/unit/os_test.ts b/cli/tests/unit/os_test.ts
index 79f70afac..12caed156 100644
--- a/cli/tests/unit/os_test.ts
+++ b/cli/tests/unit/os_test.ts
@@ -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);
+});