Add butil::Seqlock and use it for TaskGroup CPU time stat - #62
Open
chenBright wants to merge 1 commit into
Open
Conversation
There was a problem hiding this comment.
Pull request overview
This PR introduces a general-purpose butil::Seqlock primitive to provide consistent, lock-free snapshot reads over a multi-word payload, and applies it to bthread::TaskGroup’s per-group CPU time statistic to avoid relying on non-portable 128-bit atomic behavior.
Changes:
- Add
butil::Seqlock(single-writer and mutex-serialized multi-writer variants) plus a unit test suite. - Replace
TaskGroup’s prior 128-bit “atomic” CPU time stat mechanism with a seqlock-based implementation. - Centralize
cpu_relax()/barrier()in a newbutil/processor.hand updatebthread/processor.hto include it; add an ASan-only LSan suppression hook for a known benign leak in controller tests.
Reviewed changes
Copilot reviewed 10 out of 10 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| test/seqlock_unittest.cpp | Adds Seqlock unit tests (single-threaded and concurrent reader/writer scenarios). |
| test/Makefile | Registers seqlock_unittest.cpp in the Make-based test sources. |
| test/CMakeLists.txt | Registers seqlock_unittest.cpp in the CMake test sources. |
| test/BUILD.bazel | Registers seqlock_unittest.cpp in the Bazel test sources. |
| test/brpc_controller_unittest.cpp | Adds an ASan-only __lsan_default_suppressions hook to suppress a known shutdown leak report. |
| src/butil/synchronization/seqlock.h | Introduces butil::Seqlock built on a cacheline-aligned sequence counter. |
| src/butil/processor.h | New shared header defining cpu_relax() and barrier() macros. |
| src/bthread/task_group.h | Reworks CPU time stat storage to use Seqlock (replacing prior 128-bit “atomic” approach). |
| src/bthread/task_group.cpp | Removes the previous AtomicInteger128 implementation. |
| src/bthread/processor.h | Switches to including butil/processor.h instead of duplicating macros. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What problem does this PR solve?
Issue Number: resolve
Problem Summary:
TaskGrouptracks per-group CPU time as a 128-bit stat (a packedlast_run_ns+ task-type word and acumulated_cputime_nsword) that aworker updates while other threads (e.g. bvar sampling) read it
concurrently. This was implemented with
AtomicInteger128, whose 128-bit"atomic" load/store had no portable, guaranteed lock-free backing:
not guaranteed by the ISA -- Intel and AMD do not officially promise 128-bit
AVX load/store atomicity; it merely happens to hold on current microarchitectures
(Skylake, Zen 2). Depending on it is relying on unspecified hardware behavior.
readers and blocks them behind the writer -- exactly what a consistent-snapshot
read is meant to avoid.
What is changed and the side effects?
Changed:
butil::Seqlock(butil/synchronization/seqlock.h), a general sequence lockproviding lock-free, consistent snapshot reads around a caller-owned atomic payload.
TaskGroup::AtomicInteger128with anAtomicCPUTimeStatbacked bybutil::Seqlock<>over aCPUTimeStatpayload accessed via relaxed atomics.Side effects:
Performance effects:
Breaking backward compatibility:
Check List: