grit-lib 0.5.29

Core library for the grit Git implementation
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
#![allow(missing_docs)]
//! Per-[`crate::odb::Odb`] hot-path counters for regression tests (test builds only).

use std::cell::RefCell;
use std::collections::HashSet;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};
use std::thread::ThreadId;

/// Counters and opt-in recording flags for one object database / repository.
#[derive(Default)]
pub struct HotPathTestMetrics {
    tree_writes: AtomicUsize,
    sparse_expanded_rows: AtomicUsize,
    cache_tree_builds: AtomicUsize,
    freshen_calls: AtomicUsize,
    blob_content_reads: AtomicUsize,
    stream_hash_calls: AtomicUsize,
    staging_passes: AtomicUsize,
    stream_write_calls: AtomicUsize,
    blob_read_threads: Mutex<HashSet<ThreadId>>,
    pack_signature_stats: AtomicUsize,
    midx_stamp_stats: AtomicUsize,
    pack_dir_stats: AtomicUsize,
    loose_path_open_attempts: AtomicUsize,
    loose_objects_root_metadata: AtomicUsize,
    loose_fanout_metadata: AtomicUsize,
    loose_path_exists_probes: AtomicUsize,
    pack_dir_metadata: AtomicUsize,
    commit_parse_calls: AtomicUsize,
    freshen_counting: AtomicBool,
    blob_counting: AtomicBool,
    stamp_counting: AtomicBool,
    loose_open_counting: AtomicBool,
    loose_metadata_counting: AtomicBool,
    commit_parse_counting: AtomicBool,
}

impl HotPathTestMetrics {
    /// Allocate a fresh counter bundle for one [`crate::odb::Odb`].
    #[must_use]
    pub fn new() -> Arc<Self> {
        Arc::new(Self::default())
    }

    pub fn sparse_expanded_rows(&self) -> usize {
        self.sparse_expanded_rows.load(Ordering::Relaxed)
    }

    pub(crate) fn record_sparse_expanded_rows(&self, count: usize) {
        self.sparse_expanded_rows
            .fetch_add(count, Ordering::Relaxed);
    }

    /// Clear tree-write count.
    pub fn reset_tree_writes(&self) {
        self.tree_writes.store(0, Ordering::SeqCst);
    }

    /// Tree objects written through this ODB since the last reset.
    #[must_use]
    pub fn tree_writes(&self) -> usize {
        self.tree_writes.load(Ordering::SeqCst)
    }

    pub(crate) fn record_tree_write(&self) {
        self.tree_writes.fetch_add(1, Ordering::Relaxed);
    }

    /// Whole-tree cache-tree builds (`build_cache_tree_from_tree`) on this ODB.
    #[must_use]
    pub fn cache_tree_builds(&self) -> usize {
        self.cache_tree_builds.load(Ordering::SeqCst)
    }

    pub(crate) fn record_cache_tree_build(&self) {
        self.cache_tree_builds.fetch_add(1, Ordering::Relaxed);
    }

    /// Clear freshen-touch count.
    pub fn reset_freshen_calls(&self) {
        self.freshen_calls.store(0, Ordering::SeqCst);
    }

    pub fn set_freshen_counting(&self, enabled: bool) {
        self.freshen_counting.store(enabled, Ordering::SeqCst);
    }

    #[must_use]
    pub fn freshen_calls(&self) -> usize {
        self.freshen_calls.load(Ordering::SeqCst)
    }

    pub(crate) fn record_freshen(&self) {
        if self.freshen_counting.load(Ordering::Relaxed) {
            self.freshen_calls.fetch_add(1, Ordering::Relaxed);
        }
    }

    pub(crate) fn record_stream_write(&self) {
        self.stream_write_calls.fetch_add(1, Ordering::Relaxed);
    }

    pub(crate) fn stream_write_calls(&self) -> usize {
        self.stream_write_calls.load(Ordering::Relaxed)
    }

    pub(crate) fn record_staging_pass(&self) {
        self.staging_passes.fetch_add(1, Ordering::Relaxed);
    }

    pub(crate) fn staging_passes(&self) -> usize {
        self.staging_passes.load(Ordering::Relaxed)
    }

    pub(crate) fn record_stream_hash(&self) {
        self.stream_hash_calls.fetch_add(1, Ordering::Relaxed);
    }

    pub(crate) fn stream_hash_calls(&self) -> usize {
        self.stream_hash_calls.load(Ordering::Relaxed)
    }

    /// Clear worktree blob read count.
    pub fn reset_blob_content_reads(&self) {
        self.blob_content_reads.store(0, Ordering::SeqCst);
        self.blob_read_threads
            .lock()
            .unwrap_or_else(|e| e.into_inner())
            .clear();
    }

    pub fn set_blob_counting(&self, enabled: bool) {
        self.blob_counting.store(enabled, Ordering::Relaxed);
    }

