[Android sleep] Android sleep mechanism

Susceptible to cold2022-06-23 18:05:54

One 、 Sleep Overview

Sleep , In short, it means that the equipment puts some parts when it doesn't need to work 、 Turn off the peripherals ( Power down or put it into low power mode ). Why sleep ? In a word : Save electricity . Dormancy is divided into active dormancy and passive dormancy . Active sleep : For example, I don't use my computer , Just set the system to sleep mode ; Passive dormancy : The system detects its own idle panic , In order to save money , I went to sleep .

Two 、Android Sleep

Hibernation is the core work of the kernel , and Android Is based on Linux Kernel , therefore Android Sleep is inextricably linked to the kernel ; because Android Special application scenario of : Mobile devices , therefore Android Hibernation and kernel have special requirements . 1、 contact : Android Equipment out of service , The system has nothing to do , The function of entering sleep state is finally realized by the kernel ; Each type of hardware has its own driver , The specific driver determines how to enter sleep and at what level of sleep . such as : about platform_device, Just follow platform_driver The rules of definition , stay suspend When called , Do the things mentioned above :

struct platform_driver {
int (*probe)(struct platform_device *);
int (*remove)(struct platform_device *);
void (*shutdown)(struct platform_device *);
int (*suspend)(struct platform_device *, pm_message_t state);
int (*resume)(struct platform_device *);
struct device_driver driver;
const struct platform_device_id *id_table;
};

2、Android Special needs of : For example, for your own computer , Don't let it sleep ; But for our inseparable mobile phones , Keep one eye open while sleeping : I'll let you know when I call ,QQ Ah, wechat, I'll let you know if there's any information , therefore Android stay Linux Kernel hibernation mechanism , Put forward “Opportunistic Suspend”.

3、 ... and 、 Sleep practice

Talking so much , Let's actually experience sleep . 1、 Sleep mode Dormancy is divided into several modes , Different modes of implementation 、 Different power consumption , The following is from Documentation/power/states.txt:

The kernel supports four power management states generically, though
one is generic and the other three are dependent on platform support
code to implement the low-level details for each state.
This file describes each state, what they are
commonly called, what ACPI state they map to, and what string to write
to /sys/power/state to enter that state
state: Freeze / Low-Power Idle
ACPI state: S0
String: "freeze"
This state is a generic, pure software, light-weight, low-power state.
It allows more energy to be saved relative to idle by freezing user
space and putting all I/O devices into low-power states (possibly
lower-power than available at run time), such that the processors can
spend more time in their idle states.
This state can be used for platforms without Standby/Suspend-to-RAM
support, or it can be used in addition to Suspend-to-RAM (memory sleep)
to provide reduced resume latency.
State: Standby / Power-On Suspend
ACPI State: S1
String: "standby"
This state offers minimal, though real, power savings, while providing
a very low-latency transition back to a working system. No operating
state is lost (the CPU retains power), so the system easily starts up
again where it left off.
We try to put devices in a low-power state equivalent to D1, which
also offers low power savings, but low resume latency. Not all devices
support D1, and those that don't are left on.
State: Suspend-to-RAM
ACPI State: S3
String: "mem"
This state offers significant power savings as everything in the
system is put into a low-power state, except for memory, which is
placed in self-refresh mode to retain its contents.
System and device state is saved and kept in memory. All devices are
suspended and put into D3. In many cases, all peripheral buses lose
power when entering STR, so devices must be able to handle the
transition back to the On state.
For at least ACPI, STR requires some minimal boot-strapping code to
resume the system from STR. This may be true on other platforms.
State: Suspend-to-disk
ACPI State: S4
String: "disk"
This state offers the greatest power savings, and can be used even in
the absence of low-level platform support for power management. This
state operates similarly to Suspend-to-RAM, but includes a final step
of writing memory contents to disk. On resume, this is read and memory
is restored to its pre-suspend state.

although kernel Support the above four sleep modes , But which is available depends on your hardware . So how do you know your own Android What about the sleep mode supported by the device ?

answer : adopt /sys/ file system . The sleep mode supported by query can cat file /sys/power/state:

cat /sys/power/state
freeze mem

If we go to /sys/power/state file echo The string of one of the above modes , The system will enter the corresponding sleep mode :

echo "mem" > /sys/power/state

If you've searched Android Sleep related content , In the old version Android(4.4 Before the release ) The meeting mentioned PowerManager Of setPowerState() Method , This method is to put the system into sleep through the above methods . But since the introduction of Autosleep after , Not anymore ,setPowerState() The method also disappeared .

2、/sys/power/ Files under directory

File info :

  • /sys/power/state: Used to control the system Power state . Read this file to get the sleep mode supported by the system , Writing this file is a sleep mode , The system enters the specified sleep mode . As an example above .
  • /sys/power/autosleep: from Android wakelocks Patches evolve centrally , Used in place of Android wakelocks Auto sleep function in . Write... To the file /sys/power/state Some kind of return value , The system will enter the specified sleep mode at the appropriate time ; Read the file and return the previously written value .
  • /sys/power/wake_lock、/sys/power/wake_unlock: That is, what we often call sleep lock , If the app holds a sleep lock , The system will not be able to enter sleep mode . stay Android wakelocks Time , Write wake_lock Get the lock , Write wake_unlock Release the lock ; stay AutoSleep Time , Specific see 【Android Sleep 】 And AutoSleep
  • wakeup_count: For resolution “system suspend and system wakeup events The problem of synchronization between ”.
  • /sys/power/pm_async: State change-over switch , allow / prohibit User The device is asynchronously controlled by space suspend and resume operation .
  • /sys/power/pm_freeze_timeout: The system should be frozen when performing hibernation (freeze) User control processes and kernel space allow frozen kernel threads , It takes time to perform these operations ? This file specifies the maximum time required .

Four 、 Other questions that need to be clarified

1、Android When the device screen goes dark , Not immediately into sleep mode ; When all wake-up sources are in de-avtive Post state , The system will go into sleep .

2、Android The equipment is connected adb In case of wiring to other equipment , The device will not enter sleep mode .

3、 When there is sleep operation, there is wake-up , You need to wake up the source . There are many kinds of wake-up sources , Register in the kernel , For example, common Power Key .

4、 A question that was once confused : How does the system know that it should enter sleep mode ? What is its judgment basis ?

stay wakelock Time , Detect the sleep lock during system sleep ; If no other part of the system holds a sleep lock , Just try to go into sleep mode , If there are no abnormal events, enter sleep mode . Android from 4.4 Start using autosleep Mechanism , As long as there is no active Wake up source (wakeup_source) 了 , Go into sleep mode .

5、 System Power Manager Overall process


thank
Similar articles