max_by_counter_value

Function max_by_counter_value 

Source
pub fn max_by_counter_value(
    fields: &[(&str, SnapshotField<'_>)],
) -> Option<usize>
Expand description

Pick the candidate whose as_u64() value is largest. The operation is purely mechanical: project each candidate via SnapshotField::as_u64, drop those that fail to project (wrong type, missing field, etc.), and return the index of the surviving candidate with the largest u64.

§Active-bss heuristic

For a cumulative non-decreasing counter where the inactive instance’s bss stays at BSS-zero init (e.g. the just-killed scheduler’s bpf_bpf.bss copy after Op::ReplaceScheduler), the live instance has by definition accumulated more events, so max-by-value reliably picks the live bss copy. This is the load-bearing usage pattern multi-instance A/B tests reach for.

§When this picks WRONG

Treat the picker as “max by u64,” not as “the live instance”:

  • Gauges or stateful fields. An inactive copy can carry a sticky non-zero value from before detach (e.g. a high-water mark, a last-seen-timestamp). Max-by-value would still pick it after the live instance starts from zero.
  • Sentinel-init counters (e.g. u64::MAX on init meaning “unset”). Max is meaningless here — the uninitialized copy wins every comparison.
  • Immediately-post-swap reads before the new instance has any events. The just-killed copy still has the larger cumulative count for some window after the swap. For Op::ReplaceScheduler races the new wait_for_accessor_publish_advance gate already serializes most of that window away, but a snapshot fired before the first post-swap counter increment will still observe the old instance’s cumulative value as the maximum.

For any of these, write a picker that names the live instance explicitly — by binary fingerprint, by obj-name prefix equality, or by a separately captured liveness signal — and pass it to live_var_via instead.

§Return semantic

Returns the index of the max candidate, or None if every candidate failed to project to u64 (in which case live_var_via surfaces a SnapshotError::ProjectionFailed naming the picker as the source). Ties resolve to the LAST tied index (deterministic; matches Iterator::max_by_key’s “last element of equal-maximum group wins” rule). Test authors who need a different tie-break write a sibling picker rather than relying on a specific ordering rule that’s not load-bearing for the active-bss heuristic this picker targets.