    #[must_use]
    pub fn blob_content_reads(&self) -> usize {
        self.blob_content_reads.load(Ordering::SeqCst)
    }

    /// Distinct threads that read worktree blob content since the last reset.
    #[must_use]
    pub fn blob_read_thread_count(&self) -> usize {
        self.blob_read_threads
            .lock()
            .unwrap_or_else(|e| e.into_inner())
            .len()
    }

    pub(crate) fn record_blob_content_read(&self) {
        if self.blob_counting.load(Ordering::Relaxed) {
            self.blob_content_reads.fetch_add(1, Ordering::Relaxed);
            self.blob_read_threads
                .lock()
                .unwrap_or_else(|e| e.into_inner())
                .insert(std::thread::current().id());
        }
    }

    pub fn reset_pack_stamp_stats(&self) {
        self.pack_signature_stats.store(0, Ordering::SeqCst);
    }

    pub fn reset_midx_stamp_stats(&self) {
        self.midx_stamp_stats.store(0, Ordering::SeqCst);
    }

    pub fn reset_pack_dir_stats(&self) {
        self.pack_dir_stats.store(0, Ordering::SeqCst);
    }

    /// Clear loose-object `open` attempt count.
    pub fn reset_loose_path_open_attempts(&self) {
        self.loose_path_open_attempts.store(0, Ordering::SeqCst);
    }

    pub fn set_loose_open_counting(&self, enabled: bool) {
        self.loose_open_counting.store(enabled, Ordering::Relaxed);
    }

    pub fn set_loose_metadata_counting(&self, enabled: bool) {
        self.loose_metadata_counting
            .store(enabled, Ordering::Relaxed);
    }

    pub fn reset_loose_metadata_probes(&self) {
        self.loose_objects_root_metadata.store(0, Ordering::SeqCst);
        self.loose_fanout_metadata.store(0, Ordering::SeqCst);
        self.loose_path_exists_probes.store(0, Ordering::SeqCst);
    }

    #[must_use]
    pub fn loose_objects_root_metadata_probes(&self) -> usize {
        self.loose_objects_root_metadata.load(Ordering::SeqCst)
    }

    #[must_use]
    pub fn loose_fanout_metadata_probes(&self) -> usize {
        self.loose_fanout_metadata.load(Ordering::SeqCst)
    }

    #[must_use]
    pub fn loose_path_exists_probes(&self) -> usize {
        self.loose_path_exists_probes.load(Ordering::SeqCst)
    }

    #[must_use]
    pub fn loose_metadata_probe_total(&self) -> usize {
        self.loose_objects_root_metadata_probes()
            + self.loose_fanout_metadata_probes()
            + self.loose_path_exists_probes()
    }

    pub fn reset_pack_dir_metadata_probes(&self) {
        self.pack_dir_metadata.store(0, Ordering::SeqCst);
    }

    #[must_use]
    pub fn pack_dir_metadata_probes(&self) -> usize {
        self.pack_dir_metadata.load(Ordering::SeqCst)
    }

    pub(crate) fn record_loose_objects_root_metadata(&self) {
        if self.loose_metadata_counting.load(Ordering::Relaxed) {
            self.loose_objects_root_metadata
                .fetch_add(1, Ordering::Relaxed);
        }
    }

    pub(crate) fn record_loose_fanout_metadata(&self) {
        if self.loose_metadata_counting.load(Ordering::Relaxed) {
            self.loose_fanout_metadata.fetch_add(1, Ordering::Relaxed);
        }
    }

    pub(crate) fn record_loose_path_exists_probe(&self) {
        if self.loose_metadata_counting.load(Ordering::Relaxed) {
            self.loose_path_exists_probes
                .fetch_add(1, Ordering::Relaxed);
        }
    }

    pub(crate) fn record_pack_dir_metadata(&self) {
        if self.stamp_counting.load(Ordering::Relaxed) {
            self.pack_dir_metadata.fetch_add(1, Ordering::Relaxed);
        }
    }

    pub fn reset_commit_parse_calls(&self) {
        self.commit_parse_calls.store(0, Ordering::SeqCst);
    }

    pub fn set_commit_parse_counting(&self, enabled: bool) {
        self.commit_parse_counting.store(enabled, Ordering::Relaxed);
    }

    #[must_use]
    pub fn commit_parse_calls(&self) -> usize {
        self.commit_parse_calls.load(Ordering::SeqCst)
    }

    pub(crate) fn record_commit_parse(&self) {
        if self.commit_parse_counting.load(Ordering::Relaxed) {
            self.commit_parse_calls.fetch_add(1, Ordering::Relaxed);
        }
    }

    #[must_use]
    pub fn loose_path_open_attempts(&self) -> usize {
        self.loose_path_open_attempts.load(Ordering::SeqCst)
    }

