1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
//! **Where a read's allocations go**, when the `perf-probe` feature is on.
//!
//! Off by default, and *not compiled* when off — every call site is `#[cfg(feature = "perf-probe")]`, so the build a
//! reader runs has no counter, no atomic and no branch on it. The rule this crate's `no_global_state` test enforces is
//! about state an owner should hold; this is an opt-in instrument, and its state is a build-time choice rather than
//! something a running server carries.
//!
//! The counter itself lives in the allocator the probe installs (`tests/allocation_probe.rs` wraps mimalloc and calls
//! [`note`]). The **marks** are what turn one total into a table: `cook`, `parse`, `index_tree`, `files`, `commit` each
//! record the running counts when they finish, and [`report`] prints the deltas in order.
//!
//! # What a mark can and cannot attribute
//!
//! **The total is exact; a label is only as exact as the threading allows.** The index runs a file's reading on
//! scoped threads (`index::store`), so two marks recorded on different threads are not on one timeline: a
//! process-wide counter makes every per-phase delta include whatever the other workers did in between. Measured, a
//! `facts loop` label read 43 M, 20 M and 82 M for the same code in three runs — which is why the per-fact marks
//! that produced it were removed, and why a round of this work is verified by the **total** count and the wall time
//! rather than by a label. The phase-level marks (`cook`, `parse`, `index_tree`, `files`, `commit`) are the ones that
//! mean something: they bracket work that runs on one thread.
//!
//! A per-thread counter was tried as the fix and does not work either, for a reason worth writing down: the
//! threads that outlive a `reset` keep counting, so their first mark after it carries everything they did before —
//! and a worker pool that predates the measurement is not something the instrument can see.
use Mutex;
use ;
/// Allocations reported so far — see [`note`].
pub static ALLOCATIONS: AtomicUsize = new;
/// Bytes requested so far.
pub static BYTES: AtomicUsize = new;
/// **One allocation**, as the probe's wrapping allocator reports it.
/// The allocation count so far.
/// The bytes requested so far.
/// One phase of a read, as it finished.
static MARKS: = new;
/// **A phase of the read has finished.** Recorded in call order; [`report`] turns the sequence into per-phase deltas.
/// The phases so far, as `(label, allocations, bytes)` **since the previous mark**, summed per label.
///
/// Summed because the passes that matter run once per file: a per-file index calls `build_facts` hundreds of times for
/// one read, and a table with hundreds of `facts loop` rows answers nothing. First-appearance order is kept.
/// **Which measurement a count belongs to.** `reset` bumps this, and a per-thread counter that sees a stale generation
/// starts again from zero — which is what makes a **worker thread spawned before the reset** count only its work after
/// it. Without it, the first mark such a thread records carries everything it did since it started, and the table
/// over-counts by hundreds of percent (measured: 700 M of labels against a 143 M total).
pub static GENERATION: AtomicUsize = new;
/// The generation [`reset`] last started — see [`GENERATION`].
/// Forget everything, for a second measurement in the same process.