Skip to content

Harden ref-counting concurrency semantics - #8583

Merged
normanmaurer merged 5 commits into
netty:4.1from
njhill:ref-compromise2
Nov 29, 2018
Merged

normanmaurer merged 5 commits into
netty:4.1from
njhill:ref-compromise2

Conversation

@njhill

@njhill njhill commented Nov 20, 2018 •

Copy link
Copy Markdown
Member

Motivation

#8563 highlighted race conditions introduced by the prior optimistic update optimization in 83a19d5. These were known at the time but considered acceptable given the perf benefit in high contention scenarios.

This PR proposes a modified approach which provides roughly half the gains but stronger concurrency semantics. Race conditions still exist but their scope is narrowed to much less likely cases (releases
coinciding with retain overflow), and even in those cases certain guarantees are still assured. Once release() returns true, all subsequent release/retains are guaranteed to throw, and in particular deallocate() will be called at most once.

Modifications

  • Use even numbers internally (including -ve) for live refcounts
  • "Final" release changes to odd number (equivalent to refcount 0)
  • Retain still uses faster getAndAdd, release uses CAS loop
  • First CAS attempt uses non-volatile read
  • Thread.yield() after a failed CAS appears to provide a net gain
  • New tests in AbstractReferenceCountedTest written by @normanmaurer

Result

More (though not completely) robust concurrency semantics for ref counting; increased latency under high contention, but still roughly twice as fast as the original logic. Bench results to follow.

Fixes #8563

Motivation

netty#8563 highlighted race conditions introduced by the prior optimistic
update optimization in 83a19d5. These
were known at the time but considered acceptable given the perf
benefit in high contention scenarios.

This PR proposes a modified approach which provides roughly half the
gains but stronger concurrency semantics. Race conditions still exist
but their scope is narrowed to much less likely cases (releases
coinciding with retain overflow), and even in those
cases certain guarantees are still assured. Once release() returns true,
all subsequent release/retains are guaranteed to throw, and in
particular deallocate will be called at most once.

Modifications

- Use even numbers internally (including -ve) for live refcounts
- "Final" release changes to odd number (equivalent to refcount 0)
- Retain still uses faster getAndAdd, release uses CAS loop
- First CAS attempt uses non-volatile read
- Thread.yield() after a failed CAS provides a net gain

Result

More (though not completely) robust concurrency semantics for ref
counting; increased latency under high contention, but still roughly
twice as fast as the original logic. Bench results to follow
@netty-bot

Copy link
Copy Markdown

Can one of the admins verify this patch?

@normanmaurer

Copy link
Copy Markdown
Member

@netty-bot test this please

@njhill

njhill commented Nov 20, 2018

Copy link
Copy Markdown
Member Author

Benchmark results:

Pre- 83a19d5:

