3 ms·
This is a good overview, but even by 2014 we weren't calling hlt - there's a few problems there. Imagine the situation where you fork() a process on one CPU, an
by mjg59 4y ago
This is a good overview, but even by 2014 we weren't calling hlt - there's a few problems there. Imagine the situation where you fork() a process on one CPU, and you have another CPU that recently called hlt. Ideally you'd schedule it immediately instead of waiting for the next scheduler interrupt, but sending interrupts from one CPU to another (to break it out of hlt) is kind of expensive. The replacement is an instruction called mwait, which will (like hlt) wake the CPU up on interrupts, but will also wake up the CPU on a write to a specific memory address. If you want to wake up an idle CPU, the working CPU can just do that memory write and then the idle CPU will wake up back into the scheduler and figure out it has work to do.
(Edit: there are still actually some circumstances in which hlt is called rather than mwait, but this is very CPU dependent and is based on vendor recommendations. Also, before mwait showed up, switching to deep CPU power saving states tended to involve performing magic IO port accesses rather than calling hlt, and yes the world was worse back then)
- deleted 4y ago[deleted]
- majke 4y agoThanks for an awesome explanation. I always thought modern x86 uses IPI (Inter-Processor Interrupts) for that. Is there a benefit of mwait vs IPI?
- unsafecast 4y agoFrom the GP: >[...] but sending interrupts from one CPU to another (to break it out of hlt) is kind of expensive. The replacement is an instruction called mwait, which will (like hlt) wake the CPU up on interrupts, but will also wake up the CPU on a write to a specific memory address.