mapsample 0.1.0

In-process Windows x64 stack sampler with a rolling stall recorder, P/E-core placement readings, and an MSVC /MAP symbolizer
Documentation
//! An in-process stack sampler for Windows x64, a rolling stall recorder, processor placement
//! readings, and an MSVC `/MAP` symbolizer.
//!
//! [`Sampler`] runs one thread that pauses the matching threads of the calling process a
//! thousand times a second (by default), copies their stacks, resumes them, and unwinds the
//! copies with the x64 unwind data the images carry. It needs no administrator rights, no ETW
//! session, no PDB files and no debug help library. The result is a [`Profile`]: folded stacks
//! with module-relative addresses, which [`symbolize()`] (or the `mapsample` command) names
//! from the linker map of the same build, on any host.
//!
//! The last few seconds are always kept in a [`Ring`], so a stall can be written out after the
//! fact with [`Sampler::dump_recent`] or a [`DumpHandle`] from a watchdog thread. On Windows 11
//! the sampler can also record which logical processor each thread ran on
//! ([`PlacementMode`]), to see efficiency-core time, migrations and hyperthread sharing.
//!
//! ```no_run
//! use mapsample::{Config, Sampler};
//! use std::path::Path;
//!
//! let config = Config::parse("worker,render:1000")?;
//! let sampler = Sampler::start(config)?;
//! // ... run the workload ...
//! let profile = sampler.stop()?;
//! profile.write_to(Path::new("."))?; // samples.folded, samples.txt, samples.modules.txt
//! # Ok::<(), mapsample::Error>(())
//! ```
//!
//! Everything but [`Sampler::start`] works on every platform; there it returns
//! [`Error::UnsupportedPlatform`] unless the target is Windows on x86-64.
//!
//! # Safety model
//!
//! While a thread is paused the sampler allocates nothing, takes no lock and cannot panic: the
//! paused thread may hold the heap lock or any other. A guard resumes the thread on every
//! path. Unwinding happens after the thread runs again, in safe Rust over the copied stack
//! bytes, so a damaged frame stops the walk instead of reading past the copy. Each module the
//! walk enters is pinned for the rest of the walk and read only through fault-safe copies
//! inside the readable ranges of its own header. Placement APCs only touch a static table of
//! atomics in a module pinned for the life of the process, and a slot is never reused while an
//! APC aimed at it may still run.

mod config;
mod error;
mod placement;
mod profile;
mod ring;
mod sampler;
pub mod symbolize;
#[cfg(any(test, all(windows, target_arch = "x86_64")))]
mod unwind_info;
#[cfg(all(windows, target_arch = "x86_64"))]
mod win64;

pub use config::{
    Config, DEFAULT_BUSIEST_THREADS, DEFAULT_HZ, DEFAULT_MAX_DUMPS, DEFAULT_PREFIX,
    DEFAULT_RING_SECONDS, MAX_HZ, MAX_RING_SECONDS, PlacementMode, Window,
};
pub use error::Error;
pub use placement::{
    Cpu, PLACEMENT_HZ, PlacedThread, Placement, PlacementSummary, Reading, SIMULTANEOUS_US,
    ThreadPlacement, parse_cpu_sets,
};
pub use profile::{MAX_FRAMES, Module, Profile, Stack, Stats, TOP};
pub use ring::Ring;
pub use sampler::{DUMP_WAIT, DumpHandle, MAX_DUMP_NAME, Sampler};
pub use symbolize::{SymbolMap, Symbolized, demangle, parse_map, symbolize};