Benchmark                           (delay)    Mode       Cnt         Score    Error  Units
retainReleaseContended                    1  sample  36460170      1152.856 ± 14.343  ns/op
retainReleaseContended·p0.00              1  sample                  37.000           ns/op
retainReleaseContended·p0.50              1  sample                 807.000           ns/op
retainReleaseContended·p0.90              1  sample                1858.000           ns/op
retainReleaseContended·p0.95              1  sample                2320.000           ns/op
retainReleaseContended·p0.99              1  sample                3524.000           ns/op
retainReleaseContended·p0.999             1  sample               16704.000           ns/op
retainReleaseContended·p0.9999            1  sample              250880.000           ns/op
retainReleaseContended·p1.00              1  sample            32309248.000           ns/op
retainReleaseContended                   10  sample  38184690      1119.333 ± 15.226  ns/op
retainReleaseContended·p0.00             10  sample                  45.000           ns/op
retainReleaseContended·p0.50             10  sample                 770.000           ns/op
retainReleaseContended·p0.90             10  sample                1748.000           ns/op
retainReleaseContended·p0.95             10  sample                2184.000           ns/op
retainReleaseContended·p0.99             10  sample                3312.000           ns/op
retainReleaseContended·p0.999            10  sample               16224.000           ns/op
retainReleaseContended·p0.9999           10  sample              258695.910           ns/op
retainReleaseContended·p1.00             10  sample            28803072.000           ns/op
retainReleaseContended                  100  sample  40444975      1040.120 ± 11.997  ns/op
retainReleaseContended·p0.00            100  sample                 229.000           ns/op
retainReleaseContended·p0.50            100  sample                 791.000           ns/op
retainReleaseContended·p0.90            100  sample                1482.000           ns/op
retainReleaseContended·p0.95            100  sample                1780.000           ns/op
retainReleaseContended·p0.99            100  sample                2540.000           ns/op
retainReleaseContended·p0.999           100  sample               14528.000           ns/op
retainReleaseContended·p0.9999          100  sample              184576.000           ns/op
retainReleaseContended·p1.00            100  sample            27295744.000           ns/op
retainReleaseContended                 1000  sample  29429689      2842.698 ± 26.030  ns/op
retainReleaseContended·p0.00           1000  sample                2164.000           ns/op
retainReleaseContended·p0.50           1000  sample                2444.000           ns/op
retainReleaseContended·p0.90           1000  sample                2520.000           ns/op
retainReleaseContended·p0.95           1000  sample                2588.000           ns/op
retainReleaseContended·p0.99           1000  sample                2820.000           ns/op
retainReleaseContended·p0.999          1000  sample               38912.000           ns/op
retainReleaseContended·p0.9999         1000  sample              535552.000           ns/op
retainReleaseContended·p1.00           1000  sample            29949952.000           ns/op
retainReleaseContended                10000  sample  24367466     25048.634 ± 56.891  ns/op
retainReleaseContended·p0.00          10000  sample               20736.000           ns/op
retainReleaseContended·p0.50          10000  sample               23616.000           ns/op
retainReleaseContended·p0.90          10000  sample               23744.000           ns/op
retainReleaseContended·p0.95          10000  sample               24352.000           ns/op
retainReleaseContended·p0.99          10000  sample               38144.000           ns/op
retainReleaseContended·p0.999         10000  sample              273680.896           ns/op
retainReleaseContended·p0.9999        10000  sample             2613248.000           ns/op
retainReleaseContended·p1.00          10000  sample            59965440.000           ns/op
retainReleaseUncontended                1  sample   7509511        55.367 ±  2.721  ns/op
retainReleaseUncontended·p0.00          1  sample                  35.000           ns/op
retainReleaseUncontended·p0.50          1  sample                  41.000           ns/op
retainReleaseUncontended·p0.90          1  sample                  52.000           ns/op
retainReleaseUncontended·p0.95          1  sample                  62.000           ns/op
retainReleaseUncontended·p0.99          1  sample                  98.000           ns/op
retainReleaseUncontended·p0.999         1  sample                 359.488           ns/op
retainReleaseUncontended·p0.9999        1  sample               18305.562           ns/op
retainReleaseUncontended·p1.00          1  sample             5824512.000           ns/op
retainReleaseUncontended               10  sample   6129780        73.046 ±  5.091  ns/op
retainReleaseUncontended·p0.00         10  sample                  43.000           ns/op
retainReleaseUncontended·p0.50         10  sample                  53.000           ns/op
retainReleaseUncontended·p0.90         10  sample                  68.000           ns/op
retainReleaseUncontended·p0.95         10  sample                  73.000           ns/op
retainReleaseUncontended·p0.99         10  sample                 106.000           ns/op
retainReleaseUncontended·p0.999        10  sample                2280.876           ns/op
retainReleaseUncontended·p0.9999       10  sample               18816.000           ns/op
retainReleaseUncontended·p1.00         10  sample             8978432.000           ns/op
retainReleaseUncontended              100  sample   6601891       279.857 ±  1.515  ns/op
retainReleaseUncontended·p0.00        100  sample                 219.000           ns/op
retainReleaseUncontended·p0.50        100  sample                 247.000           ns/op
retainReleaseUncontended·p0.90        100  sample                 273.000           ns/op
retainReleaseUncontended·p0.95        100  sample                 291.000           ns/op
retainReleaseUncontended·p0.99        100  sample                 337.000           ns/op
retainReleaseUncontended·p0.999       100  sample               13824.000           ns/op
retainReleaseUncontended·p0.9999      100  sample               21440.000           ns/op
retainReleaseUncontended·p1.00        100  sample             2428928.000           ns/op
retainReleaseUncontended             1000  sample   5385984      2354.879 ±  6.543  ns/op
retainReleaseUncontended·p0.00       1000  sample                1970.000           ns/op
retainReleaseUncontended·p0.50       1000  sample                2156.000           ns/op
retainReleaseUncontended·p0.90       1000  sample                2312.000           ns/op
retainReleaseUncontended·p0.95       1000  sample                2448.000           ns/op
retainReleaseUncontended·p0.99       1000  sample               15088.000           ns/op
retainReleaseUncontended·p0.999      1000  sample               20896.000           ns/op
retainReleaseUncontended·p0.9999     1000  sample               32793.696           ns/op
retainReleaseUncontended·p1.00       1000  sample             4546560.000           ns/op
retainReleaseUncontended            10000  sample   4314810     23143.254 ± 28.804  ns/op
retainReleaseUncontended·p0.00      10000  sample               19488.000           ns/op
retainReleaseUncontended·p0.50      10000  sample               21344.000           ns/op
retainReleaseUncontended·p0.90      10000  sample               26144.000           ns/op
retainReleaseUncontended·p0.95      10000  sample               35712.000           ns/op
retainReleaseUncontended·p0.99      10000  sample               50688.000           ns/op
retainReleaseUncontended·p0.999     10000  sample               65792.000           ns/op
retainReleaseUncontended·p0.9999    10000  sample              143492.838           ns/op
retainReleaseUncontended·p1.00      10000  sample            12255232.000           ns/op

Current (before):

