Seeing via {hardware} counters: a journey to threefold efficiency improve | by Netflix Technology Blog | Nov, 2022

0
83
Seeing via {hardware} counters: a journey to threefold efficiency improve | by Netflix Technology Blog | Nov, 2022


By Vadim Filanovsky and Harshad Sane

In one in all our earlier blogposts, A Microscope on Microservices we outlined three broad domains of observability (or “levels of magnification,” as we referred to them) — Fleet-wide, Microservice and Instance. We described the instruments and strategies we use to realize perception inside every area. There is, nonetheless, a category of issues that requires a fair stronger degree of magnification going deeper down the stack to introspect CPU microarchitecture. In this blogpost we describe one such downside 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 in all 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. While we perceive it’s nearly unattainable to realize a linear improve in throughput because the variety of vCPUs develop, a near-linear improve is attainable. Consolidating on the bigger situations reduces the amortized price of background duties, liberating up extra assets for serving requests and doubtlessly offsetting the sub-linear scaling. Thus, we anticipated to roughly triple throughput per occasion from this migration, as 12xl situations have thrice the variety of vCPUs in comparison with 4xl situations. A fast canary check was freed from errors and confirmed decrease latency, which is anticipated on condition that our customary canary setup routes an equal quantity of visitors to each the baseline working 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 look ahead to the ASG (Auto Scaling Group) to decide on the CPU goal. Unfortunately, the preliminary outcomes have been removed from our expectations:

The first graph above represents common per-node throughput overlaid with common CPU utilization, whereas the second graph exhibits common request latency. We can see that as we reached roughly the identical CPU goal of 55%, the throughput elevated solely by ~25% on common, falling far wanting our desired aim. What’s worse, common latency degraded by greater than 50%, with each CPU and latency patterns turning into extra “choppy.” GS2 is a stateless service that receives visitors via a taste of round-robin load balancer, so all nodes ought to obtain practically equal quantities of visitors. Indeed, the RPS (Requests Per Second) knowledge exhibits little or no variation in throughput between nodes:

But as we began trying on the breakdown of CPU and latency by node, a wierd sample emerged:

Although we confirmed pretty equal visitors distribution between nodes, CPU and latency metrics surprisingly demonstrated a really totally different, bimodal distribution sample. There is a “lower band” of nodes exhibiting a lot decrease CPU and latency with hardly any variation; and there’s an “upper band” of nodes with considerably increased CPU/latency and extensive variation. We seen 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 slightly apparent) step at fixing the issue was to match flame graphs for the “slow” and “fast” nodes. While 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 extra 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 match the distribution of the occasions. Again, we got here away empty-handed as there was no noticeable distinction within the quantity or the distribution of the occasions between the “slow” and “fast” nodes. Still suspecting one thing may be off with JIT conduct, we ran some primary stats towards image maps obtained by perf-map-agent solely to hit one other lifeless finish.

Convinced we’re not lacking something on the app-, OS- and JVM- ranges, we felt the reply may be hidden at a decrease degree. Luckily, the m5.12xl occasion sort exposes a set of core PMCs (Performance Monitoring Counters, a.okay.a. PMU counters), so we began by gathering a baseline set of counters utilizing PerfSpect:

In the desk above, the nodes displaying low CPU and low latency signify a “fast node”, whereas the nodes with increased CPU/latency signify a “slow node”. Aside from apparent CPU variations, we will see that the gradual node has virtually 3x CPI (Cycles Per Instruction) of the quick node. We additionally see a lot increased L1 cache exercise mixed with 4x increased 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 information (sometimes 64 bytes on x86 programs) transferred to and from the cache. This diagram illustrates it:

Each 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 “red” 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, despite the fact that the “blue” variable will not be 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. Additionally, ping-ponging coherency visitors needs to be monitored via the final degree shared cache’s controller, which ends up in much more stalls. We take CPU cache consistency without any consideration, however this “false sharing” sample illustrates there’s an enormous efficiency penalty for merely studying a variable that’s neighboring with another unrelated knowledge.

