Skip to content

ARM64 threading optimization - atomic ordering and cache line alignment - #95

Merged
glorv merged 3 commits into
tikv:masterfrom
MonakaResearch:yatp_optimization
Aug 25, 2026
Merged

ARM64 threading optimization - atomic ordering and cache line alignment#95
glorv merged 3 commits into
tikv:masterfrom
MonakaResearch:yatp_optimization

Conversation

@mohakgoel1

@mohakgoel1 mohakgoel1 commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

yatp: ARM64 threading optimization, Atomic ordering and cache line alignment

Background

yatp was originally optimized primarily for x86. On ARM64, unnecessary SeqCst atomic ordering and false sharing can add performance overhead due to ARM's weaker memory model and 128-byte cache lines on server CPUs such as AWS Graviton and ARM Neoverse.

Changes

1. Atomic ordering

Replaced unnecessary SeqCst operations with the minimum required memory ordering across 33 operations in 4 files:

  • task/future.rs
  • pool/spawn.rs
  • pool/builder.rs
  • queue/multilevel.rs

Used Acquire, Release, or AcqRel based on the synchronization requirements of each operation.

These changes reduce unnecessary memory barriers on ARM64 while preserving the existing synchronization behavior. No logic changes were made.

2. Cache line alignment

Added a custom CacheAligned struct in pool/spawn.rs to prevent false sharing between active_workers and global_queue.

The struct uses explicit alignment:

  • 128 bytes for aarch64, x86_64, and powerpc64
  • 64 bytes for 32-bit arm

This ensures appropriate cache line separation across supported architectures.

Benchmark Results

TPC-C benchmark with 1 TiKV server and 750 warehouses:

Platform Optimized TPMC Base TPMC Improvement
ARM64 153,464 146,582 +5%
x86 169,087 166,125 +2%

The optimization provides a larger improvement on ARM64, while also showing a smaller positive impact on x86.

Correctness

  • All 40 existing yatp unit tests pass.
  • No functional or logic changes were introduced.
  • Each memory ordering change was reviewed against its corresponding synchronization operation.
  • ARM64 disassembly was checked to verify the expected instruction selection.

This PR is a joint contribution by:

Summary by CodeRabbit

  • Performance

    • Reduced contention around worker activity tracking.
    • Lowered synchronization overhead during task wake-up, polling, scaling, and queue coordination.
  • Bug Fixes

    • Improved reliability of concurrent scheduling, including worker shutdown, sleeping, waking, and task state transitions.
    • Strengthened synchronization for thread-count configuration and multilevel queue maintenance.

@ti-chi-bot

ti-chi-bot Bot commented Jul 28, 2026

Copy link
Copy Markdown

Welcome @mohakgoel1! It looks like this is your first PR to tikv/yatp 🎉

@coderabbitai

coderabbitai Bot commented Jul 28, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 05e05d92-3c57-4fe1-a0fb-c5d291bd0974

📥 Commits

Reviewing files that changed from the base of the PR and between cffc852 and 9d513e5.

📒 Files selected for processing (1)
  • src/pool/spawn.rs

Included review availability: Your plan includes up to 2 reviews per rolling hour; 1 remains after this review.


📝 Walkthrough

Walkthrough

This change replaces selected SeqCst atomic operations with targeted Acquire, Release, and AcqRel orderings. It also adds cache-line padding for active worker state. Existing pool, queue, and task state transitions remain unchanged.

Changes

Atomic synchronization updates

Layer / File(s) Summary
Pool configuration ordering
src/pool/builder.rs
Core thread count cloning, updates, and range clamping now use targeted atomic orderings.
Worker state and cache alignment
src/pool/spawn.rs
active_workers uses CachePadded<AtomicUsize>. Worker scaling, sleeping, waking, shutdown, and queue checks use targeted orderings.
Queue and task synchronization
src/queue/multilevel.rs, src/task/future.rs
Multilevel queue maintenance and task status transitions replace SeqCst operations with Acquire, Release, and AcqRel orderings.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Merge Risk: 🔵 Low · up to 9d513

The optimization may not fully prevent cache-line contention on some AArch64 systems, limiting the expected performance improvement. The PR remains mergeable with explicit owner follow-up to verify or provide target-specific alignment.