Benchmark                           (delay)    Mode       Cnt         Score    Error  Units
retainReleaseContended                    1  sample  33617682       396.846 ± 12.371  ns/op
retainReleaseContended·p0.00              1  sample                  36.000           ns/op
retainReleaseContended·p0.50              1  sample                 267.000           ns/op
retainReleaseContended·p0.90              1  sample                 484.000           ns/op
retainReleaseContended·p0.95              1  sample                 558.000           ns/op
retainReleaseContended·p0.99              1  sample                 725.000           ns/op
retainReleaseContended·p0.999             1  sample                5232.000           ns/op
retainReleaseContended·p0.9999            1  sample              124544.000           ns/op
retainReleaseContended·p1.00              1  sample            40239104.000           ns/op
retainReleaseContended                   10  sample  30765523       422.296 ± 12.817  ns/op
retainReleaseContended·p0.00             10  sample                  45.000           ns/op
retainReleaseContended·p0.50             10  sample                 289.000           ns/op
retainReleaseContended·p0.90             10  sample                 479.000           ns/op
retainReleaseContended·p0.95             10  sample                 551.000           ns/op
retainReleaseContended·p0.99             10  sample                 728.000           ns/op
retainReleaseContended·p0.999            10  sample                5888.000           ns/op
retainReleaseContended·p0.9999           10  sample              140032.000           ns/op
retainReleaseContended·p1.00             10  sample            23265280.000           ns/op
retainReleaseContended                  100  sample  26334520       475.976 ± 13.347  ns/op
retainReleaseContended·p0.00            100  sample                 235.000           ns/op
retainReleaseContended·p0.50            100  sample                 353.000           ns/op
retainReleaseContended·p0.90            100  sample                 433.000           ns/op
retainReleaseContended·p0.95            100  sample                 462.000           ns/op
retainReleaseContended·p0.99            100  sample                 535.000           ns/op
retainReleaseContended·p0.999           100  sample               11152.000           ns/op
retainReleaseContended·p0.9999          100  sample              156300.262           ns/op
retainReleaseContended·p1.00            100  sample            26771456.000           ns/op
retainReleaseContended                 1000  sample  29409220      2954.896 ± 35.426  ns/op
retainReleaseContended·p0.00           1000  sample                2096.000           ns/op
retainReleaseContended·p0.50           1000  sample                2424.000           ns/op
retainReleaseContended·p0.90           1000  sample                2468.000           ns/op
retainReleaseContended·p0.95           1000  sample                2496.000           ns/op
retainReleaseContended·p0.99           1000  sample                2668.000           ns/op
retainReleaseContended·p0.999          1000  sample               56192.000           ns/op
retainReleaseContended·p0.9999         1000  sample              668672.000           ns/op
retainReleaseContended·p1.00           1000  sample            49283072.000           ns/op
retainReleaseContended                10000  sample  24335420     25097.878 ± 65.738  ns/op
retainReleaseContended·p0.00          10000  sample               21344.000           ns/op
retainReleaseContended·p0.50          10000  sample               23616.000           ns/op
retainReleaseContended·p0.90          10000  sample               23712.000           ns/op
retainReleaseContended·p0.95          10000  sample               24352.000           ns/op
retainReleaseContended·p0.99          10000  sample               38976.000           ns/op
retainReleaseContended·p0.999         10000  sample              253952.000           ns/op
retainReleaseContended·p0.9999        10000  sample             2793472.000           ns/op
retainReleaseContended·p1.00          10000  sample            58261504.000           ns/op
retainReleaseUncontended                1  sample   4484558        58.703 ±  1.546  ns/op
retainReleaseUncontended·p0.00          1  sample                  33.000           ns/op
retainReleaseUncontended·p0.50          1  sample                  39.000           ns/op
retainReleaseUncontended·p0.90          1  sample                  50.000           ns/op
retainReleaseUncontended·p0.95          1  sample                  65.000           ns/op
retainReleaseUncontended·p0.99          1  sample                  98.000           ns/op
retainReleaseUncontended·p0.999         1  sample                4296.000           ns/op
retainReleaseUncontended·p0.9999        1  sample               19889.411           ns/op
retainReleaseUncontended·p1.00          1  sample             1775616.000           ns/op
retainReleaseUncontended               10  sample   6199062        73.845 ±  1.339  ns/op
retainReleaseUncontended·p0.00         10  sample                  43.000           ns/op
retainReleaseUncontended·p0.50         10  sample                  53.000           ns/op
retainReleaseUncontended·p0.90         10  sample                  71.000           ns/op
retainReleaseUncontended·p0.95         10  sample                  87.000           ns/op
retainReleaseUncontended·p0.99         10  sample                 122.000           ns/op
retainReleaseUncontended·p0.999        10  sample                2643.748           ns/op
retainReleaseUncontended·p0.9999       10  sample               18752.000           ns/op
retainReleaseUncontended·p1.00         10  sample             1251328.000           ns/op
retainReleaseUncontended              100  sample   6570441       283.853 ±  5.069  ns/op
retainReleaseUncontended·p0.00        100  sample                 219.000           ns/op
retainReleaseUncontended·p0.50        100  sample                 244.000           ns/op
retainReleaseUncontended·p0.90        100  sample                 268.000           ns/op
retainReleaseUncontended·p0.95        100  sample                 280.000           ns/op
retainReleaseUncontended·p0.99        100  sample                 325.000           ns/op
retainReleaseUncontended·p0.999       100  sample               13952.000           ns/op
retainReleaseUncontended·p0.9999      100  sample               21856.000           ns/op
retainReleaseUncontended·p1.00        100  sample             9158656.000           ns/op
retainReleaseUncontended             1000  sample   5345561      2368.951 ±  4.646  ns/op
retainReleaseUncontended·p0.00       1000  sample                1970.000           ns/op
retainReleaseUncontended·p0.50       1000  sample                2112.000           ns/op
retainReleaseUncontended·p0.90       1000  sample                2268.000           ns/op
retainReleaseUncontended·p0.95       1000  sample                2376.000           ns/op
retainReleaseUncontended·p0.99       1000  sample               15424.000           ns/op
retainReleaseUncontended·p0.999      1000  sample               21184.000           ns/op
retainReleaseUncontended·p0.9999     1000  sample               30862.202           ns/op
retainReleaseUncontended·p1.00       1000  sample             2605056.000           ns/op
retainReleaseUncontended            10000  sample   4293861     23226.581 ± 20.925  ns/op
retainReleaseUncontended·p0.00      10000  sample               19488.000           ns/op
retainReleaseUncontended·p0.50      10000  sample               21344.000           ns/op
retainReleaseUncontended·p0.90      10000  sample               27968.000           ns/op
retainReleaseUncontended·p0.95      10000  sample               35840.000           ns/op
retainReleaseUncontended·p0.99      10000  sample               51776.000           ns/op
retainReleaseUncontended·p0.999     10000  sample               65408.000           ns/op
retainReleaseUncontended·p0.9999    10000  sample               98048.000           ns/op
retainReleaseUncontended·p1.00      10000  sample            12369920.000           ns/op

