Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

runtime: optimization to reduce P churn #32113

Open
amscanne opened this issue May 17, 2019 · 15 comments
Open

runtime: optimization to reduce P churn #32113

amscanne opened this issue May 17, 2019 · 15 comments

Comments

@amscanne
Copy link

@amscanne amscanne commented May 17, 2019

Background

The following is a fairly frequent pattern that appears in our code and others:

goroutine1:

ch1 <- data (1)
result = <-ch2 (2)

goroutine2:

data = <- ch1 (3)
// do work...
ch2 <- result (4)

The scheduler exhibits two different behaviors, depending on whether goroutine2 is busy and there are available Ps.

  • If goroutine2 is busy or there are no idle Ps, then the behavior is fine. The item will be enqueued in the channel, goroutine2 is marked as runnable if needed, and eventually goroutine1 will yield.
  • If goroutine2 is not busy and there are idle Ps, then the behavior is sub-optimal. The operation in (1) will mark goroutine2 as runnable, and wake up some idle P via a relatively expensive system call [1]. Ultimately the wake will likely result in an IPI to wake an idle core, if there are any. The next P will be scheduled and a race to (2) and (3) ensures.

In the second case, if the P wakes and successfully steals the now runnable goroutine2, i.e. (3) happens first, then it will start executing on the new P. Unfortunately, the whole dance will happen again with the result. If the P wakes but does not successfully steal the now runnable goroutine2, i.e. (4) happens first and goroutine2 is run locally, then a large number of cycles are wasted. Either way, this dance happens again with the result. In both cases, we spend a large number of cycles and interprocessor co-ordination costs for what should be a goroutine context switch.

These are further problems caused by this, as it will introduce unnecessary work stealing and bouncing of goroutines between system threads and cores. (Leading to locality inefficiencies.)

Ideal schedule

With an oracle, the ideal schedule after (1) would be:

  • If goroutine2 is running or there are no idle Ps, enqueue only (current behavior).
  • If goroutine1 will not block or has other goroutines in its runqueue, wake idle Ps (current behavior).
  • If goroutine1 will block immediately, and there are no other goroutines in P's local runqueue, do not wake up any other Ps. The goroutine2 will be executed by the current P immediately after goroutine1 blocks.

In essence, we want to yield the goroutine1's time to goroutine2 in this case, or at least avoid all the wasted signaling overhead. To put it another way: if goroutine1's P will block, then it fills the role of the "idle P" far more efficiently.

Proposal

It may be possible to specifically optimize for this case in the compiler, just as certain loop patterns are optimized.

In the case where a blocking channel send is immediately followed by a blocking channel receive, I propose an optimization that tries to avoid these scheduler round trips.

Here's a rough sketch of the idea:

  • runqput returns a bool that indicates whether the newly placed G is the only item on the queue. (Alternatively we could just check the runq length below.)
  • goready takes an additional parameter "deferwake" which skips the wake operation if true. By default this will be false everywhere, which implements current behavior.
  • chansend accepts a similar "deferwake" parameter. This is plumbed through to send, and will be AND'ed with the result of runqput. The deferwake parameter will be passed as true if the compiler detects a blocking receive immediately following the blocking send statement (or possibly in the same block, see below).
  • chanrecv also accepts a "deferwake" parameter, which will be set to true only when proceeded by a call to chansend with deferwake also set to true. If this is true AND the current goroutine will not yield as a result of the recv AND the current runqueue length > 0 AND there are idle Ps, at this point we can call wakeup.

Rejected alternatives

I thought about this problem a few years ago when it caused issues. In the past, I considered the possibility of a different channel operator. Something like:

ch1 <~ data

This operator would write to the channel and immediately yield to the other goroutine, if it was not already running (otherwise would fall back to the existing channel behavior). Using this operator in the above situation would make it much more efficient in general.

However, this is a language change, and confusing to users. When do you use which operator? It would be good to have the effect of this optimization out of the box.

Extensions

  • This optimization may apply to other kinds of wakes. I consider only channels today.
  • The optimization could be extended to cases where a blocking channel receive appears following the blocking send in the same block, not necessary the subsequent statement.

[1] https://github.com/golang/go/blob/master/src/runtime/proc.go#L665

@gopherbot gopherbot added this to the Proposal milestone May 17, 2019
@randall77
Copy link
Contributor

@randall77 randall77 commented May 17, 2019

What about just enforcing a minimum delay between when a G is created and when it can be stolen? That gives the local P time to finish the spawning G (finish = either done or block) and pick up the new G itself.

The delay would be on the order of the overhead to move a G between processors (sys calls, cache warmup, etc.)

The tricky part is to not even wake the remote P when the goroutine is queued. We want a timer somehow that can be cancelled if the G is started locally.

Loading