    pub(crate) fn record_loose_path_open(&self) {
        if self.loose_open_counting.load(Ordering::Relaxed) {
            self.loose_path_open_attempts
                .fetch_add(1, Ordering::Relaxed);
        }
    }

    pub fn set_stamp_counting(&self, enabled: bool) {
        self.stamp_counting.store(enabled, Ordering::SeqCst);
    }

    #[must_use]
    pub fn pack_signature_stat_calls(&self) -> usize {
        self.pack_signature_stats.load(Ordering::SeqCst)
    }

    #[must_use]
    pub fn midx_stamp_stat_calls(&self) -> usize {
        self.midx_stamp_stats.load(Ordering::SeqCst)
    }

    pub(crate) fn record_pack_signature_stat(&self) {
        if self.stamp_counting.load(Ordering::Relaxed) {
            self.pack_signature_stats.fetch_add(1, Ordering::Relaxed);
        }
    }

    /// `stat` calls on a `pack/` directory to check whether its listing changed.
    #[must_use]
    pub fn pack_dir_stat_calls(&self) -> usize {
        self.pack_dir_stats.load(Ordering::SeqCst)
    }

    pub(crate) fn record_pack_dir_stat(&self) {
        if self.stamp_counting.load(Ordering::Relaxed) {
            self.pack_dir_stats.fetch_add(1, Ordering::Relaxed);
        }
    }
    pub(crate) fn record_midx_stamp_stat(&self) {
        if self.stamp_counting.load(Ordering::Relaxed) {
            self.midx_stamp_stats.fetch_add(1, Ordering::Relaxed);
        }
    }

    /// Disable all opt-in recording (safe to call from [`HotPathMetricsScope`] drop).
    pub fn disable_all_counting(&self) {
        self.set_freshen_counting(false);
        self.set_blob_counting(false);
        self.set_stamp_counting(false);
        self.set_loose_open_counting(false);
        self.set_loose_metadata_counting(false);
        self.set_commit_parse_counting(false);
    }
}

// hygiene: test-only thread-local scope for hot-path stat attribution in unit tests
thread_local! {
    static ACTIVE: RefCell<Option<Arc<HotPathTestMetrics>>> = const { RefCell::new(None) };
}

fn with_active<F, R>(f: F) -> R
where
    F: FnOnce(Option<Arc<HotPathTestMetrics>>) -> R,
{
    ACTIVE.with(|cell| f(cell.borrow().clone()))
}

pub(crate) fn record_pack_signature_stat_for_active_scope() {
    with_active(|active| {
        if let Some(m) = active {
            m.record_pack_signature_stat();
        }
    });
}

pub(crate) fn record_midx_stamp_stat_for_active_scope() {
    with_active(|active| {
        if let Some(m) = active {
            m.record_midx_stamp_stat();
        }
    });
}

pub(crate) fn record_pack_dir_stat_for_active_scope() {
    with_active(|active| {
        if let Some(m) = active {
            m.record_pack_dir_stat();
        }
    });
}

pub(crate) fn record_loose_path_open_for_active_scope() {
    with_active(|active| {
        if let Some(m) = active {
            m.record_loose_path_open();
        }
    });
}

pub(crate) fn record_loose_objects_root_metadata_for_active_scope() {
    with_active(|active| {
        if let Some(m) = active {
            m.record_loose_objects_root_metadata();
        }
    });
}

pub(crate) fn record_loose_fanout_metadata_for_active_scope() {
    with_active(|active| {
        if let Some(m) = active {
            m.record_loose_fanout_metadata();
        }
    });
}

pub(crate) fn record_loose_path_exists_probe_for_active_scope() {
    with_active(|active| {
        if let Some(m) = active {
            m.record_loose_path_exists_probe();
        }
    });
}

pub(crate) fn record_pack_dir_metadata_for_active_scope() {
    with_active(|active| {
        if let Some(m) = active {
            m.record_pack_dir_metadata();
        }
    });
}

/// Installs `metrics` as the active scope for pack/MIDX stat attribution on this thread.
pub struct HotPathMetricsScope {
    prev: Option<Arc<HotPathTestMetrics>>,
    installed: Option<Arc<HotPathTestMetrics>>,
}

impl HotPathMetricsScope {
    /// Begin attributing pack/MIDX stat probes to `metrics` on this thread.
    #[must_use]
    pub fn install(metrics: Arc<HotPathTestMetrics>) -> Self {
        let prev = ACTIVE.with(|cell| cell.borrow_mut().replace(Arc::clone(&metrics)));
        Self {
            prev,
            installed: Some(metrics),
        }
    }
}

impl Drop for HotPathMetricsScope {
    fn drop(&mut self) {
        if let Some(m) = self.installed.take() {
            m.disable_all_counting();
        }
        ACTIVE.with(|cell| {
            *cell.borrow_mut() = self.prev.take();
        });
    }
}