This PR (after):

Benchmark                           (delay)    Mode       Cnt         Score    Error  Units
retainReleaseContended                    1  sample  30710079       566.717 ± 13.469  ns/op
retainReleaseContended·p0.00              1  sample                  40.000           ns/op
retainReleaseContended·p0.50              1  sample                  82.000           ns/op
retainReleaseContended·p0.90              1  sample                 337.000           ns/op
retainReleaseContended·p0.95              1  sample                1972.000           ns/op
retainReleaseContended·p0.99              1  sample                8528.000           ns/op
retainReleaseContended·p0.999             1  sample               27488.000           ns/op
retainReleaseContended·p0.9999            1  sample              211200.000           ns/op
retainReleaseContended·p1.00              1  sample            40632320.000           ns/op
retainReleaseContended                   10  sample  33563252       584.657 ± 14.439  ns/op
retainReleaseContended·p0.00             10  sample                  48.000           ns/op
retainReleaseContended·p0.50             10  sample                  69.000           ns/op
retainReleaseContended·p0.90             10  sample                 223.000           ns/op
retainReleaseContended·p0.95             10  sample                1386.000           ns/op
retainReleaseContended·p0.99             10  sample               10288.000           ns/op
retainReleaseContended·p0.999            10  sample               39872.000           ns/op
retainReleaseContended·p0.9999           10  sample              217088.000           ns/op
retainReleaseContended·p1.00             10  sample            42663936.000           ns/op
retainReleaseContended                  100  sample  26297644       948.177 ± 18.478  ns/op
retainReleaseContended·p0.00            100  sample                 222.000           ns/op
retainReleaseContended·p0.50            100  sample                 386.000           ns/op
retainReleaseContended·p0.90            100  sample                1666.000           ns/op
retainReleaseContended·p0.95            100  sample                2928.000           ns/op
retainReleaseContended·p0.99            100  sample                6704.000           ns/op
retainReleaseContended·p0.999           100  sample               29472.000           ns/op
retainReleaseContended·p0.9999          100  sample              319488.000           ns/op
retainReleaseContended·p1.00            100  sample            55312384.000           ns/op
retainReleaseContended                 1000  sample  29410837      2857.274 ± 24.380  ns/op
retainReleaseContended·p0.00           1000  sample                1984.000           ns/op
retainReleaseContended·p0.50           1000  sample                2440.000           ns/op
retainReleaseContended·p0.90           1000  sample                2500.000           ns/op
retainReleaseContended·p0.95           1000  sample                2552.000           ns/op
retainReleaseContended·p0.99           1000  sample                4064.000           ns/op
retainReleaseContended·p0.999          1000  sample               57600.000           ns/op
retainReleaseContended·p0.9999         1000  sample              547754.189           ns/op
retainReleaseContended·p1.00           1000  sample            48758784.000           ns/op
retainReleaseContended                10000  sample  24728900     24539.149 ± 42.932  ns/op
retainReleaseContended·p0.00          10000  sample               20960.000           ns/op
retainReleaseContended·p0.50          10000  sample               23616.000           ns/op
retainReleaseContended·p0.90          10000  sample               23712.000           ns/op
retainReleaseContended·p0.95          10000  sample               24256.000           ns/op
retainReleaseContended·p0.99          10000  sample               37504.000           ns/op
retainReleaseContended·p0.999         10000  sample              212224.000           ns/op
retainReleaseContended·p0.9999        10000  sample             1312993.075           ns/op
retainReleaseContended·p1.00          10000  sample            45088768.000           ns/op
retainReleaseUncontended                1  sample   6955400        57.365 ±  0.557  ns/op
retainReleaseUncontended·p0.00          1  sample                  37.000           ns/op
retainReleaseUncontended·p0.50          1  sample                  43.000           ns/op
retainReleaseUncontended·p0.90          1  sample                  51.000           ns/op
retainReleaseUncontended·p0.95          1  sample                  58.000           ns/op
retainReleaseUncontended·p0.99          1  sample                  96.000           ns/op
retainReleaseUncontended·p0.999         1  sample                1542.000           ns/op
retainReleaseUncontended·p0.9999        1  sample               18496.000           ns/op
retainReleaseUncontended·p1.00          1  sample              245504.000           ns/op
retainReleaseUncontended               10  sample   6118153        69.344 ±  0.698  ns/op
retainReleaseUncontended·p0.00         10  sample                  43.000           ns/op
retainReleaseUncontended·p0.50         10  sample                  52.000           ns/op
retainReleaseUncontended·p0.90         10  sample                  64.000           ns/op
retainReleaseUncontended·p0.95         10  sample                  68.000           ns/op
retainReleaseUncontended·p0.99         10  sample                 104.000           ns/op
retainReleaseUncontended·p0.999        10  sample                2708.000           ns/op
retainReleaseUncontended·p0.9999       10  sample               18752.000           ns/op
retainReleaseUncontended·p1.00         10  sample              315392.000           ns/op
retainReleaseUncontended              100  sample   6545758       282.383 ±  1.654  ns/op
retainReleaseUncontended·p0.00        100  sample                 220.000           ns/op
retainReleaseUncontended·p0.50        100  sample                 243.000           ns/op
retainReleaseUncontended·p0.90        100  sample                 265.000           ns/op
retainReleaseUncontended·p0.95        100  sample                 275.000           ns/op
retainReleaseUncontended·p0.99        100  sample                 328.000           ns/op
retainReleaseUncontended·p0.999       100  sample               14112.000           ns/op
retainReleaseUncontended·p0.9999      100  sample               21997.571           ns/op
retainReleaseUncontended·p1.00        100  sample             2334720.000           ns/op
retainReleaseUncontended             1000  sample   5326852      2378.207 ±  6.946  ns/op
retainReleaseUncontended·p0.00       1000  sample                1972.000           ns/op
retainReleaseUncontended·p0.50       1000  sample                2104.000           ns/op
retainReleaseUncontended·p0.90       1000  sample                2244.000           ns/op
retainReleaseUncontended·p0.95       1000  sample                2312.000           ns/op
retainReleaseUncontended·p0.99       1000  sample               15488.000           ns/op
retainReleaseUncontended·p0.999      1000  sample               21088.000           ns/op
retainReleaseUncontended·p0.9999     1000  sample               30634.070           ns/op
retainReleaseUncontended·p1.00       1000  sample             5775360.000           ns/op
retainReleaseUncontended            10000  sample   4277948     23315.241 ± 15.261  ns/op
retainReleaseUncontended·p0.00      10000  sample               19488.000           ns/op
retainReleaseUncontended·p0.50      10000  sample               20736.000           ns/op
retainReleaseUncontended·p0.90      10000  sample               34688.000           ns/op
retainReleaseUncontended·p0.95      10000  sample               36992.000           ns/op
retainReleaseUncontended·p0.99      10000  sample               52544.000           ns/op
retainReleaseUncontended·p0.999     10000  sample               66432.000           ns/op
retainReleaseUncontended·p0.9999    10000  sample               95642.253           ns/op
retainReleaseUncontended·p1.00      10000  sample             7290880.000           ns/op

