git clone https://garage44.eu/jeroen/pinnacle.git.. /kernel/src/profile.rs
//! A sampling profiler: when QEMU is given the fw_cfg file
//! `opt/pinnacle/profile` (xtask's `PINNACLE_PROFILE`), every timer tick
//! notes where the core it interrupted was, a program's address or the
//! kernel's, and every ten seconds the commonest are said on the console
//! (`profile: <count> <address>`), for the host to name from the ELF's
//! symbols. A tick comes every 10 ms on a core that shares its time, so a
//! busy program is sampled about a hundred times a second per core.
use crate::exceptions::Frame;
use crate::sync::SpinLock;
use core::sync::atomic::{AtomicBool, AtomicU64, Ordering};
/// Whether QEMU asked for it ([`init`]).
static ON: AtomicBool = AtomicBool::new(false);
/// Places sampled: an open-addressed table of (address, count).
const SLOTS: usize = 8192;
static TABLE: SpinLock<[(u64, u32); SLOTS]> = SpinLock::new([(0, 0); SLOTS]);
/// The same for the return address (x30) at each sample: the caller of a
/// leaf function such as `memcpy`.
static CALLERS: SpinLock<[(u64, u32); SLOTS]> = SpinLock::new([(0, 0); SLOTS]);
/// Samples taken since the last report, and the samples that found no slot.
static TAKEN: AtomicU64 = AtomicU64::new(0);
static DROPPED: AtomicU64 = AtomicU64::new(0);
/// When the last report was said (ms since boot).
static SAID: AtomicU64 = AtomicU64::new(0);
const EVERY_MS: u64 = 10_000;
/// How many places a report names.
const SAY: usize = 40;
/// Reads whether QEMU asked for the profiler: at boot, on one core, before
/// any tick samples.
pub fn init() {
let asked = crate::board::FW_CFG
.and_then(|fw_cfg| crate::drivers::display::fw_cfg::find_sized(fw_cfg, b"opt/pinnacle/profile"))
.is_some();
ON.store(asked, Ordering::Relaxed);
if asked {
crate::println!("profile: sampling every tick");
}
}
/// Notes where the interrupted core was; said every ten seconds, from core 0.
pub fn sample(frame: *const Frame) {
if !ON.load(Ordering::Relaxed) {
return;
}
// SAFETY: the vector stub's frame, live for this interrupt.
let (address, caller) = unsafe { ((*frame).elr, (*frame).x[30]) };
note(&TABLE, address);
note(&CALLERS, caller);
TAKEN.fetch_add(1, Ordering::Relaxed);
let now = crate::timer::now_ms();
if crate::arch::core_id() == 0 && now >= SAID.load(Ordering::Relaxed) + EVERY_MS {
SAID.store(now, Ordering::Relaxed);
say();
}
}
/// Whether the profiler runs.
pub fn on() -> bool {
ON.load(Ordering::Relaxed)
}
/// The generic timer's count, for timing a system call.
pub fn counter() -> u64 {
let count: u64;
// SAFETY: reading the virtual counter has no side effects.
unsafe { core::arch::asm!("mrs {}, cntvct_el0", out(reg) count, options(nomem, nostack)) };
count
}
/// Per system call number: how many were made, and the counter ticks
/// they took in the kernel.
const NUMBERS: usize = 64;
static CALLS: [AtomicU64; NUMBERS] = [const { AtomicU64::new(0) }; NUMBERS];
static CALL_TICKS: [AtomicU64; NUMBERS] = [const { AtomicU64::new(0) }; NUMBERS];
/// File calls: how many, the ticks spent waiting for their disk's lock, and
/// the ticks spent working under it.
static DISK: [AtomicU64; 3] = [const { AtomicU64::new(0) }; 3];
/// Counts a file call that waited `wait` ticks for its disk and worked `work`.
pub fn disk(wait: u64, work: u64) {
DISK[0].fetch_add(1, Ordering::Relaxed);
DISK[1].fetch_add(wait, Ordering::Relaxed);
DISK[2].fetch_add(work, Ordering::Relaxed);
}
/// Counts system call `number`, which began at counter `start`.
pub fn called(number: u64, start: u64) {
let at = (number as usize).min(NUMBERS - 1);
CALLS[at].fetch_add(1, Ordering::Relaxed);
CALL_TICKS[at].fetch_add(counter().saturating_sub(start), Ordering::Relaxed);
}
/// Counts `address` in `table`.
fn note(table: &SpinLock<[(u64, u32); SLOTS]>, address: u64) {
let mut table = table.lock();
let mut slot = (address as usize >> 2).wrapping_mul(0x9e37_79b9) % SLOTS;
for _ in 0..32 {
let entry = &mut table[slot];
if entry.0 == address || entry.1 == 0 {
*entry = (address, entry.1 + 1);
return;
}
slot = (slot + 1) % SLOTS;
}
DROPPED.fetch_add(1, Ordering::Relaxed);
}
/// Says the commonest places, and starts counting afresh.
fn say() {
let taken = TAKEN.swap(0, Ordering::Relaxed);
let dropped = DROPPED.swap(0, Ordering::Relaxed);
crate::println!("profile: {taken} samples ({dropped} without a slot)");
for (address, count) in commonest(&TABLE).iter().filter(|e| e.1 > 0) {
crate::println!("profile: {count} {address:#x}");
}
for (address, count) in commonest(&CALLERS).iter().filter(|e| e.1 > 0) {
crate::println!("profile-caller: {count} {address:#x}");
}
let per_us = (crate::timer::frequency() / 1_000_000).max(1);
let calls = DISK[0].swap(0, Ordering::Relaxed);
if calls > 0 {
let wait = DISK[1].swap(0, Ordering::Relaxed) / per_us / 1000;
let work = DISK[2].swap(0, Ordering::Relaxed) / per_us / 1000;
crate::println!("profile-disk: {calls} calls waited {wait} ms for the disk, worked {work} ms");
}
for number in 0..NUMBERS {
let calls = CALLS[number].swap(0, Ordering::Relaxed);
let us = CALL_TICKS[number].swap(0, Ordering::Relaxed) / per_us;
if calls > 0 {
crate::println!("profile-syscall: {number} {calls} calls {us} us");
}
}
}
/// The [`SAY`] commonest in `table`, which is emptied.
fn commonest(table: &SpinLock<[(u64, u32); SLOTS]>) -> [(u64, u32); SAY] {
let mut top = [(0u64, 0u32); SAY];
let mut table = table.lock();
for entry in table.iter_mut() {
if entry.1 > top[SAY - 1].1 {
top[SAY - 1] = *entry;
top.sort_unstable_by_key(|a| core::cmp::Reverse(a.1));
}
*entry = (0, 0);
}
top
}