Android Power Management-Hibernation brief analysis

Source: Internet
Author: User
Tags lock queue

First, the outset

1.Linux Description of power state

-On (s0-working)

-Standby (Standby) S1-cpu and RAM is powered but not executed

-Suspend to RAM (MEM) S3-ram is powered and the running content are saved to ram

-Suspend to Disk,hibernation (Disk) s4-all content was saved to disk and power down

S3 aka STR (suspend to RAM), hangs to memory, abbreviated to standby. The computer stores the current state of operation and other data in memory, shuts down the hard disk, peripherals and other devices and enters the waiting state. At this point, the memory still needs power to maintain its data, but the machine consumes little. When recovering, the computer reads the data from memory and returns to its pre-suspended state for faster recovery. Optimizing the power consumption of DDR is the key to S3 performance, most handheld devices are S3 standby.

S4 aka STD (suspend to disk), hangs to hard disk, referred to as hibernation. Store data such as running status on a file on the hard disk or a specific area, turn off the hard disk, peripherals and other devices, into the shutdown state. At this point the computer shuts down completely and does not consume power. When recovering, the computer reads data from the hibernation file/partition, returning to the state before hibernation and recovering slowly. e-book project, saw a Sony ebook, did not define the shutdown state, only defined the S4, thereby increasing the boot speed.

Excerpt from: http://cache.baiducontent.com/c?m=9f65cb4a8c8507ed4fece763104

6893b4c4380147d8c8c4668d4e419ce3b4c413037bfa6663f405a8e906b6075fc

4d5bedfb6079370123b598938f4a85ac925f75ce786a6459db0144dc5bf0dc475

5d627e44de8df4aa0fcad7384afa28d880311dd52756d87849c5b704f9634b6&p

=c933cc16d9c116f51ebd9b7d0a13cd&newp=8366c54ad5c444e411b3c22d0214cf2

31610db2151d6db10349dcd1e&user=baidu&fm=sc&query=pm_autosleep_init&qid=&p1=1

Before reading the following, it is strongly recommended to read below.

Android has added three to the original sleep wake mechanism on the Linux kernel, as follows:

Wake Lock Wake-up lock mechanism;
early Suspend pre-suspend mechanism;
late Resume late awakening mechanism;

Let's look at a frame diagram of the Android sleep wake mechanism:

Android-specific earlysuspend:request_suspend_state (state)
Linux Standard suspend:enter_state (state)

Second, the relevant code related to the document

Frameworks

Interface for upper-level application invocation
Frameworks/base/core/java/android/os/powermanager.java

Implements the interface in the PowerManager class specifically
Frameworks/base/services/java/com/android/server/powermanagerservice.java

Called by the Powermanagerservice class

Frameworks/base/core/java/android/os/power.java

JNI

Implementing the Jni interface in the Power class
Frameworks/base/core/jni/android_os_power.cpp

Hal

Perform the SYSFS user interface operation
Hardware/libhardware_legacy/power/power.c

kernel

Kernel/kernel/power/main.c
Kernel/kernel/power/earlysuspend.c
Kernel/kernel/power/suspend.c
Kernel/kernel/power/wakelock.c
Kernel/kernel/power/userwakelock.c

In the application framework layer, the PowerManager class is an interface class for upper-level applications that provides the basic interface for the Wake Lock mechanism (and also the Sleep Wake Subsystem) (Wake-up lock acquisition and release). The upper-level application implements monitoring of the system power state by invoking these interfaces.

The PowerManager class communicates with the Powermanagerservice class by ibinder This unique communication pattern in Android.

Powermanagerservice is a concrete implementation of the interfaces defined in the PowerManager class, and further calls to the power class to communicate with the next layer. The Powermanagerservice class is the wakelock mechanism at the core of the application framework layer, where they perform preliminary analysis and corresponding settings for the parameters passed when the application calls the PowerManager class interface, and manage a wake-up lock queue, Then, with the state information of other modules (such as watchdog, Batteryservice, Shutdownthread, etc.), make the decision, call the corresponding interface of the Power class, and finally through the JNI interface, call to the function in the hardware abstraction layer, to SYSFS User interface, which triggers the implementation of the kernel-state function.