@normanmaurer

Copy link
Copy Markdown
Member

/cc @merlimat

@normanmaurer

Copy link
Copy Markdown
Member

int oldRef = refCntUpdater.getAndAdd(this, adjustIncrement);
if ((oldRef & 1) != 0) {
throw new IllegalReferenceCountException(0, increment);
} else if (oldRef <= 0 ? oldRef + adjustIncrement > 0

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.

nit: remove the else as you return in the previous if block

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.

Also please use {} to make it more readable and consistent with our code style.

return true;
}
if (!firstTry) {
Thread.yield();

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.

please add a comment

return false;
}
if (!firstTry) {
Thread.yield();

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.

please adda comment

Thread.yield();
}
} else if (decrement < realCnt) {
if (refCntUpdater.compareAndSet(this, rawCnt, rawCnt - decrement - decrement)) {

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.

please add a comment why you subtract decrement twice.

@njhill

njhill commented Nov 20, 2018

Copy link
Copy Markdown
Member Author

@normanmaurer sure, sorry I missed your new test somehow. I was thinking to hold off on the AbstractReferenceCounted update until this had been reviewed, but I will do it now given that test uses it.

Another thing which would probably be good to change - currently the count can actually go one further before "overflowing" (Integer.MAX_COUNT + 1). I tweaked the tests to reflect this but realized that when the count is in this state refCnt() will return a confusing (negative) value, so maybe it's best to just match the prior limit.

Finally I did also benchmark your PR #8574 on the same system; let me know if you would like me to add those results, I omitted them above for brevity.

@njhill

njhill commented Nov 20, 2018

Copy link
Copy Markdown
Member Author

@normanmaurer now updated with all the above

@njhill

njhill commented Nov 21, 2018 •

Copy link
Copy Markdown
Member Author

@normanmaurer kind of a tangent but I also see it's possible to de-duplicate the common refcount logic, see 1ef2aa8. If you think this is of interest I can open a follow-on PR.

Also match original Integer.MAX_VALUE overflow behaviour, other tidyup
referenceCounted.retain();
}

public static void main(String[] args) {

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.

remove :)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

oops, fixed (checkstyle was same thing)

@normanmaurer

Copy link
Copy Markdown
Member

@netty-bot test this please

@normanmaurer

Copy link
Copy Markdown
Member

@njhill de-duplicate as followup sounds good.

@normanmaurer
normanmaurer requested a review from trustin November 21, 2018 05:49
@normanmaurer

Copy link
Copy Markdown
Member

@normanmaurer

Copy link
Copy Markdown
Member

@netty-bot test this please

@merlimat

Copy link
Copy Markdown
Contributor

@njhill @normanmaurer Thanks for looking into this! And nice idea on skipping the CAS on one leg!

@normanmaurer

Copy link
Copy Markdown
Member

@netty-bot test this please

1 similar comment
@normanmaurer

Copy link
Copy Markdown
Member

@netty-bot test this please

@normanmaurer

Copy link
Copy Markdown
Member

@franz1981 PTAL as well

@normanmaurer

Copy link
Copy Markdown
Member

@merlimat so does this fix your problem (I think it does but want to confirm) ?

@merlimat

Copy link
Copy Markdown
Contributor

@normanmaurer It does indeed. Thanks!

@franz1981

Copy link
Copy Markdown
Contributor

I would group all the uncommon paths into separate methods while leaving the common paths simple and clean: tomorrow I will take another look to the logic, nice work!

@franz1981

Copy link
Copy Markdown
Contributor

Re "Thread.yield() after a failed CAS appears to provide a net gain": it is just an apparent gain (as seen in other context). It will trade latencies to get more throughput and I suppose the percentiles would show that.
I think that using a mixed idle strategy would be better depending how much is expected to get a failed cas...

@normanmaurer

Copy link
Copy Markdown
Member

I think what we could do is factor out the less common code path into an extra method to ensure inlining can happen

@trustin

trustin commented Nov 27, 2018

Copy link
Copy Markdown
Member

If contention is a real problem then perhaps a user can create a retained slide/duplicate which will practically make contention none.

@normanmaurer told me that a retained slice/duplicate of an unpooled buffer does not have its own reference count, but then why don't we just implement retainedSlice() and retainedDuplicate() properly like we did for pooled buffers?

I think what we could do is factor out the less common code path into an extra method to ensure inlining can happen.

That'd be nice if addressed in this PR.

@trustin

trustin commented Nov 27, 2018

Copy link
Copy Markdown
Member

I'm not super against the proposed change but I'm worried about:

  • losing half of the gain for less common cases even if we are still faster than before
  • the amount of code complexity it brings to us, i.e. more hand-written comments to maintain, more time to explain when someone tries to understand

@normanmaurer

Copy link
Copy Markdown
Member

@trustin I think we have two options here...

  1. use a CAS and loose all the improvements
  2. go with this change while make it more correct but also more complex.

I would vote for 2) that said I can also life with 1). I mainly think 2) is fine as this code usually does not need to get touched very often so the complexity does not worry me as much .

