My gut reaction is that this[1] looks like a NUMA issue. The flip-flop performance profile immediately reminds me of a very gnarly prod issue i lead the investigation of. I’d want to eliminate NUMA first from my investigation.<p>You can use “numactl --cpunodebind=0 --membind=0 <your original java benchmark command>” to pin the JVM to a particular NUMA node and immediately discount my notion as bunkum.<p>[1] all the reasons mentioned that make me think numa:<p><pre><code> 1. The machine affected has multiple NUMA domains
2. The M1 is a single NUMA domain chip and does not exhibit the behaviour
3. The real thing making me think this - the consistent performance flip flop is close to the ~40% performance overhead of accessing remote memory vs local that i’ve seen previously. You’re seeing higher overheads but your code is a much tighter loop of memory access than my previous experience so that could explain the difference i think.</code></pre>
Have you tried setting up a JMH benchmark? This should allow you to see if the JIT is the cause of your slowdowns.
Also, running it under a profiler (I recommend async-profiler[1]) should give you a good idea of <i>where</i> the slowdown occurs which might help you pin it down further.<p>[1] <a href="https://github.com/jvm-profiling-tools/async-profiler" rel="nofollow">https://github.com/jvm-profiling-tools/async-profiler</a>
I'm stumbling onto a strange Java performance irregularity, especially easy to hit with Docker on Linux. A super straightforward implementation of Eratosthenes sieve using a Java BitSet for storage, sometimes drops in performance by 50%. Even more with JDK8. Any JAVA/JDK/JVM experts here that can shed some light on the mystery for me? I've been at it a while, but it is quite a bit outside my field of knowledge, and I am out of ideas for how to proceed. The blog article linked + the Git repository is an attempt to summarize/minimize things.
First make sure there are no user or kernel tasks that may be hogging resources sometimes. Maybe even try disabling some peripherals and similar, at the hardware or kernel level.<p>Lots of stuff you could try then:<p>* disabling ASLR<p>* disabling Turbo Boost and similar CPU settings<p>* changing the CPU performance scaling governor (from "powersave" to "performance"): printf performance | tee /sys/devices/system/cpu/cpufreq/policy*/scaling_governor<p>* run the code under a real-time scheduling policy (like SCHED_FIFO) with the highest priority. If you do try this, you need to also enable actual real-time scheduling by writing "-1" to /proc/sys/kernel/sched_rt_runtime_us.<p>But modern CPUs are not predictable in their performance, that's why microcontrollers are usually used for hard real time requirements. So I doubt you'll ever be able to get absolutely consistent performance across all benchmark runs.<p>I played similar benchmarking games myself before, and it turns out that, although I did most of the stuff described above, and my code was C++ (no JIT), big slowdowns do happen with some inevitable but predictable frequency.
My first thought was that it's a bug in the deoptimizer, i.e. the JIT compiler dynamically switches back to a deoptimized form to throw away invalid optimizations (or so it it thinks) to apply new optimizations which are more relevant to the current load/usage patterns. [0]<p>I think I've seen a bug report once about this deoptimization/optimization process happening in an infinite loop, but why would it happen only under Docker on Linux, but not Mac? Perhaps, the deoptimizer relies on heuristics which depend on the current environment.<p>[0] <a href="https://stackoverflow.com/a/20542675" rel="nofollow">https://stackoverflow.com/a/20542675</a>
So far my attempts to reproduce the alleged performance degradation have not been fruitful. I've written up a fairly detailed gist [1] on how to get CPU performance metrics; appendix also has a dump of C1 and C2 compiled methods (useful for comparison). I also ran on 2-node NUMA; binding cpu and memory to different nodes didn't yield a repro. either.<p>[1] <a href="https://gist.github.com/srosenberg/41611d5f40cfcbad51aa27eb0dba1af0" rel="nofollow">https://gist.github.com/srosenberg/41611d5f40cfcbad51aa27eb0...</a>
Seeing that the benchmarks are running inside Docker, would it be Docker related? Does Docker throttle CPU on different machines differently?<p>Check the temperature of the CPU. Modern CPU will slow down when it runs too hot. Also does anti-virus got run from time to time? Does expensive backup or expensive telemetry got run during the benchmarks?<p>Reading the blog again and seeing the results are dropped to the exact same level step wise, it really looks like the CPU has been throttled or some quota limit has been triggered.
This issue isn't directly related to BitSet. I have observed the same thing in my programming language interpreter that runs on the JVM (well, it's written in Kotlin multiplatform so it runs on JS and Natively as well).<p>I start the interpreter and measue the time it takes to all all then numbers below 1000000000.<p>The first time I run it after starting the interpreter it always takes 1.4 seconds (within 0.1 second precision). The second time I measure the time it takes 1.7, and for every invocation following that it takes 2 seconds.<p>If I stop the interpreter and try again, I get exactly the same result.<p>I have not been able to explain this behaviour. This is on OpenJDK 11 by the way.<p>If anyone wants to test this, just run the interpreter from here: <a href="https://github.com/lokedhs/array" rel="nofollow">https://github.com/lokedhs/array</a><p>To run the benchmark, type the following command in the UI:<p><pre><code> time:measureTime { +/⍳1000000000 }
</code></pre>
My current best guess is that the optimiser decides to recompile the bytecode to improve performance, but the recompiled version actually ends up being slower.
I echo the earlier comments to use jmh for benchmarking. There's lots of subtle issues that jmh solves.<p>One thing I notice is that your sieve run doesn't return any values. Java can optimise out code that produces no effect in some circumstances.<p>EDIT: Although in that case, you'd expect to see it run insanely fast. Anyway, the point stands, there's lots of non obvious issues in benchmarking.
A lot of your time is spent on the branching and on accessing the memory. So on the ASM level, memory access patterns, caching and branch prediction will affect your performance.<p>My bet is on the branch predictor, since IIRC AMD has a novel branch predictor that's pretty different from the Intel branch predictor (not sure about M1): In C-land you should try loop unrolling (in fact a decent compiler will do that for you). If the JVM has a control for that, force it; else do it manually and pray the JIT does the right thing.<p>My first intuition was the BitSet's cache & memory behavior, which might also be the case for some ranges of `p`: Internally the BitSet is probably something like a chunk of memory with bitwise indexing. So to change a bit, the machine has to load a bunch of bytes into a register, set the bit, and write that register then back to memory. This is bad(*) if you want to set e.g. bits 0 to 31 in your BitSet, because you now got 64 memory accesses instead of two. This might affect you for small p, but with p >= 64 the access stride will be larger than 64. Thinking about it, in fact, this could be something that might throw of a naive JIT optimizer.
I would have to think a little bit on how to improve here were your code written in C, with the JVM I have no idea how you could optimize here. Maybe do two loops, first for p<=64 and the other for p>64.<p>Regarding cache misses, hm, 1M bits are just shy of 64kByte. On any modern machine that should fit into the cache; I would still monitor for cache misses though.<p>(*) Short story: I have a light case of PTSD since I once had to reimplement the storage backend for a Java SQL engine university project because a friend was trying to be too clever and did exactly that.<p>Anyway, interesting question and I wish you best of luck with figuring out what's going on there :)<p>//edited a few times; sorry, I'm bad at writing a comment in on go...
It could be that something else is contending for L1/L2 cache.<p>As others have mentioned, take JMH for a spin. Benchmark after a few hundred thousand iterations warmup.<p>Also as mentioned here, thermal throttling could have a big impact. Maybe you have access to a desktop Xeon or similar?
There have been issues[1] with seccomp. Maybe try with seccomp disabled for that container?<p><pre><code> --security-opt seccomp:unconfined
</code></pre>
More info here[2].<p>[1]: <a href="https://github.com/docker/for-linux/issues/738" rel="nofollow">https://github.com/docker/for-linux/issues/738</a><p>[2]: <a href="http://mamememo.blogspot.com/2020/05/cpu-intensive-rubypython-code-runs.html" rel="nofollow">http://mamememo.blogspot.com/2020/05/cpu-intensive-rubypytho...</a>
I ran into a similar problem with docker on the same problem in zig; podman and on-metal had no performance regression but docker did. I kind of gave the fuck up on the plummer's sieve shootout at that point because (of this and other performance regressions I was finding, like CPU throttling) I felt like I was fighting the stupid rules of the contest more than I was discovering things about performance.<p>Anyways for the authors: try running it in podman and see if it eliminates the perf regression
What happens if you invert the test condition? That is, you run it 10k times and then see how long that took rather than for X time and see how many times you could do it?<p>You’re using System.getCurrentTimeMillis() which should be w fast. My first thought was if you were using Instant and then sometimes there’s one call to VM.getNanoTimeAdjustment and sometimes two.<p>Man, this is a tough one. I’ll try when I’m home.
Every other theory listed here is far more likely, but I would try changing your loop from using System.currentTimeMillis() to using System.nanoTime(). It's a higher-resolution time source that has no relation to wall clock time but is more appropriate for timings. Classes like Stopwatch from Google's Guava use it.
How do you calculate the time?<p>Windows had famously low timer resolution on java for a while. What happens if you run each round for increasingly longer periods?