Three, kernel user spatial interface analysis

1. Sysfs's Properties file

The interface provided by the power management kernel layer to the application layer is the Sysfs file system, and all related interfaces are implemented through SYSFS. Android Upper Frameworks is also based on SYSFS, which is ultimately provided to Android Java applications in the form of Java classes.
The Android system will be created in SYSFS with entry:
/sys/power/state
/sys/power/wake_lock
/sys/power/wake_unlock

echo mem >/sys/power/state or echo Standby >/sys/power/state: Command System into Earlysuspend state, those registered early suspend The driver of the handler will enter the respective Earlysuspend state in turn.

echo on >/sys/power/state: Will exit early suspend status

echo Disk >/sys/power/state: Command system enters hibernation state

echo lockname >/sys/power/wake_lock: Lock "Lockname"
echo lockname >/sys/power/wake_unlock: Unlocking "lockname"
The above is a separate lock and unlock the command, once the system all Wakelock is unlocked, the system will enter the suspend state, the Linux central Plains to make the system suspend operation (echo mem >/sys/power/state etc.) is replaced in Android to bring the system into early suspend, while the wake lock mechanism becomes the only way for the user to enter the suspend state of the command system.

The kernel and HAL interfaces are implemented by/sys/power the following system files, such as:/sys/power/state. In the user-space interface, a set of SYSFS properties files is defined, one of which is:

1 power_attr (state)

The code for this macro is as follows:

1 #define POWER_ATTR (_name) 2 static struct Kobj_attribute _name# #_attr = {    3     . attr    = {                4         . Name = __st Ringify (_name),    5         . Mode = 0644,            6     },                    7     . Show    = _name# #_show,            8     . Store    = _name# #_store,        

The expanded code is:

1 #define POWER_ATTR (state) 2 static struct Kobj_attribute state_attr = {    3     . attr    = {                4         . Name = ' state ' ",    5         . Mode = 0644,            6     },                    7     . Show    = State_show,            8     . Store    = State_store ,        

2. Create a Sysfs file

1 static int __init pm_init (void) 2 {3     int error = Pm_start_workqueue () 4     if (Error) 5         return error; 6     h Ibernate_image_size_init (); 7     Hibernate_reserved_size_init (); 8     power_kobj = Kobject_create_and_add ("Power", NULL); 9     if (!power_  kobj)         return-enomem;11     error = Sysfs_create_group (Power_kobj, &attr_group); Create SYS file Interface     if (error)         return error;14     pm_print_times_init ();     return Pm_autosleep_init ()  ; Create Auto_sleep task queue, and also write user-state to Autosleep as Wakeup_source16}17 core_initcall (pm_init)//Call Pm_init

After the Pm_init function executes, the/sys/power directory is created, and a series of property files, one of which are/sys/power/state files, are established under the directory. The user space to write the file will cause State_store to be called, and reading the file will cause the State_show function to be called.

2. The standard Linux kernel calls the suspend process

