perf(butil): ARM64 (aarch64) performance and modernisation improvements across atomics, CRC32C, spin-wait hints, and fast timers
維護者通常 2 天內回覆
@jimmystewpot 已經在處理了。
開始於 2026年11月18日。
評估
- 難度
- 5/5
- 預估耗時
- 一週以上
- 新手友好度
- 12/100
- Issue 類型
- 功能
- 描述清晰度
- 基本清楚
- 活躍度
- 活躍
- 技術堆疊
- cpp, linux, macos
- 領域
- performance
研究方向
This is a cross-cutting ARM64 optimisation proposal spanning src/butil/atomicops_internals_arm64_gcc.h, src/butil/atomicops.h, src/butil/crc32c.cc, src/butil/processor.h, src/butil/time.h and src/butil/time.cpp. Read those files and the two already-linked pull requests (#3752 and #3756) before starting, since work is under way. Done would mean baseline -march=armv8-a builds pass, runtime dispatch works on Linux and Apple Silicon, and the stated benchmarks improve.
由索引模型根據 Issue 內容生成。
描述
Is your feature request related to a problem?
While profiling StarRocks (which relies heavily on brpc, bthread, and butil for RPC and concurrency) on modern ARM64 (aarch64) servers, CPU profiles highlighted several legacy ARM64 bottlenecks in butil where brpc was falling back to unoptimised software paths or legacy ARMv8.0 inline assembly:
-
Legacy
ldxr/stxrinline assembly and redundantdmb ishfences insrc/butil/atomicops_internals_arm64_gcc.h:
atomicops_internals_arm64_gcc.his a 2014 Chromium import that hardcodes literalldxr/stxrload-linked/store-conditional loops forNoBarrier_CompareAndSwap,NoBarrier_AtomicExchange, andNoBarrier_AtomicIncrement, and wraps them in fullMemoryBarrier()(dmb ish) fences forAcquire_CompareAndSwap,Release_CompareAndSwap, andBarrier_AtomicIncrement(even emitting a fulldmb ishwhenAcquire_CompareAndSwapfails). Because these are opaque assembly strings, GCC and Clang cannot emit ARMv8.1-A LSE instructions (cas,casa,casl,swp,ldadd,ldaddal) or-moutline-atomicsruntime LSE helpers (__aarch64_cas*,__aarch64_ldadd*,__aarch64_swp*). Additionally,src/butil/atomicops.hroutes Apple Silicon (OS_MACOSX+ARCH_CPU_ARM64) to deprecated<libkern/OSAtomic.h>routines instead ofatomicops_internals_arm64_gcc.h. -
Missing ARM64 hardware CRC32C acceleration in
src/butil/crc32c.cc:
src/butil/crc32c.ccaccelerates CRC32C on x86_64 (__SSE4_2__) and RISC-V (__riscv_zbc/__riscv_zvbc), but has no ARM64 (__aarch64__) hardware path. On ARM64,butil::crc32c::IsFastCrc32Supported()returnsfalseandbutil::crc32c::Extend()falls back toExtendImpl<SlowCRC32Functor>(a ~1.2 GiB/s 4-byte software table lookup), despiteFEAT_CRC32andFEAT_PMULLbeing standard on modern ARM64 server and desktop CPUs. -
Ineffective
yieldspin-wait hint on 64-bit ARM insrc/butil/processor.h:
cpu_relax()onARCH_CPU_ARM_FAMILYemitsasm volatile("yield\n": : :"memory"). On single-threaded-per-core ARM64 CPUs (Neoverse N1/N2/V1/V2, Graviton 2/3/4, Ampere Altra, Apple Silicon),yieldexecutes as a 1-cycle (~0.34 ns)nop, causing spin-wait loops (BT_LOOP_WHENinbthread, mutex/butex spinloops, and work-stealing queues) to hammer the load/store unit and cache-coherency fabric at full frequency. -
Unserialised
cntvct_el0reads and runtime 64-bit integer divisions insrc/butil/time.h/src/butil/time.cpp:
WhenBUTIL_USE_CPU_FREQUENCYis enabled on__aarch64__,butil::detail::clock_cycles()readscntvct_el0without anisbbarrier, allowing out-of-order execution to speculatively hoist counter reads ahead of earlier loads or branches (per ARM DDI 0487, Chapter D12 "The Generic Timer in AArch64 state", Section D12.2.2 "The virtual counter"). Furthermore,cpuwide_time_ns()executes two dependent 64-bit integer divisions (tsc / cpu_freqandremain * 1000000000L / cpu_freq) on every call instead of using a precomputed fixed-point multiplier and shift.
Describe the solution you'd like
Address these four areas in small, self-contained PRs that preserve full backward API/ABI compatibility and support both GCC and Clang on Linux AArch64 and Apple Silicon (__aarch64__):
- Modernise ARM64
butil::subtleatomics (src/butil/atomicops_internals_arm64_gcc.h,src/butil/atomicops.h):- Replace hardcoded
ldxr/stxrinline assembly anddmb ishwrappers forAtomic32andAtomic64with GCC/Clang__atomic_*built-ins (__atomic_compare_exchange_n,__atomic_exchange_n,__atomic_add_fetch,__atomic_store_n,__atomic_load_n, and__atomic_thread_fence), and routeCOMPILER_GCC && ARCH_CPU_ARM64ahead ofOS_MACOSX.
- Replace hardcoded
- Add ARM64 hardware CRC32C (
+crcand+crypto/PMULL) acceleration (src/butil/crc32c.cc):- Add an
#if defined(__aarch64__)hardware path using<arm_acle.h>(__crc32cb,__crc32ch,__crc32cw,__crc32cd) and<arm_neon.h>(vmull_p643-way pipelined lane folding) with function target attributes (target("+crc")/target("+crc+crypto")) and runtime capability detection (getauxval(AT_HWCAP)on Linux,sysctlbynameon macOS) so baseline-march=armv8-abuilds compile cleanly and dispatch at runtime.
- Add an
- Use
isbforcpu_relax()on 64-bit ARM (src/butil/processor.h):- Update
cpu_relax()onARCH_CPU_ARM64to emitasm volatile("isb\n": : :"memory")(~15–30 cycle local pipeline pause without coherency traffic, matching MySQL, PostgreSQL, RocksDB, Folly, and OpenJDK), while keeping 32-bitARCH_CPU_ARM_FAMILYonyield.
- Update
- Serialise
clock_cycles()and optimisecpuwide_time_ns()(src/butil/time.h,src/butil/time.cpp):- Precede
mrs %0, cntvct_el0withisbinbutil::detail::clock_cycles()on__aarch64__, and replace the two runtime 64-bit integer divisions incpuwide_time_ns()with a precomputed 128-bitmul + shiftscale factor initialised alongsidedetail::invariant_cpu_freq.
- Precede
Describe alternatives you've considered
- Requiring global
-march=armv8.2-a+crc+cryptobuild flags: rejected so thatbrpccontinues to build and run safely on baseline-march=armv8-atargets while using runtime detection (AT_HWCAP/-moutline-atomics) to take advantage of modern ARM64 extensions where available.
Additional context/screenshots
Verified on both Linux AArch64 (ARM Neoverse-V2, Ubuntu 24.04, GCC 13.3.0) and Apple Silicon (macOS arm64, Apple Clang 21):
- Atomics (4-thread contention on Neoverse-V2):
Barrier_AtomicIncrementimproves from46.34 ns/opto15.39 ns/op(3.01x faster with baseline-march=armv8-aoutline LSE) and7.48 ns/op(6.20x faster with-march=armv8-a+lse); contended CAS spinlock improves from68.01 ns/opto29.74 ns/op(2.29x faster). - CRC32C (64 KiB buffers, compiled with baseline
-march=armv8-a): Throughput increases from1.20 GiB/sto20.73 GiB/s(17.3x faster) on Neoverse-V2 and from1.12 GiB/sto28.20 GiB/s(25.2x faster) on Apple Silicon. - Spin-wait (
cpu_relax()on Neoverse-V2): Replaces a0.34 ns(1-cyclenop)yieldwith a9.04 ns(~26-cycle) localisbpipeline pause. - Fast timer (
cpuwide_time_ns()scaling math): Replaces two dependent 64-bitudivinstructions withmul/umulh+lsr, reducing scaling latency from7.91 nsto3.04 ns(2.6x faster) on Neoverse-V2 (18 nsto8 nsper call on Apple Silicon).
- 主要語言
- C++
- 星號
- 17.6k
- 分支
- 4.2k
- 平均合併
- 3 天 17 小時
- 30 天內合併 PR
- 72
環境準備
- 提供 Dockerfile 或 Docker Compose 檔案
- 有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
apache/brpc 的其他 Issue
-
難度 1/5 1-3 小時 新手友好度 88/100
維護者通常 2 天內回覆
-
endpoint2hostname() prints a byte-swapped port for IPv6 endpoints可能已有人在做 @0z5a 於 8 天前認領。 未關閉
難度 2/5 1-3 小時 新手友好度 86/100
維護者通常 2 天內回覆
-
file:// naming service takes a comment after the address as the tag可能已有人在做 @0z5a 於 8 天前認領。 未關閉
難度 2/5 1-3 小時 新手友好度 35/100
維護者通常 2 天內回覆
-
難度 5/5 一週以上 新手友好度 25/100
維護者通常 2 天內回覆
-
enhancement
難度 5/5 一週以上 新手友好度 20/100
維護者通常 2 天內回覆
相似的 Issue
-
agent:Windows bug in progress pri:medium sev:low testing windows
難度 1/5 1 小時以內 新手友好度 91/100
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 77/100
protocolbuffers/protobuf#31768 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 76/100
godotengine/godot#125651 ·
維護者通常 1 天內回覆
-
Android: with view recycling on, a recycled View keeps its nextFocusLeft可能已有人在做 關聯的 PR 仍在進行中或已合併。 未關閉Needs: Triage :mag: Platform: Android
難度 2/5 1-3 小時 新手友好度 78/100
react/react-native#60167 ·
維護者通常 2 天內回覆
-
documentation
難度 2/5 1-3 小時 新手友好度 63/100