[android 休眠喚醒機制分析(二) — early_suspend ](https://blog.csdn.net/g_salamander/article/details/7982170) 是Android休眠流程的第一階段即淺度休眠,不會受到wake_lock的阻止,一般用於關閉lcd、t ...
android 休眠喚醒機制分析(二) — early_suspend
early_suspend
是Android休眠流程的第一階段即淺度休眠,不會受到wake_lock的阻止,一般用於關閉lcd、tp等設備為運行的應用節約電能。Android的PowerManagerService
會根據用戶的操作情況調整電源狀態,如果需要休眠則會調用到HAL層的set_screen_state()
介面,在set_screen_state()
中會向/sys/power/state
節點寫入"mem"值讓驅動層開始進入休眠流程。
一、休眠喚醒機制及其用戶空間介面
Linux系統支持如下休眠喚醒等級
const char *const pm_states[PM_SUSPEND_MAX] = {
#ifdef CONFIG_EARLYSUSPEND
[PM_SUSPEND_ON] = "on",
#endif
[PM_SUSPEND_STANDBY] = "standby",
[PM_SUSPEND_MEM] = "mem",
};
但在Android中一般只支持"on"和"mem",其中"on"為喚醒設備,"mem"為休眠設備。/sys/power/state
節點的讀寫操作如下:
static ssize_t state_show(struct kobject *kobj, struct kobj_attribute *attr,
char *buf)
{
char *s = buf;
#ifdef CONFIG_SUSPEND
int i;
for (i = 0; i < PM_SUSPEND_MAX; i++) {
if (pm_states[i] && valid_state(i))
s += sprintf(s,"%s ", pm_states[i]); // 列印系統支持的休眠等級
}
#endif
#ifdef CONFIG_HIBERNATION
s += sprintf(s, "%s\n", "disk");
#else
if (s != buf)
/* convert the last space to a newline */
*(s-1) = '\n';
#endif
return (s - buf);
}
static ssize_t state_store(struct kobject *kobj, struct kobj_attribute *attr,
const char *buf, size_t n)
{
#ifdef CONFIG_SUSPEND
#ifdef CONFIG_EARLYSUSPEND
suspend_state_t state = PM_SUSPEND_ON;
#else
suspend_state_t state = PM_SUSPEND_STANDBY;
#endif
const char * const *s;
#endif
char *p;
int len;
int error = -EINVAL;
p = memchr(buf, '\n', n);
len = p ? p - buf : n;
/* First, check if we are requested to hibernate */
if (len == 4 && !strncmp(buf, "disk", len)) {
error = hibernate();
goto Exit;
}
#ifdef CONFIG_SUSPEND
for (s = &pm_states[state]; state < PM_SUSPEND_MAX; s++, state++) {
if (*s && len == strlen(*s) && !strncmp(buf, *s, len))
break;
}
if (state < PM_SUSPEND_MAX && *s)
#ifdef CONFIG_EARLYSUSPEND
if (state == PM_SUSPEND_ON || valid_state(state)) {
error = 0;
request_suspend_state(state); // 請求進入android的休眠流程
}
#else
error = enter_state(state); // linux的標準休眠流程
#endif
#endif
Exit:
return error ? error : n;
}
power_attr(state);
其中state_show()
為節點的讀函數,主要列印出系統支持的休眠等級;state_store()
為節點的寫函數,根據參數請求休眠或者喚醒流程。節點的創建代碼如下:
static struct attribute * g[] = {
&state_attr.attr, // state節點
#ifdef CONFIG_PM_TRACE
&pm_trace_attr.attr,
#endif
#if defined(CONFIG_PM_SLEEP) && defined(CONFIG_PM_DEBUG)
&pm_test_attr.attr, // pm_test節點
#endif
#ifdef CONFIG_USER_WAKELOCK
&wake_lock_attr.attr, // wake_lock節點
&wake_unlock_attr.attr, // wake_unlock節點
#endif
NULL,
};
static struct attribute_group attr_group = {
.attrs = g,
};
static int __init pm_init(void)
{
int error = pm_start_workqueue();
if (error)
return error;
power_kobj = kobject_create_and_add("power", NULL); // 創建power節點
if (!power_kobj)
return -ENOMEM;
return sysfs_create_group(power_kobj, &attr_group); // 創建一組屬性節點
}
core_initcall(pm_init);
二、early_suspend 實現
1、early_suspend 定義、介面及其用法
enum {
EARLY_SUSPEND_LEVEL_BLANK_SCREEN = 50,
EARLY_SUSPEND_LEVEL_STOP_DRAWING = 100,
EARLY_SUSPEND_LEVEL_DISABLE_FB = 150,
};
struct early_suspend {
#ifdef CONFIG_HAS_EARLYSUSPEND
struct list_head link; // 鏈表節點
int level; // 優先等級
void (*suspend)(struct early_suspend *h);
void (*resume)(struct early_suspend *h);
#endif
};
可以看到early_suspend
由兩個函數指針、鏈表節點、優先等級組成;內核預設定義了3個優先等級,在suspend的時候先執行優先等級低的handler,在resume的時候則先執行等級高的handler,用戶可以定義自己的優先等級;early_suspend
向內核空間提供了2個介面用於註冊和註銷handler:
void register_early_suspend(struct early_suspend *handler);
void unregister_early_suspend(struct early_suspend *handler);
其中register_early_suspend()
用於註冊,unregister_early_suspend
用於註銷;一般early_suspend
的使用方式如下:
ts->earlysuspend.suspend = sitronix_i2c_suspend_early;
ts->earlysuspend.resume = sitronix_i2c_resume_late;
ts->earlysuspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN;
register_early_suspend(&ts->earlysuspend);
設置好suspend
和resume
介面,定義優先等級,然後註冊結構即可。
2、初始化信息
我們看一下early_suspend
需要用到的一些數據:
static DEFINE_MUTEX(early_suspend_lock);
static LIST_HEAD(early_suspend_handlers); // 初始化淺度休眠鏈表
// 聲明3個工作隊列用於同步、淺度休眠和喚醒
static void early_sys_sync(struct work_struct *work);
static void early_suspend(struct work_struct *work);
static void late_resume(struct work_struct *work);
static DECLARE_WORK(early_sys_sync_work,early_sys_sync);
static DECLARE_WORK(early_suspend_work, early_suspend);
static DECLARE_WORK(late_resume_work, late_resume);
static DEFINE_SPINLOCK(state_lock);
enum {
SUSPEND_REQUESTED = 0x1, // 當前正在請求淺度休眠
SUSPENDED = 0x2, // 淺度休眠完成
SUSPEND_REQUESTED_AND_SUSPENDED = SUSPEND_REQUESTED | SUSPENDED,
};
static int state;
初始化了一個鏈表early_suspend_handlers用於管理early_suspend,還定義讀寫鏈表用到的互斥體;另外還聲明瞭3個工作隊列,分別用於緩存同步、淺度休眠和喚醒;還聲明瞭early_suspend操作的3個狀態。
3、register_early_suspend 和 unregister_early_suspend
void register_early_suspend(struct early_suspend *handler)
{
struct list_head *pos;
mutex_lock(&early_suspend_lock);
// 遍歷淺度休眠鏈表
list_for_each(pos, &early_suspend_handlers) {
struct early_suspend *e;
e = list_entry(pos, struct early_suspend, link);
// 判斷當前節點的優先等級是否大於handler的優先等級
// 以此決定handler在鏈表中的順序
if (e->level > handler->level)
break;
}
// 將handler加入當前節點之前,優先等級越低越靠前
list_add_tail(&handler->link, pos);
if ((state & SUSPENDED) && handler->suspend)
handler->suspend(handler);
mutex_unlock(&early_suspend_lock);
}
EXPORT_SYMBOL(register_early_suspend);
註冊的流程比較簡單,首先遍歷鏈表,依次比較每個節點的優先等級,如果遇到優先等級比新節點優先等級高則跳出,然後將新節點加入優先等級較高的節點前面,這樣就確保了鏈表是優先等級低在前高在後的順序;在將節點加入鏈表後查看當前狀態是否為淺度休眠完成狀態,如果是則執行handler的suspend函數。
4、request_suspend_state
前面我們看到用戶空間在寫/sys/power/state
節點的時候會執行request_suspend_state()
函數,該函數代碼如下:
void request_suspend_state(suspend_state_t new_state)
{
unsigned long irqflags;
int old_sleep;
spin_lock_irqsave(&state_lock, irqflags);
old_sleep = state & SUSPEND_REQUESTED;
// 列印當前狀態
if (debug_mask & DEBUG_USER_STATE) {
struct timespec ts;
struct rtc_time tm;
getnstimeofday(&ts);
rtc_time_to_tm(ts.tv_sec, &tm);
pr_info("request_suspend_state: %s (%d->%d) at %lld "
"(%d-%02d-%02d %02d:%02d:%02d.%09lu UTC)\n",
new_state != PM_SUSPEND_ON ? "sleep" : "wakeup",
requested_suspend_state, new_state,
ktime_to_ns(ktime_get()),
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
tm.tm_hour, tm.tm_min, tm.tm_sec, ts.tv_nsec);
}
// 如果新狀態是休眠狀態
if (!old_sleep && new_state != PM_SUSPEND_ON) {
state |= SUSPEND_REQUESTED;
pr_info("sys_sync_work_queue early_sys_sync_work.\n");
// 執行緩存同步與淺度休眠的工作隊列
queue_work(sys_sync_work_queue, &early_sys_sync_work);
queue_work(suspend_work_queue, &early_suspend_work);
} else if (old_sleep && new_state == PM_SUSPEND_ON) {
// 如果新狀態是喚醒狀態
state &= ~SUSPEND_REQUESTED;
// 激活內核鎖
wake_lock(&main_wake_lock);
// 執行淺度喚醒的工作隊列
queue_work(suspend_work_queue, &late_resume_work);
}
// 更新全局狀態
requested_suspend_state = new_state;
spin_unlock_irqrestore(&state_lock, irqflags);
}
函數首先列印出當前狀態變化的log,然後判斷新狀態,如果是休眠狀態則置位SUSPEND_REQUESTED標誌,然後將同步緩存、淺度休眠工作隊列加入相應的內核線程執行;如果新狀態是喚醒則首先將main_wake_lock激活,然後再將淺度喚醒工作隊列加入內核線程執行;最後更新全局狀態變數,因為提供了一個內核空間介面用於獲取當前休眠喚醒狀態:
// 返回系統狀態值
suspend_state_t get_suspend_state(void)
{
return requested_suspend_state;
}
5、early_suspend_work、late_resume_work 和 early_sys_sync
static void early_suspend(struct work_struct *work)
{
struct early_suspend *pos;
unsigned long irqflags;
int abort = 0;
mutex_lock(&early_suspend_lock);
spin_lock_irqsave(&state_lock, irqflags);
if (state == SUSPEND_REQUESTED) // 判斷當前狀態是否在請求淺度休眠
state |= SUSPENDED; // 如果是則置位SUSPENDED
else
abort = 1;
spin_unlock_irqrestore(&state_lock, irqflags);
if (abort) { // 取消early_suspend
if (debug_mask & DEBUG_SUSPEND)
pr_info("early_suspend: abort, state %d\n", state);
mutex_unlock(&early_suspend_lock);
goto abort;
}
if (debug_mask & DEBUG_SUSPEND)
pr_info("early_suspend: call handlers\n");
// 遍歷淺度休眠鏈表並執行其中所有suspend函數
// 執行順序根據優先等級而定,等級越低越先執行
list_for_each_entry(pos, &early_suspend_handlers, link) {
if (pos->suspend != NULL)
pos->suspend(pos);
}
mutex_unlock(&early_suspend_lock);
if (debug_mask & DEBUG_SUSPEND)
pr_info("early_suspend: sync\n");
/* Remove sys_sync from early_suspend, and use work queue to complete sys_sync */
//sys_sync();
abort:
spin_lock_irqsave(&state_lock, irqflags);
if (state == SUSPEND_REQUESTED_AND_SUSPENDED)
wake_unlock(&main_wake_lock);
spin_unlock_irqrestore(&state_lock, irqflags);
}
在suspend流程中首先判斷當前狀態是否為SUSPEND_REQUESTED,如果是則置位SUSPENDED標誌,如果不是則取消suspend流程;然後遍歷淺度休眠鏈表,從鏈表頭部到尾部依次調用各節點的suspend()函數,執行完後判斷當前狀態是否為SUSPEND_REQUESTED_AND_SUSPENDED
,如果是則釋放main_wake_lock,當前系統中如果只存在main_wake_lock這個有效鎖,則會在wake_unlock()
裡面啟動深度休眠線程,如果還有其他其他wake_lock則保持當前狀態。
static void late_resume(struct work_struct *work)
{
struct early_suspend *pos;
unsigned long irqflags;
int abort = 0;
mutex_lock(&early_suspend_lock);
spin_lock_irqsave(&state_lock, irqflags);
if (state == SUSPENDED) // 清除淺度休眠完成標誌
state &= ~SUSPENDED;
else
abort = 1;
spin_unlock_irqrestore(&state_lock, irqflags);
if (abort) {
if (debug_mask & DEBUG_SUSPEND)
pr_info("late_resume: abort, state %d\n", state);
goto abort;
}
if (debug_mask & DEBUG_SUSPEND)
pr_info("late_resume: call handlers\n");
// 反向遍歷淺度休眠鏈表並執行其中所有resume函數
// 執行順序根據優先等級而定,等級越高越先執行
list_for_each_entry_reverse(pos, &early_suspend_handlers, link)
if (pos->resume != NULL)
pos->resume(pos);
if (debug_mask & DEBUG_SUSPEND)
pr_info("late_resume: done\n");
abort:
mutex_unlock(&early_suspend_lock);
}
在resume流程中同樣首先判斷當前狀態是否為SUSPENDED,如果是則清除SUSPENDED標誌,然後反向遍歷淺度休眠鏈表,按照優先等級從高到低的順序執行節點的resume()函數。
static void early_sys_sync(struct work_struct *work)
{
wake_lock(&sys_sync_wake_lock);
sys_sync();
wake_unlock(&sys_sync_wake_lock);
}
內核專門為緩存同步建立了一個線程,同時還創建了sys_sync_wake_lock
防止在同步緩存時系統進入深度休眠。