10 ms·
What does an idle CPU do? (2014)
- assetlabel 4y agoRISC-V: WFI - WFI (Wait For Interrupt) makes the processor suspend execution (Clock is stopped) until one of the following events take place: An IRQ interrupt. An FIQ interrupt. A Debug Entry request made to the processor.
- bpye 4y agoARM has the same. Apple's CPUs are interesting as they lose most register state on WFI, though there is a chicken bit to disable that behaviour - it's mentioned in one of the Asahi Linux blog posts [0] [0] - https://asahilinux.org/2021/03/progress-report-january-february-2021/ https://asahilinux.org/2021/03/progress-report-january-febru...
- frank_bb 4y ago
- randyrand 4y agoI still don’t really see the need to wake up every 4ms when all processes are suspended or killed. Just halt the CPU permanently until the next interrupt. 4ms interrupts are only needed when there are multiple active threads that need preempting.
- smcameron 4y agoI mean, a couple paragraphs down, the article says: > The solution here is to have a dynamic tick so that when the CPU is idle, the timer interrupt is either deactivated or reprogrammed to happen at a point where the kernel knows there will be work to do (for example, a process might have a timer expiring in 5 seconds, so we must not sleep past that). This is also called tickless mode.
- kevingadd 4y agoModern kernels are tickless like you describe.
- tibbydudeza 4y agoIn the days before MMU's and fancy DRAM controllers and newer DRAM chips the CPU had to do memory refresh during "idle" time.
- tyingq 4y agoDepends on what point in time you warp back to. There was a point where most home computers had static RAM.
- randombits0 4y agoOh, look at Mr. Moneybags over here!
- tibbydudeza 4y agoYep but cost and when memory starting exceeding 64K.
- Tuna-Fish 4y agoEr, when? Apple II was sort of the first big successful home computer, and it had DRAM (The CPU and the display accessed the ram on alternate cycles, and Woz placed the display buffers so that most of the refresh was "free"). As far as I can tell, all the actually widespread successful computers after that have used DRAM too.
- shadowofneptune 4y agoIt's earlier than that, dating to the time of the Altair clones. Refresh hardware didn't come with those machines so adding it yourself complicated the memory board.
- tyingq 4y agoYes, before the widespread successful bit, almost by definition, since SRAM was expensive.
- Someone 4y agohttps://en.wikipedia.org/wiki/Static_random-access_memory#In_computers https://en.wikipedia.org/wiki/Static_random-access_memory#In...: “SRAM was used for the main memory of most early personal computers such as the ZX80, TRS-80 Model 100 and Commodore VIC-20.” It wouldn’t call it “widespread successful”, but he Macintosh Portable also had SRAM (https://en.wikipedia.org/wiki/Macintosh_Portable https://en.wikipedia.org/wiki/Macintosh_Portable)
- mcdonje 4y ago>Whenever you write an if statement, Nyan Cat cries. If I ever offended Nyan Cat, then I'm very sorry.
- silisili 4y agoWhile I offend Nyan Cat,
- contravariant 4y agoNot sure if this loop ever exits.
- politelemon 4y agoDo until Nyan cat is offended
- mhh__ 4y agoBut why does Nyan cat get upset? Is this a bad nod towards branchless programming?
- shric 4y ago(2014)
- iasay 4y agoOn my Windows laptop, Windows Defender is the devil waiting for idle hands.
- Rodeoclash 4y agoMy gosh this. I recently had Windows update on my sons laptop, a 2012 "hand me down" Thinkpad. We'd composed a song together in Ableton that used 4 soft-synths and after the update it could not play the song without pegging the CPU! I eventually tracked this down to Windows Defender sitting at 40% CPU! Disabling it entirely brought the performance of the laptop back to what it was pre-update. I find this behaviour extremely disappointing. This is a perfectly serviceable laptop that would have likely been replaced by someone who was less technically inclined had it happened to them and they did not have the wherewithal to track down the offending piece of software. Your original quote reminded me of a Cormac McCarthy quote which seems to apply to the Windows Defender engineers right now (I kid): > But when God made man the devil was at his elbow. A creature that can do anything. Make a machine. And a machine to make the machine. And evil that can run itself a thousand years, no need to tend it.
- seanw444 4y agoIt's crazy because even slightly heavier Linux distros run smooth as butter on laptops that Windows chugs resources on. There's nothing fancier about Windows. There's literally no reason for it to be as poorly optimized as it is, and yet it is. It's crazy to me.
- ticviking 4y agoThe reason is simple. It doesn’t impact Microsoft’s bottom line. Poor Linux performance will eventually annoy someone enough to fix it, and being open source they actually can fix it
- beachy 4y agoIt actually negatively impacts Microsoft's bottom line. A slow machine means a new PC purchased which usually means a new Windows licence, so there are negative incentives on Microsoft to improve Windows performance.
- 3a2d29 4y agoAnyone know other resources like this? I am currently in the middle of learning about Operating Systems by reading my roommates old CS textbook and doing his projects. Would love to look at stuff like this because I just learned a chapter on interrupts and this supplemented it well.
- mhh__ 4y agoHennessey and Patterson contains a little bit of soft information/intuition like this (as well as the computer architecture)
- userbinator 4y agoIn DOS and Win3.x/9x, I believe the idle is instead an input polling loop. That's why VMs running those OSs will appear to be taking a full host core all the time, unless a special "idle TSR" utility is used. I'm not sure if that polling instead of interrupts is also responsible for their perceived lower input latency relative to other OSs of the time.
- baisq 4y agoYes, I remember having to install a 3rd-party application to stop Windows 95 or 98 inside of a VM from using my entire host CPU.
- rsch 4y agoWindows 95 didn’t use the HLT instruction because it didn’t work reliably on many PCs back in the day. It would sometimes freeze instead of just halt. There was a post on the oldnewthing blog about this but I can’t immediately find it.
- mikequinlan 4y agohttps://devblogs.microsoft.com/oldnewthing/20030828-00/?p=42753 https://devblogs.microsoft.com/oldnewthing/20030828-00/?p=42...
- userbinator 4y agoSome of his posts are possibly more on the fictional side, but given that the Linux kernel also has (or had?) code to check for and also avoid a HLT instruction that doesn't work (search for "Checking 'HLT' instruction..."), I think this one is close to the truth (but of course, also begs the question of why MS didn't do what Linux did) On the other hand, HLT was commonly found in the DOS software of the time, often for quick and approximate timing delays, and I don't remember any specific discussions about that causing problems, so I'm still not sure what the true problem is; my guess is that the combination of protected mode, which increases power consumption over real mode, and the sudden drop in current draw caused by a HLT, was enough to cause marginal power supply circuitry to fall out of regulation, similar to how stress-testing when searching for a stable overclock will sometimes crash the machine when it ends, not when it starts. On the more digital side, interactions with SMM might also be relevant: http://www.rcollins.org/ddj/Mar97/Mar97.html http://www.rcollins.org/ddj/Mar97/Mar97.html
- deleted 4y ago[deleted]
- Graffur 4y agoDoes anyone know if projects that aim to use CPU idle time are actually useful? One example I found: https://einsteinathome.org/ https://einsteinathome.org/
- colechristensen 4y agoFolding@home list of published results https://foldingathome.org/papers-results/?lng=en https://foldingathome.org/papers-results/?lng=en
- ilrwbwrkhv 4y agoI remember the seti one. Probably dedicated a decade of idle cpu usage to it.
- tiffanyh 4y agoPeople make the argument that unused RAM is wasted RAM. I wonder if the same logic applies to CPUs. Unused clock cycles is wasted CPU.
- tails4e 4y agoIdle CPUs use much less power, so no its not wasted in that sense.
- vbezhenar 4y agoLet's say we have CPU with $500 cost which consumes 100W. 1 kWh is $0.1. Case 1: we used this CPU for year with 1% average load. We paid $500 + 365 * 24h * 100W * 1% * $0.1 = $500 + $876 = $1 376. Case 2: we used this CPU for year with 100% average load. We paid $500 + 365 * 24h * 100W * 100% * $0.1 = $500 + $87 600. Case 3: like case 2, but we have cheap electricity with $0.01 for kWh. We will pay $500 + $8 760. In any case CPU cost itself is tiny compared to energy price. So we can consider CPU cost is zero and all computations just require spending electricity. So when some project asks to use your CPU at idle times, they're asking to use your electricity. And that's about it. You're donating electricity. It would be wrong to think that they're asking to use CPU which is not used anyway. Now whether you want to donate electricity or not is another matter. But one should clearly understand what he does. Edit: I forgot to divide by 1000, so those numbers are completely off, sorry.
- NavinF 4y ago> $500 + $87 600 100W * $0.10/kWh * 1 year = $87.66 You forgot to convert Wh to kWh. Google can evaluate the LHS in one step. That aside, I agree. The price difference is even larger with 400W GPUs and data center bills which include cooling in the electricity cost. Initial cost matters very little vs efficiency.
- vbezhenar 4y agoWell, I guess that my conclusions were completely wrong for home usage. Spending $87 vs $8 per year is not a big deal and $500 CPU cost now dominates the total cost. So it makes sense to donate idle CPU time if you're fine with extra heat and some extra wear.
- usr1106 4y ago2014 can be like stone age if you think a new kernel release gets released less than every 90 days. On the other hand development of some features just takes longer and longer than ever expected (not a surprise for any developer). Well, stone age at least if you run a bit more fresh distro than some CentOS or oldstable Debian. So are typical modern desktop Linuxes tickless already or not yet?
- usr1106 4y agoAnswering to myself. From Ubuntu 20.04 generic kernel 5.13: CONFIG_NO_HZ_COMMON=y # CONFIG_HZ_PERIODIC is not set CONFIG_NO_HZ_IDLE=y # CONFIG_NO_HZ_FULL is not set CONFIG_NO_HZ=y # CONFIG_HZ_100 is not set CONFIG_HZ_250=y # CONFIG_HZ_300 is not set # CONFIG_HZ_1000 is not set CONFIG_HZ=250 Without looking up all details now I guess CONFIG_NO_HZ_IDLE=y means tickless when idle? That's of course not a very new kernel. Archlinux has 5.18, but don't have my machine accessible at this moment.
- metadat 4y ago5.13 is pretty recent...in fact any 5.x is super up to date, still tons of 3.x and 4.x kernels running out there in the wild, wild real world.
- dmd 4y ago3? Try 2. Linux my-host-name-here 2.6.18-128.el5 #1 SMP Wed Dec 17 11:41:38 EST 2008 x86_64 x86_64 x86_64 GNU/Linux Nearly all the (medical instrument) hardware where I work (a major hospital) is on 2.x, mostly running RHEL 5.
- usr1106 4y agoOne would assume that medical device vendors could pay for extended support. However, even extended support for HEL 5 has ended now. And even before that in heavily regulated industry that uses such old stuff I would assume rolling out updates is a very slow progress. Maybe not all that stuff connects to the internet, but I bet an ever increasing share does. So hospitals might have wose firewalls than others, so I would not surprised about such equipment being successfully attacked. Of course old versions are unaffected because by newly introduced vulnerabilities. But old ones that have existed "forever" get detected. I guess it's not the kernal alone that is so old, but user space, too.
- mjg59 4y agoThis 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.
- kochie 4y agoI'm surprised no one has mentioned Halt and Catch Fire - https://en.wikipedia.org/wiki/Halt_and_Catch_Fire_(computing) https://en.wikipedia.org/wiki/Halt_and_Catch_Fire_(computing...
- deleted 4y ago[deleted]
- dang 4y agoDiscussed at the time: What Does an Idle CPU Do? - https://news.ycombinator.com/item?id=8529658 https://news.ycombinator.com/item?id=8529658 - Oct 2014 (55 comments)
- mominMondol 4y ago
- kaetemi 4y ago> if the CPU is consistently busy for a normal user It's probably Windows Store going bonkers with some insane backlog of periodic tasks, because you didn't open your laptop for a few months, that it absolutely at all costs must process for some reason, especially during a power outage when you're running on battery and need to use your laptop for once.
- danuker 4y agoI agree. "Windows" and "emergency" don't go well together.
- no_time 4y agoYou can beat it into submission if you are okay with "purchasing" an enterprise license and set a bunch of GPOs and firewall rules. You can even go deeper and load unsigned drivers if you absolutely must. I wonder how much of this will be lost if MS starts enforcing policies via attestation on its services.
- Bayart 4y agoOn one hand I'd like to say I don't want to become a Windows admin to extract expected behaviour from a laptop, on the other that's exactly how I learned Linux administration.
- aaaaaaaaaaab 4y agoJust uploading telemetry ;)
- switch007 4y agoOh, you’ve not moved your mouse for 5 seconds? Let me just defrag, build the windows search index and generally turn your laptop in to a portable heater.
- ChuckMcM 4y agoBack when computers had front panels full of lights they would often make pretty patterns with the lights when they had nothing else to do. One of the fun things about building a recreation of the PDP-11/70 is to watch the idle display when RSX-11M+ isn't doing anything.
- segfaultbuserr 4y agoThe RT-11 operation system for the PDP-11 was filled with amusing quotations all over the kernel source code. The following is the source code of the CPU idle loop routine, with blinkenlights [0]. Note the funny quotations, and also note the use of self-modifying code - light direction reversal was implemented by overwriting the ROL/ROR instruction dynamically. ; "A source of innocent merriment!" ; - W.S. Gilbert, "Mikado" ; "Did nothing in particular, and did it very well" ; - W.S. Gilbert, "Iolanthe" ; "To be idle is the ultimate purpose of the busy" ; - Samuel Johnson, "The Idler" ; "I got plenty of nothin', and nothin's plenty fo' me!" ; - George and Ira Gershwin, "Porgy and Bess" ;- 30$: .IF NE LIGH$T DEC (PC)+ ;The RT-11 lights routine! LITECT: .WORD 1 BNE ..NULJ ;Not too often ADD #<512.>,LITECT ;Reset count, clear carry 40$: ROL 70$ ;Juggle the lights BNE 50$ ;Not clear yet COM 70$ ;Turn on lights, set Carry 50$: BCC 60$ ;Nothing fell off, keep moving ADD #<100>,40$ ;Reverse direction BIC #<200>,40$ ;ROL/ROR flip 60$: BIT #<LIGHT$>,CONFG2 ;Does CPU have a light register? BEQ ..NULJ ;No MOV (PC)+,@(PC)+ ;Put in lights (for 11/45) 70$: .WORD 0, SR .ENDC ;NE LIGH$T If the code doesn't detect a light register, it runs the following NOP loop instead. ..NULJ:: .IF EQ RTE$M ;;; WAIT ;Nothin' to do, so don't NOP ;Nothin' to do, so don't .IFF ;EQ RTE$M NOP ;Let the host do the waiting .ENDC ;EQ RTE$M NOP ;Second pad instruction .BR SCNALL ;Drop into ready job scan loop setup [0] http://www.kpxx.ru/DEC/PDP-11/Software/OS/RT-11/05.07/05.07.abl/Unpacked/RMON.MAC http://www.kpxx.ru/DEC/PDP-11/Software/OS/RT-11/05.07/05.07....
- JoeAltmaier 4y agoBack in the day (8086) my idle loop would start at timer (2 sec) and if it expired without exiting idle, it would flush file system buffers. So if you powered off without shutting down, the file system(s) would be consistent. Also every couple of seconds it would 'wash' an ECC memory page. So the machine wouldn't accumulate single-bit errors over weeks of uptime. The hardware didn't do that for you back then. After putting that stuff in, our incidence of corrupt file systems went way down!
- drudru 4y agoCan you tell us more about this system? ECC on an 8086 was rare.
- JoeAltmaier 4y agoCTOS running on a 'MegaFrame' which had 6 processors on cards the size of a pizza box. Slid them in a case the size of a dishwasher. Each had 1MB(!) of RAM, all mappable into each others' address spaces.
- JoeAltmaier 4y agoAlso a desktop modular workstation, and NGen (Next Generations, hah). It went through 8086,186, 286 and 386 versions.
- oldwisewuman 4y ago