runtime icon indicating copy to clipboard operation
runtime copied to clipboard

JIT's allocator performance seems to be low

Open mikedn opened this issue 10 years ago • 63 comments

I profiled crossgen mscorlib.dll out of curiosity and noticed that ~5% of the time is spent in VirtualFree. These free calls come from the ArenaAllocator (formerly known as norls_allocator). It's kind of funny that freeing memory takes about the same time as other JIT actions that do actual work (for example, SsaBuilder::Build takes ~5% as well).

I suspect that this happens because the allocator's page is 64 kbytes and as a result a lot of pages are allocated and a lot of free calls have to be made. Though it's peculiar that allocation is very fast (time spent in VirtualAlloc is less than 1%), perhaps this is a Windows kernel issue.

In any case, 64 kbytes seems too low for 64 bit architectures. I tried with 1 megabyte and crossgen mscorlib.dll time drops down from ~6.8 seconds to ~6.0 seconds.

@pgavlin Perhaps you'll find this interesting, as far as I understand from your recent commit you're planning to do some more work around the allocator.

mikedn avatar Feb 27 '16 12:02 mikedn

Thanks for catching that, @mikedn - it looks like the chunk size hasn't changed since I created norls_alloc back in 1997, so it's probably due for an update ;). For NGen it's probably best to use the largest chunk size we feel we can get away with, but for running apps it doesn't look that simple - I've run a bunch of experiments with ASP.NET / MusicStore and Roslyn, and what seemed to work best was to start with 128K and then add 256K chunks after that. Could you please try again (trying a few options if you have the time and patience) and could you please also share the exact config / command for the NGen test so that we can reproduce it precisely? Basic stuff - what OS, how much RAM, what options on NGen, whatever else you can think of. Thanks!

pkukol avatar Sep 18 '16 23:09 pkukol

The command line is simply crossgen System.Private.CoreLib.dll (it was mscorlib.dll before). The 6.8 seconds result was obtained on a Haswell i5-4440 (6 MB cache, 3.10 GHz), 8 GB RAM and Windows 10 x64.

I can try later today various things, let me know what you have in mind. For now I can say that the 6.8 seconds persists so there weren't any positive or negative changes since I reported this.

but for running apps it doesn't look that simple - I've run a bunch of experiments with ASP.NET / MusicStore and Roslyn, and what seemed to work best was to start with 128K and then add 256K chunks after that

I wonder if we can't estimate an initial size based on method IL code size and number of local variables. Probably it's possible but we'd need to keep this estimation up to date with compiler changes and that may turn out to be a problem.

mikedn avatar Sep 19 '16 12:09 mikedn

Here are the results from a series of crossgen runs with 64, 128, 256, 512 and 1024 kbytes pages. It's pretty easy to observe that 512 kbytes is enough and event 256 would do, 1024 is too much.

mikedn avatar Sep 19 '16 17:09 mikedn

Thanks for getting those measurements - the results basically jive with what I've seen. The allocator I think is initialized before we know much about the IL so we may not have the data available for the initial chunk, but after that we should probably add at least 128 or 256K at a pop and, for bonus points, come up with a simple heuristic based on whatever we think is easy to gauge and is relevant. Does this sound like a reasonable plan?

pkukol avatar Sep 19 '16 17:09 pkukol

Does this sound like a reasonable plan?

Yes.

The allocator I think is initialized before we know much about the IL so we may not have the data available for the initial chunk.

Good point. Though it would be interesting to know how much memory the JIT needs to compile a small method. If we discover that it needs more than 256k of memory to compile a 10 bytes method then it's clear that we can allocate even more at first. Though I really hope that it doesn't need that much memory for 10 bytes of IL :)

mikedn avatar Sep 19 '16 17:09 mikedn

One thing that is important to note about the allocator when considering the size of the initial page is that the same allocator instance is shared between most methods and the first page will remain allocated as long as the JIT instance is in use (i.e. it is allocated when jitStartup is called and freed during jitShutdown). It would be interesting to measure the average memory consumption per method when crossgen'ing S.P.C and see how things perform when the initial allocation is just large enough to cover that amount.

pgavlin avatar Sep 19 '16 18:09 pgavlin

@pkukol: that plan seems reasonable to me as well. It seems like we might also want to vary our allocation sizes based on the host architecture if we aren't already doing so.

pgavlin avatar Sep 19 '16 18:09 pgavlin

You're right @pgavlin, and @mikedn - though I think the initial page should probably remain pretty modest, based on what we discover about the size distribution (except maybe in specific scenarios like NGen on x64 since there's likely lots of memory available there). Past experience tuning things like this tells me that the problem space is likely very hard to map, though (the graph usually looks like a crazy spiky fractal) so I wouldn't aim for perfection (unless someone wants to spend their time doing it, of course ;). I'd also tend to be on the conservative side because the harm done by trying to grab massive blocks could be much worse than sipping memory a bit slower than we could in other cases. Perhaps the thing to do is collect total memory consumed per method histograms for a few of the major scenarios, and then try a few allocator configs and move on. Sound OK or does anyone have a better suggestion? And of course volunteers to help with this are always welcome as well ;).

