Android Power Management Mechanism Analysis (zz)

Source: Internet
Author: User

Android Power Management Mechanism Analysis (zz)
In general, the power management of Android is relatively simple. It mainly uses locks and timers to switch the system status and minimize the power consumption of the system. The power management architecture of the entire system is as follows: (Note that this figure is from Steve Guo)

(Note that this figure is from Steve Guo) (see Figure 1)

Next we will discuss in detail at the Java application level, Android framework level, and Linux kernel level:
Application Layer usage:
Android provides the ready-made Android. OS. powermanager class, which is used to control the switching of the Power status of the device.
This class has three interface functions:
Void Gotosleep (long time); // force the device to enter the sleep status
Note:
If you try to call this function at the application layer, but it cannot be successful, it seems that the permission is not enough, but it is possible to call this function in the service under the framework.
Newwakelock (INT flags, string tag); // obtain the lock of the corresponding level
Flags parameter description:
Partial_wake_lock: screen off, keyboard light off
Screen_dim_wake_lock: screen dim, keyboard light off
Screen_bright_wake_lock: screen bright, keyboard light off
Full_wake_lock: screen bright, keyboard bright
Acquire_causes_wakeup: Forces screen and keyboard light to be turned on when a request is locked.
On_after_release: reset activity timer when the lock is released
Note:
If you have applied for partial wakelock, the system will not sleep even if you press the power key, such as when playing music.
If you have applied for another wakelocks, press the power key and the system will still enter sleep.
Void useractivity (long when, Boolean nochangelights); // when a user activity event occurs, the device will be switched to the full on status and reset screen off timer.
Sample Code:
Powermanager PM = (powermanager) getsystemservice (context. power_service );
Powermanager. wakelock WL = PM. newwakelock (powermanager. screen_dim_wake_lock, "my tag ");
WL. Acquire ();
.......
WL. Release ();
Note:
1. In applications that use the preceding functions, you must add the following permissions to the manifest. xml file:
<Uses-Permission Android: Name = "android. Permission. wake_lock"/>
<Uses-Permission Android: Name = "android. Permission. device_power"/>
2. all locks must be used in pairs. If the lock is applied and not released in time, the system will fail. if you have applied for a partial wakelock but have not released it in time, the system will never be able to enter the sleep mode.

Android framework:
The main code file is as follows:
Frameworks/base/CORE/Java/Android/OS/powermanager. Java
Frameworks/base/services/Java/COM/Android/Server/powermanagerservice. Java
Frameworks/base/CORE/Java/Android/OS/power. Java
Frameworks/base/CORE/JNI/android_ OS _power.cpp
Hardware/libhardware/power. c
Powermanagerservice. java is the core, power. java provides underlying function interfaces for interaction with the JNI layer. The JNI Layer Code is mainly in the file android_ OS _power.cpp. The interaction with Linux kernel is through power. c. The interaction between andriod and kernel is mainly implemented through the Sys File. For details, refer to the introduction of the kernel layer.
 
The functions of this layer are relatively complex, such as system status switching, backlight adjustment and switch, and wake lock application and release. However, this layer has nothing to do with the hardware platform, in addition, Google is responsible for maintenance and has fewer problems. If you are interested, you can view the relevant code on your own.

Kernel layer:
The main code is in the following locations:
Drivers/Android/power. c
Its interface functions for Kernel include:
Export_symbol (android_init_suspend_lock); // initialize the suspend lock before use.
Export_symbol (android_uninit_suspend_lock); // release resources related to suspend lock.
Export_symbol (android_lock_suspend); // apply for a lock. The corresponding unlock must be called to release it.
Export_symbol (android_lock_suspend_auto_expire); // apply for partial wakelock, which is automatically released after the scheduled time.
Export_symbol (android_unlock_suspend); // release lock
Export_symbol (android_power_wakeup); // wake up the system to the on
Export_symbol (android_register_early_suspend); // register the early suspend driver
Export_symbol (android_unregister_early_suspend); // cancel the registered early suspend driver.
 
The proc file provided to the android framework layer is as follows:
"/Sys/android_power/acquire_partial_wake_lock" // apply for partial wake lock
"/Sys/android_power/acquire_full_wake_lock" // apply for full wake lock
"/Sys/android_power/release_wake_lock" // release the corresponding wake lock
"/Sys/android_power/request_state" // The request changes the system status, including standby and wakeup.
"/Sys/android_power/state" // indicates the status of the current system
 
The power management of Android is mainly implemented through the wake lock. At the underlying layer, it is managed through the following three Queues:
Static list_head (g_inactive_locks );
Static list_head (g_active_partial_wake_locks );
Static list_head (g_active_full_wake_locks );
All initialized locks will be inserted into the g_inactive_locks queue, and the currently active partial wake lock will be inserted into the g_active_partial_wake_locks queue, the full wake lock of the activity is inserted into the g_active_full_wake_locks queue. All the partial wake locks and full wake locks will be moved to the inactive queue after expiration or unlock, waiting for the next call.
To use wake lock on the kernel layer, follow these steps:
1. Call the android_init_suspend_lock function to initialize a wake lock.
2. call the related functions android_lock_suspend or android_lock_suspend_auto_expire to request the lock. Here, you can only apply for the partial wake lock. If you want to apply for the full wake lock, you need to call the handler function (this function does not ), this name is a bit strange. Do not confuse it with android_lock_suspend_auto_expire.
3. If it is auto expire's wake lock, you can ignore it. Otherwise, you must release the relevant wake lock in time. Otherwise, the system will run at High Power Consumption for a long time.
4. Remember to call android_uninit_suspend_lock to release resources in time when the driver is uninstalled or no longer used.
 
System status:
User_awake, // full on status
User_notification, // early suspended driver but CPU keep on
User_sleep // CPU enter sleep mode
The status switching is as follows:

After the system is started normally, it enters the awake status. The backlight will gradually adjust from the brightest to the User-Defined brightness, system screen off timer (settings-> sound & Display-> display settings-> screen timeout) starts timing. If any activity event occurs before the timer time reaches, for example, touch click, keyboard pressed, and other events, the reset screen off timer will be kept in awake state. if an application has applied for full wake lock during this period, the system will remain in the awake status unless the user presses the power key. in awake state, if the battery power is low or the screen off timer time is reached and the keep screen on while pluged in option is selected, the backlight is forcibly adjusted to the dim state.

If the screen off timer time is reached and there is no full wake lock or the user presses the power key, the system status will be switched to notification and all registered g_early_suspend_handlers functions will be called, generally, the LCD and backlight drivers are registered as early suspend. If necessary, you can register other drivers as early suspend, which will be disabled in the first phase. next, the system will determine whether there is partial wake lock acquired. If yes, it will wait for it to be released. If a user activity event occurs during the waiting process, the system will return to the awake status immediately; if there is no partial wake lock acquired, the system will immediately call the pm_suspend function to disable other related drivers and bring the CPU to sleep.
If the system detects any wakeup source during sleep, the CPU will be awakened from sleep and the related driver's Resume function will be called, next, call the previously registered early suspend-driven Resume function, and return the system status to awake. the problem is that all functions that have registered early suspend can be called in the first stage of suspend. However, during resume, Linux will first call all the resume functions of the driver, what is the significance of calling the previously registered early suspend-driven Resume function? I personally think that the early suspend and late resume functions of Android should be used together with suspend and resume in Linux, rather than using a queue for management separately.
I have not studied Android for a long time. Maybe some of them are incorrect or even incorrect. Please forgive me. if you have any questions, you can discuss them together.

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.