3

I would like to get my Core i7 CPU to enter sleep state just momentarily, for one millisecond or so from a batch file or executable.

I know sleep can be induced with SetSuspendState, but I'm looking for a solution that does not put the entire system to sleep, but just the CPU momentarily.

CPU is Core i7 3632QM, and OS is Windows 7 and 10. Thanks

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • Do you want to pause your script for a while? An easy solution is to use the ping command : `PING 1.1.1.1 -n 1 -w 60000 >NUL` will delay execution for 60 seconds and will "suspend" the current executing thread. You can't actually get the whole CPU to just "sleep" from user space. – Hatted Rooster Jan 02 '16 at 10:32
  • 1
    No, I actually need the CPU to enter sleep state, because there's a lock on the HM70 chipset that it shuts down every 30 minutes if used with the core series. This is an artificial limitation from Intel. They sent me the wrong motherboard from China, so I'm looking to program a workaround. I was thinking this might be possible in some low level language like C, or any idea what it would require ? Thanks –  Jan 02 '16 at 10:39
  • Only thing I know of is the `PAUSE` instruction (`_mm_pause` intrinsic). The CPU sleep states would otherwise be handled by OS kernel with higher abstractions (processes, threads) provided to programs. It might also help if you explained what OS are you using and what sleep state you'd like to enter. – Zdeněk Jelínek Jan 02 '16 at 11:26
  • I'm not sure which CPU state I need to enter, but I know that making it sleep with SetSuspendState does the job (hibernation must be off otherwise this will hibernate instead of sleep). My OS is Windows 7 and Windows 10. The CPU is the i7 3632QM. Thanks a lot –  Jan 02 '16 at 11:56
  • 1
    So you're calling `SetSuspendState(false, x, false)` from `PowrProf.h`? I would expect this to set the CPU to S3 sleep state (S4 is hibernate) - see https://software.intel.com/sites/manageability/AMT_Implementation_and_Reference_Guide/default.htm?turl=WordDocuments%2Fsleepstates.htm. And I wouldn't expect this to be doable without the OS clutter since resetting the CPU/chipset context might as well destabilize the system. – Zdeněk Jelínek Jan 02 '16 at 12:09
  • Thanks, then my question is, is there any way to induce S1 or S2 programatically? Those might work also, and as I can see in S1 the context isn't lost. –  Jan 02 '16 at 12:29
  • 1
    @Florian Your other question was closed then deleted because you re-posted this question again. If you think that it hasn't gotten enough attention, post a bounty. Don't just repost it. – fuz Jul 23 '16 at 07:59
  • 2
    Putting a system into a deep S state has only partially to do with the CPU. Putting the "CPU to sleep" may not solve your problem, also such phrase is meaning less. The CPU has a complex power management that you cannot control from user space (expect maybe with `mwait`) and that requires coordination with the OS and the entire system. You should investigate the lock further (is it HW only? It involves SMM?), then design a workaround and write the *driver* necessary to implement it. Chapter 14 of the Intel Manuals enums all the PM capabilities. Simply return the MB may be an easier solution. – Margaret Bloom Jul 23 '16 at 14:07
  • 1
    Like Margaret Bloom suggests there's no way to just put the CPU to sleep in same way that it does when your whole system goes to sleep. Also there's no reason to believe that just putting the CPU to sleep would solve your problem, it could easily be the chipset or the BIOS (through SMM) that's locking out your CPU. Regardless you'll need to go through entire process of entering the S3 sleep state, you can't pick and choose what goes to sleep. Unfortunately this is fundamentally slow, the OS and drivers need to save device state to RAM. – Ross Ridge Jul 23 '16 at 16:57
  • Margaret, Ross et. al., Thanks very much, I see this is much more complex than I imagined. –  Jul 23 '16 at 23:59
  • re: your bounty: Maybe a better idea is to look into the options for programmatically waking from sleep. This is possible. e.g. on Linux, use [`rtcwake`](http://askubuntu.com/questions/61708/automatically-sleep-and-wake-up-at-specific-times). Try a google search for `wake up laptop at a certain time` or something to find Windowsy ways of doing the same task. I didn't look at any of the hits, but they look promising. – Peter Cordes Jul 29 '16 at 11:38

1 Answers1

7

Based on your comment about defeating some kind of shutdown every 30 mins, it sounds like you need the whole CPU (all cores) to sleep. We need much more detail on that to do more than guess about which sleep states will serve your purpose and which won't.

Based on comments, it's likely that ACPI S3 sleep will be needed. Ross's comment about the hardware supporting an S1 sleep didn't mention an S2 (CPU actually powered down), so it's probably not even possible to power down just the CPU.

So your best bet is to look into programmatically doing a sleep/wake cycle, which is possible on at least some hardware. On Linux, the rtcwake command has an option to do that. I assume it programs a wakeup time into the BIOS's NVRAM before initiating a sleep. (I think there are only a few commonly-used formats/locations for storing this, so there's a good chance it's possible on your computer.)

Try a google search for wake up laptop at a certain time or something to find Windows equivalent of rtcwake. I didn't look at any of the hits, but they look promising.


I'm not an expert at this system power-state management stuff, but you probably need the system to enter an ACPI sleep state. S3 is the usual "suspend to RAM"; OSes that support suspend usually use this as their non-hibernate option.

For your use, maybe S1 or S2 will do (and anything less than this, like CPU power-saving C-states probably won't be sufficient, especially not states that are just per-core).

ACPI global sleep states (from Wikipedia). Systems are not required to implement all levels.

  • S1, Power on Suspend (POS): Processor caches are flushed, and the CPU(s) stops executing instructions. The power to the CPU(s) and RAM is maintained. Devices that do not indicate they must remain on may be powered off.
  • S2: CPU powered off. Dirty cache is flushed to RAM.
  • S3, commonly referred to as Standby, Sleep, or Suspend to RAM (STR): RAM remains powered. (But hard drives and everything else powers down)
  • S4, hibernate

I'm not going to try to write Windows API function calls to do this. I wouldn't be surprised if there's an program for requesting Windows to enter S1 or S2 state (ideally with some kind of triggered wakeup).

@RossRidge says that the HM70 chipset does implement S1 sleep (and implies that it doesn't support an S2 sleep.) Since S1 doesn't power down the CPU, it may not reset the timer. Even a hypothetical S2 sleep might not do the trick, because the timer may be external to the CPU and/or managed by the BIOS.

Software exists to program the BIOS to wake at a certain time. That's one possible way to trigger coming out of suspend. So it might be possible to write a script that programs a wakeup time for 2 seconds in the future, then initiates a sleep.


@MargaretBloom comments that Chapter 14 of the Intel Manuals enumerates all the power-management capabilities. (See the tag wiki for links). Also that a totally different workaround may be possible, by using SMM.


re: your your followup question which was downvoted into oblivion:

enter sleep state just momentarily, for one millisecond

1ms is about 3 million core clock cycles. That's not momentary for a computer, especially from an asm programming perspective.

You definitely don't want to write assembly by hand to enter these states. Instead, use your OS's existing ACPI interface. This is a big part of the reason that everyone downvoted the crap out of your followup question.

Other than short per-core sleeps from mwait, pause, and hlt insns, the OS needs to know what's going on. For more about pause, see this. There aren't specific instructions to enter deeper sleeps anyway; you program ACPI by writing to device registers in MMIO space.


When all cores are HLTed at the same time, the whole CPU can opportunistically power down more stuff until the next timer or other interrupt wakes it up again (this is or at least is related to ACPI C-states, as I understand it). But this happens all the time during normal operation, because modern OSes run HLT on cores that are idle. The only interesting thing you could do here is get the CPU to sleep like this occasionally even if the system was running some CPU-intensive processes. (e.g. some threads with non-idle priority that run hlt in a loop). Since HLT is a privileged instruction, this would require a kernel thread or a syscall. You probably can't actually raise the priority of the system idle process so it steals time from other runable processes.

This may be an oversimplification: I haven't looked at kernel idle tasks recently to see if they still just run HLT when they want the current core to sleep until the next interrupt. For a while (when CPU power management was in its infancy) idle loops used to run some other stuff to enter a low-power C-state. But HLT may do that now.

Community
  • 1
  • 1
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • Wow thanks, that's a great explanation. I just checked with both "powercfg -a" and "powercfg energy" and it turns out my BIOS does not support either S1 or S2, so it looks like that options is out. Do you think it would be possible to emulate whatever is happening to the CPU when I make it go to S3 sleep by calling SetSuspendState, but keep it limited to the CPU only? I'm thinking like a program written in C? Or am I in over my head here? My problem is I can't use my expensive laptop because chinese sent me the wrong motherboard after my psycho girlfriend broke my brand new laptop... –  Jul 23 '16 at 16:25
  • It works perfectly with this motherboard, but Intel has the CPU lock on this HM70 chipset, so with my CPU it shuts down hard every 30 mins. Putting it to S3 resets the clock. I really appreciate your help. –  Jul 23 '16 at 16:26
  • 2
    @Florian: You should update your question with that info on what kind of sleep is required. As is, it's a pretty bad question; I only upvoted because you clarified in comments what the motivation was, but I was lazy and didn't edit it in for you. – Peter Cordes Jul 23 '16 at 16:35
  • @Florian: Unless you write a kernel module / driver or something, it doesn't matter what language you write a program in. Many different languages can make Win32 API calls. C is a good choice for systems programming, though. – Peter Cordes Jul 23 '16 at 16:35
  • 1
    @Florian: IDK if it's common for the hardware to support anything like S1 or S2, even if the BIOS doesn't. It's probably *really* hard to do anything other than enter sleep states through standard ACPI means. Doing it yourself would mean re-implementing all the stuff that the ACPI code does on the way to sleeping, with a couple changes. This is tremendously more complicated than just asking the OS to enter S3. – Peter Cordes Jul 23 '16 at 16:39
  • 1
    @PeterCordes The HM70 chipset supports S1, but this doesn't power down the processor, and it appears to be an obsolete chipset driven way of putting the processor into a package-wide Cx state. – Ross Ridge Jul 23 '16 at 17:00