Developing
Profiling
Most standard profilers do not work with Motor OS yet, but a Motor OS
program can be instrumented with the tracing crate and its trace turned into
a flame graph on the host, which is enough to find, for example, the causes of high CPU
contention.
Instrument the code
- Add
tracingto the crates you want to instrument:tracing = { version = "0.1", default-features = false, features = ["attributes"] }. - Add
use tracing::instrument;to the modules and put#[instrument(skip_all)]on the functions you want to see (docs). - Wrap code blocks inside functions with
tracing::trace_span!("name").in_scope(|| { /* ... */ });(docs). - Add to the binary's
Cargo.toml:tracing = "0.1" tracing-subscriber = "0.3" tracing-flame = "0.2" - In the binary, wrap the region you want to profile. Write the trace somewhere an
interactive program may create files, such as
/user/tmp:use tracing_flame::FlameLayer; use tracing_subscriber::prelude::*; const FNAME: &str = "/user/tmp/profile.folded"; let (flame_layer, guard) = FlameLayer::with_file(FNAME).unwrap(); tracing_subscriber::registry().with(flame_layer).init(); tracing::info_span!("profiling_root").in_scope(|| { /* the code you want to profile */ }); core::mem::drop(guard); // writes the trace to the file
Collect and visualize
- Run the binary in Motor OS.
- Copy the profile to the Linux host:
scp -P 2222 -o IdentitiesOnly=yes -i test.key [email protected]:/user/tmp/profile.folded . cargo install infernocat profile.folded | inferno-flamegraph > profile.svg
profile.svg is the CPU profile flame graph. profile.folded is a
text file; you may need to edit it by hand or adjust inferno's options to make the graph
readable.