Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

melpomene: tracing improvements #2

Merged
merged 4 commits into from
Jul 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions source/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions source/kernel/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,9 @@ impl Kernel {
const INIT_IDLE: usize = 2;
const INIT_LOCK: usize = 3;

pub unsafe fn new(
settings: KernelSettings,
) -> Result<HeapBox<Self>, ()> {
pub unsafe fn new(settings: KernelSettings) -> Result<HeapBox<Self>, ()> {
info!(
start = settings.heap_start as usize,
start = ?settings.heap_start,
size = settings.heap_size,
"Initializing heap"
);
Expand Down
6 changes: 5 additions & 1 deletion source/melpomene/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,12 @@ path = "../mstd"
version = "0.7.3"
default-features = false

[dependencies.atty]
version = "0.2"
optional = true

[features]
trace-fmt = ["tracing-subscriber"]
trace-fmt = ["tracing-subscriber", "atty"]
# Note, the "trace-modality" feature requires the use of the Auxon modality tool.
# More information: https://auxon.io/products/modality
trace-modality = ["tracing-modality"]
Expand Down
33 changes: 19 additions & 14 deletions source/melpomene/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,35 @@
use std::{
sync::atomic::{Ordering, AtomicBool},
thread::{spawn, yield_now, sleep},
time::{Duration, Instant},
future::Future,
sync::atomic::{AtomicBool, Ordering},
task::Poll,
thread::{sleep, spawn, yield_now},
time::{Duration, Instant},
};

use abi::{bbqueue_ipc::BBBuffer, syscall::DriverKind};
use mnemos_kernel::{Kernel, KernelSettings, DriverHandle, KChannel};
use melpomene::sim_tracing::setup_tracing;
use mnemos_kernel::{DriverHandle, KChannel, Kernel, KernelSettings};

const HEAP_SIZE: usize = 192 * 1024;
static KERNEL_LOCK: AtomicBool = AtomicBool::new(true);

fn main() {
setup_tracing();
let _span = tracing::info_span!("Melpo").entered();

println!("========================================");
let kernel = spawn(move || {
kernel_entry();
});
println!("[Melpo]: Kernel started.");
tracing::info!("Kernel started.");

// Wait for the kernel to complete initialization...
while KERNEL_LOCK.load(Ordering::Acquire) {
yield_now();
}

tracing::debug!("Kernel initialized.");

// let userspace = spawn(move || {
// userspace_entry();
// });
Expand All @@ -40,14 +43,14 @@ fn main() {

let kj = kernel.join();
sleep(Duration::from_millis(50));
println!("[Melpo]: Kernel ended: {:?}", kj);

tracing::info!("Kernel ended: {:?}", kj);

println!("========================================");

println!("[Melpo]: You've met with a terrible fate, haven't you?");
tracing::error!("You've met with a terrible fate, haven't you?");
}

#[tracing::instrument(name = "Kernel", level = "info")]
fn kernel_entry() {
// First, we'll do some stuff that later the linker script will do...
let kernel_heap = Box::into_raw(Box::new([0u8; HEAP_SIZE]));
Expand All @@ -72,7 +75,9 @@ fn kernel_entry() {
k.register_driver(DriverHandle {
kind: DriverKind::Todo,
queue: dummy_chan.clone(),
}).map_err(drop).unwrap();
})
.map_err(drop)
.unwrap();

let dummy_fut = async move {
let _ = dummy_chan;
Expand All @@ -87,7 +92,6 @@ fn kernel_entry() {
k.spawn_allocated(boxed_dummy);
}


//////////////////////////////////////////////////////////////////////////////
// TODO: Userspace doesn't really do anything yet! Simulate initialization of
// the userspace structures, and just periodically wake the kernel for now.
Expand All @@ -104,6 +108,7 @@ fn kernel_entry() {
}

let _userspace = spawn(|| {
let _span = tracing::info_span!("userspace").entered();
loop {
while KERNEL_LOCK.load(Ordering::Acquire) {
sleep(Duration::from_millis(10));
Expand All @@ -114,7 +119,6 @@ fn kernel_entry() {
}
});


loop {
while !KERNEL_LOCK.load(Ordering::Acquire) {
sleep(Duration::from_millis(10));
Expand Down Expand Up @@ -158,8 +162,6 @@ fn kernel_entry() {
// };
// mstd::executor::mailbox::MAILBOX.set_rings(rings);



// let start = Instant::now();
// loop {
// *mstd::executor::time::CURRENT_TIME.borrow_mut().unwrap() = start.elapsed().as_micros() as u64;
Expand Down Expand Up @@ -215,7 +217,10 @@ impl Sleepy {
impl Future for Sleepy {
type Output = ();

fn poll(self: std::pin::Pin<&mut Self>, cx: &mut std::task::Context<'_>) -> std::task::Poll<Self::Output> {
fn poll(
self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Self::Output> {
cx.waker().wake_by_ref();
if self.start.elapsed() < self.dur {
Poll::Pending
Expand Down
34 changes: 28 additions & 6 deletions source/melpomene/src/sim_tracing.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,31 @@
#[cfg(feature = "trace-fmt")]
pub fn setup_tracing() {
tracing_subscriber::fmt::init();
}
const ENV_FILTER: &str = "MELPOMENE_TRACE";

#[cfg(feature = "trace-modality")]
pub fn setup_tracing() {
tracing_modality::TracingModality::init().expect("init");
use tracing_subscriber::prelude::*;

let subscriber = tracing_subscriber::registry();

// if `trace-fmt` is enabled, add a `tracing-subscriber::fmt` layer along
// with an `EnvFilter`
#[cfg(feature = "trace-fmt")]
let subscriber = {
use tracing_subscriber::{filter, fmt};

let filter = filter::EnvFilter::builder()
.with_default_directive(filter::LevelFilter::INFO.into())
.with_env_var(ENV_FILTER)
.from_env_lossy();

let fmt = fmt::layer()
.with_timer(fmt::time::uptime())
.with_ansi(atty::is(atty::Stream::Stdout))
.with_filter(filter);
subscriber.with(fmt)
};

// if `trace-modality` is enabled, add the Modality layer as well.
#[cfg(feature = "trace-modality")]
let subscriber = subscriber.with(tracing_modality::ModalityLayer::new());

subscriber.init();
}