GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Adaptive pool false-sharing · Issue #14757 · netty/netty

Adaptive pool false-sharing #14757

New issue
Copy link
New issue
Copy link
Open
Task
Open
Adaptive pool false-sharing#14757
Task
Copy link
Labels
benchmarkAffect the JMH benchmarks

Description

@franz1981
franz1981
opened on Jan 31, 2025
Issue body actions

Running

netty/microbench/src/main/java/io/netty/microbench/buffer/AdaptiveByteBufAllocatorConcurrentNoCacheBenchmark.java

Line 33 in 1cfd3a6

public class AdaptiveByteBufAllocatorConcurrentNoCacheBenchmark extends AbstractMicrobenchmark {
results shows a huge degrees of variability which seems to point to a false-sharing problem - if using perfasm to check what's going on.

This is pretty visible in my beefy machine (Ryzen) and -prof perfasm pointed to this

   0.16%     0x00007f4ff013aebb:   mov    0x14(%r11),%r11d             ;*getfield direct {reexecute=0 rethrow=0 return_oop=0}
                                                                       ; - io.netty.buffer.AdaptiveByteBufAllocator::newDirectBuffer@1 (line 70)
                                                                       ; - io.netty.buffer.AbstractByteBufAllocator::directBuffer@21 (line 188)
                                                                       ; - io.netty.buffer.AbstractByteBufAllocator::directBuffer@4 (line 179)
                                                                       ; - io.netty.microbench.buffer.ByteBufAllocatorConcurrentBenchmark::allocateReleaseAdaptive@7 (line 61)
                                                                       ; - io.netty.microbench.buffer.jmh_generated.ByteBufAllocatorConcurrentBenchmark_allocateReleaseAdaptive_jmhTest::allocateReleaseAdaptive_thrpt_jmhStub@17 (line 121)
  35.75%     0x00007f4ff013aebf:   mov    0x20(%r12,%r11,8),%ebp       ; implicit exception: dispatches to 0x00007f4ff013cb00
                                                                       ;*getfield threadLocalMagazine {reexecute=0 rethrow=0 return_oop=0}

where the 35% cost (!!!!) is the field at byte 32 of the allocator i.e. the final field threadLocalMagazine
which is super stable and just got Magazine[] near to it.
These findings can change a lot based on the run, so it's not easy to find "all" the culprits/hot spots, if not running enough forks and manually inspecting the assembly to see if something popup for "slow" runs.

Indeed, in another run I got

   0.00%     0x00007f087c1eb497:   tzcnt  %ecx,%ebx                                                                       
   0.02%     0x00007f087c1eb49b:   test   %ebx,%ebx
             0x00007f087c1eb49d:   jle    0x00007f087c1eb998                                                                       
   0.00%     0x00007f087c1eb4a3:   movslq %esi,%rcx
   0.03%     0x00007f087c1eb4a6:   lea    -0x1(%rsi),%r11d
             0x00007f087c1eb4aa:   dec    %rcx
             0x00007f087c1eb4ad:   and    %r10,%rcx
             0x00007f087c1eb4b0:   mov    %ecx,%ecx                                                                       
   0.20%     0x00007f087c1eb4b2:   mov    %r11d,%r13d
             0x00007f087c1eb4b5:   and    %ecx,%r13d                                                                       
   0.00%     0x00007f087c1eb4b8:   test   %esi,%esi
             0x00007f087c1eb4ba:   jbe    0x00007f087c1eb7cc
   0.00%     0x00007f087c1eb4c0:   lea    (%r12,%r9,8),%rax
   0.09%     0x00007f087c1eb4c4:   mov    0x10(%rax,%r13,4),%r13d      
  28.30%     0x00007f087c1eb4c9:   vmovq  %xmm0,%rsi

where now the cost is

               for (int i = 0, m = Integer.numberOfTrailingZeros(~mask); i < m; i++) {
                    Magazine mag = mags[index + i & mask];

the last mov at 0x00007f087c1eb4c4 which is reading out of the mags array in the loop, is getting blamed - whilst in other runs, it never did...

In term of variability, on a NUMA-like system like the Ryzen, is pretty visible (even running with -t 4 - but got more fuzzy and still evident with t === number of cores clearly) - and make evident a bi-modal distribution of outcomes.

Image

this shows pretty easily which perfasm output we have to search for since, in the second fork, there's an evident performance difference compared to the first one - but sadly is pretty randomic.

Detecting false-sharing is never easy since even heap objects (e.g. the reference counters in buffers) which are not padded, whilst modified over and over, can interfere with loading of final stable and immutable innocent fields, causing it.
For this reason, I won't consider this as a blocker to adopt the pool and to be very dependent by the memory layout the microbenchmark would provide (both at startup or after compacting objects), but still is an existing problem.
If that can be solved, better - if not, let's make sure it doesn't involve sharing which can happen regardless the heap positioning of "noisy neighbours" (heavy writers) vs heavy readers (or among heavy writers) - but that requires some bit of assembly analysis and profiling, I believe.

Reacted by old driver

Activity

  1. franz1981
    added
    benchmarkAffect the JMH benchmarks
    on Jan 31, 2025
  2. franz1981 commented on Jan 31, 2025

    @franz1981
    franz1981
    on Jan 31, 2025
    ContributorAuthor
    More actions

    @laosijikaichele @chrisvest if you are curious

  3. laosijikaichele commented on Feb 2, 2025

    @laosijikaichele
    laosijikaichele
    on Feb 2, 2025
    Contributor
    More actions

    where now the cost is

               for (int i = 0, m = Integer.numberOfTrailingZeros(~mask); i < m; i++) {
                    Magazine mag = mags[index + i & mask];
    

    the last mov at 0x00007f087c1eb4c4 which is reading out of the mags array in the loop, is getting blamed - whilst in other runs, it never did...

    So, if we can reduce this loop counts, then the cost of false-sharing will be reduced too? Just like this PR did, which may reduce the loop counts.

  4. franz1981 commented on Feb 2, 2025

    @franz1981
    franz1981
    on Feb 2, 2025
    ContributorAuthor
    More actions

    Depends, statistically yep, but the reality is that since the array is read mostly just padding it would help - apart from the length field which is stored near the class header (at 0xc iirc) and can still suffer from noisy neighbors

    Reacted by old driver
  5. franz1981 commented on Feb 2, 2025

    @franz1981
    franz1981
    on Feb 2, 2025
    ContributorAuthor
    More actions

    And consider that I run the tests already with that changes in

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    benchmarkAffect the JMH benchmarks

    Type

    Task

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.