Android Power Management: Power States, WakeLock, and Suspend/Resume Mechanisms
The article explains Android’s power management architecture, detailing kernel power states, CPU and device idle modes, the multi‑layer framework, WakeLock types and flags, and the early‑suspend/late‑resume mechanisms, while providing commands and examples for monitoring and controlling device sleep behavior.
This article provides a comprehensive overview of Android power management module, covering fundamental concepts and implementation details.
1. Power Management Basic Knowledge
1.1 Power Management States
The Android kernel defines three power states in kernel/power/suspend.c. The macro definitions are located in include/linux/ suspend.h.
1.2 Power Management State Descriptions
PM_SUSPEND_ON : Device is in normal working state.
PM_SUSPEND_STANDBY : Device is in power-saving mode but can still receive certain events.
PM_SUSPEND_MEM : Device enters sleep state, saves system running context to memory and suspends the system. Only specific external interrupts can wake up the device.
PM_SUSPEND_TO_IDLE : Device enters idle state, freezes user space and sets peripheral devices to low-power mode, forcing CPU into idle.
ADB command to 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 has an idle process. When the system has no CPU resources scheduled, it enters the idle process, whose purpose is to not use CPU to achieve power saving.
Device Idle : Device Idle is part of Android's Doze mode concept, referring to when the phone screen is off, not charging, and stationary.
In Doze mode, according to Google's official documentation, Wakelocks, network access, job schedule, alarms, and GPS/WiFi scans all stop. The system periodically exits Doze for a short time to allow applications to complete their deferred activities. During this maintenance window, the system runs all pending syncs, jobs, and alarms, and allows applications to access the network.
2. Android Power Management Framework
Android's power management framework is divided into five parts: Application Layer, Framework Layer, Native Layer, HAL Layer, and Kernel Layer.
Application Layer : PowerManager.java provides a series of interfaces to applications, such as acquiring and releasing wakelocks, to allow the system to sleep or wake.
Framework Layer : PowerManagerService.java is the core service of Android power management. It provides application interfaces upward and controls 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 the basic framework for device power management.
3. WakeLock
In Android, wakelock is a locking mechanism used to prevent the system from entering sleep state. As long as any application holds a wakelock, the system cannot enter sleep state.
When applying for wakelock with newWakeLock(int flags, String tag) , there is a key parameter flags with the following options:
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 are mutually exclusive, but they can be combined with the following two flags:
ACQUIRE_CAUSES_WAKEUP : Once a lock is requested, force open Screen and keyboard light
ON_AFTER_RELEASE : Reset activity timer when releasing the lock
If the system applies for PARTIAL_WAKE_LOCK, the system will not enter sleep even when the power button is pressed, such as during music playback. If other wakelocks are applied, pressing the power button will still cause the system to sleep.
Wakelocks have two states: permanent lock and timeout lock. Permanent lock will not unlock unless explicitly released later. Timeout lock will automatically unlock after a certain period.
Two types of power locks :
(1) WAKE_LOCK_SUSPEND : Prevents system from entering sleep. This is a permanent lock. The timeout version is WAKE_LOCK_AUTO_EXPIRE.
(2) WAKE_LOCK_IDLE : Prevents the system from entering idle state while holding this lock.
Android uses two linked lists to save active suspend locks and idle locks, and to save inactive wakelocks.
There are two mechanisms for acquiring and releasing locks: non-counting lock and counting lock. This can be set via PowerManager.WakeLock.setReferenceCounted(boolean value) , with counting mechanism as default. The difference is that the former can directly unlock with one release() regardless of how many times acquire() was called. The latter truly unlocks when (--count == 0).
3.1 Wakelock in Framework Layer
After kernel startup, the power management system creates two nodes in the file system:
/sys/power/wake_lock
/sys/power/wake_unlock
Applications 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 abort the suspend process until the lock is released.
If an application crashes or exits, the system automatically releases all power locks they acquired. If acquired in a service, they will also be automatically released when the service crashes or unregisters.
The power lock content in the Framework layer is implemented through the PowerManagerService class, which manages all wakelocks applied by applications, such as those from audio/video players and camera.
ADB debug commands:
echo lockname > /sys/power/wake_lock - Acquire lock "lockname"
echo lockname > /sys/power/wake_unlock - Release lock "lockname"
4. Early Suspend and Late Resume
Early Suspend and Late Resume are features added by Android on top of standard Linux. When user space applies to enter suspend, it first enters the early suspend state. Peripheral device drivers can register early suspend callback functions, which are called one by one when entering early suspend. For example, after entering early suspend, callback functions will turn off the screen and backlight through the screen driver, but the system is still running normally. After entering early suspend state, once all wakelocks are released, the system immediately enters the real suspend process.
Starting from Android 4.4 (the version that introduced ART), the early suspend mechanism was abandoned and replaced with fb event notification mechanism. Later Android versions only have suspend, resume, and runtime suspend, runtime resume.
OPPO Kernel Craftsman
Sharing Linux kernel-related cutting-edge technology, technical articles, technical news, and curated tutorials
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.