This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Split Stack performance, signals


> I can tell you a lot about the Go stack-copy implementation, I worked on a lot of it.

Thanks! Keith. I appreciate the insight.

> The big advantage is that you only have to copy once (amortized).  The stack grows as big as you need it and then you don't need to do any more work from then on.  This saves lots of morestack calls, but perhaps more importantly it saves you from having to optimize everything from morestack on down.  morestack is almost never called, so you can write it in a high-level language, put lots of debugging checks in it, etc.

Note: In my experiment, I only simulated pushing the hottest path out
of generic_morestack in C into assembly, more or less by pre-staging
all the information all the information necessary to flip from one
segment and the next already valid one with a simple assembler stub
that just interprets it or bails out to a more generic_morestack. I
ended up with about 15 assembler instructions; the test is actually
quite a bit smaller than the current morestack.S. I definitely agree
that the main work should be in a HLL, perhaps even more than in the
current state.

> Another advantage is runtime predictability.  Split stacks were not a large performance problem, but they were a large source of unexplained performance variance.  We'd often see apparently no-op changes make runtime vary by +/- 5% because they change frame sizes by just a little bit and trigger or untrigger the hot split.  So if you test a code change and get a 4% improvement, is your code really better code, or did you just get lucky?  Maybe your code change slows things down by 1% but happens to avoid a hot split.

This is a really interesting insight. Clearly the problem would be the
same for all GCC programs using -fsplit-stack, not just Go. What this
means is that you could make a change to some unrelated part of the
program, perhaps even reduce stack usage in some other routine, and
the overhead of some non-inline call somewhere completely else goes
from < 1 clock to > 4000. I think that +/- 5% fluctuation observed in
the wild is just related to the hot-split not being pathologically bad
(i.e. a fast function called in a loop). The natural call graph just
happens to be spanning the split or not as it likes.

But in the worst case the overall program execution time could go up
over 100:1 (presuming the function was a minimal ~40 clocks). That's a
hang, not a performance issue. Even without the system calls, it could
cause the overall program time to double or triple in the pathological
worst case.

I was really surprised to find that what looks like a function call
sometimes resulted in a couple system calls. I think that the signal
issue might be able to be handled a different way. This would at least
reduce the overhead by two orders of magnitude. I just read Ian's
response now and I hadn't noticed the __splitstack_block_signals
trick.

Now, does Go still use the same function prologs? I presume it does
and has just replaces __morestack. The other problem is that the
prolog itself is quite big. I looked at the average size of functions
for example in vmlinux and it was around 216 bytes. So 36 additional
bytes is around 16% overhead. I wouldn't worry about that too much
except for its effect on i-cache.

I think the mitigation for this is to avoid the prolog on leaf
functions. I notice that at the moment even leaf functions that use
the red-zone optimization and don't touch %rsp still get the prolog
applied, which I think is unnecessary.

-- Anders