Suggested reviewers: busyjay

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the ARM64 threading optimizations, including atomic ordering changes and cache-line alignment.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/pool/spawn.rs`:
- Around line 17-22: Update the CACHE_LINE_SIZE architecture configuration in
spawn.rs to separate aarch64 from the 128-byte architectures and set it to 256
bytes. Apply the corresponding 256-byte #[repr(C, align(...))] alignment and
padding to the active_workers-related structures or fields in the referenced
sections, while preserving the existing 128-byte behavior for x86_64 and
powerpc64.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 4417f7c0-9b58-4156-8c5d-381eefa116bd

📥 Commits

Reviewing files that changed from the base of the PR and between 64ccad6 and fdcf665.

📒 Files selected for processing (4)
  • src/pool/builder.rs
  • src/pool/spawn.rs
  • src/queue/multilevel.rs
  • src/task/future.rs

Comment thread src/pool/spawn.rs Outdated
Signed-off-by: mohakgoel1 <mohak.goel@fujitsu.com>
Signed-off-by: mohakgoel1 <mohak.goel@fujitsu.com>
Comment thread src/pool/spawn.rs Outdated
QueueCore {
global_queue,
active_workers: AtomicUsize::new(config.max_thread_count << WORKER_COUNT_SHIFT),
active_workers: CacheAligned::new(config.max_thread_count << WORKER_COUNT_SHIFT),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

How about crossbeam::utils::CachePadded?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hi @BusyJay,
Thanks for the review and for pointing this out.

We considered crossbeam_utils::CachePadded but moved away from it for two reasons that came up earlier in this review:

  • On 32-bit arm, CachePadded aligns to only 32 bytes, which may not fully prevent false sharing on all arm32 cache line sizes.
  • On aarch64, CachePadded aligns to 128 bytes, but the Fujitsu A64FX a production aarch64 server chip used in supercomputing uses 256-byte cache lines. With 128-byte alignment, active_workers could still share a cache line with an adjacent QueueCore field on that platform.

The explicit #[repr(C, align(N))] struct with per-platform #[cfg] blocks gives us full control over both size and alignment on every target, covering all the above cases correctly. The comment above the struct documents this reasoning so future readers understand why CachePadded was not used.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you also send a patch to crossbeam and see whether they accept the changes?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If they don't, we can reuse definitions from crossbeam by reexporting them in wrapper mode as a fallback, and only define what we know to be wrong in our own crate.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thank you for the suggestion. Our thought is to go with Option 2 the wrapper/fallback pattern feels like the right approach here. It keeps the custom logic minimal by only overriding the two platforms where crossbeam's alignment is known to be insufficient (aarch64 at 256 bytes and arm at 64 bytes), and re-exports CachePadded directly for everything else. This avoids duplicating crossbeam's full platform logic in yatp, is easier to maintain long term, and if crossbeam ever updates their alignment values upstream, removing our overrides becomes a one-line cleanup.

What are your thoughts on proceeding with Option 2?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hi @BusyJay, Could you please confirm if we should proceed with the wrapper/fallback approach?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hi @BusyJay, Just following up on my previous comment. Could you please confirm if we can proceed with the wrapper/fallback approach? Thanks!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hi @BusyJay,
We've pushed a commit removing the custom CacheAligned struct and replacing it with CachePadded for active_workers in QueueCore. On aarch64 this gives 128-byte alignment, which is safe and correct.

Let me know if this looks good to you, or if you'd like any further changes before merging.

@mohakgoel1
mohakgoel1 requested a review from BusyJay July 29, 2026 10:34
…ive_workers

Signed-off-by: mohakgoel1 <mohak.goel@fujitsu.com>

@glorv glorv left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@mohakgoel1

Copy link
Copy Markdown
Contributor Author

Hi @BusyJay, @Connor1996, @gengliqi,
Just a gentle reminder regarding the review of this PR.

We have addressed the previous feedback and updated the PR accordingly. Could you please take a look when you get a chance? Your review and feedback would be greatly appreciated.

@glorv
glorv merged commit 7ca1723 into tikv:master Aug 25, 2026
22 checks passed
@glorv

glorv commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

Thank you @mohakgoel1 for your contribution~

@mohakgoel1

Copy link
Copy Markdown
Contributor Author

Thank you @mohakgoel1 for your contribution~

Thank you for taking the time to review the PR and for your valuable feedback. I really appreciate your support and help in getting it merged!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants