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
-
难度 2/5 1-3 小时 新手友好度 77/100
protocolbuffers/protobuf#31768 ·
维护者通常 1 天内回复
-
Needs: Triage :mag: Platform: Android
难度 2/5 1-3 小时 新手友好度 78/100
react/react-native#60167 ·
维护者通常 2 天内回复
-
documentation
难度 2/5 1-3 小时 新手友好度 63/100
-
tests: ofx_render_test doesn't compile against FFmpeg 4.x (const AVCodec* to av_find_best_stream)未关闭
难度 2/5 1-3 小时 新手友好度 78/100
NatronGitHub/openfx-io#221 · 1 条评论 ·
维护者通常 1 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 72/100
subsurface/subsurface#5145 ·
维护者通常 2 天内回复