@trustin

trustin commented Nov 27, 2018

Copy link
Copy Markdown
Member

For sure it's better than reverting the optimization completely. :-) If we want correctness in the mentioned case, then (2) is the way to go. It'd still be nice if we could extract the slow path to a separate method.

@normanmaurer

Copy link
Copy Markdown
Member

@trustin sounds good to me! @njhill can you do this ?

throw new IllegalReferenceCountException(realCnt, -decrement);
}
rawCnt = refCntUpdater.get(this);
}

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.

nit: consider simplify this:

        for (boolean firstTry = true, int rawCnt = nonVolatileRawCnt();; firstTry = false, rawCnt = refCntUpdater.get(this)) {
             if ((rawCnt & 1) != 0) {
                 throw new IllegalReferenceCountException(0, -decrement);
             }
             int realCnt = rawCnt >>> 1;
             if (decrement == realCnt) {
                 // most likely case
                 if (refCntUpdater.compareAndSet(this, rawCnt, 1)) { // any odd number
                     deallocate();
                     return true;
                 }
             } else if (decrement < realCnt) {
                 // all changes to the raw count are 2x the "real" change
                 if (refCntUpdater.compareAndSet(this, rawCnt, rawCnt - (decrement << 1))) {
                     return false;
                 }
             } else if (!firstTry) {
                 throw new IllegalReferenceCountException(realCnt, -decrement);
             }
             if (!firstTry) {
                 // this benefits throughput under high contention
                 Thread.yield();
             }
             rawCnt = refCntUpdater.get(this);
         }

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.

This should also make the method size a bit smaller and so help with inlining.

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.

