-
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: expose systemMemoryInfo API for main worker (#338)
Adding it to EdgeRuntime namespace instead of Deno namespace, to prevent accidentally exposing it in user workers. Also, we are implementing customized version of it for performance reasons (ie. only support Linux via sysinfo)
- Loading branch information
Showing
3 changed files
with
65 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,57 @@ | ||
use deno_core::op2; | ||
use std::collections::HashMap; | ||
|
||
pub type EnvVars = HashMap<String, String>; | ||
|
||
deno_core::extension!(sb_os, esm_entry_point = "ext:sb_os/os.js", esm = ["os.js"]); | ||
#[derive(serde::Serialize)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct MemInfo { | ||
pub total: u64, | ||
pub free: u64, | ||
pub available: u64, | ||
pub buffers: u64, | ||
pub cached: u64, | ||
pub swap_total: u64, | ||
pub swap_free: u64, | ||
} | ||
|
||
#[op2] | ||
#[serde] | ||
fn op_system_memory_info() -> Option<MemInfo> { | ||
let mut mem_info = MemInfo { | ||
total: 0, | ||
free: 0, | ||
available: 0, | ||
buffers: 0, | ||
cached: 0, | ||
swap_total: 0, | ||
swap_free: 0, | ||
}; | ||
|
||
#[cfg(any(target_os = "android", target_os = "linux"))] | ||
{ | ||
let mut info = std::mem::MaybeUninit::uninit(); | ||
// SAFETY: `info` is a valid pointer to a `libc::sysinfo` struct. | ||
let res = unsafe { libc::sysinfo(info.as_mut_ptr()) }; | ||
if res == 0 { | ||
// SAFETY: `sysinfo` initializes the struct. | ||
let info = unsafe { info.assume_init() }; | ||
let mem_unit = info.mem_unit as u64; | ||
mem_info.swap_total = info.totalswap * mem_unit; | ||
mem_info.swap_free = info.freeswap * mem_unit; | ||
mem_info.total = info.totalram * mem_unit; | ||
mem_info.free = info.freeram * mem_unit; | ||
mem_info.available = mem_info.free; | ||
mem_info.buffers = info.bufferram * mem_unit; | ||
} | ||
} | ||
|
||
Some(mem_info) | ||
} | ||
|
||
deno_core::extension!( | ||
sb_os, | ||
ops = [op_system_memory_info], | ||
esm_entry_point = "ext:sb_os/os.js", | ||
esm = ["os.js"] | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters