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
//! **A byte counter a reader can turn on**, so a stage that allocates can be named without a profiler.
//!
//! The server installs an allocator that reports every request here; the counter is off unless
//! `CPPLS_TRACE_ALLOCS` is in the environment, and the switch is one relaxed load per allocation.
//!
//! # Two numbers, and the one that matters
//!
//! [`total`] counts every byte ever asked for and never subtracts: it answers *"how much churn did this stage
//! cause"*, and it is **not** a memory measurement. [`live`] counts what the process is holding — every
//! allocation adds, every free subtracts — and [`peak`] is its high-water mark. Only the second pair says what a
//! machine runs out of.
//!
//! The distinction is not pedantry. Every memory claim in this codebase was read off the first number, and it
//! produced a story that does not survive the second one: **~380 bytes per token of parse tree** was the churn of a
//! 218 KB header's parse, while the tree it actually held was 80 bytes per token and the whole reading's steady state
//! was 151 (`tests/parse_memory_probe.rs`). A ceiling built on that number was regulating something that was never
//! measured.
//!
//! # Why this lives in the engine and not in the server
//!
//! Because the question is about the engine's own phases: a wave preparation that holds **663 MB** while it has
//! nothing to prepare (`test_cpp/src/containers.cpp`, one open file, no requests) is a question about
//! `Session::prepare_wave`'s three calls, and they are private to this crate. The server's side of the same counter
//! is [`cpp_ls::note_allocation`], which forwards here.
//!
//! It is an instrument, not a statistic: nothing reads it in an answer, and with the variable unset it costs the
//! branch.
use ;
/// Bytes this process has asked the allocator for since the counter was turned on — **cumulative, never reduced**.
static TOTAL: AtomicUsize = new;
/// Bytes this process is **holding right now** — every allocation adds, every free subtracts.
static LIVE: AtomicUsize = new;
/// The high-water mark of [`LIVE`].
static PEAK: AtomicUsize = new;
/// Whether [`note`] should count.
static COUNTING: AtomicBool = new;
/// **Turn the counter on from `CPPLS_TRACE_ALLOCS`** — called once, while the server starts.
/// One allocation, as the installed allocator reports it.
/// **One free**, as the installed allocator reports it — the half that makes [`live`] a measurement rather than a
/// running total.
/// **One reallocation**, which is a release of the old size and an allocation of the new one.
/// **The bytes held right now** — the number a machine runs out of.
/// The high-water mark of [`live`].
/// Every byte ever asked for — churn, not memory. See the module documentation for why the difference matters.
/// Whether a run asked for the counter — what a stage's `eprintln!` is gated on.