Same is true for AbstractByteBuf

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@normanmaurer lol I actually had the rawCnt = refCntUpdater.get(this) inside the for statement like that originally but thought better of it readability-wise (I have a bad habit of making things too compact), happy to do it though (I'm assuming you also meant to remove it inside the loop).

I don't think refCnt initialization can go in there though since the types have to match if there are multiple.

I didn't move out the Thread.yield() like that since it results in an additional check on one of the paths: when firstTry is true and decrement > realCount, which could happen in non-error cases especially since the first read is nonvolatile. But I guess probably uncommon enough to justify the simplification, I will do it.

I was also still planning to separate the less common paths although this will likely require some more duplication.

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.

@njhill I see... so yeah maybe just do the firstTry change + the split into methods. Thanks a lot for all your effort!

int realCnt = rawCnt >>> 1;
if (decrement == realCnt) {
// most likely case
if (refCntUpdater.compareAndSet(this, rawCnt, 1)) { // any odd number

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.

optional: this could just be getAndAdd(1) right?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@carl-mastrangelo I think the compare is needed here since the count may have increased in which case we don't want the release to succeed (as soon as the ref changes to an odd number all other releases/retains will fail)

@carl-mastrangelo carl-mastrangelo left a comment

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.

Code generally LGTM. One thing: I think the semantics of ref counting need to be better defined in Netty5, probably closer to what C++ programs do. I'll open an issue.

@njhill

njhill commented Nov 28, 2018

Copy link
Copy Markdown
Member Author

@normanmaurer @trustin @franz1981 I've had a go at decomposing the release logic to facilitate inlining of fast paths, PTAL

@normanmaurer

Copy link
Copy Markdown
Member

@netty-bot test this please

@franz1981

Copy link
Copy Markdown
Contributor

Changes seems fine 2 me: can you please do another round of benchmark with these changes?

@trustin trustin left a comment

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.

Very nice :-)

@normanmaurer normanmaurer added this to the 4.1.32.Final milestone Nov 28, 2018
@njhill

njhill commented Nov 28, 2018

Copy link
Copy Markdown
Member Author

These "after" numbers look slightly too good to me relative to those ran before. I ran on the same system as the prior ones but maybe there was less other stuff going on, I will repeat the before ones too when I get a chance.

Benchmark                           (delay)    Mode       Cnt         Score    Error  Units
retainReleaseContended                    1  sample  32728741       480.785 ±  6.210  ns/op
retainReleaseContended·p0.00              1  sample                  40.000           ns/op
retainReleaseContended·p0.50              1  sample                  81.000           ns/op
retainReleaseContended·p0.90              1  sample                 355.000           ns/op
retainReleaseContended·p0.95              1  sample                2072.000           ns/op
retainReleaseContended·p0.99              1  sample                8448.000           ns/op
retainReleaseContended·p0.999             1  sample               23136.000           ns/op
retainReleaseContended·p0.9999            1  sample               41088.000           ns/op
retainReleaseContended·p1.00              1  sample            10682368.000           ns/op
retainReleaseContended                   10  sample  35218697       529.245 ±  7.930  ns/op
retainReleaseContended·p0.00             10  sample                  49.000           ns/op
retainReleaseContended·p0.50             10  sample                  91.000           ns/op
retainReleaseContended·p0.90             10  sample                 354.000           ns/op
retainReleaseContended·p0.95             10  sample                2148.000           ns/op
retainReleaseContended·p0.99             10  sample                9520.000           ns/op
retainReleaseContended·p0.999            10  sample               26624.000           ns/op
retainReleaseContended·p0.9999           10  sample               51136.000           ns/op
retainReleaseContended·p1.00             10  sample            36700160.000           ns/op
retainReleaseContended                  100  sample  28391507       811.639 ± 10.042  ns/op
retainReleaseContended·p0.00            100  sample                 222.000           ns/op
retainReleaseContended·p0.50            100  sample                 391.000           ns/op
retainReleaseContended·p0.90            100  sample                1698.000           ns/op
retainReleaseContended·p0.95            100  sample                2980.000           ns/op
retainReleaseContended·p0.99            100  sample                6616.000           ns/op
retainReleaseContended·p0.999           100  sample               16368.000           ns/op
retainReleaseContended·p0.9999          100  sample               70528.000           ns/op
retainReleaseContended·p1.00            100  sample            34078720.000           ns/op
retainReleaseContended                 1000  sample  30531259      2523.372 ±  9.072  ns/op
retainReleaseContended·p0.00           1000  sample                1984.000           ns/op
retainReleaseContended·p0.50           1000  sample                2436.000           ns/op
retainReleaseContended·p0.90           1000  sample                2504.000           ns/op
retainReleaseContended·p0.95           1000  sample                2560.000           ns/op
retainReleaseContended·p0.99           1000  sample                3856.000           ns/op
retainReleaseContended·p0.999          1000  sample                9632.000           ns/op
retainReleaseContended·p0.9999         1000  sample               88320.000           ns/op
retainReleaseContended·p1.00           1000  sample            44040192.000           ns/op
retainReleaseContended                10000  sample  25534533     23492.988 ± 10.779  ns/op
retainReleaseContended·p0.00          10000  sample               21344.000           ns/op
retainReleaseContended·p0.50          10000  sample               23616.000           ns/op
retainReleaseContended·p0.90          10000  sample               23712.000           ns/op
retainReleaseContended·p0.95          10000  sample               23776.000           ns/op
retainReleaseContended·p0.99          10000  sample               27136.000           ns/op
retainReleaseContended·p0.999         10000  sample               55744.000           ns/op
retainReleaseContended·p0.9999        10000  sample              291328.000           ns/op
retainReleaseContended·p1.00          10000  sample            15155200.000           ns/op
retainReleaseUncontended                1  sample   4242912        53.613 ±  0.705  ns/op
retainReleaseUncontended·p0.00          1  sample                  36.000           ns/op
retainReleaseUncontended·p0.50          1  sample                  40.000           ns/op
retainReleaseUncontended·p0.90          1  sample                  43.000           ns/op
retainReleaseUncontended·p0.95          1  sample                  46.000           ns/op
retainReleaseUncontended·p0.99          1  sample                  53.000           ns/op
retainReleaseUncontended·p0.999         1  sample                1952.000           ns/op
retainReleaseUncontended·p0.9999        1  sample               18560.000           ns/op
retainReleaseUncontended·p1.00          1  sample               27360.000           ns/op
retainReleaseUncontended               10  sample   6325881        62.036 ±  0.524  ns/op
retainReleaseUncontended·p0.00         10  sample                  43.000           ns/op
retainReleaseUncontended·p0.50         10  sample                  51.000           ns/op
retainReleaseUncontended·p0.90         10  sample                  57.000           ns/op
retainReleaseUncontended·p0.95         10  sample                  60.000           ns/op
retainReleaseUncontended·p0.99         10  sample                  68.000           ns/op
retainReleaseUncontended·p0.999        10  sample                 257.000           ns/op
retainReleaseUncontended·p0.9999       10  sample               18432.000           ns/op
retainReleaseUncontended·p1.00         10  sample               26656.000           ns/op
retainReleaseUncontended              100  sample   7161320       251.660 ±  0.761  ns/op
retainReleaseUncontended·p0.00        100  sample                 219.000           ns/op
retainReleaseUncontended·p0.50        100  sample                 228.000           ns/op
retainReleaseUncontended·p0.90        100  sample                 248.000           ns/op
retainReleaseUncontended·p0.95        100  sample                 252.000           ns/op
retainReleaseUncontended·p0.99        100  sample                 268.000           ns/op
retainReleaseUncontended·p0.999       100  sample               13168.000           ns/op
retainReleaseUncontended·p0.9999      100  sample               20320.000           ns/op
retainReleaseUncontended·p1.00        100  sample              624640.000           ns/op
retainReleaseUncontended             1000  sample   5812948      2167.782 ±  1.965  ns/op
retainReleaseUncontended·p0.00       1000  sample                1970.000           ns/op
retainReleaseUncontended·p0.50       1000  sample                1986.000           ns/op
retainReleaseUncontended·p0.90       1000  sample                2164.000           ns/op
retainReleaseUncontended·p0.95       1000  sample                2176.000           ns/op
retainReleaseUncontended·p0.99       1000  sample                3768.000           ns/op
retainReleaseUncontended·p0.999      1000  sample               20672.000           ns/op
retainReleaseUncontended·p0.9999     1000  sample               25472.000           ns/op
retainReleaseUncontended·p1.00       1000  sample               70912.000           ns/op
retainReleaseUncontended            10000  sample   4667998     21369.149 ±  8.200  ns/op
retainReleaseUncontended·p0.00      10000  sample               19488.000           ns/op
retainReleaseUncontended·p0.50      10000  sample               19584.000           ns/op
retainReleaseUncontended·p0.90      10000  sample               21440.000           ns/op
retainReleaseUncontended·p0.95      10000  sample               34752.000           ns/op
retainReleaseUncontended·p0.99      10000  sample               50880.000           ns/op
retainReleaseUncontended·p0.999     10000  sample               59136.000           ns/op
retainReleaseUncontended·p0.9999    10000  sample               71936.000           ns/op
retainReleaseUncontended·p1.00      10000  sample              112128.000           ns/op

@trustin

trustin commented Nov 29, 2018

Copy link
Copy Markdown
Member

Still LGTM.

@normanmaurer
normanmaurer merged commit fedf3cc into netty:4.1 Nov 29, 2018
@normanmaurer

Copy link
Copy Markdown
Member

@njhill thanks a lot again!

normanmaurer pushed a commit that referenced this pull request Nov 29, 2018
Motivation

#8563 highlighted race conditions introduced by the prior optimistic
update optimization in 83a19d5. These
were known at the time but considered acceptable given the perf
benefit in high contention scenarios.

This PR proposes a modified approach which provides roughly half the
gains but stronger concurrency semantics. Race conditions still exist
but their scope is narrowed to much less likely cases (releases
coinciding with retain overflow), and even in those
cases certain guarantees are still assured. Once release() returns true,
all subsequent release/retains are guaranteed to throw, and in
particular deallocate will be called at most once.

Modifications

- Use even numbers internally (including -ve) for live refcounts
- "Final" release changes to odd number (equivalent to refcount 0)
- Retain still uses faster getAndAdd, release uses CAS loop
- First CAS attempt uses non-volatile read
- Thread.yield() after a failed CAS provides a net gain

Result

More (though not completely) robust concurrency semantics for ref
counting; increased latency under high contention, but still roughly
twice as fast as the original logic. Bench results to follow
@njhill
njhill deleted the ref-compromise2 branch November 29, 2018 07:56
njhill added a commit to njhill/netty that referenced this pull request Mar 29, 2019
Motivation

AbstractReferenceCounted and AbstractReferenceCountedByteBuf contain
duplicate logic for managing the volatile refcount in an optimized and
consistent manner, which increased in complexity in netty#8583. It's possible
to extract this into a common helper class now that all access is via an
AtomicIntegerFieldUpdater.

Modifications

- Move duplicate logic into a shared ReferenceCountUpdater class
- Incorporate some additional simplification for the most common single
increment/decrement cases (fewer checks/operations)

Result

Less code duplication, better encapsulation of the "non-trivial"
internal volatile refcount manipulation
normanmaurer pushed a commit that referenced this pull request Apr 9, 2019
Motivation

AbstractReferenceCounted and AbstractReferenceCountedByteBuf contain
duplicate logic for managing the volatile refcount in an optimized and
consistent manner, which increased in complexity in #8583. It's possible
to extract this into a common helper class now that all access is via an
AtomicIntegerFieldUpdater.

Modifications

- Move duplicate logic into a shared ReferenceCountUpdater class
- Incorporate some additional simplification for the most common single
increment/decrement cases (fewer checks/operations)

Result

Less code duplication, better encapsulation of the "non-trivial"
internal volatile refcount manipulation
normanmaurer pushed a commit that referenced this pull request Apr 9, 2019
Motivation

AbstractReferenceCounted and AbstractReferenceCountedByteBuf contain
duplicate logic for managing the volatile refcount in an optimized and
consistent manner, which increased in complexity in #8583. It's possible
to extract this into a common helper class now that all access is via an
AtomicIntegerFieldUpdater.

Modifications

- Move duplicate logic into a shared ReferenceCountUpdater class
- Incorporate some additional simplification for the most common single
increment/decrement cases (fewer checks/operations)

Result

Less code duplication, better encapsulation of the "non-trivial"
internal volatile refcount manipulation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants