Introduction to Android Power Management

1. Basic knowledge of power management

1.1 Several states of power management

In the Android kernel source code, three power states are defined in kernel/power/suspend.c:

b6ead2e9386cef124976ce6d584a574a.png

Corresponding macro definition /include/linux/suspend.h

ef1b7e5f304cc61f8b1e0b2cfddb31f6.png

1.2 Introduction of power management status:

PM_SUSPEND_ON

The device is in normal working condition

PM_SUSPEND_STANDBY

The device is in a power-saving state, but can still receive certain events

PM_SUSPEND_MEM

The device enters the sleep state, saves the running context of the system to the memory and then suspends the system. Only a specific external interrupt can wake up the device

PM_SUSPEND_TO_IDLE

The device enters the idle state, freezes user space and transfers peripherals to low power consumption mode, forcing the CPU to enter idle

ADB view supported power modes

cat /sys/power/state

1.3 Idle State

Android's Idle state is divided into two categories: CPU Idle and Device Idle

CPU Idle

Each CPU core will have an idle process, and the idle process will enter the idle process when the system does not schedule CPU resources, and the role of the idle process is not to use the CPU, so as to achieve the purpose of power saving.

For information about CPU idle, you can check the article " Introduction to Linux Cpuidle " before the official account

Device Idle

Device Idle belongs to the concept in the android Doze mode, which means that the screen of the mobile phone is off, not charging, and standing still

In Doze mode, according to Google's official statement, Wakelocks, network access, jobshedule, alarm clock, GPS/WiFi scanning will all stop.

5f1903544218b60010e2cc54c66bc598.png

Periodically, the system quits Doze for a short period of time to allow the application to complete its delayed activity. During this maintenance window, the system runs all pending syncs, jobs, and alerts, and allows applications to access the network.

f703a4408f59018027c497afed848f77.png

2. Android power management framework

The power management framework of the Android system is divided into five parts: application layer, framework layer, Native layer, HAL layer and kernel layer. The power management architecture diagram is as follows:

503d455a8bf95b653e34932c7a74a832.png

Application interface layer: PowerManager.java is responsible for providing a series of interfaces to applications, such as the application and release of wakelock, and then allowing the system to sleep or wake up

Framework layer: PowerManagerService.java PowerManagerServic is the core service of android power management, providing application program interface upwards, and controlling the standby state and system hardware device state downward through the hal layer and kernel layer

HAL layer: power.c receives upper layer parameters and communicates with the kernel layer by writing nodes

Kernel layer: Kernel/Power implements the system power management framework mechanism and provides a basic framework for device power management

Three, WakeLock

Wakelock in Android is a lock mechanism used to prevent the system from entering the sleep state. As long as any application holds the wakelock, the system cannot enter the sleep state.

newWakeLock(int flags, String tag)

There is a key parameter flags when applying for wakelock, which has the following situations:

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

The above four flags are mutually exclusive, that is, only one of them can be specified, but they can not be mutually exclusive with the following two flags:

ACQUIRE_CAUSES_WAKEUP: Once there is a request lock, the Screen and keyboard light are forced to open

ON_AFTER_RELEASE: reset activity timer when the lock is released

If the system has applied for PARTIAL_WAKE_LOCK, then even if you press the power button, the system will not enter sleep, such as when playing music. If you apply for other wakelocks, press the power button, the system will still enter sleep

wakelock has two states: locked and unlocked:

One is a permanent lock, which will not be unlocked unless it is subsequently released;

The other is a timeout lock, which will automatically unlock the system after a period of time.

Two types of power locks:

(1) WAKE_LOCK_SUSPEND: Prevent the system from going to sleep, which is a permanent lock, and the timeout lock is WAKE_LOCK_AUTO_EXPIRE

(2) WAKE_LOCK_IDLE: Prevent the system holding the lock from entering the idle state

Android uses two linked lists to save the suspend lock and idle lock in the active state and the wakelock in the inactive state respectively.

The system implements two mechanisms for adding and releasing locks. The first is non-counting locks, and the other is counting locks. It can be specified by PowerManager.WakeLock.setReferenceCounted(boolean value), and the default is the counting mechanism. The difference between these two mechanisms is that no matter how many times acquire() is used for the former, it can be directly unlocked with one release(). The latter is actually unlocked when (--count == 0), and when (count == 0), it will apply for locking. In other cases, the isHeld status will not change. Therefore, the counting mechanism of wakeLock does not really apply/release each lock for each request, it only executes the operation after counting the number of times the same lock is applied/released.

3.1wakelock is in the framework layer

After the kernel is started, the power management system will create two nodes in the file system:

/sys/power/wake_lock

/sys/power/wake_unlock

The application can apply for a WAKE_LOCK_SUSPEND type lock through /sys/power/wake_lock, and release a lock through /sys/power/wake_unlock. If the kernel detects that a lock has not been released before entering suspend, it will give up the suspend process until the lock is released. After Android holds the power lock, the lock-holding process can continue to execute, even if it enters sleep mode.

If the app crashes or exits, the system will automatically release all power locks they acquired; if acquired in a service, it will also be automatically released when the service crashes or logs out;

The framework layer related to the power lock is implemented through the PowerManagerService class. This class is used to manage the wakelocks applied by all applications. For example, the wakelocks applied for by audio and video players, cameras, etc. are managed through this class. like:

static final String PARTIAL_NAME = "PowerManagerService"

PARTIAL_NAME is passed as a parameter to the bottom layer.

ADB debug commands

echo lockname > /sys/power/wake_lock

lock "lockname"

echo lockname > /sys/power/wake_unlock

unlock "lockname"

4. Earlysuspend and Lateresume

Early Suspend and Late Resume are a feature added to Android on the basis of standard Linux. When the user space applies to enter the suspend, it will first enter the early suspend state. The peripheral driver can register the early suspend callback function. When entering the early suspend, the kernel will call these callback functions one by one. For example, after entering early suspend, the callback function will turn off the screen and backlight through the screen driver, but the system is still running normally. After entering the early suspend state, once all wakelocks are released, the system will immediately enter the real suspend process.

Starting from Android 4.4, that is, the version that introduced ART, the early suspend mechanism was abandoned, and the fb event notification mechanism was used instead. The subsequent Android versions only have suspend, resume, runtime suspend, and runtime resume.

epilogue

This article describes the main content of the Android power management module, aiming to let readers have a preliminary understanding of the Android power state and wakelock, so as to facilitate the in-depth introduction of the implementation of the kernel wakelock and the Android standby wake-up process.

Citation:

[1]https://developer.android.google.cn/training/monitoring-device-state/doze-standby?hl=en

[2] In-depth Understanding of the LINUX Kernel (Third Edition) (US) Bowei, Xister, Chen Lijun, Feng Rui, Niu Xinyuan Translated

3b34c81fe51d912cb1cac0ff44bfbb5c.gif

Long press to follow Kernel Craftsman WeChat

Linux Kernel Black Technology | Technical Articles | Featured Tutorials

Guess you like

Origin blog.csdn.net/feelabclihu/article/details/128423838