hs-opentelemetry-api
Copyright(c) Ian Duncan 2024-2026
LicenseBSD-3
Stabilityexperimental
Safe HaskellNone
LanguageHaskell2010

OpenTelemetry.Internal.AtomicBucketArray

Description

One MutableByteArray# holds all bucket counts in a single allocation, indexed by bucket number. Each slot supports atomic fetch-and-add via fetchAddIntArray#, a single ldadd (AArch64) or lock xadd (x86) instruction with no CAS retry and no allocation.

Used to eliminate the O(n) bucket vector copy that modify performs on every histogram recording inside an atomicModifyIORef' CAS loop.

Synopsis

Documentation

data AtomicBucketArray Source #

Contiguous mutable array of atomic counters. Each element is one machine-word Int, accessible via hardware fetch-and-add.

Since: 0.0.1.0

newAtomicBucketArray :: Int -> IO AtomicBucketArray Source #

Allocate a new bucket array with all counters initialized to zero.

Since: 0.0.1.0

atomicAddBucket :: AtomicBucketArray -> Int -> IO () Source #

Atomically increment the bucket at the given index by 1. No bounds checking.

Since: 0.0.1.0

readBucketArray :: AtomicBucketArray -> IO (Vector Word64) Source #

Read all bucket values into an immutable 'U.Vector Word64'. Each element is read individually; not globally atomic across buckets (acceptable for metric snapshots).

Since: 0.0.1.0

readAndResetBucketArray :: AtomicBucketArray -> IO (Vector Word64) Source #

Read all bucket values and atomically reset each to zero. Uses fetch-and-add with the negated current value. Between the read and the subtract another thread may increment, but the net effect is correct across consecutive delta collections: no sample is lost, at worst one sample shifts to the next cycle.

Since: 0.0.1.0