3 ms·
> Context-switching between the kernel and userspace is expensive in terms of CPU cycles. > OS threads have a large pre-allocated stack, which increases per-thr
by IshKebab 3y ago
> Context-switching between the kernel and userspace is expensive in terms of CPU cycles.
> OS threads have a large pre-allocated stack, which increases per-thread memory overhead.
It feels like it would be totally possibly to improve OSes to remove this limitation. Is anyone actually working on that?
For example I don't see why you couldn't have growable stacks for threads. Or have first class hardware support for context switching. (Yes that would take a long time to arrive.)
- zeroCalories 3y ago> It feels like it would be totally possibly to improve OSes to remove this limitation. Is anyone actually working on that? This is just my guess, but I assume they don't use growable stacks for the same reason Rust doesn't. C doesn't have a garbage collector, and making a fragmented stack would incur unacceptable performance penalties for many workloads. Getting around that would require a ton of work, but maybe with hardware support, like automatically following the stack pointer to bring it into cache like a normal stack could get around that.
- duped 3y agoOne problem is that when the stack is allocated it goes into the rest of the same virtual address space, and "growing" it might result in overlapping other address space allocated to the rest of the program. That's why there are guard pages around the stack (also, worth knowing that the stack doesn't grow up on some architectures, it can also grow down like on x86). That's why the post points out that growable stacks have to be moveable. The main solution is to just reserve a ton of virtual address space but avoid committing to it until the process actually writes to it, which is exactly what OS threads do. They reserve a large amount of virtual address space to start but it's more or less free until a thread actually uses it. However you may not see it released back to the OS until the process exits.
- IshKebab 3y agoYeah maybe an issue on 32-bit, but on 64-bit virtual address space is basically unlimited.
- duped 3y agoThe problem isn't the amount of virtual address space but guaranteeing that when you grow you don't hit anything else. And the solution is to just reserve a ton of it. But then the problem becomes reclaiming memory.
- dwattttt 3y agoAt this point though you start to accumulate other performance related issues, such as TLB pressure from all the reservations.
- spease 3y agoI’m pretty sure that modern CPUs do generally have shadow registers to make context switching faster. However, you also have to consider cache contention, the fact it needs to access an indefinite amount of machine code during the context switch, it may service other processes during the context switch, and the effects all this will have on the instruction pipeline. Not to mention bugs in the CPU that might require it to flush the pipeline to maintain complete process separation. So you can keep dedicating more and more silicon to redundant components to get closer to physically representing each thread, or you can code more efficiently. Eg even if you did nothing but loop over a do-nothing system call, it would still need to have two separate executable pages in the cache instead of just one. Not only that, but often the kernel is acting as a mediator for the hardware - which could mean synchronizing between cores, which brings its own obstacles (using slower shared cache, waiting for other cores, etc)
- jrvidal 3y agoI kinda recall some comments by pcwalton about APIs that would ameliorate context-switching costs at the kernel level: https://hn.algolia.com/?dateRange=all&page=0&prefix=true&query=swichto%20user%3Apcwalton&sort=byDate&type=comment https://hn.algolia.com/?dateRange=all&page=0&prefix=true&que...
- IshKebab 3y agoAh that Switchto thing looks really interesting. From my brief skimming it sounds like the actual biggest cost of context switching is not saving and restoring all the state, but deciding which thread to switch to. The Switchto patch allows you to just tell Linux which thread to switch to, so it doesn't need to figure it out. Looks like they reduced the costs by ~20x. I wonder why it was never merged. I guess my intuition was right then. I mean, async is still useful for WASM and microcontrollers, but for "I need to support 10000000 concurrent connections" (which is the usual motivation), it's a hack around poor OS APIs.
- smw 3y agoio_uring is an interesting effort to drastically reduce the context switch overhead by not context switching.
- lukeh 3y agoGood match for async/await, too: I wrote a wrapper in Swift for it. [1] [1] https://github.com/PADL/IORingSwift https://github.com/PADL/IORingSwift