Android Power Management Basics finishing

Foreword

  1. Standby, the difference between sleep and hibernate?
  2. Android developer's official website which referred to "idle states", how to understand, what impact will this state of our equipment and procedures?
  3. Doze mode into the idle state, our program can run it?
  4. After the phone to sleep, why we wrote the program Alarm, Caller ID program will still take effect?

If you have more questions, then this article will tell you unlock the doubts of some help

ACPI Profile

To understand the first question, must start with the ACPI (Advanced Configuration and Power Interface) Speaking of, ACPI is a specification (includes software and hardware), is used for the operating system applications to manage all power connector.

ACPI state of the computer system is divided into four global state (G0-G3), a total of seven states, which correspond to G0 S0; G1 low power consumption state is subdivided into four states, S1-S4 correspond; G2, G3 representatives off states correspond S5, S6.

ACPI State Description
S0 Normal working condition
S1 CPU and RAM supply is normal, but the CPU does not execute instructions
S2 A sleep deeper than S1 level, this model is not usually used
S3 Suspend to RAM
S4 Suspend to disk
S5 Soft Off, CPU, and other peripherals off, but the power supply is part of the device will still very low consumption
S6 Mechanical Off, all off

Here only need to have a general understanding, there will be specific examples of the next section of the seven states ACPI to illustrate each state.

Linux system power state

In the Linux operating system, the power supply is divided into the following states:

ACPI State Linux State Description
S0 Is is) Working
S1 Standby(standby) CPU and RAM are powered but not executed
S2 ------ ------
S3 Suspend to RAM(mem) CPU is Off,RAM is powered and the running content is saved to RAM
S4 Suspend to Disk(disk) All content is saved to Disk and power down
S5 Shutdown Shutdown the system

On: normal operation

STR(Suspend to RAM):

Suspend the main working memory, commonly known as standby, sleep (Sleep), the state enters the system as follows:

1, the current state of the system data stored in the memory, in which case still needs to power RAM, to ensure that the subsequent fast return to the working state

2, freezing user mode and kernel mode processes task (or kernel into the kernel mode process of their task)

3, turn off the peripheral devices, such as display, mouse, and peripherals will not interrupt wake-up close, such as the power button

4, CPU stops working

Standby also belongs to a way of sleep, light sleep belong. In this mode CPU does not power off, you can still receive processing certain events, as the device may be restored to normal operation speed is faster than the STR, but also more power consumption. Take, for example, when entering sleep in this manner, the follow-up by clicking on the keyboard can also wake up the system. The mem to enter sleep to deep sleep, only to wake up the device via an interrupt wake up the system, such as the power button (then press the power button, not through the normal boot process BIOS, BOOTLOAD, etc.), then press the keyboard can not wake up systematic.

STD(Suspend to Disk):

Suspend to disk, commonly known as sleep (Hibernation) to save the current state data to the hard disk system or the like, and automatically shut down. Next time begins on the hard disk read previously saved data when the power to return to the state before sleep shutdown.

For instance, in the sleep off the desktop to open an application, then when the next start-up, the application is also open. The normal shutdown - start-up procedure, the application is not open.

Linux kernel code the following statement, located in kernel / power / suspend.c
image

In the new kernel functions are the process of freeze pulled out as a single power state, this state is only the process of freezing, the system does not make the low-power state (e.g., cut off the CPU clock, the peripheral power off, etc.).

Related macro definition is located: linux / include / linux / suspend.h

image

4 is a state in which the STD, the so-called dormant state (Hibernation)

summary:

At this point, we can know, sleep and hibernation are two different concepts, sleep belong to STR, and sleep belong to STD, do not be confused.

There are many online articles about "Android sleep", in fact, Android phones simply did not support sleep mode.

View Linux supported power mode

#查看系统支持的电源模式
$ cat /sys/power/state
#休眠系统命令
$ sudo pm-hibernate

image

看来Ubuntu-17.0.4版本是不支持休眠功能了,state当中并没有disk,执行休眠命令也提示找不到。

在公司测试Ubuntu-16.0.4是支持休眠的,休眠时会将当前RAM中的数据保持至swap分区,以供后续恢复。

image

查看Android支持的电源模式

image

这里我使用的是模拟器查看的,真机也一样,Android手机是不支持休眠模式的,休眠模式需要一块与RAM大小一致存储空间,这在移动设备上可是个不小的开销。

Idle State

Android上的Idle状态分为二类:Cpu Idle和Device Idle

Cpu Idle

