Skip to main content

Module alloc_trace

Module alloc_trace 

Source
Expand description

Names the operation behind large or bursty heap use in the daemon.

The aft binary installs TracingAllocator as its global allocator. It forwards every call to the process allocator and watches two things:

  • One allocation or reallocation of LARGE_ALLOCATION_BYTES or more records an event: its size, the old size for a reallocation, the thread, the current operation label and the caller’s return addresses.
  • Allocations made while an operation label is set (see scope) add to that label’s live bytes, and frees made under the same label subtract from them. When one label’s live bytes cross LABEL_BURST_BYTES an event is recorded, and another when they fall back below half of it. A label’s count starts again from zero whenever no scope of it is open, so memory an operation hands to someone else does not pile up on it over the daemon’s life.

The allocator never allocates, locks or logs on behalf of a caller. Its common path is a thread-local add; every FLUSH_QUANTUM_BYTES of net change it adds the thread’s share to the label’s shared counter with a relaxed atomic. Events go into a fixed ring that a logger thread drains, names and writes as WARN lines (see report).

Only Rust heap allocations pass through a global allocator. Memory that C libraries (SQLite, tree-sitter, ONNX Runtime) take from malloc directly is not seen here.

Re-exports§

pub use report::drain_now;
pub use report::start_logger;

Structs§

Carry
The current thread’s label and root, to carry into work handed to a pool.
Label
An operation the allocator attributes live bytes to.
LabelPeak
One label’s high-water mark for the memory census.
Labelled
A future whose polls run under a label; see future.
RootName
The repository an operation works on, kept inline so that the allocator can copy it into an event without allocating: the last component of the root path, cut to fit.
ScopeGuard
While alive, allocations on this thread are attributed to its label. Dropping it restores the label and root that were current before.
TracingAllocator
The global allocator wrapper the aft binary installs. It forwards to jemalloc on Linux and the system allocator elsewhere, then reports each call.

Constants§

FLUSH_QUANTUM_BYTES
A thread hands its pending bytes to the label’s shared counter whenever they move this far from zero, so the shared counter lags each thread by less than this.
LABEL_BURST_BYTES
One label’s live bytes crossing this records a burst event.
LARGE_ALLOCATION_BYTES
A single allocation at least this large is recorded with its backtrace.

Functions§

carry
Captures the current label and root for a pool task; see Carry::enter.
drain_now
Writes every queued event now, on the calling thread.
future
inner, with every poll run under label. For async work, where a ScopeGuard cannot be held across an await: the label is open from the first poll until the future is dropped.
install
pool.install(op), with op run under the caller’s label. Parallel iterators inside op carry it further with Carry::each.
scope
Attributes this thread’s allocations to label until the guard drops. With a root, events name that repository; without one the current root, if any, stays.
start_logger
Starts the logger thread once. Events recorded before it starts wait in the ring, which keeps the most recent ones that fit.
take_census_peaks
The limit labels with the highest live bytes since the previous call, highest first; each label’s mark then restarts from its current count. Labels that never held a byte are left out. Empty unless the logger runs, which only the aft binary starts: without the tracer installed nothing counts real allocations, and unit tests that drive the hooks by hand must not leak their labels into other tests’ census.