Skip to main content

Crate mapsample

Crate mapsample 

Source
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.txt

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.

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.
DumpHandle
Asks the running sampler for dumps from any thread, for example a watchdog.
Module
A loaded module.
PlacedThread
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.
PlacementSummary
The figures the placement section reports.
Profile
The samples of a run, or of part of one.
Reading
Thread thread (an index in Placement::threads) ran on logical processor cpu when the time stamp counter read tsc.
Ring
The samples of the last capacity seconds 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.
ThreadPlacement
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.
PlacementMode
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_env reads.
DEFAULT_RING_SECONDS
Seconds of samples the rolling recorder keeps by default.
DUMP_WAIT
How long Sampler::dump_recent waits 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 GetSystemCpuSetInformation buffer: SYSTEM_CPU_SET_INFORMATION entries with Size (u32) at 0, Type (u32) at 4 (0 is a CPU set), Group (u16) at 12, LogicalProcessorIndex at 14, CoreIndex at 15 and EfficiencyClass at 18. Other entry types are skipped; a truncated or undersized entry ends the walk.