Skip to content

[hist] Implement RHistEngine::SnapshotAtomic - #22492

Merged
hahnjo merged 8 commits into
root-project:masterfrom
hahnjo:hist-snapshot
Jun 26, 2026
Merged

[hist] Implement RHistEngine::SnapshotAtomic#22492
hahnjo merged 8 commits into
root-project:masterfrom
hahnjo:hist-snapshot

Conversation

@hahnjo

@hahnjo hahnjo commented Jun 5, 2026

Copy link
Copy Markdown
Member

It returns a consistent copy of the histogram, during concurrent filling. The implementation is based on a successful double collect as described by Afek et al in their 1993 paper "Atomic Snapshots of Shared Memory." The idea is to iterate twice over all bins until no change is observed.

This is guaranteed to work for unweighted filling because bin contents are monotonically increasing. With weighted filling and potentially negative weights, there could be the situation where a bin goes back to its previous content and a change is missed. This is probably unlikely in practice, or can be entirely avoided by using RBinWithError that also tracks the sum of squares, which is monotonically increasing even for negative weights.

@hahnjo
hahnjo requested a review from jblomer June 5, 2026 12:37
@hahnjo hahnjo self-assigned this Jun 5, 2026
@hahnjo hahnjo added the in:Hist label Jun 5, 2026
@github-actions

github-actions Bot commented Jun 5, 2026

Copy link
Copy Markdown

Test Results

    22 files      22 suites   3d 15h 4m 41s ⏱️
 3 859 tests  3 859 ✅ 0 💤 0 ❌
77 159 runs  77 159 ✅ 0 💤 0 ❌

Results for commit c957255.

♻️ This comment has been updated with latest results.

@hahnjo
hahnjo force-pushed the hist-snapshot branch 2 times, most recently from e9e8303 to fad7ce2 Compare June 8, 2026 13:07
hahnjo added 8 commits June 8, 2026 15:13
After the changes introduced in commit 43566dc ("Ensure consistency
of RBinWithError::AtomicAdd"), it is possible to detect
a. ongoing updates if fSum2 is negative, and
b. inconsistent reads if the value of fSum2 changes.
This ensures correctness on weakly-ordered systems. This also requires
a store release of fSum in AtomicAdd to make sure a previous write of
fSum2 is visible in AtomicLoad.
It returns a consistent copy of the histogram, during concurrent
filling. The implementation is based on a successful double collect
as described by Afek et al in their 1993 paper "Atomic Snapshots of
Shared Memory." The idea is to iterate twice over all bins until no
change is observed.

This is guaranteed to work for unweighted filling because bin contents
are monotonically increasing. With weighted filling and potentially
negative weights, there could be the situation where a bin goes back
to its previous content and a change is missed. This is probably
unlikely in practice, or can be entirely avoided by using RBinWithError
that also tracks the sum of squares, which is monotonically increasing
even for negative weights.
This ensures correctness on weakly-ordered systems. If not using
SnapshotAtomic, release stores would not be needed in FillAtomic.
However, tests indicate that it's not measurable in practice so
we use it unconditionally to keep the implementation simple.
@hahnjo
hahnjo marked this pull request as ready for review June 8, 2026 13:18
@hahnjo
hahnjo requested a review from hageboeck June 8, 2026 13:19

@jblomer jblomer left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool stuff!

Maybe one general point on progress (not for this PR): IIUC, theoretically, the snapshot operation may never return. I wonder if this can be a problem, and if it is, if we can use the spinlocks after some waiting to block fillings and let a snapshot finish.

@hahnjo

hahnjo commented Jun 23, 2026

Copy link
Copy Markdown
Member Author

Maybe one general point on progress (not for this PR): IIUC, theoretically, the snapshot operation may never return. I wonder if this can be a problem

Yes, this is a problem especially because fill operations are much faster than snapshots.

and if it is, if we can use the spinlocks after some waiting to block fillings and let a snapshot finish.

... and yes, I already have this implemented in a branch locally 😃

@hahnjo
hahnjo merged commit 14f1f15 into root-project:master Jun 26, 2026
34 checks passed
@hahnjo
hahnjo deleted the hist-snapshot branch June 26, 2026 09:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants