From 8d42feb0514b634389d1a5b7fe17e92582aaa408 Mon Sep 17 00:00:00 2001 From: bunnie Date: Wed, 28 Dec 2022 19:48:40 +0800 Subject: [PATCH] increase heap for mtxcli; add memory usage reporting --- apps/mtxcli/src/cmds.rs | 13 +++++++++++++ apps/mtxcli/src/cmds/web.rs | 3 +++ apps/mtxcli/src/main.rs | 32 +++++++++++++++++++++++++++++++- 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/apps/mtxcli/src/cmds.rs b/apps/mtxcli/src/cmds.rs index 08dc52c55..2559f73ad 100644 --- a/apps/mtxcli/src/cmds.rs +++ b/apps/mtxcli/src/cmds.rs @@ -620,3 +620,16 @@ pub fn tokenize(line: &mut XousString::<1024>) -> Option> { None } } + +pub(crate) fn heap_usage() -> usize { + match xous::rsyscall(xous::SysCall::IncreaseHeap(0, xous::MemoryFlags::R)).expect("couldn't get heap size") { + xous::Result::MemoryRange(m) => { + let usage = m.len(); + usage + } + _ => { + log::error!("Couldn't measure heap usage"); + 0 + }, + } +} diff --git a/apps/mtxcli/src/cmds/web.rs b/apps/mtxcli/src/cmds/web.rs index 4527cec8d..6cd988972 100644 --- a/apps/mtxcli/src/cmds/web.rs +++ b/apps/mtxcli/src/cmds/web.rs @@ -330,6 +330,7 @@ pub fn get_filter(user: &str, server: &str, token: &str) -> Option { } fn get_messages(body: Map, room_id: &str) -> String { + log::debug!("heap usage: {}", crate::cmds::heap_usage()); let mut messages = String::new(); if let Some(Value::Object(rooms)) = body.get("rooms") { if let Some(Value::Object(join)) = rooms.get("join") { @@ -371,6 +372,7 @@ fn get_messages(body: Map, room_id: &str) -> String { pub fn client_sync(server: &str, filter: &str, since: &str, timeout: i32, room_id: &str, token: &str) -> Option<(String, String)> { + log::debug!("heap usage: {}", crate::cmds::heap_usage()); let mut url = String::from(server); url.push_str("/_matrix/client/r0/sync?filter="); url.push_str(filter); @@ -417,6 +419,7 @@ impl MessageRequest { } pub fn send_message(server: &str, room_id: &str, text: &str, txn_id: &str, token: &str) -> bool { + log::debug!("heap usage: {}", crate::cmds::heap_usage()); let room_id_encoded = url::encode(room_id); let mut url = String::from(server); url.push_str("/_matrix/client/r0/rooms/"); diff --git a/apps/mtxcli/src/main.rs b/apps/mtxcli/src/main.rs index 9084c32d0..fc767aacd 100644 --- a/apps/mtxcli/src/main.rs +++ b/apps/mtxcli/src/main.rs @@ -23,11 +23,41 @@ pub(crate) enum MtxcliOp { // This name should be (1) unique (2) under 64 characters long and (3) ideally descriptive. pub(crate) const SERVER_NAME_MTXCLI: &str = "_Matrix chat_"; -fn main() -> ! { +fn main () -> ! { + let stack_size = 1024 * 1024; + std::thread::Builder::new() + .stack_size(stack_size) + .spawn(wrapped_main) + .unwrap() + .join() + .unwrap() +} + +fn wrapped_main() -> ! { log_server::init_wait().unwrap(); log::set_max_level(log::LevelFilter::Info); log::info!("my PID is {}", xous::process::id()); + const HEAP_LARGER_LIMIT: usize = 2048 * 1024; + let new_limit = HEAP_LARGER_LIMIT; + let result = xous::rsyscall(xous::SysCall::AdjustProcessLimit( + xous::Limits::HeapMaximum as usize, + 0, + new_limit, + )); + + if let Ok(xous::Result::Scalar2(1, current_limit)) = result { + xous::rsyscall(xous::SysCall::AdjustProcessLimit( + xous::Limits::HeapMaximum as usize, + current_limit, + new_limit, + )) + .unwrap(); + log::info!("Heap limit increased to: {}", new_limit); + } else { + panic!("Unsupported syscall!"); + } + let xns = xous_names::XousNames::new().unwrap(); // unlimited connections allowed, this is a user app and it's up to the app to decide its policy let sid = xns.register_name(SERVER_NAME_MTXCLI, None).expect("can't register server");