Linux系统运行的基础是基于进程调度,实际上内核调度的线程(task),内核并不会区分线程与进程,都将他们当做一个线程(task)来处理;当所有的进程都没事儿干的时候,系统就会启用idle进程,使系统进入低功耗状态(如关闭一些服务、模块功能,降低CPU工作频率等),即idle状态,以达到省电的目的。

idle状态又可以划分为不同的层级,以MTK的芯片为例,通常划分为以下几个状态:

状态 描述
soidle(screen on idle) 亮屏 Idle 模式,该模式下与正常工作状态差别不大,唯一的区别就cpu处于空闲状态
rgidle 浅度 Idle 模式,cpu处于 WFI(wait for interrupt),屏幕熄灭,同时关闭一些不需要的服务及模块,注意此状态cpu的时钟源与RTC模块是工作正常的,此时是可以通过TimerTask的定时触发激活系统的,TimerTask依赖于CPU的RTC模块,而Alarm则依赖于PMIC的RTC模块
dpidle(deep idle) 深度idle模式,该模式下cpu的时钟源和hrtimer(高精度定时器模块(RTC))被关闭,所有进程(包括系统进程)被冻结,即进入上文所述的睡眠状态

idle进程是由原始进程(pid=0)在初始化init进程(pid=1)之后演变而来,可以说是init进程的祖先,关于其详细介绍可参考如下链接:

Linux Idle基础

CPUIDLE 之低功耗定时器

Device Idle

Device Idle属于Doze模式中概念,即指当手机屏幕熄屏、不充电、静置不动,有网友分析了源码,指出6.0手机需要静置1时4分30秒才能进入Doze模式。

Doze模式的限制
网络接入被暂停
系统忽略wake locks
标准的AlarmManager alarms(包括setExact()和setWindow())被延缓到下一个maintenance window
如果你需要在Doze状态下启动设置的alarms,使用setAndAllowWhileIdle()或者setExactAndAllowWhileIdle()。当有setAlarmClock()的alarms启动时,系统会短暂退出Doze模式
系统不会扫描Wi-Fi
系统不允许sync adapters运行
系统不允许JobScheduler运行

结合上文分析的cpu idle不难发现Doze模式中的idle状态在概念属于浅idle状态,只是关闭了一些特定服务和模块,并非立即进入睡眠,当然这个过程当中依旧有可能满足睡眠条件而进入睡眠状态,至于如何进入请参考下文【睡眠触发入口】一节。

Android Doze模式源码分析

Android电源管理框架

Android采用linux内核,所以电源状态整体上是与linux操作系统相同,下图是Android的电源管理框架:

image

WakeLock

唤醒锁,一种锁机制,用于阻止系统进入睡眠状态,只要有应用获取到改锁,那么系统就无法进入睡眠状态。

该机制起初是早期Android为Linux内核打得一个补丁,并想合入到linux内核,但被Linux社区拒绝,后续Linux内核引入自己的Wakelock机制,Android系统也使用的是linux的Wakelock机制,所以该机制并非Android特有的机制。

Android系统提供了两种类型的锁,每一个类型又可分为超时锁与普通锁,超时锁,超时会自动释放,而普通锁则必需要手动释放:

类型 描述
WAKE_LOCK_SUSPEND 阻止系统进入睡眠状态(STR)
WAKE_LOCK_IDLE 阻止系统从idle进程进入那些具有较大中断时延、禁用了较多中断源的低功耗状态(睡眠除外),持有该类型的锁,不影响系统进入睡眠状态。自Android API-17(对应android linux内核版本3.4)移除了该类型的唤醒锁。

中断时延:计算机接收到中断信号到操作系统作出响应,并完成转入中断服务程序(ISR)的时间。

内核当中关于WakeLock的主要源码位于:

kernel_common/include/linux/wakelock.h

kernel_common/kernel/power/wakelock.c

image

Android Linux内核3.0版本

image

Android Linux内核3.4版本

应用层提供的锁类型如下,这些锁都需要手动释放:

FLAG CPU 屏幕 键盘
PARTIAL_WAKE_LOCK 开启 关闭 关闭
SCREEN_DIM_WAKE_LOCK 开启 变暗 关闭
SCREEN_BRIGHT_WAKE_LOCK 开启 变亮 关闭
FULL_WAKE_LOCK 开启 变亮 变亮

锁的释放

Linux3.4内核中摒弃了之前的wakelock机制,引入wakeup source机制来进行睡眠管理,为了保证上层接口不变,Android的Linux内核便将wakeup source包装成wakelock,WakeLock的数据结构如下:

image

wakelock数据结构

当我们应用层释放锁之后,它并不会马上消失。wakelock分为激活和非激活状态,非激活状态300S之内,无人在申请wakelock,那么它将从红黑二叉树,LRU链表当中删除,如此便可复用锁,节省系统开销。

睡眠触发入口

在wakelock中,有3个地方可以让系统从early_suspend进入suspend状态。

wake_unlock,系统每释放一个锁,就会检查是否还存其他激活的wakelock,若不存在则执行Linux的标准suspend流程进入睡眠状态
在超时锁的超时回调函数,判断是否存在其他激活的wakelock,若不存在,则进入睡眠状态
autosleep机制,android 4.1引入该机制,亮屏时会向autosleep节点写入off,熄屏则会写入mem。Android一灭屏,就会尝试进入睡眠,失败之后系统处于idle进程超过一定时间,则又尝试进入睡眠,判断标准同上,若存在wakelock则进入失败

image

关于autosleep机制的内核源码分析,可以参考如下文章:

Android autosleep机制

Early Suspend

预挂起机制是Android特有的挂起机制, 这个机制作用是关闭一些与显示相关的外设,比如LCD背光、重力感应器、 触摸屏,但是其他外设如WIFI、蓝牙等模块等并未关闭。
此时,系统依旧可以处理事件,如音乐播放软件,息屏后依旧能播放音乐。
需要注意的是Early Suspend机制与WakeLock机制相互独立,就算有应用持有wakelock锁,系统依旧可以通过Early Suspend机制关闭与显示相关的外设。
注意:
Android 4.4起,也就是引入ART的版本,摒弃了early suspend机制,改用了fb event通知机制,即后续版本只有suspend、resume以及runtime suspend、runtime resume。

Late Resume

迟唤醒机制,用于唤醒预挂起的设备

睡眠状态转换

一般情况下,当我们息屏后,系统将先通过Early Suspend机制进入Idle状态,如果满足进入睡眠的条件(没有进程持有唤醒锁)则会通过Linux的Suspend机制进入Sleep(睡眠)状态。

image

内核源码流程分析可参考如下文章:
源码位于kernel_common/kernel/power/main.c

Android中休眠与唤醒之wake_lock, early_suspend, late_resume

看到这儿,不知你是否疑问,既然系统睡眠了,CPU断电不执行指令了,为何我们定的Alarm会生效以及能接收到来电?

手机来电与Alarm为何能唤醒系统

原来Android在硬件架构上将处理器分为二类:Application Processor(AP)和Baseband Processor(BP),AP是ARM架构的处理器,用于运行Linux+Android系统,耗电量高;BP用于运行实时操作系统(RTOS),用于处理手机通信,耗电量低。

image

当AP进入睡眠,有来电时,Modem(调制解调器)将唤醒AP;而我们平时所用的Alarm在硬件上则是依赖PMIC(电源管理芯片)中的RTC模块,所以即使AP断电进入睡眠,我们定的闹钟依旧会生效。

image

若想更深入的了解,则可参考Android RIL机制相关的文章。

总结

  1. 待机、睡眠与休眠的区别

实际上待机(standby)与睡眠(mem)属于不同模式,但现在大多操作系统都不支持待机模式了,我们也习惯将待机等同于睡眠,睡眠属于STR,休眠属于STD,Android手机不支持休眠!!!

  1. Android开发者官网当中提到“idle state”,该如何理解,这个状态会对设备及我们的程序造成何种影响

所谓的idle状态,就是指系统进入某个低功耗状态,以MTK为例,常见的状态有soidle、rgidle以及dpidle。rgidle只是限制我们程序使用某些模块,如Doze模式中不能访问网络;而dpidle则会冻结所有进程,系统进入睡眠。

  1. 进入Doze模式中的idle状态,我们的程序还能运行吗?

Doze mode idle concept belongs rgidle state, at this time our program is able to run, but can not access the network and so on, but in the process, the system goes to sleep condition may be satisfied, to freeze all processes, so that our program It will not be implemented.
Can write their own infinite loop thread (common threads, non looper thread) to force the phone to enter the Doze the idle mode, you will find that your program is still in execution, but left after a period of time where you will find your thread frozen, will not be executed, when you light up the screen, your thread will continue to work.

  1. After the phone to sleep, why we wrote the program Alarm, Caller ID program will still take effect?

Android hardware on the processor architecture divided among the AP and BP, applications running with AP, sleep just off the AP, BP (Modem) will not power off when there is an incoming call, BP will wake AP.

Alarm is dependent on the hardware of the PMIC Modem RTC module, rather than the AP RTC module, when the timer is triggered, you can wake up AP, so that our program will still be implemented Alarm

Guess you like

Origin www.cnblogs.com/linhaostudy/p/12119487.html