Skip to content

Add butil::Seqlock and use it for TaskGroup CPU time stat - #62

Open
chenBright wants to merge 1 commit into
masterfrom
seqlock
Open

Add butil::Seqlock and use it for TaskGroup CPU time stat#62
chenBright wants to merge 1 commit into
masterfrom
seqlock

Conversation

@chenBright

Copy link
Copy Markdown
Owner

What problem does this PR solve?

Issue Number: resolve

Problem Summary:

TaskGroup tracks per-group CPU time as a 128-bit stat (a packed
last_run_ns + task-type word and a cumulated_cputime_ns word) that a
worker 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:

  • On x86 it relied on wide SSE/AVX aligned loads/stores being atomic. This is
    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.
  • On platforms without such a wide atomic it fell back to a mutex, which serializes
    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:

  • Add butil::Seqlock (butil/synchronization/seqlock.h), a general sequence lock
    providing lock-free, consistent snapshot reads around a caller-owned atomic payload.
  • Replace TaskGroup::AtomicInteger128 with an AtomicCPUTimeStat backed by
    butil::Seqlock<> over a CPUTimeStat payload accessed via relaxed atomics.

Side effects:

  • Performance effects:

  • Breaking backward compatibility:


Check List:

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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 new butil/processor.h and update bthread/processor.h to 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.

Comment thread src/bthread/task_group.h
Comment thread test/seqlock_unittest.cpp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants