Sleep and wake-up for Android & instances

Source: Internet
Author: User
Tags linux sleep

Android sleep (suspend). In a kernel that has been installed with Android patches, The state_store () function will go to another path and enter request_suspend_state (). The file is located in earlysuspend. c. These functions are added to the Android system. earlysuspend and late resume will be introduced later. Involved files:
Linux_source/kernel/power/Main. c
Linux_source/kernel/power/earlysuspend. c
Linux_source/kernel/power/wakelock. c
(1) Features
Early suspend: Early suspend is a mechanism introduced by Android. This mechanism is highly controversial in the upstream, so we will not comment on it here. This mechanism is used when the display is disabled. Some display-related devices, such as LCD backlights, such as gravity sensors, touch screens, and these devices are all switched off. However, the system may still be running (wake lock is available at this time) for task processing, such as scanning files on the SD card. In embedded devices, backlight is a huge power consumption, so android will add such a mechanism.
Late resume: late resume is a mechanism supporting suspend and starts execution after kernel wake-up. It is mainly used to wake up the device that is sleeping during early suspend.

Wake lock: Wake lock plays a core role in the android power management system. The wake lock is a lock mechanism. As long as someone holds this lock, the system will not be able to sleep and can be obtained by user State programs and kernels. The lock can be time-out or not time-out. The lock will be automatically unlocked after the time expires. If no lock or timeout occurs, the kernel starts the sleep mechanism to sleep.

(2) Process
A, Android suspend, when the user writes mem or standby to/sys/power/State, state_store () will be called, and Android will call request_suspend_state () Here (), standard Linux will enter the enter_state () function here. If the request is sleep, the workqueue early_suspend will be called and enters the early_suspend state.
B, early suspend. In the early_suspend () function, first check whether the current request status is suspend, to prevent suspend requests from being canceled at this time (because the user process is still running at this time). to exit, simply quit. If not, this function will call a series of callbacks registered in early suspend once, synchronize the file system, and then discard main_wake_lock. The wake lock is a lock without timeout. If the lock is not released, the system will not be able to sleep.

C, late resume. After all the wake-up operations are completed, the user processes have started to run. Wake-up is usually due to the following reasons:
1) Incoming call: if it is an incoming call, the modem sends a command to rild to notify windowmanager of incoming call response, in this way, the device that remotely calls powermanagerservice to write "on" to/sys/power/State to execute late resume, such as lighting the screen.
2) User buttons: User button events are sent to windowmanager, which processes these button events. Buttons can be divided into several situations. If the case is not a wake-up key (the system can be awakened), windowmanager will take the initiative to discard wakelock to enable the system to sleep again; if the key is a wake-up key, windowmanger then calls the interface in powermanagerservice to execute late resume. Late resume will wake up the device that previously called early suspend.
(3) Terms
Wake lock: Let's take a look at how the wake lock mechanism runs and works. Focus on the wakelock. c file. A wake lock has two statuses: locking and unlocking. One is a permanent lock. Such a lock will not be unlocked unless it is displayed, therefore, the use of such locks is very careful; the second is the timeout lock, which will lock the system to wake up for a period of time. If the time passes, the lock will be automatically lifted. There are two types of locks:
1) The Locks such as wake_lock_suspend prevent the system from sleep.
2) The Locks such as wake_lock_idle do not affect system sleep.
In the wake lock, there will be three places for the system to start suspend () directly, namely: In the wake_unlock (), if no other wake lock is found after unlocking, then start sleep. After the timer has reached the time, the timer callback function will check whether there are other wake locks. If not, let the system go to sleep. In wake_lock (), after a wake lock is locked, the system checks whether there is a lock again.
Suspend: After wake_lock runs suspend. c's suspend () function will be called. This function first synchronizes the file system and then calls pm_suspend (request_suspend_state). Then pm_suspend () will call enter_state () to enter the Linux sleep process.

(4) differences between Android and standard Linux sleep
Although pm_suspend () will call enter_state () to enter the standard Linux sleep process, there are some differences: When the frozen process is started, Android will first check whether there is a wake lock. If not, these processes will be frozen, because some people may apply for a wake lock during the start of the suspend and the freezing process. If so, the freezing process will be interrupted.
(5) Wake lock instance

When debugging a mobile phone, we found a bug: when the phone is close to the ear during a call, the distance sensor (PS) will make the LCD sleep off the screen, however, after the phone is removed, the LCD will not turn on again. It is suspected that the PS Chip did not wake up from sleep. Add a wake-up lock, so that during the call, the PS can never enter the sleep state and the external status changes normally. Note: even if the phone is close to the ears and the screen is off, the PS is not disable. During the entire call, the PS is enable, and the phone is truly disable only when it is sleeping in standby mode.

In this case, you only need to initialize a wake_lock when initializing ps. When the mobile phone is working normally (enable PS chip), The waken lock is obtained. When the mobile phone is sleeping (disable PS chip), The waken lock is released.

 

Http://edu.codepub.com/2010/0626/23815_2.php ()

Http://elinux.org/Android_Power_Management ()

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.