hotpath 0.30.2

One profiler for CPU, time, memory, SQL, and async code - quickly find and debug performance bottlenecks.
Documentation
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
use std::cell::Cell;
use std::sync::atomic::{AtomicU32, AtomicU64, AtomicU8, Ordering};

#[cfg(feature = "axum-0-8")]
use crate::lib_on::caller_stack::RequestAlloc;
use crate::tid::current_tid;

pub(crate) const MAX_DEPTH: usize = 64;

/// Fixed registry capacity; slots are intentionally never reclaimed so that
/// exited threads stay reportable. After this many unique allocating threads,
/// new threads lose per-thread attribution (function-level alloc stats are
/// unaffected); their bytes still count toward the process-wide overflow
/// totals.
const MAX_THREADS: usize = 256;
const SLOT_UNSET: u32 = u32::MAX;
/// Cached slot index of a hidden internal thread, which never gets a slot.
const SLOT_INTERNAL: u32 = u32::MAX - 1;
/// Cached slot index of a thread that found the registry full. Slots are
/// never reclaimed, so it stays full.
const SLOT_OVERFLOW: u32 = u32::MAX - 2;
const THREAD_NAME_LEN: usize = 64;
/// Base of the synthetic tid range for dead threads whose OS tid was recycled
/// by a new thread. The slot's counters and name stay reportable, and each
/// orphaned slot keeps a distinct, stable identity derived from its index, so
/// report consumers can still use the tid as a row identity.
pub(crate) const TID_ORPHANED_BASE: u64 = u64::MAX - MAX_THREADS as u64;

/// Whether a tid is a synthetic identity of an orphaned slot rather than a
/// live OS tid.
#[cfg(feature = "threads")]
pub(crate) fn is_orphaned_tid(tid: u64) -> bool {
    tid >= TID_ORPHANED_BASE
}

#[repr(align(64))]
pub(crate) struct ThreadAllocStats {
    /// Thread ID (0 unused)
    pub(crate) tid: AtomicU64,
    pub(crate) alloc_bytes: AtomicU64,
    pub(crate) dealloc_bytes: AtomicU64,
    /// Length of `name`; 0 = no name captured. Stored with Release after the
    /// name bytes, so a reader observing a non-zero length sees the full name.
    name_len: AtomicU32,
    name: [AtomicU8; THREAD_NAME_LEN],
}

impl Default for ThreadAllocStats {
    fn default() -> Self {
        Self::new()
    }
}

impl ThreadAllocStats {
    pub(crate) const fn new() -> Self {
        Self {
            tid: AtomicU64::new(0),
            alloc_bytes: AtomicU64::new(0),
            dealloc_bytes: AtomicU64::new(0),
            name_len: AtomicU32::new(0),
            name: [const { AtomicU8::new(0) }; THREAD_NAME_LEN],
        }
    }

    fn store_name(&self, name: &[u8]) {
        if name.is_empty() {
            return;
        }
        for (i, &b) in name.iter().enumerate() {
            self.name[i].store(b, Ordering::Relaxed);
        }
        self.name_len.store(name.len() as u32, Ordering::Release);
    }

    fn name(&self) -> Option<String> {
        let len = self.name_len.load(Ordering::Acquire) as usize;
        if len == 0 || len > THREAD_NAME_LEN {
            return None;
        }
        let bytes: Vec<u8> = self.name[..len]
            .iter()
            .map(|b| b.load(Ordering::Relaxed))
            .collect();
        String::from_utf8(bytes).ok()
    }
}

#[allow(clippy::declare_interior_mutable_const)]
static THREAD_ALLOC_STATS: [ThreadAllocStats; MAX_THREADS] = {
    const INIT: ThreadAllocStats = ThreadAllocStats::new();
    [INIT; MAX_THREADS]
};

static THREAD_TRACKING_ENABLED: AtomicU64 = AtomicU64::new(0);

/// Bytes allocated/deallocated by threads that never got a registry slot
/// (registry full). Keeps process-wide totals accurate past `MAX_THREADS`.
static OVERFLOW_ALLOC_BYTES: AtomicU64 = AtomicU64::new(0);
static OVERFLOW_DEALLOC_BYTES: AtomicU64 = AtomicU64::new(0);

