JMH RefreshIf you never heard of JMH here are a few links to catch up on:
- The JMH project
- My own introductory post on the topic (in need of some update as JMH evolved in the past year)
- A few other posts demonstrating JMH usage:
- Some masterful posts from Shipilev who is part of the team behind JMH:
- Use google to find more...
Experiment RefreshHere's a brief overview of the steps I took to update the original benchmark to the current:
- At the time I wrote the original post JMH was pretty new but already had support for thread groups. Alas there was no way to tweak their size from the command line. Now there is so I could drop the boilerplate for different thread counts.
- Hide counter types behind an interface/factory and use single benchmark (was possible before, just tidying up my own mess there).
- Switched to using @Param to select the benchmarked counter type. With JMH 0.9 the framework will pick up an enum and run through it's values for me, yay!
The revised benchmark is pretty concise:
The JMH infrastructure takes care of launching requested numbers of incrementing/getting threads and sums up all the results neatly for us. The @Param defaults will run all variant if we don't pick a particular implementation from the command line. All together a more pleasant experience than the rough and tumble of version 0.1. Code repository is here.
AtomicLong: CAS vs LOCK XADDWith JDK8 a change has been made to the AtomicLong class to replace the CAS loop:
With a single intrinsic:
getAndAddLong() (which corresponds to fetch-and-add) translates into LOCK XADD on x86 CPUs which atomically returns the current value and increments it. This translates into better performance under contention as we leave the hardware to negotiate the atomic increment.
Incrementing onlyRunning the benchmark with incrementing threads only on a nice (but slightly old) dual socket Xeon X5650@2.67GHz (HyperThreading is off, 2 CPUs, 6 cores each, JDK7u51, JDK8u5) shows the improvement:
- Left hand column is number of threads, all threads increment
- All numbers are in nanoseconds measuring the average cost per op
- Values are averaged across threads, so the cost per op is presented from a single threaded method call perspective.
Notes on the how benchmarks were run:
- I used taskset to pin the benchmark process to a set of cores
- The number of cores allocated matches the number of threads required by the benchmark
- Each socket has 6 cores and I taskset the cores from a single socket from 1 to 6, then spilt over to utilizing the other socket. In the particular layout of cores on this machine this turned out to translate into taskset -c 0-<number-of-threads - 1>
- There are no get() threads in use, only inc() threads. This is controlled from the command line by setting the -tg option. E.g. -tg 0,6 will spin no get() threads and 6 inc() threads
- JDK 8 AtomicLong is consistently faster as expected.
- LOCK XADD does NOT cure the scalability issue in this case. This echoes the rule of thumb by D. Yukov here, which is that shared data writes are a scalability bottleneck (he is comparing private writes to a LOCK XADD on shared). The chart in his post demonstrates throughput rather than cost per op and the benchmark he uses is somewhat different. Importantly his chart demonstrates LOCK XADD hits a sustained throughput which remains fixed as threads are added. The large noise in the dual socket measurements renders the data less than conclusive in my measurements here but the throughput does converge.
- When we cross the socket boundary (>6 threads) the ratio between JDK7 and JDK8 increases.
- Crossing the socket boundary also increases results variability (as expressed by the error).
This benchmark demonstrates cost under contention. In a real application with many threads doing a variety of tasks you are unlikely to experience this kind of contention, but when you hit it you will pay.
From the LongAdder JavaDoc:
LongAdder JDK7 backport vs. JDK8 LongAdderIs a very boring comparison as they turn out to scale and cost pretty much the same (minor win for the native LongAdder implementation). It is perhaps comforting to anyone who needs to support a JDK7 client base that the backport should work fine on both and that no further work is required for now. Below are the results, LA7 is the LongAdder backport, LA8 is the JDK8 implementation:
JDK8: AtomicLong vs LongAdderSimilar results were demonstrated has been discussed in the prev. post, but here's the JDK8 versions side by side:
Should I use AtomicLong or LongAdder?Firstly this question is only relevant if you are not using AtomicLong as a unique sequence generator. LongAdder does not claim to, nor makes any attempt to give you that guarantee. So LongAdder is definitely NOT a drop in replacement for AtomicLong, they have very different semantics.
From the LongAdder JavaDoc:
Assuming you were using AtomicLong as a counter you will need to consider a few tradeoffs:This class is usually preferable to AtomicLong when multiple threads update a common sum that is used for purposes such as collecting statistics, not for fine-grained synchronizationcontrol. Under low update contention, the two classes have similar characteristics. But under high contention, expected throughput of this class is significantly higher, at the expense of higher space consumption.
- When NO contention is present, AtomicLong performs slightly better than LongAdder.
- To avoid contention LongAdder will allocate Cells (see previous post for implementation discussion) each Cell will consume at least 256 bytes (current implementation of @Contended) and you may have as many Cells as CPUs. If you are on a tight memory budget and have allot of counters this is perhaps not the tool for the job.
- If you prefer get() performance to inc() performance than you should definitely stick with AtomicLong.
- When you prefer inc() performance and expect contention, and when you have some memory to spare then LongAdder is indeed a great choice.
Bonus material: How the Observer effects the ExperimentWhat is the impact of reading a value that is being rapidly mutated by another thread? On the observing thread side we expect to pay a read-miss, but as discussed prev. here there is a price to pay on the mutator side as well. I run the same benchmark with an equal number of inc()/get() threads. The process is pinned as before but as the roles are not uniform I have no control on which socket the readers/writers end up on, so we should expect more noise as we cross the socket line (LA - LongAdder, AL - AtomicLong, both on JDK8, same type ing/get are within the same run, left column is number of inc()/get() threads):
Now that is a very different picture... 2 factors come into play:
- The reads disturb the writes by generating cache coherency noise as discussed here. A writer must have the cache line in an Exclusive/Mutated state, but the read will cause it to shift into Shared.
- The get() measurement does not differentiate between new values and old values.
In this light I'm not sure we can read much into the effect of readers in this particular use case. The 'model' represented by a hot reading thread does not sit well with the use case I have in mind for these counters which is normally as performance indicators to be sampled at some set frequency (once a second or millisecond). A different experiment is more appropriate, perhaps utilising Blackhole.consumeCPU to set a gap between get() (see a great insight into this method here).
With this explanation in mind we can see perhaps more sense in the following comparison between AtomicLong on JDK7 and JDK8:
Now consider that the implementation of get() has not changed. The reason for the change in cost for get is down to the increase in visible values and thus cache misses caused by the change to the increment method.