make kernelprint a response instead of a raw print, update top and kill scripts to use this

This commit is contained in:
dr-frmr 2024-05-25 16:51:23 -06:00
parent f692b1b66b
commit b6c54c884b
No known key found for this signature in database
12 changed files with 270 additions and 99 deletions

37
Cargo.lock generated
View File

@ -3044,6 +3044,17 @@ dependencies = [
"sha3-asm",
]
[[package]]
name = "kill"
version = "0.1.0"
dependencies = [
"anyhow",
"kinode_process_lib 0.8.0 (git+https://github.com/kinode-dao/process_lib?rev=010e175)",
"serde",
"serde_json",
"wit-bindgen",
]
[[package]]
name = "kinode"
version = "0.8.0"
@ -3128,6 +3139,28 @@ dependencies = [
"lib",
]
[[package]]
name = "kinode_process_lib"
version = "0.8.0"
source = "git+https://github.com/kinode-dao/process_lib?rev=010e175#010e175b4e66242c2ef9422088e355698728600d"
dependencies = [
"alloy-json-rpc 0.1.0 (git+https://github.com/alloy-rs/alloy.git?rev=cad7935)",
"alloy-primitives 0.7.0",
"alloy-rpc-types 0.1.0 (git+https://github.com/alloy-rs/alloy.git?rev=cad7935)",
"alloy-transport 0.1.0 (git+https://github.com/alloy-rs/alloy.git?rev=cad7935)",
"anyhow",
"bincode",
"http 1.1.0",
"mime_guess",
"rand 0.8.5",
"rmp-serde",
"serde",
"serde_json",
"thiserror",
"url",
"wit-bindgen",
]
[[package]]
name = "kinode_process_lib"
version = "0.8.0"
@ -5542,10 +5575,10 @@ dependencies = [
[[package]]
name = "top"
version = "0.1.0"
version = "0.2.0"
dependencies = [
"anyhow",
"kinode_process_lib 0.8.0 (git+https://github.com/kinode-dao/process_lib?rev=09dc9f9)",
"kinode_process_lib 0.8.0 (git+https://github.com/kinode-dao/process_lib?rev=010e175)",
"serde",
"serde_json",
"wit-bindgen",

View File

@ -22,7 +22,7 @@ members = [
"kinode/packages/kns_indexer/kns_indexer", "kinode/packages/kns_indexer/get_block", "kinode/packages/kns_indexer/state",
"kinode/packages/settings/settings",
"kinode/packages/terminal/terminal",
"kinode/packages/terminal/alias", "kinode/packages/terminal/cat", "kinode/packages/terminal/echo", "kinode/packages/terminal/hi", "kinode/packages/terminal/m", "kinode/packages/terminal/top",
"kinode/packages/terminal/alias", "kinode/packages/terminal/cat", "kinode/packages/terminal/echo", "kinode/packages/terminal/hi", "kinode/packages/terminal/kill", "kinode/packages/terminal/m", "kinode/packages/terminal/top",
"kinode/packages/terminal/namehash_to_name", "kinode/packages/terminal/net_diagnostics", "kinode/packages/terminal/peer", "kinode/packages/terminal/peers",
"kinode/packages/tester/tester", "kinode/packages/tester/test_runner",
]

View File

@ -0,0 +1,20 @@
[package]
name = "kill"
version = "0.1.0"
edition = "2021"
[features]
simulation-mode = []
[dependencies]
anyhow = "1.0"
kinode_process_lib = { git = "https://github.com/kinode-dao/process_lib", rev = "010e175" }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
wit-bindgen = "0.24.0"
[lib]
crate-type = ["cdylib"]
[package.metadata.component]
package = "kinode:process"

View File

@ -0,0 +1,48 @@
use kinode_process_lib::kernel_types::{KernelCommand, KernelPrint, KernelResponse};
use kinode_process_lib::{
await_next_message_body, call_init, println, Address, Message, ProcessId, Request,
};
wit_bindgen::generate!({
path: "target/wit",
world: "process-v0",
});
call_init!(init);
fn init(_our: Address) {
let Ok(args) = await_next_message_body() else {
println!("failed to get args");
return;
};
let Ok(proc_id) = String::from_utf8(args) else {
println!("failed to stringify arguments");
return;
};
let body = match proc_id.parse::<ProcessId>() {
Ok(proc_id) => serde_json::to_vec(&KernelCommand::KillProcess(proc_id)).unwrap(),
Err(_) => {
println!("invalid process id");
return;
}
};
let Ok(Message::Response { body, .. }) = Request::new()
.target(("our", "kernel", "distro", "sys"))
.body(body)
.send_and_await_response(60)
.unwrap()
else {
println!("failed to get response from kernel");
return;
};
let Ok(KernelResponse::KilledProcess(proc_id)) =
serde_json::from_slice::<KernelResponse>(&body)
else {
println!("failed to parse kernel response");
return;
};
println!("killed process {}", proc_id);
}

View File

@ -9,7 +9,7 @@ call_init!(init);
fn init(_our: Address) {
let Ok(Ok(Message::Response { body, .. })) = Request::to(("our", "net", "distro", "sys"))
.body(rmp_serde::to_vec(&net::NetAction::GetDiagnostics).unwrap())
.send_and_await_response(5)
.send_and_await_response(60)
else {
println!("failed to get diagnostics from networking module");
return;
@ -18,5 +18,5 @@ fn init(_our: Address) {
println!("got malformed response from networking module");
return;
};
println!("{printout}");
println!("\r\n{printout}");
}

View File

@ -45,6 +45,18 @@
],
"wit_version": 0
},
"kill.wasm": {
"root": false,
"public": false,
"request_networking": false,
"request_capabilities": [
"kernel:distro:sys"
],
"grant_capabilities": [
"kernel:distro:sys"
],
"wit_version": 0
},
"m.wasm": {
"root": true,
"public": true,
@ -106,7 +118,9 @@
"request_capabilities": [
"kernel:distro:sys"
],
"grant_capabilities": [],
"grant_capabilities": [
"kernel:distro:sys"
],
"wit_version": 0
}
}

View File

@ -71,6 +71,10 @@ fn init(our: Address) {
"hi".to_string(),
ProcessId::new(Some("hi"), "terminal", "sys"),
),
(
"kill".to_string(),
ProcessId::new(Some("kill"), "terminal", "sys"),
),
(
"m".to_string(),
ProcessId::new(Some("m"), "terminal", "sys"),

View File

@ -1,6 +1,6 @@
[package]
name = "top"
version = "0.1.0"
version = "0.2.0"
edition = "2021"
[features]
@ -8,7 +8,7 @@ simulation-mode = []
[dependencies]
anyhow = "1.0"
kinode_process_lib = { git = "https://github.com/kinode-dao/process_lib", rev = "09dc9f9" }
kinode_process_lib = { git = "https://github.com/kinode-dao/process_lib", rev = "010e175" }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
wit-bindgen = "0.24.0"

View File

@ -1,6 +1,8 @@
use kinode_process_lib::kernel_types::{KernelCommand, KernelPrint};
use kinode_process_lib::kernel_types::{
KernelCommand, KernelPrint, KernelPrintResponse, KernelResponse, PersistedProcess,
};
use kinode_process_lib::{
await_next_message_body, call_init, println, Address, ProcessId, Request,
await_next_message_body, call_init, println, Address, Message, ProcessId, Request,
};
wit_bindgen::generate!({
@ -20,25 +22,76 @@ fn init(_our: Address) {
return;
};
if proc_id.is_empty() {
let _ = Request::new()
.target(("our", "kernel", "distro", "sys"))
.body(serde_json::to_vec(&KernelCommand::Debug(KernelPrint::ProcessMap)).unwrap())
.send();
} else {
match proc_id.parse::<ProcessId>() {
Ok(proc_id) => {
let _ = Request::new()
.target(("our", "kernel", "distro", "sys"))
.body(
serde_json::to_vec(&KernelCommand::Debug(KernelPrint::Process(proc_id)))
.unwrap(),
)
.send();
let Ok(Message::Response { body, .. }) = Request::new()
.target(("our", "kernel", "distro", "sys"))
.body(if proc_id.is_empty() {
serde_json::to_vec(&KernelCommand::Debug(KernelPrint::ProcessMap)).unwrap()
} else {
match proc_id.parse::<ProcessId>() {
Ok(proc_id) => {
serde_json::to_vec(&KernelCommand::Debug(KernelPrint::Process(proc_id)))
.unwrap()
}
Err(_) => {
println!("invalid process id");
return;
}
}
Err(_) => {
println!("invalid process id");
})
.send_and_await_response(60)
.unwrap()
else {
println!("failed to get response from kernel");
return;
};
let Ok(KernelResponse::Debug(kernel_print_response)) =
serde_json::from_slice::<KernelResponse>(&body)
else {
println!("failed to parse kernel response");
return;
};
match kernel_print_response {
KernelPrintResponse::ProcessMap(process_map) => {
let len = process_map.len();
let printout = process_map
.iter()
.map(|(proc_id, process)| print_process(proc_id, process))
.collect::<Vec<_>>()
.join("\r\n");
println!("\r\n{printout}\r\n\r\ntop: {len} running processes");
}
KernelPrintResponse::Process(process) => match process {
None => {
println!("process {} not running", proc_id);
return;
}
Some(process) => {
println!("{}", print_process(&proc_id.parse().unwrap(), &process));
}
},
KernelPrintResponse::HasCap(_) => {
println!("kernel gave wrong kind of response");
}
}
}
fn print_process(id: &ProcessId, process: &PersistedProcess) -> String {
format!(
"{}:\r\n {}\r\n wit: {}\r\n on-exit: {:?}\r\n public: {}\r\n capabilities: {:?}",
id,
if process.wasm_bytes_handle.is_empty() {
"(runtime)"
} else {
&process.wasm_bytes_handle
},
process.wit_version.unwrap_or_default(),
process.on_exit,
process.public,
process
.capabilities
.iter()
.map(|c| c.to_string())
.collect::<Vec<_>>()
)
}

View File

@ -520,52 +520,47 @@ async fn handle_kernel_request(
.await
.expect("event loop: fatal: sender died");
}
t::KernelCommand::Debug(kind) => match kind {
t::KernelPrint::ProcessMap => {
let mut process_map_string = "".to_string();
for (id, process) in &mut *process_map {
process_map_string.push_str(&format!("{}: {}\r\n", id, process));
}
let _ = send_to_terminal
.send(t::Printout {
verbosity: 0,
content: format!("kernel process map:\r\n{process_map_string}\r\nfound {} running processes", process_map.len()),
})
.await;
}
t::KernelPrint::Process(process_id) => {
let Some(proc) = process_map.get(&process_id) else {
let _ = send_to_terminal
.send(t::Printout {
verbosity: 0,
content: format!("kernel: no such running process {}", process_id),
})
.await;
return None;
};
let _ = send_to_terminal
.send(t::Printout {
verbosity: 0,
content: format!("process info for {process_id}:\r\n{proc}",),
})
.await;
}
t::KernelPrint::HasCap { on, cap } => {
let _ = send_to_terminal
.send(t::Printout {
verbosity: 0,
content: format!(
"process {} has cap:\r\n{}",
on,
process_map
.get(&on)
.map(|p| p.capabilities.contains_key(&cap))
.unwrap_or(false)
),
})
.await;
}
},
t::KernelCommand::Debug(kind) => {
let response = match kind {
t::KernelPrint::ProcessMap => t::KernelPrintResponse::ProcessMap(
process_map
.clone()
.into_iter()
.map(|(k, v)| (k, v.into()))
.collect(),
),
t::KernelPrint::Process(process_id) => t::KernelPrintResponse::Process(
process_map.get(&process_id).cloned().map(|p| p.into()),
),
t::KernelPrint::HasCap { on, cap } => t::KernelPrintResponse::HasCap(
process_map
.get(&on)
.map(|p| p.capabilities.contains_key(&cap)),
),
};
send_to_loop
.send(t::KernelMessage {
id: km.id,
source: t::Address {
node: our_name.clone(),
process: KERNEL_PROCESS_ID.clone(),
},
target: km.rsvp.unwrap_or(km.source),
rsvp: None,
message: t::Message::Response((
t::Response {
inherit: false,
body: serde_json::to_vec(&t::KernelResponse::Debug(response)).unwrap(),
metadata: None,
capabilities: vec![],
},
None,
)),
lazy_load_blob: None,
})
.await
.expect("event loop: fatal: sender died");
}
}
None
}

View File

@ -395,7 +395,7 @@ async fn main() {
match res {
Ok(_) => "graceful exit".into(),
Err(e) => format!(
"uh oh, a kernel process crashed -- this should never happen: {e:?}"
"runtime crash: {e:?}"
),
}

View File

@ -43,7 +43,7 @@ pub struct ProcessId {
impl Serialize for ProcessId {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::ser::Serializer,
S: serde::Serializer,
{
format!("{}", self).serialize(serializer)
}
@ -52,7 +52,7 @@ impl Serialize for ProcessId {
impl<'a> Deserialize<'a> for ProcessId {
fn deserialize<D>(deserializer: D) -> Result<ProcessId, D::Error>
where
D: serde::de::Deserializer<'a>,
D: serde::Deserializer<'a>,
{
let s = String::deserialize(deserializer)?;
s.parse().map_err(serde::de::Error::custom)
@ -1248,6 +1248,14 @@ pub enum KernelResponse {
StartedProcess,
RunProcessError,
KilledProcess(ProcessId),
Debug(KernelPrintResponse),
}
#[derive(Debug, Serialize, Deserialize)]
pub enum KernelPrintResponse {
ProcessMap(UserspaceProcessMap),
Process(Option<UserspacePersistedProcess>),
HasCap(Option<bool>),
}
#[derive(Debug)]
@ -1293,6 +1301,7 @@ pub enum CapMessage {
pub type ReverseCapIndex = HashMap<ProcessId, HashMap<ProcessId, Vec<Capability>>>;
pub type ProcessMap = HashMap<ProcessId, PersistedProcess>;
pub type UserspaceProcessMap = HashMap<ProcessId, UserspacePersistedProcess>;
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct PersistedProcess {
@ -1303,29 +1312,24 @@ pub struct PersistedProcess {
pub public: bool, // marks if a process allows messages from any process
}
impl std::fmt::Display for PersistedProcess {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(
f,
"Process {{\n wasm_bytes_handle: {},\n wit_version: {:?},\n on_exit: {:?},\n public: {}\n capabilities: {}\n}}",
{
if &self.wasm_bytes_handle == "" {
"(none, this is a runtime process)"
} else {
&self.wasm_bytes_handle
}
},
self.wit_version,
self.on_exit,
self.public,
{
let mut caps_string = "[".to_string();
for cap in self.capabilities.keys() {
caps_string += &format!("\n {}", cap.to_string());
}
caps_string + "\n ]"
},
)
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct UserspacePersistedProcess {
pub wasm_bytes_handle: String,
pub wit_version: Option<u32>,
pub on_exit: OnExit,
pub capabilities: HashSet<Capability>,
pub public: bool,
}
impl From<PersistedProcess> for UserspacePersistedProcess {
fn from(p: PersistedProcess) -> Self {
UserspacePersistedProcess {
wasm_bytes_handle: p.wasm_bytes_handle,
wit_version: p.wit_version,
on_exit: p.on_exit,
capabilities: p.capabilities.into_keys().collect(),
public: p.public,
}
}
}