pkukol avatar Sep 19 '16 18:09 pkukol

Sound OK or does anyone have a better suggestion?

Hmm, there may be some alternatives:

  1. Attempt to reserve a large chunk of virtual memory from the OS. Commit memory only as needed and free the large chunk at the end. Tempting but might work or not depending on OS implementation.
  2. Don't do nothing at the moment, first attempt to decrease the JIT memory usage. For example, there was once a discussion about having a per phase allocator so that memory that's used only within one phase can be reused by future phases.

mikedn avatar Sep 19 '16 21:09 mikedn

For example, there was once a discussion about having a per phase allocator so that memory that's used only within one phase can be reused by future phases.

IIRC @russellhadley looked into this a bit. Russ, any feedback?

pgavlin avatar Sep 19 '16 21:09 pgavlin

Quick comment - there used to be a "page commit" allocator, but 21 years later I can't remember why I dropped it. Please feel free to experiment, of course, but my recollection was that it wasn't worth the hassle of adding #ifdef's for different targets and all that. As for freeing up no longer needed memory, well that's a pretty radical concept; seriously, though, doing that would likely make the other tweaks look silly but it's potentially zillion times more work. We could also look at making the initial block size smarter - maybe based on code we see being compiled or whatever, it could even be a commit-style thing where it expands and shrinks based on what happens. Or we could leave this kind of stuff to MSR and do something simple and move on ;). Let's hear from @russellhadley first, though.

pkukol avatar Sep 19 '16 21:09 pkukol

@pkukol, @mikedn, @pgavlin My experiments never got out of the prototype stage. I was working on a bug where the SSA build was using alloca as part of it's topological sort of the flow graph and blowing up on a large flow graph in NGen mode. I experimented with a "Temp" allocator object to see if I could get all of the SSA info into the temp pool so it could be freed in bulk after the optimizer. In concept these "temp" allocators are pretty simple (so simple it's been done in every other compiler I've ever worked on so I'm not really being creative here) but as Peter says it's probably difficult to get this kind of change seamlessly integrated with the current source. Long story short I don't have data on this so it remains an outstanding question.

russellhadley avatar Sep 19 '16 22:09 russellhadley

@russellhadley

briansull avatar May 20 '17 01:05 briansull

Given the impact that the initial block size has on JIT throughput, looking into this further seems timely.

cc @BruceForstall @CarolEidt

pgavlin avatar Jun 20 '17 17:06 pgavlin

Running release build PMI jitting Microsoft.CodeAnalysis.CSharp.dll I see about 1.7% of the time in PooledAllocator::destroy (1.1% for a minopts run).

Am going to start looking at performance tradeoffs for using larger default page sizes.

AndyAyersMS avatar Jun 12 '18 21:06 AndyAyersMS

Scaling for time in spent jit, it's 1.8% for minopts...

AndyAyersMS avatar Jun 12 '18 21:06 AndyAyersMS

Running release build PMI jitting Microsoft.CodeAnalysis.CSharp.dll I see about 1.7% of the time in PooledAllocator::destroy (1.1% for a minopts run).

Hmm, that seems to be far lower than the impact it has on crossgen corelib. I tried this recently on the latest Win 10 build and the crossgen time still drops by 0.5-1 sec if the default page size is increased to 128k.

That said, the issue is a bit strange. MemStats shows that 51% of the methods in corelib only need the default pooled page and another 36% only need to allocate an extra page. The largest method needs 56 pages but then you'd expect such a method to also take quite a bit of time to compile. It's not very clear how the numbers add up so that VirtualFree ends up taking 5% during crossgen.

I tried to replicate the VirtualAlloc/VirtualFree pattern outside the JIT but it didn't look like VirtualFree is extraordinarily expensive. Though one could question the use of virtual memory allocation vs. heap memory allocation. It's not clear why the JIT thinks it's special enough to go directly to the kernel to ask for memory but HeapAlloc/HeapFree might work better.

At the same time, the JIT has a tendency to waste memory at every step so increasing the allocator page size is a bit of a sweep the garbage under the rug case...

mikedn avatar Jun 13 '18 02:06 mikedn

This was with PMI and not crossgen, so there might be some differences from that. And I was using PerfView to measure, and can't figure out how to stop it from asking the CLR for some events that end up kind of costly.

AndyAyersMS avatar Jun 13 '18 07:06 AndyAyersMS

And I was using PerfView to measure, and can't figure out how to stop it from asking the CLR for some events that end up kind of costly.

Can't PMI somehow measure wall clock time? This is one of those rare cases where the impact is so large that you can measure by using the most primitive means, at least in the crossgen case. It would be interesting to find out that this doesn't hold true for PMI and that this issue is somehow crossgen specific.

mikedn avatar Jun 13 '18 07:06 mikedn

For example in the crossgen case I use a little program that shows the wall clock time (measured using a stopwatch) and process times reported by the Process class. I end up with something like:

wall:7.117s total:7.109s user:6.031s kernel:1.078s

where one can easily see the huge amount of time spent in kernel mode. A profiler can then show that most of that is due to VirtualFree.

mikedn avatar Jun 13 '18 07:06 mikedn