@bradfitz bradfitz changed the title proposal: optimization to reduce P churn proposal: runtime: optimization to reduce P churn May 17, 2019
@bradfitz
Copy link
Contributor

@bradfitz bradfitz commented May 17, 2019

Loading

@amscanne
Copy link
Author

@amscanne amscanne commented May 17, 2019

Yes, most of the waste is generated by the wakeup call itself. Ensuring that the other P does not steal the G is probably a minor improvement, but you're still going to waste a ton of cycles (maybe even doing these wake ups twice -- on (1) and (4)).

I think using a timer gets much trickier. This is the reason I have limited the proposal to compiler-identified sequences of "chansend(block=true); chanrecv(block=true)" calls. It's possible that the system thread could be pre-empted between those calls, but if the system is busy (though Ps in this process may still be idle) it's probably even more valuable to not waste useless cycles.

Loading

@amscanne
Copy link
Author

@amscanne amscanne commented May 17, 2019

(Totally open to a timer, but I'm concerned about replacing a P wakeup with a kick to sysmon in order to enforce the timer, which solves the locality issue but still burns cycles.)

Loading

@dvyukov
Copy link
Member

@dvyukov dvyukov commented May 18, 2019

Also see #8903 which was about a similar problem. I don't remember all details exactly now, but as far as I remember my proposal was somewhat more generic, but your wins in simplicity and most likely safer from potential negative effects for corner cases.

Loading

@rsc
Copy link
Contributor

@rsc rsc commented May 28, 2019

This has come up repeatedly. Obviously it is easy to recognize and fuse

ch1 <- data (1)
result = <-ch2 (2)

It's harder to see that in more complex code that would benefit from the optimization, though. We've fiddled with heuristics in the runtime to try to wait a little bit before stealing a G from a P, and so on. Probably more tuning is needed.

It's unclear this needs to be a proposal, unless you are proposing a language change, and it sounds like you've backed away from that.

The way forward with a suggestion like this is to try implementing it and see how much of an improvement (and how general of an improvement) it yields.

Loading

@rsc rsc removed this from the Proposal milestone May 28, 2019
@rsc rsc added this to the Unplanned milestone May 28, 2019
@rsc
Copy link
Contributor

@rsc rsc commented May 28, 2019

Loading

@golang golang deleted a comment from rsc May 28, 2019
@randall77
Copy link
Contributor

@randall77 randall77 commented May 28, 2019

Related:

#27345 (start working on a new goroutine immediately, on the parent's stack)
#18237 (lots of time in findrunnable)

I also remember an issue related to ready goroutines ping-ponging around Ps, but I can't find it at the moment.

Loading

@amscanne
Copy link
Author

@amscanne amscanne commented May 29, 2019

I backed away from a language change proposal based on the assumption that it would likely not be accepted. My personal preference would be to have an operation like <~ that immediately switches to the other goroutine if currently waiting. (And behaves like a normal channel operation if busy.) But I realize that the existence of this operator might be confusing.

I think it's unclear how much of a impact this would have in general. This is probably just be a tiny optimization that doesn't matter in the general case, but can help in a few very specific ones. For us, it might let us structure some goroutine interactions much more efficiently.

I hacked something together, and it seems like there's a decent effect on microbenchmarks at least (unless I screwed something up).

Code:

func BenchmarkPingPong(b *testing.B) {
	var wg sync.WaitGroup
	defer wg.Wait()

	ch1 := make(chan struct{}, 1)
	ch2 := make(chan struct{}, 1)
	wg.Add(2)
	go func() {
		defer wg.Done()
		for i := 0; i < b.N; i++ {
			ch1 <- struct{}{}
			<-ch2
		}
	}()
	go func() {
		defer wg.Done()
		<-ch1
		for i := 0; i < b.N-1; i++ {
			ch2 <- struct{}{}
			<-ch1
		}
		ch2 <- struct{}{}
	}()
}

Before:

/usr/bin/time /usr/bin/go test -bench=.* -benchtime=5s
goos: linux
goarch: amd64
BenchmarkPingPong-4   	20000000	       563 ns/op
PASS
ok  	_/home/amscanne/gotest/spin	11.805s
12.68user 1.00system 0:11.98elapsed 114%CPU (0avgtext+0avgdata 46036maxresident)k
0inputs+3816outputs (0major+19758minor)pagefaults 0swaps

After:

/usr/bin/time go test -bench=.* -benchtime=5s
goos: linux
goarch: amd64
BenchmarkPingPong-4   	20000000	       330 ns/op
PASS
ok  	_/home/amscanne/gotest/spin	6.949s
7.11user 0.05system 0:07.11elapsed 100%CPU (0avgtext+0avgdata 46460maxresident)k
0inputs+3824outputs (0major+19084minor)pagefaults 0swaps

The system time is telling @ 20x, and the extra 14% in CPU usage is indicative of an additional P waking up with nothing to do. (Or maybe it occasionally successfully steals the goroutine, which is also bad.)

Assuming this small optimization is readily acceptable -- what's the best way to group those operations and transform the channel calls? The runtime bits are straight-forward, but any up front guidance on the compiler side is appreciated. Otherwise, I'm just planning to call a specialized scan in walkstmt list, but maybe there's a better way.

Loading

@rsc
Copy link
Contributor

@rsc rsc commented Jun 4, 2019

Given that there is no language change here anymore, going to move this to being a regular issue.

Loading

@rsc rsc changed the title proposal: runtime: optimization to reduce P churn runtime: optimization to reduce P churn Jun 4, 2019
@rsc rsc removed the Proposal label Jun 4, 2019
@prattmic
Copy link
Member

@prattmic prattmic commented Aug 31, 2020

I've started looking into this. I've got a very naive implementation (probably very similar to Adin's) to use with his microbenchmark.

Combined with perf stat, we can see higher-level system effects of the change.

Fixed time (-benchtime=1s):

name                   old time/op  new time/op  delta
ChanPingPong-12         467ns ± 2%   247ns ± 2%  -47.07%  (p=0.000 n=9+10)

name                   old iters    new iters    delta
ChanPingPong-iters-12   2.51M ± 5%   4.44M ±14%  +76.87%  (p=0.000 n=10+10)

name                   old msec     new msec     delta
Perf-task-clock         2.01k ± 2%   1.38k ± 5%  -31.43%  (p=0.000 n=10+8)

name                   old val      new val      delta
Perf-context-switches   44.1k ± 2%    0.7k ± 8%  -98.52%  (p=0.000 n=10+8)
Perf-cpu-migrations       182 ±19%      10 ±27%  -94.39%  (p=0.000 n=10+9)
Perf-page-faults          518 ± 8%     511 ± 8%     ~     (p=0.536 n=10+9)
Perf-cycles             4.58G ± 2%   5.54G ± 6%  +21.05%  (p=0.000 n=10+8)
Perf-instructions       8.21G ± 3%  11.22G ± 6%  +36.63%  (p=0.000 n=10+8)
Perf-branches           1.69G ± 3%   2.30G ± 6%  +35.76%  (p=0.000 n=10+8)

Fixed iterations (-benchtime=10000000x):

name                   old time/op  new time/op  delta
ChanPingPong-12         473ns ± 2%   241ns ± 3%  -49.00%  (p=0.000 n=10+10)

name                   old msec     new msec     delta
Perf-task-clock         5.68k ± 3%   2.43k ± 3%  -57.15%  (p=0.000 n=10+10)

name                   old val      new val      delta
Perf-context-switches    125k ± 3%      1k ± 9%  -99.54%  (p=0.000 n=10+9)
Perf-cpu-migrations       517 ±13%      11 ±51%  -97.95%  (p=0.000 n=10+10)
Perf-page-faults          469 ± 9%     473 ±11%     ~     (p=0.928 n=10+10)
Perf-cycles             13.1G ± 2%   10.4G ± 2%  -20.56%  (p=0.000 n=10+10)
Perf-instructions       23.2G ± 0%   21.0G ± 0%   -9.52%  (p=0.000 n=10+8)
Perf-branches           4.79G ± 0%   4.31G ± 0%  -10.11%  (p=0.000 n=10+8)

I've included both since the the different fixed dimensions change the interpretation. e.g., the first case has higher cycles after because it is simply able to do a lot more work. And it still does nearly double the iterations in 30% less CPU time (== far less time stalled)!

This certainly looks worthwhile from the micro-benchmark perspective. The questions remaining to me are if we can efficiently and reliably detect these scenarios, and if they affect many programs.

Loading

@prattmic
Copy link
Member

@prattmic prattmic commented Sep 1, 2020

For future reference, here's @amscanne's prototype: amscanne@eee812b

This is a bit more advanced than mine, as I haven't made any compiler changes yet.

Loading

@gopherbot
Copy link

@gopherbot gopherbot commented Sep 14, 2020

Change https://golang.org/cl/254817 mentions this issue: WIP: merge chansend1 + chanrecv1 into unified chansendrecv1

Loading

@GuhuangLS
Copy link

@GuhuangLS GuhuangLS commented Jun 10, 2021

For future reference, here's @amscanne's prototype: amscanne@eee812b

This is a bit more advanced than mine, as I haven't made any compiler changes yet.

@prattmic Michael, I have one question, amscanne/go@eee812b needs to modify apis? and needs user's program to perceive?How does compiler make the decision?

Loading

@prattmic
Copy link
Member

@prattmic prattmic commented Jun 10, 2021

Neither @amscanne nor my prototype change any language syntax or APIs. Rather, the compiler detects channel send followed immediately by channel receive and rather than calling the typical runtime.chansend and runtime.chanrecv functions, it emits calls to alternative implementations (a single merged runtime.chansendrecv in my case).

Both prototypes are rudimentary and would probably hurt performance for many programs due to poor decisions and would need more refinement.

Loading

copybara-service bot pushed a commit to google/gvisor that referenced this issue Nov 1, 2021
Some synchronization patterns require the ability to simultaneously wake and
sleep a goroutine. For the sleep package, this is the case when a waker must be
asserted when a subsequent fetch is imminent.

Currently, this operation results in significant P churn in the runtime, which
ping-pongs execution between multiple system threads and cores and consumes a
significant amount of host CPU (and because of the context switches, this can
be significant worse with mitigations for side channel vulnerabilities).

The solution is to introduce a dedicated mechanism for a synchronous switch
which does not wake another runtime P (see golang/go#32113). This can be used
by the `AssertAndFetch` API in the sleep package.

The benchmark results for this package are very similiar to raw channel
operations for all cases, with the exception of operations that do not wait.
The primary advantage is more precise control over scheduling. This will be
used in a subsequent change.

```
BenchmarkGoAssertNonWaiting
BenchmarkGoAssertNonWaiting-8                   261364384                4.976 ns/op
BenchmarkGoSingleSelect
BenchmarkGoSingleSelect-8                       20946358                57.77 ns/op
BenchmarkGoMultiSelect
BenchmarkGoMultiSelect-8                         6071697               197.0 ns/op
BenchmarkGoWaitOnSingleSelect
BenchmarkGoWaitOnSingleSelect-8                  4978051               235.4 ns/op
BenchmarkGoWaitOnMultiSelect
BenchmarkGoWaitOnMultiSelect-8                   2309224               520.2 ns/op

BenchmarkSleeperAssertNonWaiting
BenchmarkSleeperAssertNonWaiting-8              447325033                2.657 ns/op
BenchmarkSleeperSingleSelect
BenchmarkSleeperSingleSelect-8                  21488844                55.19 ns/op
BenchmarkSleeperMultiSelect
BenchmarkSleeperMultiSelect-8                   21851674                54.89 ns/op
BenchmarkSleeperWaitOnSingleSelect
BenchmarkSleeperWaitOnSingleSelect-8             2860327               416.4 ns/op
BenchmarkSleeperWaitOnSingleSelectSync
BenchmarkSleeperWaitOnSingleSelectSync-8         2741733               427.1 ns/op
BenchmarkSleeperWaitOnMultiSelect
BenchmarkSleeperWaitOnMultiSelect-8              2867484               418.1 ns/op
BenchmarkSleeperWaitOnMultiSelectSync
BenchmarkSleeperWaitOnMultiSelectSync-8          2789158               427.9 ns/op
```

PiperOrigin-RevId: 406873844
zhuangel pushed a commit to zhuangel/go that referenced this issue Nov 16, 2021
Background:

Scheduler is very easy to get into a "P churn" problem as stated by Adin
in golang#32113. This problem is more serious
in gVisor as futex() syscall, used for wake and idle M, is a much heavier
operation from GR0 into HR0.

Adin proposed to add the context semantics into scheduler to decide if
we need to wake a new M. Let's call it a local strategy.

Here we propose another way to solve this problem. Let's call it a global
strategy. When we need to decide whether to start a new M, except the
condition of an extra P, let's calculate # of runnable Gs and # of
running Ps. When # of runnable Gs <= # of running Ps * factor, do not start
another M, as those already-running Ms will steal Gs this P. We have tried
using a factor of 1.5; but then switch to 1.

The mechnism applies when we ready a G; and we also add this to
handleoffp(). For handoffp(), the previous strategy to wake up a M is to
satisfy one of below two conditions:
  - the local runq is not empty;
  - the global runq is not empty.

We constrain the 2nd condition by checking the comparison of # of running
Gs and # of running Ps. (Note that the running P here has different
meaning with the one used above for wakep().

Two concerns are raised for this method:
  - If it adds too much contention when we do the G/P counting? A side
    info is that we usually set GOMAXPROCS to 4 or 8. And as we see from our
    result, CPU util is much lower; and we don't see too much contention
    in the flame graph.
  - If it brings worse latency? Yes, it does incur small regression in
    latency, but the CPU util seems a big enough advantage.

Signed-off-by: Shi Liu <liushi.ls@antgroup.com>
Signed-off-by: Jielong Zhou <jielong.zjl@antgroup.com>
Signed-off-by: Yong He <chenglang.hy@antgroup.com>
Signed-off-by: Jianfeng Tan <henry.tjf@antgroup.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
8 participants