mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-16 08:38:26 +00:00
log-server: refactor to use stack-allocated buffers
Instead of having a single, shared buffer, simply stack-allocate the log buffers. This paves the way in the future to allow for buffers that are larger-sized and span multiple pages. But most importantly it alleviates inter-thread locking and contention when passing messages. Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
parent
234c169809
commit
f0f1b72e0c
3 changed files with 80 additions and 106 deletions
|
|
@ -1,14 +1,19 @@
|
|||
// #[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
|
||||
#[repr(C)]
|
||||
#[repr(C, align(4096))]
|
||||
pub struct LogRecord {
|
||||
pub file_length: u32,
|
||||
pub file: [u8; 128],
|
||||
pub line: Option<u32>,
|
||||
pub line: Option<core::num::NonZeroU32>,
|
||||
pub module_length: u32,
|
||||
pub module: [u8; 128],
|
||||
pub level: u32,
|
||||
pub args_length: u32,
|
||||
pub args: [u8; 3000],
|
||||
pub args: [u8; 3820],
|
||||
}
|
||||
|
||||
impl Default for LogRecord {
|
||||
fn default() -> Self {
|
||||
LogRecord { file_length: 0, file: [0u8; 128], line: None, module_length: 0, module: [0u8; 128], level: 0, args_length: 0, args: [0u8; 3820] }
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, num_derive::FromPrimitive, num_derive::ToPrimitive)]
|
||||
|
|
|
|||
|
|
@ -1,8 +1,7 @@
|
|||
#![cfg_attr(target_os = "none", no_std)]
|
||||
use core::fmt::Write;
|
||||
use core::sync::atomic::{AtomicBool, Ordering};
|
||||
use core::sync::atomic::{AtomicU32, Ordering};
|
||||
use num_traits::ToPrimitive;
|
||||
use xous_ipc::Buffer;
|
||||
|
||||
pub mod api;
|
||||
mod cursor;
|
||||
|
|
@ -13,73 +12,60 @@ pub enum LogError {
|
|||
NoConnection,
|
||||
}
|
||||
|
||||
static XOUS_LOGGER: XousLogger = XousLogger {
|
||||
locked: AtomicBool::new(false),
|
||||
};
|
||||
struct XousLogger;
|
||||
static XOUS_LOGGER: XousLogger = XousLogger {};
|
||||
static XOUS_LOGGER_CONNECTION: AtomicU32 = AtomicU32::new(0);
|
||||
|
||||
struct XousLogger {
|
||||
locked: AtomicBool,
|
||||
}
|
||||
impl XousLogger {
|
||||
fn log_impl(&self, record: &log::Record) {
|
||||
let mut log_record = api::LogRecord::default();
|
||||
assert_eq!(core::mem::size_of::<api::LogRecord>(), 4096);
|
||||
|
||||
const BUFFER_SIZE: usize = 4096;
|
||||
static mut XOUS_LOGGER_BACKING: Option<XousLoggerBacking> = None;
|
||||
// A "line" of 0 is the same as "None" for our purposes here.
|
||||
log_record.line = core::num::NonZeroU32::new(record.line().unwrap_or_default());
|
||||
|
||||
struct XousLoggerBacking<'a> {
|
||||
conn: xous::CID,
|
||||
buffer: Buffer<'a>,
|
||||
}
|
||||
log_record.level = record.level() as u32;
|
||||
|
||||
impl<'a> XousLoggerBacking<'a> {
|
||||
pub fn new() -> Result<Self, xous::Error> {
|
||||
Ok(XousLoggerBacking {
|
||||
conn: xous::connect(xous::SID::from_bytes(b"xous-log-server ").unwrap())?,
|
||||
buffer: Buffer::new(BUFFER_SIZE),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for XousLoggerBacking<'_> {
|
||||
fn default() -> Self {
|
||||
XousLoggerBacking {
|
||||
conn: xous::connect(xous::SID::from_bytes(b"xous-log-server ").unwrap()).unwrap(),
|
||||
buffer: Buffer::new(BUFFER_SIZE),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl XousLoggerBacking<'_> {
|
||||
fn log_impl(&mut self, record: &log::Record) {
|
||||
{
|
||||
assert!(core::mem::size_of::<api::LogRecord>() < BUFFER_SIZE);
|
||||
let log_record = unsafe { &mut *(self.buffer.as_mut_ptr() as *mut api::LogRecord) };
|
||||
|
||||
log_record.line = record.line();
|
||||
log_record.level = record.level() as u32;
|
||||
|
||||
let file = record.file().unwrap_or_default().as_bytes();
|
||||
log_record.file_length = file.len() as u32;
|
||||
for (dest, src) in log_record.file.iter_mut().zip(file) {
|
||||
*dest = *src;
|
||||
}
|
||||
|
||||
let module = record.module_path().unwrap_or_default().as_bytes();
|
||||
log_record.module_length = module.len() as u32;
|
||||
for (dest, src) in log_record.module.iter_mut().zip(module) {
|
||||
*dest = *src;
|
||||
}
|
||||
|
||||
let mut wrapper = cursor::BufferWrapper::new(&mut log_record.args);
|
||||
write!(wrapper, "{}", record.args()).ok(); // truncate if error
|
||||
log_record.args_length = wrapper.len() as u32;
|
||||
let file = record.file().unwrap_or_default().as_bytes();
|
||||
log_record.file_length = file.len().min(file.len()) as u32;
|
||||
for (dest, src) in log_record.file.iter_mut().zip(file) {
|
||||
*dest = *src;
|
||||
}
|
||||
|
||||
self.buffer
|
||||
.lend(self.conn, crate::api::Opcode::LogRecord.to_u32().unwrap())
|
||||
.unwrap();
|
||||
let module = record.module_path().unwrap_or_default().as_bytes();
|
||||
log_record.module_length = module.len().min(module.len()) as u32;
|
||||
for (dest, src) in log_record.module.iter_mut().zip(module) {
|
||||
*dest = *src;
|
||||
}
|
||||
|
||||
// Serialize the text to our record buffer
|
||||
let mut wrapper = cursor::BufferWrapper::new(&mut log_record.args);
|
||||
write!(wrapper, "{}", record.args()).ok(); // truncate if error
|
||||
log_record.args_length = wrapper.len().min(log_record.args.len()) as u32;
|
||||
|
||||
let buf = unsafe {
|
||||
xous::MemoryRange::new(
|
||||
&log_record as *const api::LogRecord as usize,
|
||||
core::mem::size_of::<api::LogRecord>(),
|
||||
)
|
||||
.unwrap()
|
||||
};
|
||||
|
||||
xous::send_message(
|
||||
XOUS_LOGGER_CONNECTION.load(Ordering::Relaxed),
|
||||
xous::Message::new_lend(
|
||||
crate::api::Opcode::LogRecord.to_usize().unwrap(),
|
||||
buf,
|
||||
None,
|
||||
None,
|
||||
),
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
fn resume(&self) {
|
||||
xous::send_message(
|
||||
self.conn,
|
||||
XOUS_LOGGER_CONNECTION.load(Ordering::Relaxed),
|
||||
xous::Message::new_scalar(2000, 0, 0, 0, 0), // logger is one of the few servers that uses special, non-encoded message IDs.
|
||||
)
|
||||
.expect("couldn't send resume message to the logger implementation");
|
||||
|
|
@ -92,53 +78,33 @@ impl log::Log for XousLogger {
|
|||
}
|
||||
|
||||
fn log(&self, record: &log::Record) {
|
||||
while self
|
||||
.locked
|
||||
.compare_exchange(false, true, Ordering::SeqCst, Ordering::Acquire)
|
||||
.is_err()
|
||||
{
|
||||
xous::yield_slice();
|
||||
}
|
||||
|
||||
if unsafe { XOUS_LOGGER_BACKING.is_none() } {
|
||||
unsafe { XOUS_LOGGER_BACKING = Some(XousLoggerBacking::default()) };
|
||||
}
|
||||
unsafe { XOUS_LOGGER_BACKING.as_mut().unwrap().log_impl(record) };
|
||||
self.locked
|
||||
.compare_exchange(true, false, Ordering::SeqCst, Ordering::Acquire)
|
||||
.expect("LOG: logger became unlocked somehow");
|
||||
XOUS_LOGGER.log_impl(record);
|
||||
}
|
||||
|
||||
fn flush(&self) {}
|
||||
}
|
||||
|
||||
pub fn init() -> Result<(), LogError> {
|
||||
if let Ok(backing) = XousLoggerBacking::new() {
|
||||
unsafe {
|
||||
XOUS_LOGGER_BACKING = Some(backing);
|
||||
}
|
||||
log::set_logger(&XOUS_LOGGER).map_err(|_| LogError::LoggerExists)?;
|
||||
log::set_max_level(log::LevelFilter::Info);
|
||||
Ok(())
|
||||
} else {
|
||||
Err(LogError::NoConnection)
|
||||
}
|
||||
XOUS_LOGGER_CONNECTION.store(
|
||||
xous::try_connect(xous::SID::from_bytes(b"xous-log-server ").unwrap())
|
||||
.or(Err(LogError::NoConnection))?,
|
||||
Ordering::Relaxed,
|
||||
);
|
||||
log::set_logger(&XOUS_LOGGER).map_err(|_| LogError::LoggerExists)?;
|
||||
log::set_max_level(log::LevelFilter::Info);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn init_wait() -> Result<(), log::SetLoggerError> {
|
||||
loop {
|
||||
if let Ok(backing) = XousLoggerBacking::new() {
|
||||
unsafe {
|
||||
XOUS_LOGGER_BACKING = Some(backing);
|
||||
break;
|
||||
}
|
||||
}
|
||||
xous::yield_slice();
|
||||
}
|
||||
log::set_logger(&XOUS_LOGGER)?;
|
||||
pub fn init_wait() -> Result<(), ()> {
|
||||
XOUS_LOGGER_CONNECTION.store(
|
||||
xous::connect(xous::SID::from_bytes(b"xous-log-server ").unwrap()).or(Err(()))?,
|
||||
Ordering::Relaxed,
|
||||
);
|
||||
log::set_logger(&XOUS_LOGGER).or(Err(()))?;
|
||||
log::set_max_level(log::LevelFilter::Info);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn resume() {
|
||||
unsafe { XOUS_LOGGER_BACKING.as_mut().unwrap().resume() };
|
||||
XOUS_LOGGER.resume();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -174,8 +174,10 @@ mod implementation {
|
|||
// println!("rxe {}", uart_csr.rf(utra::uart::RXEMPTY_RXEMPTY));
|
||||
while uart_csr.rf(utra::uart::RXEMPTY_RXEMPTY) == 0 {
|
||||
// I really rather think this is more readable, than the "Rusty" version below.
|
||||
inject_csr.wfo(utra::keyinject::UART_CHAR_CHAR,
|
||||
uart_csr.rf(utra::uart::RXTX_RXTX));
|
||||
inject_csr.wfo(
|
||||
utra::keyinject::UART_CHAR_CHAR,
|
||||
uart_csr.rf(utra::uart::RXTX_RXTX),
|
||||
);
|
||||
uart_csr.wfo(utra::uart::EV_PENDING_RX, 1);
|
||||
|
||||
// I guess this is how you would do it if you were "really doing Rust"
|
||||
|
|
@ -307,8 +309,9 @@ fn handle_opcode(
|
|||
if let Some(mem) = message.memory_message() {
|
||||
match opcode {
|
||||
api::Opcode::LogRecord => {
|
||||
let buffer = unsafe { xous_ipc::Buffer::from_memory_message(mem) };
|
||||
let lr = unsafe { &*(buffer.as_ptr() as *const LogRecord) };
|
||||
// This transmute is safe because even if the resulting buffer is garbage,
|
||||
// there are no invalid values in the resulting struct.
|
||||
let lr = unsafe { &*(mem.buf.as_ptr() as *const LogRecord) };
|
||||
let level = if log::Level::Error as u32 == lr.level {
|
||||
"ERR "
|
||||
} else if log::Level::Warn as u32 == lr.level {
|
||||
|
|
@ -352,7 +355,7 @@ fn handle_opcode(
|
|||
output.putc(*c);
|
||||
}
|
||||
if let Some(line) = lr.line {
|
||||
write!(output, ":{}", line).ok();
|
||||
write!(output, ":{}", line.get()).ok();
|
||||
}
|
||||
writeln!(output, ")").ok();
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue