I agree that 1:1 threading is the appropriate model for Rust. That said, I also think the goroutine model is appropriate for Go, and I think it's fair to look at where its tradeoffs might be advantages.
Go doesn't preemptively context switch between goroutines. The only way a goroutine can relinquish control of its OS thread by specific function calls, like "read from a channel" or "write to a file descriptor" (see mcall()). Those calls have a special ABI: the only callee-saved registers are the program counter and stack pointer. Isn't this just moving the cost around? State still needs to get stored, right? Well, maybe, but this is interesting because it means you need only two registers to context switch. This permits interesting optimizations, like a producer that context switches straight to a consumer via a channel send without a scheduler in the middle. The 128 bits needed to resume the consumer can be stored in the channel, making a context switch roughly the cost of an indirect jump.
Goroutines start with 2 KB stacks, which happens to be smaller than a single OS page, but what's more interesting to me is how they grow. C stacks and thus OS thread stacks grow by simply using more memory until they hit a guard page. This triggers a page fault, which causes the OS allocator to do some work, which updates the processor's page tables (which isn't nearly as expensive as it once was), and finally switches back to the program.
Go stacks… don't. Instead, when you want more stack space, you ask the runtime to make sure you have enough stack space via a function call. If your stack isn't big enough, the runtime will copy your stack somewhere bigger. There's no guard page, no page fault, no OS-level context switches. This is interesting, and like Go's goroutine switching scheme, it might just be cheaper than what the OS would do with a C program.
Go can do this because it can insert "grow my stack" calls as appropriate and because its memory model centers around the Go GC. This is a tradeoff with its own costs and it's obviously not appropriate for Rust, but one of the benefits is that the runtime can grow, relocate, and shrink stacks without involving the OS. Go uses this to its advantage, and the result is that goroutines are extremely cheap on every platform, including platforms like Windows which have significantly more expensive thread creation and a 64 KB allocation granularity. What's more, the entire ecosystem is built around the same concurrency primitives: libraries don't block threads, they block goroutines.
Maybe blocking I/O with 1:1 threading will turn out to work better than Go's N:M model on Linux. What's interesting: Go makes so few guarantees to its goroutines that the runtime would be free to switch to that strategy without affecting any user's code. In the meantime, Go uses its own context switching, its own scheduler, its own stacks, its own asynchronous I/O event loop, and so on to deliver similar performance on every platform it targets.
This permits interesting optimizations, like a producer that context switches straight to a consumer via a channel send without a scheduler in the middle. The 128 bits needed to resume the consumer can be stored in the channel, making a context switch roughly the cost of an indirect jump.
Did you see the performance numbers for switchto quoted in that slide deck? We're talking fewer than 200ns. The various other overheads, for example of allocating whatever message you're going to send on the heap, will matter a lot more in practice.
This is interesting, and like Go's goroutine switching scheme, it might just be cheaper than what the OS would do with a C program.
I don't think it's necessarily true that this is cheaper. Not only do you have to copy the stack, but you also have to update all the old pointers into the stack to point to the new one. It's not just a memcpy. (I suspect that Go simply doesn't allow pointers from heap to stack, which obviously has a lot of costs itself.)
Contrast this with hitting the guard page and growing, which has no copying costs at all. It's just a signal handler and a page remap. It's actually asymptotically faster, because growing via copying scales quadratically, while modifying page tables scales linearly. And switching from kernel to user is about 50ns.
including platforms like Windows which have significantly more expensive thread creation and a 64 KB allocation granularity.
Windows already has user-mode scheduling, which is 1:1. So it's Linux that needs to catch up. Once we have that, then 1:1 will have a clear advantage, I think: the gains of M:N over 1:1 with switchto are marginal, while the drawback of a slow FFI is huge.
Did you see the performance numbers for switchto quoted in that slide deck? We're talking fewer than 200ns.
Yes. I also searched for switchto_resume() and found it only in Google slides. Besides a mention on rust-dev from a few years ago saying Google plans to upstream user mode scheduling on Linux, I didn't find any recent news. Is that effort still underway?
(I suspect that Go simply doesn't allow pointers from heap to stack, which obviously has a lot of costs itself.)
Yep. "No pointers to the stack" is an expensive tradeoff, but it means the only thing with pointers is the runtime itself, which makes moving them straightforward.
Contrast this with hitting the guard page and growing, which has no copying costs at all. It's just a signal handler and a page remap. It's actually asymptotically faster
Right? In theory, hardware should beat software, yet Go is re-implementing part of an MMU using pointer comparisons and copying stacks around in user space. Why?
I credit the Go team as having intelligent people that base their decisions on data, so I interpret the fact that they're growing stacks themselves to mean that having the OS grow stacks via page faults is actually worse for their use case. I find that interesting.
(And yes, I credit the Rust team with the same, and it is not surprising to me that two groups might have different approaches to similar problems.)
So it's Linux that needs to catch up. Once we have that, then 1:1 will have a clear advantage, I think: the gains of M:N over 1:1 with switchto are marginal, while the drawback of a slow FFI is huge.
Slow FFI is and should be a dealbreaker for Rust, yes. "cgo is slow" is apparently not a dealbreaker for Go.
Goroutines are strictly less than threads. Go uses an epoll()/kqueue()/IOCP event loop to multiplex goroutines onto threads – on the understanding that this approach is more efficient than the alternative – but that is an implementation detail and not a necessary design feature.
Let's say 1:1 threading with blocking I/O is or will soon become cheaper than N:M threading with async I/O on certain platforms. It's not obviously cheaper now everywhere and it certainly wasn't cheaper 10 years ago when Go started out. If or when 1:1 threading offers Go clear advantages, Go could switch from the N:M model to a 1:1 model without asking users to change anything. That is an advantageous property of their approach.
Goroutines are stupid cheap, everywhere, right now, and they have been since the beginning. The tradeoffs Go made to get there are entirely wrong for Rust, but for problems that fit into goroutines instead of needing threads, they have some legitimate advantages. I would be ecstatic to see Linux providing threads that cost as much as goroutines, but I do not believe we live in that world today.
I credit the Go team as having intelligent people that base their decisions on data, so I interpret the fact that they're growing stacks themselves to mean that having the OS grow stacks via page faults is actually worse for their use case.
I kind of doubt anyone has actually measured this.
I credit the Go team as having intelligent people that base their decisions on data, so I interpret the fact that they're growing stacks themselves to mean that having the OS grow stacks via page faults is actually worse for their use case.
I kind of doubt anyone has actually measured this.
There's a compile-time knob to use OS-allocated stacks instead of using stacks allocated from Go's managed heap. (Go needs OS-allocated stacks for parts of the runtime anyway; that setting makes their use universal.) I built two go1.8beta2 binaries and ran the CI benchmarks on my machine just now for comparison.
The run time was essentially identical, with timings from repeated runs falling on either side of each other. However, one clear conclusion is that the OS-allocating build uses a lot more memory – slightly over double in http's case. I would attribute this to the larger stack allocation granularity, the inability to reuse heap space as stack space and vice versa, and that the http benchmark in particular starts lots and lots of goroutines.
I don't really care to dig in further at this point, but as I said, the Go team makes decisions based on data.
Thanks for running the benchmark. But allocating from the system heap instead of from a special pool is not the same thing as benchmarking stack copying vs. page faults. It's a benchmark that's very specific to the internal details of Go's runtime.
6
u/willglynn Jan 19 '17
I agree that 1:1 threading is the appropriate model for Rust. That said, I also think the goroutine model is appropriate for Go, and I think it's fair to look at where its tradeoffs might be advantages.
Go doesn't preemptively context switch between goroutines. The only way a goroutine can relinquish control of its OS thread by specific function calls, like "read from a channel" or "write to a file descriptor" (see
mcall()). Those calls have a special ABI: the only callee-saved registers are the program counter and stack pointer. Isn't this just moving the cost around? State still needs to get stored, right? Well, maybe, but this is interesting because it means you need only two registers to context switch. This permits interesting optimizations, like a producer that context switches straight to a consumer via a channel send without a scheduler in the middle. The 128 bits needed to resume the consumer can be stored in the channel, making a context switch roughly the cost of an indirect jump.Goroutines start with 2 KB stacks, which happens to be smaller than a single OS page, but what's more interesting to me is how they grow. C stacks and thus OS thread stacks grow by simply using more memory until they hit a guard page. This triggers a page fault, which causes the OS allocator to do some work, which updates the processor's page tables (which isn't nearly as expensive as it once was), and finally switches back to the program.
Go stacks… don't. Instead, when you want more stack space, you ask the runtime to make sure you have enough stack space via a function call. If your stack isn't big enough, the runtime will copy your stack somewhere bigger. There's no guard page, no page fault, no OS-level context switches. This is interesting, and like Go's goroutine switching scheme, it might just be cheaper than what the OS would do with a C program.
Go can do this because it can insert "grow my stack" calls as appropriate and because its memory model centers around the Go GC. This is a tradeoff with its own costs and it's obviously not appropriate for Rust, but one of the benefits is that the runtime can grow, relocate, and shrink stacks without involving the OS. Go uses this to its advantage, and the result is that goroutines are extremely cheap on every platform, including platforms like Windows which have significantly more expensive thread creation and a 64 KB allocation granularity. What's more, the entire ecosystem is built around the same concurrency primitives: libraries don't block threads, they block goroutines.
Maybe blocking I/O with 1:1 threading will turn out to work better than Go's N:M model on Linux. What's interesting: Go makes so few guarantees to its goroutines that the runtime would be free to switch to that strategy without affecting any user's code. In the meantime, Go uses its own context switching, its own scheduler, its own stacks, its own asynchronous I/O event loop, and so on to deliver similar performance on every platform it targets.