Seeing via {hardware} counters: a journey to threefold efficiency enhance | by Netflix Expertise Weblog | Nov, 2022

By Vadim Filanovsky and Harshad Sane
In one among our earlier blogposts, A Microscope on Microservices we outlined three broad domains of observability (or “ranges of magnification,” as we referred to them) — Fleet-wide, Microservice and Occasion. We described the instruments and methods we use to realize perception inside every area. There may be, nonetheless, a category of issues that requires a fair stronger stage of magnification going deeper down the stack to introspect CPU microarchitecture. On this blogpost we describe one such drawback and the instruments we used to resolve it.
It began off as a routine migration. At Netflix, we periodically reevaluate our workloads to optimize utilization of accessible capability. We determined to maneuver one among our Java microservices — let’s name it GS2 — to a bigger AWS occasion dimension, from m5.4xl (16 vCPUs) to m5.12xl (48 vCPUs). The workload of GS2 is computationally heavy the place CPU is the limiting useful resource. Whereas we perceive it’s nearly unimaginable to realize a linear enhance in throughput because the variety of vCPUs develop, a near-linear enhance is attainable. Consolidating on the bigger cases reduces the amortized value of background duties, liberating up further sources for serving requests and doubtlessly offsetting the sub-linear scaling. Thus, we anticipated to roughly triple throughput per occasion from this migration, as 12xl cases have thrice the variety of vCPUs in comparison with 4xl cases. A fast canary take a look at was freed from errors and confirmed decrease latency, which is anticipated provided that our customary canary setup routes an equal quantity of site visitors to each the baseline operating on 4xl and the canary on 12xl. As GS2 depends on AWS EC2 Auto Scaling to target-track CPU utilization, we thought we simply needed to redeploy the service on the bigger occasion sort and await the ASG (Auto Scaling Group) to choose the CPU goal. Sadly, the preliminary outcomes had been removed from our expectations:
The primary graph above represents common per-node throughput overlaid with common CPU utilization, whereas the second graph exhibits common request latency. We are able to see that as we reached roughly the identical CPU goal of 55%, the throughput elevated solely by ~25% on common, falling far in need of our desired objective. What’s worse, common latency degraded by greater than 50%, with each CPU and latency patterns turning into extra “uneven.” GS2 is a stateless service that receives site visitors via a taste of round-robin load balancer, so all nodes ought to obtain practically equal quantities of site visitors. Certainly, the RPS (Requests Per Second) information exhibits little or no variation in throughput between nodes:
However as we began wanting on the breakdown of CPU and latency by node, a wierd sample emerged:
Though we confirmed pretty equal site visitors distribution between nodes, CPU and latency metrics surprisingly demonstrated a really completely different, bimodal distribution sample. There’s a “decrease band” of nodes exhibiting a lot decrease CPU and latency with hardly any variation; and there may be an “higher band” of nodes with considerably larger CPU/latency and vast variation. We observed solely ~12% of the nodes fall into the decrease band, a determine that was suspiciously constant over time. In each bands, efficiency traits stay constant for the whole uptime of the JVM on the node, i.e. nodes by no means jumped the bands. This was our start line for troubleshooting.
Our first (and fairly apparent) step at fixing the issue was to check flame graphs for the “sluggish” and “quick” nodes. Whereas flame graphs clearly mirrored the distinction in CPU utilization because the variety of collected samples, the distribution throughout the stacks remained the identical, thus leaving us with no further perception. We turned to JVM-specific profiling, beginning with the essential hotspot stats, after which switching to extra detailed JFR (Java Flight Recorder) captures to check the distribution of the occasions. Once more, we got here away empty-handed as there was no noticeable distinction within the quantity or the distribution of the occasions between the “sluggish” and “quick” nodes. Nonetheless suspecting one thing is likely to be off with JIT conduct, we ran some primary stats towards image maps obtained by perf-map-agent solely to hit one other useless finish.
Satisfied we’re not lacking something on the app-, OS- and JVM- ranges, we felt the reply is likely to be hidden at a decrease stage. Fortunately, the m5.12xl occasion sort exposes a set of core PMCs (Efficiency Monitoring Counters, a.okay.a. PMU counters), so we began by amassing a baseline set of counters utilizing PerfSpect:
Within the desk above, the nodes displaying low CPU and low latency characterize a “quick node”, whereas the nodes with larger CPU/latency characterize a “sluggish node”. Apart from apparent CPU variations, we are able to see that the sluggish node has virtually 3x CPI (Cycles Per Instruction) of the quick node. We additionally see a lot larger L1 cache exercise mixed with 4x larger rely of MACHINE_CLEARS. One widespread trigger of those signs is so-called “false sharing” — a utilization sample occurring when 2 cores studying from / writing to unrelated variables that occur to share the identical L1 cache line. Cache line is an idea just like reminiscence web page — a contiguous chunk of knowledge (usually 64 bytes on x86 techniques) transferred to and from the cache. This diagram illustrates it:
Every core on this diagram has its personal non-public cache. Since each cores are accessing the identical reminiscence area, caches must be constant. This consistency is ensured with so-called “cache coherency protocol.” As Thread 0 writes to the “crimson” variable, coherency protocol marks the entire cache line as “modified” in Thread 0’s cache and as “invalidated” in Thread 1’s cache. Later, when Thread 1 reads the “blue” variable, although the “blue” variable isn’t modified, coherency protocol forces the whole cache line to be reloaded from the cache that had the final modification — Thread 0’s cache on this instance. Resolving coherency throughout non-public caches takes time and causes CPU stalls. Moreover, ping-ponging coherency site visitors must be monitored via the last level shared cache’s controller, which results in much more stalls. We take CPU cache consistency as a right, however this “false sharing” sample illustrates there’s an enormous efficiency penalty for merely studying a variable that’s neighboring with another unrelated information.
Armed with this information, we used Intel vTune to run microarchitecture profiling. Drilling down into “sizzling” strategies and additional into the meeting code confirmed us blocks of code with some directions exceeding 100 CPI, which is extraordinarily sluggish. That is the abstract of our findings:
Numbered markers from 1 to six denote the identical code/variables throughout the sources and vTune meeting view. The crimson arrow signifies that the CPI worth doubtless belongs to the earlier instruction — that is as a result of profiling skid in absence of PEBS (Processor Occasion-Based mostly Sampling), and often it’s off by a single instruction. Based mostly on the truth that (5) “repne scan” is a fairly uncommon operation within the JVM codebase, we had been in a position to hyperlink this snippet to the routine for subclass checking (the identical code exists in JDK mainline as of the writing of this blogpost). Going into the main points of subtype checking in HotSpot is way past the scope of this blogpost, however curious readers can be taught extra about it from the 2002 publication Fast Subtype Checking in the HotSpot JVM. As a result of nature of the category hierarchy used on this specific workload, we preserve hitting the code path that retains updating (6) the “_secondary_super_cache” subject, which is a single-element cache for the last-found secondary superclass. Observe how this subject is adjoining to the “_secondary_supers”, which is an inventory of all superclasses and is being learn (1) to start with of the scan. A number of threads do these read-write operations, and if fields (1) and (6) fall into the identical cache line, then we hit a false sharing use case. We highlighted these fields with crimson and blue colours to hook up with the false sharing diagram above.
Observe that because the cache line dimension is 64 bytes and the pointer dimension is 8 bytes, we’ve a 1 in 8 probability of those fields falling on separate cache strains, and a 7 in 8 probability of them sharing a cache line. This 1-in-8 probability is 12.5%, matching our earlier remark on the proportion of the “quick” nodes. Fascinating!
Though the repair concerned patching the JDK, it was a easy change. We inserted padding between “_secondary_super_cache” and “_secondary_supers” fields to make sure they by no means fall into the identical cache line. Observe that we didn’t change the practical side of JDK conduct, however fairly the info format:
The outcomes of deploying the patch had been instantly noticeable. The graph beneath is a breakdown of CPU by node. Right here we are able to see a red-black deployment taking place at midday, and the brand new ASG with the patched JDK taking up by 12:15:
Each CPU and latency (graph omitted for brevity) confirmed an identical image — the “sluggish” band of nodes was gone!
We didn’t have a lot time to marvel at these outcomes, nonetheless. Because the autoscaling reached our CPU goal, we observed that we nonetheless couldn’t push greater than ~150 RPS per node — properly in need of our objective of ~250 RPS. One other spherical of vTune profiling on the patched JDK model confirmed the identical bottleneck round secondary superclass cache lookup. It was puzzling at first to see seemingly the identical drawback coming again proper after we put in a repair, however upon nearer inspection we realized we’re coping with “true sharing” now. In contrast to “false sharing,” the place 2 unbiased variables share a cache line, “true sharing” refers back to the similar variable being learn and written by a number of threads/cores. On this case, CPU-enforced memory ordering is the reason for slowdown. We reasoned that eradicating the impediment of false sharing and rising the general throughput resulted in elevated execution of the identical JVM superclass caching code path. Basically, we’ve larger execution concurrency, inflicting extreme strain on the superclass cache attributable to CPU-enforced reminiscence ordering protocols. The widespread method to resolve that is to keep away from writing to the shared variable altogether, successfully bypassing the JVM’s secondary superclass cache. Since this variation altered the conduct of the JDK, we gated it behind a command line flag. That is everything of our patch:
And listed below are the outcomes of operating with disabled superclass cache writes:
Our repair pushed the throughput to ~350 RPS on the similar CPU autoscaling goal of 55%. To place this in perspective, that’s a 3.5x enchancment over the throughput we initially reached on m5.12xl, together with a discount in each common and tail latency.
Disabling writes to the secondary superclass cache labored properly in our case, and although this won’t be a fascinating resolution in all instances, we needed to share our methodology, toolset and the repair within the hope that it might assist others encountering comparable signs. Whereas working via this drawback, we got here throughout JDK-8180450 — a bug that’s been dormant for greater than 5 years that describes precisely the issue we had been dealing with. It appears ironic that we couldn’t discover this bug till we truly found out the reply. We consider our findings complement the nice work that has been achieved in diagnosing and remediating it.
We have a tendency to consider trendy JVMs as extremely optimized runtime environments, in lots of instances rivaling extra “performance-oriented” languages like C++. Whereas it holds true for almost all of workloads, we had been reminded that efficiency of sure workloads operating inside JVMs will be affected not solely by the design and implementation of the appliance code, but additionally by the implementation of the JVM itself. On this blogpost we described how we had been in a position to leverage PMCs with a purpose to discover a bottleneck within the JVM’s native code, patch it, and subsequently notice higher than a threefold enhance in throughput for the workload in query. In relation to this class of efficiency points, the flexibility to introspect the execution on the stage of CPU microarchitecture proved to be the one resolution. Intel vTune gives precious perception even with the core set of PMCs, equivalent to these uncovered by m5.12xl occasion sort. Exposing a extra complete set of PMCs together with PEBS throughout all occasion sorts and sizes within the cloud atmosphere would pave the best way for deeper efficiency evaluation and doubtlessly even bigger efficiency features.
Replace: After publishing this publish we had been alerted to a separate unbiased improvement on this space, together with a writeup on how superclass cache affects regex pattern matching, in addition to a tool to automate the detection of JDK-8180450 utilizing an agent. Additionally of curiosity is this video describing another method to diagnosing the problem. Our objective in sharing our work is to offer data and perception to the open-source neighborhood, and it’s all the time thrilling to see (and share!) how others method comparable issues.