jimmystewpot opened a new pull request, #3755:
URL: https://github.com/apache/brpc/pull/3755
### What problem does this PR solve?
Issue Number: #3753
Problem Summary:
This issue was discovered while profiling StarRocks (which relies heavily on
`brpc` / `bthread` / `butil` for RPC and concurrency) on ARM64 (`__aarch64__`)
servers, where contended spin-wait loops generated excessive cache-coherency
traffic.
In `src/butil/processor.h`, `cpu_relax()` on `ARCH_CPU_ARM_FAMILY` is
currently defined as `asm volatile("yield\n": : :"memory")`. On nearly all
modern single-threaded-per-core ARM64 server and desktop CPUs (ARM Neoverse
N1/N2/V1/V2, AWS Graviton 2/3/4, Ampere Altra, and Apple Silicon), `yield` is
an SMT hardware-thread hint that executes as a 1-cycle (~0.34 ns) `nop`.
Consequently, spin-wait loops (`BT_LOOP_WHEN` in `src/bthread/processor.h`,
`bthread` mutex/butex spinloops, and work-stealing queues) hammer the
load/store unit and cache-coherency fabric at full frequency (~3 billion load
polls per second per spinning core).
In contrast, `isb` (Instruction Synchronisation Barrier) flushes the
front-end instruction pipeline locally on the core, introducing a ~15–30 cycle
(~9 ns) self-throttling pause without generating any external bus or
cache-coherency traffic. Using `isb` for spin-wait loops on AArch64 matches the
established practice in MySQL, PostgreSQL, RocksDB, Meta Folly, and OpenJDK
(`-XX:OnSpinWaitInst=isb`).
### What is changed and the side effects?
Changed:
- **`src/butil/processor.h`:**
- Updated `cpu_relax()` on 64-bit ARM (`#if defined(ARCH_CPU_ARM64)`) to
emit `asm volatile("isb\n": : :"memory")`, while preserving `asm
volatile("yield\n": : :"memory")` for 32-bit ARM (`#elif
defined(ARCH_CPU_ARM_FAMILY)`).
- **`test/cpu_unittest.cc`:**
- Added `CPU.CpuRelaxArm64UsesIsb` to verify that `cpu_relax()` expands to
`isb` on `ARCH_CPU_ARM64` and executes cleanly in a spin loop.
Side effects:
- Performance effects:
- **Linux AArch64 Benchmark (ARM Neoverse-V2, Ubuntu 24.04, GCC 13.3.0):**
- Replaces a **0.34 ns** (~1 cycle `nop`) `yield` with a **9.04 ns**
(~26 cycle) local `isb` pipeline pause, reducing coherent L1/L2 snoop polling
frequency by **~26x** during spin-wait loops (`BT_LOOP_WHEN`).
- **macOS Apple Silicon `bthread` Mutex Contention
(`MutexTest.performance`, 12 threads):**
- Increases `bthread::Mutex` throughput under 12-thread contention from
**38.4M ops / 500ms** (156.9 ns/op) to **39.9M ops / 500ms** (151.9 ns/op).
- Breaking backward compatibility: No. `isb` is a base ARMv8.0-A instruction
supported on all AArch64 processors, and full API/ABI compatibility is
preserved.
---
### Check List:
- Please make sure your changes are compilable.
- When providing us with a new feature, it is best to add related tests.
- Please follow [Contributor Covenant Code of
Conduct](https://github.com/apache/brpc/blob/master/CODE_OF_CONDUCT.md).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]