/// Initialize the thread allocation tracking system
#[cfg_attr(not(feature = "threads"), allow(dead_code))]
pub(crate) fn init_thread_alloc_tracking() {
    // Read inside the allocator from here on, where a first-use env lookup
    // would allocate and re-enter it.
    std::sync::LazyLock::force(&crate::lib_on::SHOW_INTERNAL_THREADS);
    THREAD_TRACKING_ENABLED.store(1, Ordering::Release);
}

/// Get allocation stats for a thread
#[cfg_attr(not(feature = "threads"), allow(dead_code))]
pub(crate) fn get_thread_alloc_stats(os_tid: u64) -> Option<(u64, u64)> {
    if THREAD_TRACKING_ENABLED.load(Ordering::Acquire) == 0 {
        return None;
    }

    for slot in &THREAD_ALLOC_STATS {
        let slot_tid = slot.tid.load(Ordering::Acquire);
        if slot_tid == os_tid {
            return Some((
                slot.alloc_bytes.load(Ordering::Relaxed),
                slot.dealloc_bytes.load(Ordering::Relaxed),
            ));
        }
        if slot_tid == 0 {
            break;
        }
    }
    None
}

/// Where a thread's per-thread allocation stats go.
enum ThreadSlot {
    Tracked(&'static ThreadAllocStats),
    /// Registry full: counted only in the process-wide overflow totals.
    Overflow,
    /// A hidden internal thread: not counted at all.
    Internal,
}

#[inline]
fn thread_slot() -> ThreadSlot {
    THREAD_ALLOC_SLOT_IDX.with(|slot_idx| match slot_idx.get() {
        SLOT_UNSET => thread_slot_slow(slot_idx),
        SLOT_INTERNAL => ThreadSlot::Internal,
        SLOT_OVERFLOW => ThreadSlot::Overflow,
        idx => ThreadSlot::Tracked(&THREAD_ALLOC_STATS[idx as usize]),
    })
}

/// One-time per-thread registration, running on the thread itself on its
/// first tracked allocation. Captures the thread's own name so it can be
/// reported even after the thread exits, when resolving it via its pthread is
/// no longer possible.
#[cold]
fn thread_slot_slow(slot_idx: &Cell<u32>) -> ThreadSlot {
    let mut buf = [0u8; THREAD_NAME_LEN];
    let name = current_thread_name(&mut buf);

    // hotpath's own threads suspend tracking before they allocate, but the
    // unnamed threads third-party code spawns from them (tiny_http's accept
    // loop and worker pool) cannot, and on Linux inherit the parent's name.
    if crate::lib_on::is_hidden_internal_thread(name) {
        slot_idx.set(SLOT_INTERNAL);
        return ThreadSlot::Internal;
    }

    match get_or_create_slot_index_slow(current_tid(), name) {
        Some(idx) => {
            slot_idx.set(idx as u32);
            ThreadSlot::Tracked(&THREAD_ALLOC_STATS[idx])
        }
        None => {
            slot_idx.set(SLOT_OVERFLOW);
            ThreadSlot::Overflow
        }
    }
}

#[cold]
fn get_or_create_slot_index_slow(tid: u64, name: &[u8]) -> Option<usize> {
    for (idx, slot) in THREAD_ALLOC_STATS.iter().enumerate() {
        let slot_tid = slot.tid.load(Ordering::Acquire);

        if slot_tid == tid {
            // The slow path only runs on a thread's first tracked allocation
            // (afterwards the slot index is cached thread-locally), so finding
            // our tid already registered means an earlier, now dead thread
            // with a recycled OS tid owns this slot. Orphan it - its counters
            // and name stay reportable - and claim a fresh slot below.
            slot.tid
                .store(TID_ORPHANED_BASE + idx as u64, Ordering::Release);
            continue;
        }

        if slot_tid == 0 {
            match slot
                .tid
                .compare_exchange(0, tid, Ordering::AcqRel, Ordering::Acquire)
            {
                Ok(_) => {
                    slot.store_name(name);
                    return Some(idx);
                }
                Err(current) if current == tid => return Some(idx),
                Err(_) => continue,
            }
        }
    }
    None
}

/// The calling thread's name, empty when it has none or the platform cannot
/// tell. Does not allocate, so it is safe inside the allocator.
fn current_thread_name(buf: &mut [u8; THREAD_NAME_LEN]) -> &[u8] {
    #[cfg(any(target_os = "macos", target_os = "linux"))]
    {
        // SAFETY: pthread_self is always valid for the calling thread and the
        // buffer is valid for the passed length. pthread_getname_np writes a
        // NUL-terminated string and does not allocate.
        let ret = unsafe {
            libc::pthread_getname_np(
                libc::pthread_self(),
                buf.as_mut_ptr() as *mut libc::c_char,
                THREAD_NAME_LEN,
            )
        };
        if ret != 0 {
            return &[];
        }
        let len = buf.iter().position(|&b| b == 0).unwrap_or(0);
        &buf[..len]
    }
    #[cfg(not(any(target_os = "macos", target_os = "linux")))]
    {
        &buf[..0]
    }
}

/// Name a registered thread was seen with, if any.
#[cfg_attr(not(feature = "threads"), allow(dead_code))]
pub(crate) fn get_registered_thread_name(os_tid: u64) -> Option<String> {
    for slot in &THREAD_ALLOC_STATS {
        let slot_tid = slot.tid.load(Ordering::Acquire);
        if slot_tid == 0 {
            break;
        }
        if slot_tid == os_tid {
            return slot.name();
        }
    }
    None
}

/// `(alloc_bytes, dealloc_bytes)` from threads that never got a registry slot.
#[cfg_attr(not(feature = "threads"), allow(dead_code))]
pub(crate) fn get_overflow_alloc_stats() -> (u64, u64) {
    (
        OVERFLOW_ALLOC_BYTES.load(Ordering::Relaxed),
        OVERFLOW_DEALLOC_BYTES.load(Ordering::Relaxed),
    )
}

/// Snapshot of every registered per-thread allocation slot:
/// `(tid, alloc_bytes, dealloc_bytes, name)`. Slots outlive their threads, so
/// this includes threads that already exited.
#[cfg_attr(not(feature = "threads"), allow(dead_code))]
pub(crate) fn get_registered_thread_stats() -> Vec<(u64, u64, u64, Option<String>)> {
    let mut stats = Vec::new();
    for slot in &THREAD_ALLOC_STATS {
        let slot_tid = slot.tid.load(Ordering::Acquire);
        if slot_tid == 0 {
            break;
        }
        stats.push((
            slot_tid,
            slot.alloc_bytes.load(Ordering::Relaxed),
            slot.dealloc_bytes.load(Ordering::Relaxed),
            slot.name(),
        ));
    }
    stats
}

pub(crate) struct AllocationInfo {
    pub(crate) bytes_total: Cell<u64>,
    pub(crate) count_total: Cell<u64>,
}

impl std::ops::AddAssign for AllocationInfo {
    fn add_assign(&mut self, other: Self) {
        self.bytes_total
            .set(self.bytes_total.get() + other.bytes_total.get());
        self.count_total
            .set(self.count_total.get() + other.count_total.get());
    }
}

pub(crate) struct AllocationInfoStack {
    pub(crate) depth: Cell<u32>,
    pub(crate) elements: [AllocationInfo; MAX_DEPTH],
    pub(crate) tracking_enabled: Cell<bool>,
    /// Running totals of the server request whose route scope is active on
    /// this thread; fed by `pop_alloc_stack`, never by `track_alloc`.
    #[cfg(feature = "axum-0-8")]
    pub(crate) route: Cell<RequestAlloc>,
    #[cfg(feature = "axum-0-8")]
    pub(crate) route_active: Cell<bool>,
}

thread_local! {
    static THREAD_ALLOC_SLOT_IDX: Cell<u32> = const { Cell::new(SLOT_UNSET) };

    pub(crate) static ALLOCATIONS: AllocationInfoStack = const { AllocationInfoStack {
        depth: Cell::new(0),
        elements: [const { AllocationInfo {
            bytes_total: Cell::new(0),
            count_total: Cell::new(0),
        } }; MAX_DEPTH],
        tracking_enabled: Cell::new(true),
        #[cfg(feature = "axum-0-8")]
        route: Cell::new(RequestAlloc::ZERO),
        #[cfg(feature = "axum-0-8")]
        route_active: Cell::new(false),
    } };
}

/// Opens the route scope's allocation window: installs the request's totals
/// carried over from previous polls, so every allocation tracked on this
/// thread until [`route_alloc_exit`] counts towards the request.
///
/// The window is a plain counter rather than a frame on the allocation stack:
/// the route wants the inclusive total, so it needs no bucket of its own, and
/// staying off the stack keeps it independent of guards whose lifetime
/// straddles a poll boundary (`measure_block!` around an `.await`).
#[cfg(feature = "axum-0-8")]
#[inline]
pub(crate) fn route_alloc_enter(carry: RequestAlloc) {
    ALLOCATIONS.with(|stack| {
        stack.route.set(carry);
        stack.route_active.set(true);
    });
}

/// Closes the window opened by [`route_alloc_enter`] and returns the request's
/// totals so far.
#[cfg(feature = "axum-0-8")]
#[inline]
pub(crate) fn route_alloc_exit() -> RequestAlloc {
    ALLOCATIONS.with(|stack| {
        stack.route_active.set(false);
        stack.route.replace(RequestAlloc::ZERO)
    })
}

/// Counts a tracked allocation towards the active route window, if any.
#[cfg(feature = "axum-0-8")]
#[inline]
pub(crate) fn route_alloc_add(stack: &AllocationInfoStack, bytes: u64, count: u64) {
    if stack.route_active.get() {
        let mut total = stack.route.get();
        total.bytes += bytes;
        total.count += count;
        stack.route.set(total);
    }
}

#[cfg(not(feature = "axum-0-8"))]
#[inline]
pub(crate) fn route_alloc_add(_stack: &AllocationInfoStack, _bytes: u64, _count: u64) {}

#[inline]
pub(crate) fn track_alloc(size: usize) {
    let mut tracking_enabled = true;
    ALLOCATIONS.with(|stack| {
        tracking_enabled = stack.tracking_enabled.get();
        if !tracking_enabled {
            return;
        }
        let depth = stack.depth.get() as usize;
        let info = &stack.elements[depth];
        info.bytes_total.set(info.bytes_total.get() + size as u64);
        info.count_total.set(info.count_total.get() + 1);
        route_alloc_add(stack, size as u64, 1);
    });

    if !tracking_enabled {
        return;
    }

    if THREAD_TRACKING_ENABLED.load(Ordering::Relaxed) != 0 {
        match thread_slot() {
            ThreadSlot::Tracked(slot) => {
                slot.alloc_bytes.fetch_add(size as u64, Ordering::Relaxed);
            }
            ThreadSlot::Overflow => {
                OVERFLOW_ALLOC_BYTES.fetch_add(size as u64, Ordering::Relaxed);
            }
            ThreadSlot::Internal => {}
        }
    }
}

#[inline]
pub(crate) fn track_dealloc(size: usize) {
    let tracking_enabled = ALLOCATIONS.with(|stack| stack.tracking_enabled.get());
    if !tracking_enabled {
        return;
    }

    if THREAD_TRACKING_ENABLED.load(Ordering::Relaxed) != 0 {
        match thread_slot() {
            ThreadSlot::Tracked(slot) => {
                slot.dealloc_bytes.fetch_add(size as u64, Ordering::Relaxed);
            }
            ThreadSlot::Overflow => {
                OVERFLOW_DEALLOC_BYTES.fetch_add(size as u64, Ordering::Relaxed);
            }
            ThreadSlot::Internal => {}
        }
    }
}

#[inline]
pub(crate) fn set_alloc_tracking_enabled(enabled: bool) -> bool {
    ALLOCATIONS.with(|stack| {
        let previous = stack.tracking_enabled.get();
        stack.tracking_enabled.set(enabled);
        previous
    })
}

#[inline]
pub(crate) fn suspend_alloc_tracking() -> bool {
    set_alloc_tracking_enabled(false)
}

#[inline]
pub(crate) fn resume_alloc_tracking(previous_enabled: bool) {
    let _ = set_alloc_tracking_enabled(previous_enabled);
}