On Sun, Sep 13, 2015 at 12:17 AM, Keith Randall <keith.randall@gmail.com> wrote:
> I can tell you a lot about the Go stack-copy implementation, I worked
> on a lot of it.  The big drawback is that you need to ensure that you
> have accurate stack maps for all frames on the stack.  This is
> something we can do in Go with some work but would be more problematic
> for C.  Most of that work is necessary for precise garbage collection
> (GC) anyway, so any language with precise GC gets this for free.  Some
> smaller things:
>
> 1) You need to be able to find any pointers from heap->stack
> efficiently.  For Go, these were things like defer records, panic
> records, and channel queues.
>
> 2) You need to be able to find contiguous memory for the stacks.  We
> haven't seen this to be a problem in real-world servers.
>
> 3) You need to figure out when to shrink a stack.  In Go we shrink
> stacks at GC time by finding stacks less than 1/4 used and shrinking
> them by half.  Shrinking logic is complicated by the fact that the
> shrinkee may be in a syscall, in which case shrinking is not possible
> because you might have passed a stack pointer into that syscall.
>
> The big advantage is that you only have to copy once (amortized).  The
> stack grows as big as you need it and then you don't need to do any
> more work from then on.  This saves lots of morestack calls, but
> perhaps more importantly it saves you from having to optimize
> everything from morestack on down.  morestack is almost never called,
> so you can write it in a high-level language, put lots of debugging
> checks in it, etc.
>
> Another advantage is runtime predictability.  Split stacks were not a
> large performance problem, but they were a large source of unexplained
> performance variance.  We'd often see apparently no-op changes make
> runtime vary by +/- 5% because they change frame sizes by just a
> little bit and trigger or untrigger the hot split.  So if you test a
> code change and get a 4% improvement, is your code really better code,
> or did you just get lucky?  Maybe your code change slows things down
> by 1% but happens to avoid a hot split.
>
> On Sat, Sep 12, 2015 at 11:38 PM, Anders Oleson <anders@openpuma.org> wrote:
>> I have been experimenting with -fsplit-stack, in particular related to
>> performance issues I have read about in rust and go relative to a "hot
>> split" where a function call in a tight loop continuously crosses a
>> split. Originally I was interested as an analogy to approximate
>> performance issues with other similar stack manipulations. I think the
>> original idea is that __morestack would be called extremely rarely.
>> Unfortunately there is the edge case that a stack segment boundary
>> falls right in the middle of some hot function call.
>>
>> I wrote a minimal routine called in a loop a few hundred million times
>> and I adjusted the stack usages until I found the split. I was really
>> surprised to see how bad the hot-split problem can be. Presumably the
>> actual allocation is done on the first split. The subsequent ones
>> should just link to the next segment "quickly" so the one-time
>> allocation cost can be ignored. I'm using an older stock Ubuntu x64
>> GCC 4.8.4 btw., but things don't appear to have changed recently.
>>
>> It is taking >4000 clocks per __morestack call (about 1us on 4GHz Haswell)!
>>
>> The difference between a non-split call and one with the split prolog
>> in the optimistic case where __morestack is not called for comparison
>> is < 1 clock. So the prolog is fairly efficient in execution time. It
>> is unfortunately large at around 36 bytes per function. I was able to
>> nibble away slightly at both the speed and size, but there isn't much
>> to gain.
>>
>> From examining the __morestack code, I found that the sigprocmask
>> system call is being called (twice?!) per __morestack, even when it
>> should just need to switch to the next allocated segment. I did read
>> the reason for that change: to allow signal handlers to be
>> split-stack, (ignoring that detail for the moment). A quick experiment
>> shows that removing the calls to __morestack_block_signals and
>> __morestack_unblock_signals brings the overhead of the hot split down
>> to around 60 clocks which is much more reasonable.
>>
>> However in concept simply switching stack segments *should* not be
>> hugely expensive. I made a proof-of-concept that only does the very
>> minimal work to switch from one segment to another. This is done in
>> assembler (conceptually in __morestack) and eliminates the call out to
>> "C" on the likely hot path where the boundary has already been crossed
>> and the next segment is already big enough. If you cache the details
>> of the next/prev segments (if present) in the space right below the
>> bottom (limit) of each stack segment, you can shrink the time down to
>> 5-6 clocks. This is probably close to the achievable floor, which was
>> in part what I was trying to find out.
>>
>> Summary:
>>   prolog overhead, no call to __morestack : < 1 clock
>>   stock call to __morestack (hot): > 4000 clocks
>>   without signal blocking: < 60 clocks
>>   potential best case: < 6 clocks
>>
>> I have noticed that both Go and Rust have now abandoned the split
>> stack approach due to performance issues. In Go, the idea to have
>> zillions of tiny (go)co-routines or green threads is closer to my
>> interest area than the Rust use. Even on x64, I think there are still
>> reasons for wanting to break out of needing large linear stacks. Or it
>> may be useful to other embedded applications. But in Go, apparently
>> the fix is to copy the stack (all of it?) which seems pretty drastic,
>> expensive and really tricky. At least it would only happen once. I was
>> wondering if there was any thought into doing more work to optimize
>> the -fsplit-stack? Does the Go stack-copy implementation have other
>> issues?
>>
>> Another area I didn't explore was that certain leaf and small routines
>> with known maximum stack usage could avoid needing the prolog.This
>> might ameliorate much of the size issue.
>>
>> Bottom line is that I don't know whether this is something anyone
>> still has any interest in, but in theory at least the "hot-split"
>> problem could be improved significantly. At least I learned what I was
>> trying to, and I put this out in case it is of use/interest to anyone.
>>
>> -- Anders


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]