Armed with this data, we used Intel vTune to run microarchitecture profiling. Drilling down into “hot” strategies and additional into the meeting code confirmed us blocks of code with some directions exceeding 100 CPI, which is extraordinarily gradual. This 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 purple arrow signifies that the CPI worth possible belongs to the earlier instruction — that is because of the profiling skid in absence of PEBS (Processor Event-Based Sampling), and normally it’s off by a single instruction. Based on the truth that (5) “repne scan” is a slightly uncommon operation within the JVM codebase, we have been capable of 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 much past the scope of this blogpost, however curious readers can be taught extra about it from the 2002 publication Fast Subtype Checking within the HotSpot JVM. Due to the character of the category hierarchy used on this explicit workload, we hold hitting the code path that retains updating (6) the “_secondary_super_cache” area, which is a single-element cache for the last-found secondary superclass. Note how this area is adjoining to the “_secondary_supers”, which is a listing of all superclasses and is being learn (1) at first of the scan. Multiple 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 purple and blue colours to hook up with the false sharing diagram above.

Note that for the reason that 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 traces, and a 7 in 8 probability of them sharing a cache line. This 1-in-8 probability is 12.5%, matching our earlier statement on the proportion of the “fast” nodes. Fascinating!

Although 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. Note that we didn’t change the useful facet of JDK conduct, however slightly the information format:

The outcomes of deploying the patch have been instantly noticeable. The graph beneath is a breakdown of CPU by node. Here we will see a red-black deployment occurring at midday, and the brand new ASG with the patched JDK taking up by 12:15:

Both CPU and latency (graph omitted for brevity) confirmed the same image — the “slow” band of nodes was gone!

We didn’t have a lot time to marvel at these outcomes, nonetheless. As the autoscaling reached our CPU goal, we seen that we nonetheless couldn’t push greater than ~150 RPS per node — properly wanting our aim of ~250 RPS. Another 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 downside coming again proper after we put in a repair, however upon nearer inspection we realized we’re coping with “true sharing” now. Unlike “false sharing,” the place 2 impartial variables share a cache line, “true sharing” refers back to the similar variable being learn and written by a number of threads/cores. In this case, CPU-enforced reminiscence ordering is the reason for slowdown. We reasoned that eradicating the impediment of false sharing and growing the general throughput resulted in elevated execution of the identical JVM superclass caching code path. Essentially, we’ve increased execution concurrency, inflicting extreme stress on the superclass cache on account of 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 alteration altered the conduct of the JDK, we gated it behind a command line flag. This is everything of our patch:

And listed here are the outcomes of working with disabled superclass cache writes:

Our repair pushed the throughput to ~350 RPS on the similar CPU autoscaling goal of 55%. To put 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 despite the fact that this may not be a fascinating resolution in all circumstances, we needed to share our methodology, toolset and the repair within the hope that it could assist others encountering related signs. While working via this downside, we got here throughout JDK-8180450 — a bug that’s been dormant for greater than 5 years that describes precisely the issue we have been going through. It appears ironic that we couldn’t discover this bug till we truly discovered the reply. We consider our findings complement the good work that has been executed in diagnosing and remediating it.

We have a tendency to think about trendy JVMs as extremely optimized runtime environments, in lots of circumstances rivaling extra “performance-oriented” languages like C++. While it holds true for almost all of workloads, we have been reminded that efficiency of sure workloads working inside JVMs could be affected not solely by the design and implementation of the appliance code, but additionally by the implementation of the JVM itself. In this blogpost we described how we have been capable of leverage PMCs to be able to discover a bottleneck within the JVM’s native code, patch it, and subsequently understand higher than a threefold improve in throughput for the workload in query. When it involves this class of efficiency points, the flexibility to introspect the execution on the degree of CPU microarchitecture proved to be the one resolution. Intel vTune offers precious perception even with the core set of PMCs, similar 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 setting would pave the way in which for deeper efficiency evaluation and doubtlessly even bigger efficiency good points.

LEAVE A REPLY

Please enter your comment!
Please enter your name here