1 static ssize_t state_store (struct kobject *kobj, struct Kobj_attribute *attr, 2                const char *BUF, size_t N) 3 {4
   
    suspend_state_t State; 5     int error; 6  7     error = Pm_autosleep_lock (); 8     if (Error) 9         return error;10     if (pm_autosleep _state () > pm_suspend_on) {///Autosleep is the Android kernel introduced in order to be compatible with the mainline kernel the         error =-ebusy;13         goto out;14}15 16<     C11/>state = Decode_state (buf, n);     if (State < Pm_suspend_max)         error = Pm_suspend (state);              Enter SUSPEND mode,     else if (state = = Pm_suspend_max),         error = Hibernate (); Enter hibernation mode     else22         error =-einval;23  out:25     pm_autosleep_unlock ();     error:n;
   

There are many state states when the underlying accepts a number of columns to the value passed to the upper layer:

1 #define PM_SUSPEND_ON        ((__force suspend_state_t) 0)//S02 #define Pm_suspend_standby    ((__force suspend_state _t) 1)//S13 #define PM_SUSPEND_MEM        ((__force suspend_state_t) 3)//S24 #define PM_SUSPEND_MAX        ((__force Suspe nd_state_t) 4)   //S3

In State_store, if Config_earlysuspend is defined, execute request_suspend_state (state) to enter Earlysuspend first and then decide whether to enter Suspend, otherwise directly executes enter_state (state) to enter suspend. Let's take a look at the native code of Pm_suspend:

1 int pm_suspend (suspend_state_t state) 2 {3     int error, 4  5     if (State <= pm_suspend_on | | State >= pm_s Uspend_max)//The state parameter is invalid 6         return-einval; 7  8     pm_suspend_marker ("entry"); 9     error = Enter_state (state )     if (error) {One         suspend_stats.fail++;12         dpm_save_failed_errno (Error);         suspend_stats.success++;15     }16     pm_suspend_marker ("exit");     

Third, Android hibernation (suspend)

1. Related Documents
KERNEL/KERNEL/POWER/MAIN.C
KERNEL/KERNEL/POWER/EARLYSUSPEND.C
KERNEL/KERNEL/POWER/WAKELOCK.C

2. Introduction to Features
1) Early Suspend
Early suspend is a mechanism introduced by Android, which is controversial in the upstream and does not comment here. This mechanism is to turn off the display, some and display related devices, such as LCD backlight, gravity sensor, touch screen will be turned off, but the system may still be in the running state (this time there is wake Lock) for task processing, such as scanning the SD card files. In embedded devices, the backlight is a very large power consumption, so Android will join such a mechanism.


2) Late Resume
Late Resume is a mechanism that is associated with suspend and is executed when the kernel wakes up. The main thing is to wake up the device that sleeps when early suspend.


3) Wake Lock
Wake_lock plays a central role in the Android power management system. Wake_lock is a mechanism of locking, as long as someone holding this lock, the system will not be able to enter hibernation, can be obtained by the user program and the kernel. This lock can be timed out or not timed out, and the timeout lock will be automatically unlocked after the timeout. If there is no lock or timeout, the kernel will start the dormant mechanism to enter hibernation.

3. Android Suspend
The main.c file is the gateway to the entire frame. The user can implement the control system into a low power state by reading and writing the SYS file/sys/power/state. The user's read and write to/sys/power/state is called to State_store () in Main.c, and the user can write to the string defined in the const char * const pm_states[], such as "on", "Mem", "standby "," Disk ".

1 const char *const Pm_states[pm_suspend_max] = {2     [pm_suspend_freeze]    = "FREEZE", 3     [Pm_suspend_standby]    = "Standby", 4     [Pm_suspend_mem]    = "Mem", 5}

State_store () First determines whether the user writes a "disk" string, and if so, calls the Hibernate () function to command the system to enter the hibernation state.  If it is a different string, call Request_suspend_state () (If you define Config_earlysuspend) or call Enter_state () (if config_earlysuspend is not defined). The Request_suspend_state () function is where Android is relative to standard Linux changes, and it is implemented in EARLYSUSPEND.C. In the standard Linux kernel, when users write "mem" and "standby" through SYSFS, they call Enter_state () directly into suspend mode, but in Android they call Request_suspend_state () The function enters the early suspend state. The Request_suspend_state () function code is as follows:

View Code

TAG:

1 const char * Const old_paths[] = {2     "/sys/android_power/acquire_partial_wake_lock", 3     "/sys/android_power/ Release_wake_lock ", 4};5 6 const char * Const new_paths[] = {7     "/sys/power/wake_lock ", 8     "/sys/power/wake_unlock ", 9};
1 static inline void 2 Initialize_fds (void) 3 {4     //Xxx:should be this:5     //pthread_once (&g_initialized, Ope N_file_descriptors); 6     //Xxx:not this:7     if (g_initialized = = 0) {8         if (open_file_descriptors (new_paths) < 0) 9             open_f Ile_descriptors (old_paths);         g_initialized = 1;11     }

To be Continued ...

Many of the contents of this article are referenced and excerpted from:

http://blog.csdn.net/myarrow/article/details/8136691

http://blog.csdn.net/myarrow/article/details/8137952

http://blog.csdn.net/myarrow/article/details/8137566

http://blog.csdn.net/sunweizhong1024/article/details/17102047

Android Power Management-Hibernation brief analysis

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.