Expand description
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.
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.txtEverything 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.
Re-exports§
pub use symbolize::SymbolMap;pub use symbolize::Symbolized;pub use symbolize::demangle;pub use symbolize::parse_map;pub use symbolize::symbolize;
Modules§
- symbolize
- Naming folded frames from an MSVC linker map (
link.exe /MAP).
Structs§
- Config
- What to sample.
- Cpu
- One logical processor, from
GetSystemCpuSetInformation. - Dump
Handle - Asks the running sampler for dumps from any thread, for example a watchdog.
- Module
- A loaded module.
- Placed
Thread - A thread whose placement was read. Each watched thread lifetime gets its own entry, so two threads with the same name (or one id reused by a later thread) stay apart.
- Placement
- The placement readings of a run.
- Placement
Summary - The figures the placement section reports.
- Profile
- The samples of a run, or of part of one.
- Reading
- Thread
thread(an index inPlacement::threads) ran on logical processorcpuwhen the time stamp counter readtsc. - Ring
- The samples of the last
capacityseconds of a run, kept in one-second slices. - Sampler
- A running sampler.
- Stack
- One distinct stack and how often it was seen.
- Stats
- What sampling cost.
- Thread
Placement - Placement figures of one thread.
- Window
- A span of the run, in seconds after the sampler started:
start <= t < end.
Enums§
- Error
- Everything that can go wrong in this crate.
- Placement
Mode - Whether placement readings are taken.
Constants§
- DEFAULT_
BUSIEST_ THREADS - Busiest threads the placement section reports by default, besides the highlighted ones.
- DEFAULT_
HZ - Samples per second when the spec gives none.
- DEFAULT_
MAX_ DUMPS - Dumps written per run at most, by default.
- DEFAULT_
PREFIX - The environment variable prefix
Config::from_envreads. - DEFAULT_
RING_ SECONDS - Seconds of samples the rolling recorder keeps by default.
- DUMP_
WAIT - How long
Sampler::dump_recentwaits for the sampler thread to write a dump. - MAX_
DUMP_ NAME - The longest dump name accepted, in bytes.
- MAX_
FRAMES - Frames kept per stack.
- MAX_HZ
- The most samples per second accepted (the pacing timer cannot go faster).
- MAX_
RING_ SECONDS - The longest rolling recorder accepted, in seconds.
- PLACEMENT_
HZ - Placement readings asked for per thread and second, at most.
- SIMULTANEOUS_
US - Readings of two threads this close together (microseconds) count as simultaneous.
- TOP
- Rows in each top list of the text report.
Functions§
- parse_
cpu_ sets - Parse a
GetSystemCpuSetInformationbuffer:SYSTEM_CPU_SET_INFORMATIONentries withSize(u32) at 0,Type(u32) at 4 (0 is a CPU set),Group(u16) at 12,LogicalProcessorIndexat 14,CoreIndexat 15 andEfficiencyClassat 18. Other entry types are skipped; a truncated or undersized entry ends the walk.