zoukankan      html  css  js  c++  java
  • android 休眠唤醒机制分析(二) — early_suspend

    本文转自:http://blog.csdn.net/g_salamander/article/details/7982170

    early_suspend是Android休眠流程的第一阶段即浅度休眠,不会受到wake_lock的阻止,一般用于关闭lcd、tp等设备为运行的应用节约电能。Android的PowerManagerService会根据用户的操作情况调整电源状态,如果需要休眠则会调用到HAL层的set_screen_state()接口,在set_screen_state()中会向/sys/power/state节点写入"mem"值让驱动层开始进入休眠流程。

    一、休眠唤醒机制及其用户空间接口

    Linux系统支持如下休眠唤醒等级

    [cpp] view plaincopy
     
    1. const char *const pm_states[PM_SUSPEND_MAX] = {  
    2. #ifdef CONFIG_EARLYSUSPEND  
    3.     [PM_SUSPEND_ON]     = "on",  
    4. #endif  
    5.     [PM_SUSPEND_STANDBY]    = "standby",  
    6.     [PM_SUSPEND_MEM]    = "mem",  
    7. };  

    但在Android中一般只支持"on"和"mem",其中"on"为唤醒设备,"mem"为休眠设备。/sys/power/state节点的读写操作如下:

    [cpp] view plaincopy
     
    1. static ssize_t state_show(struct kobject *kobj, struct kobj_attribute *attr,  
    2.               char *buf)  
    3. {  
    4.     char *s = buf;  
    5. #ifdef CONFIG_SUSPEND  
    6.     int i;  
    7.   
    8.     for (i = 0; i < PM_SUSPEND_MAX; i++) {  
    9.         if (pm_states[i] && valid_state(i))  
    10.             s += sprintf(s,"%s ", pm_states[i]);  // 打印系统支持的休眠等级  
    11.     }  
    12. #endif  
    13. #ifdef CONFIG_HIBERNATION  
    14.     s += sprintf(s, "%s ", "disk");  
    15. #else  
    16.     if (s != buf)  
    17.         /* convert the last space to a newline */  
    18.         *(s-1) = ' ';  
    19. #endif  
    20.     return (s - buf);  
    21. }  
    22.   
    23. static ssize_t state_store(struct kobject *kobj, struct kobj_attribute *attr,  
    24.                const char *buf, size_t n)  
    25. {  
    26. #ifdef CONFIG_SUSPEND  
    27. #ifdef CONFIG_EARLYSUSPEND  
    28.     suspend_state_t state = PM_SUSPEND_ON;  
    29. #else  
    30.     suspend_state_t state = PM_SUSPEND_STANDBY;  
    31. #endif  
    32.     const char * const *s;  
    33. #endif  
    34.     char *p;  
    35.     int len;  
    36.     int error = -EINVAL;  
    37.   
    38.     p = memchr(buf, ' ', n);  
    39.     len = p ? p - buf : n;  
    40.   
    41.     /* First, check if we are requested to hibernate */  
    42.     if (len == 4 && !strncmp(buf, "disk", len)) {  
    43.         error = hibernate();  
    44.   goto Exit;  
    45.     }  
    46.   
    47. #ifdef CONFIG_SUSPEND  
    48.     for (s = &pm_states[state]; state < PM_SUSPEND_MAX; s++, state++) {  
    49.         if (*s && len == strlen(*s) && !strncmp(buf, *s, len))  
    50.             break;  
    51.     }  
    52.     if (state < PM_SUSPEND_MAX && *s)  
    53. #ifdef CONFIG_EARLYSUSPEND  
    54.         if (state == PM_SUSPEND_ON || valid_state(state)) {  
    55.             error = 0;  
    56.             request_suspend_state(state);  // 请求进入android的休眠流程  
    57.         }  
    58. #else  
    59.         error = enter_state(state);  // linux的标准休眠流程  
    60. #endif  
    61. #endif  
    62.   
    63.  Exit:  
    64.     return error ? error : n;  
    65. }  
    66.   
    67. power_attr(state);  

    其中state_show()为节点的读函数,主要打印出系统支持的休眠等级;state_store()为节点的写函数,根据参数请求休眠或者唤醒流程。节点的创建代码如下:

    [cpp] view plaincopy
     
    1. static struct attribute * g[] = {  
    2.     &state_attr.attr,        // state节点  
    3. #ifdef CONFIG_PM_TRACE  
    4.     &pm_trace_attr.attr,  
    5. #endif  
    6. #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_PM_DEBUG)  
    7.     &pm_test_attr.attr,      // pm_test节点  
    8. #endif  
    9. #ifdef CONFIG_USER_WAKELOCK  
    10.     &wake_lock_attr.attr,    // wake_lock节点  
    11.     &wake_unlock_attr.attr,  // wake_unlock节点  
    12. #endif  
    13.     NULL,  
    14. };  
    15.   
    16. static struct attribute_group attr_group = {  
    17.     .attrs = g,  
    18. };  
    19.   
    20. static int __init pm_init(void)  
    21. {  
    22.     int error = pm_start_workqueue();  
    23.     if (error)  
    24.         return error;  
    25.     power_kobj = kobject_create_and_add("power", NULL);  // 创建power节点  
    26.     if (!power_kobj)  
    27.         return -ENOMEM;  
    28.     return sysfs_create_group(power_kobj, &attr_group);  // 创建一组属性节点  
    29. }  
    30.   
    31. core_initcall(pm_init);  

    二、early_suspend 实现

    1、early_suspend 定义、接口及其用法

    [cpp] view plaincopy
     
    1. enum {  
    2.     EARLY_SUSPEND_LEVEL_BLANK_SCREEN = 50,  
    3.     EARLY_SUSPEND_LEVEL_STOP_DRAWING = 100,  
    4.     EARLY_SUSPEND_LEVEL_DISABLE_FB = 150,  
    5. };  
    6. struct early_suspend {  
    7. #ifdef CONFIG_HAS_EARLYSUSPEND  
    8.     struct list_head link;  // 链表节点  
    9.     int level;              // 优先等级  
    10.     void (*suspend)(struct early_suspend *h);  
    11.     void (*resume)(struct early_suspend *h);  
    12. #endif  
    13. };  

    可以看到early_suspend由两个函数指针、链表节点、优先等级组成;内核默认定义了3个优先等级,在suspend的时候先执行优先等级低的handler,在resume的时候则先执行等级高的handler,用户可以定义自己的优先等级;early_suspend向内核空间提供了2个接口用于注册和注销handler:

    [cpp] view plaincopy
     
    1. void register_early_suspend(struct early_suspend *handler);  
    2. void unregister_early_suspend(struct early_suspend *handler);  

    其中register_early_suspend()用于注册,unregister_early_suspend用于注销;一般early_suspend的使用方式如下:

    [cpp] view plaincopy
     
    1. ts->earlysuspend.suspend = sitronix_i2c_suspend_early;  
    2. ts->earlysuspend.resume = sitronix_i2c_resume_late;  
    3. ts->earlysuspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN;  
    4. register_early_suspend(&ts->earlysuspend);  

    设置好suspend和resume接口,定义优先等级,然后注册结构即可。

    2、初始化信息

    我们看一下early_suspend需要用到的一些数据:

    [cpp] view plaincopy
     
    1. static DEFINE_MUTEX(early_suspend_lock);  
    2. static LIST_HEAD(early_suspend_handlers);  // 初始化浅度休眠链表  
    3. // 声明3个工作队列用于同步、浅度休眠和唤醒  
    4. static void early_sys_sync(struct work_struct *work);  
    5. static void early_suspend(struct work_struct *work);  
    6. static void late_resume(struct work_struct *work);  
    7. static DECLARE_WORK(early_sys_sync_work,early_sys_sync);  
    8. static DECLARE_WORK(early_suspend_work, early_suspend);  
    9. static DECLARE_WORK(late_resume_work, late_resume);  
    10. static DEFINE_SPINLOCK(state_lock);  
    11. enum {  
    12.     SUSPEND_REQUESTED = 0x1,  // 当前正在请求浅度休眠  
    13.     SUSPENDED = 0x2,          // 浅度休眠完成  
    14.     SUSPEND_REQUESTED_AND_SUSPENDED = SUSPEND_REQUESTED | SUSPENDED,  
    15. };  
    16. static int state;  

    初始化了一个链表early_suspend_handlers用于管理early_suspend,还定义读写链表用到的互斥体;另外还声明了3个工作队列,分别用于缓存同步、浅度休眠和唤醒;还声明了early_suspend操作的3个状态。
    3、register_early_suspend 和 unregister_early_suspend

    [cpp] view plaincopy
     
    1. void register_early_suspend(struct early_suspend *handler)  
    2. {  
    3.     struct list_head *pos;  
    4.   
    5.     mutex_lock(&early_suspend_lock);  
    6.     // 遍历浅度休眠链表  
    7.     list_for_each(pos, &early_suspend_handlers) {  
    8.         struct early_suspend *e;  
    9.         e = list_entry(pos, struct early_suspend, link);  
    10.         // 判断当前节点的优先等级是否大于handler的优先等级  
    11.         // 以此决定handler在链表中的顺序  
    12.         if (e->level > handler->level)  
    13.             break;  
    14.     }  
    15.     // 将handler加入当前节点之前,优先等级越低越靠前  
    16.     list_add_tail(&handler->link, pos);  
    17.     if ((state & SUSPENDED) && handler->suspend)  
    18.         handler->suspend(handler);  
    19.     mutex_unlock(&early_suspend_lock);  
    20. }  
    21. EXPORT_SYMBOL(register_early_suspend);  

    注册的流程比较简单,首先遍历链表,依次比较每个节点的优先等级,如果遇到优先等级比新节点优先等级高则跳出,然后将新节点加入优先等级较高的节点前面,这样就确保了链表是优先等级低在前高在后的顺序;在将节点加入链表后查看当前状态是否为浅度休眠完成状态,如果是则执行handler的suspend函数。

    [cpp] view plaincopy
     
    1. void unregister_early_suspend(struct early_suspend *handler)  
    2. {  
    3.     mutex_lock(&early_suspend_lock);  
    4.     list_del(&handler->link);  
    5.     mutex_unlock(&early_suspend_lock);  
    6. }  
    7. EXPORT_SYMBOL(unregister_early_suspend);  

    注销流程则只是将节点从链表中移除。
    4、request_suspend_state

    前面我们看到用户空间在写/sys/power/state节点的时候会执行request_suspend_state()函数,该函数代码如下:

    [cpp] view plaincopy
     
    1. void request_suspend_state(suspend_state_t new_state)  
    2. {  
    3.     unsigned long irqflags;  
    4.     int old_sleep;  
    5.   
    6.     spin_lock_irqsave(&state_lock, irqflags);  
    7.     old_sleep = state & SUSPEND_REQUESTED;  
    8.     // 打印当前状态  
    9.     if (debug_mask & DEBUG_USER_STATE) {  
    10.         struct timespec ts;  
    11.         struct rtc_time tm;  
    12.         getnstimeofday(&ts);  
    13.         rtc_time_to_tm(ts.tv_sec, &tm);  
    14.         pr_info("request_suspend_state: %s (%d->%d) at %lld "  
    15.             "(%d-%02d-%02d %02d:%02d:%02d.%09lu UTC) ",  
    16.             new_state != PM_SUSPEND_ON ? "sleep" : "wakeup",  
    17.             requested_suspend_state, new_state,  
    18.             ktime_to_ns(ktime_get()),  
    19.             tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,  
    20.             tm.tm_hour, tm.tm_min, tm.tm_sec, ts.tv_nsec);  
    21.     }  
    22.     // 如果新状态是休眠状态  
    23.     if (!old_sleep && new_state != PM_SUSPEND_ON) {  
    24.         state |= SUSPEND_REQUESTED;  
    25.         pr_info("sys_sync_work_queue early_sys_sync_work. ");  
    26.         // 执行缓存同步与浅度休眠的工作队列  
    27.         queue_work(sys_sync_work_queue, &early_sys_sync_work);  
    28.         queue_work(suspend_work_queue, &early_suspend_work);  
    29.     } else if (old_sleep && new_state == PM_SUSPEND_ON) {  
    30.     // 如果新状态是唤醒状态  
    31.         state &= ~SUSPEND_REQUESTED;  
    32.         // 激活内核锁  
    33.         wake_lock(&main_wake_lock);  
    34.         // 执行浅度唤醒的工作队列  
    35.         queue_work(suspend_work_queue, &late_resume_work);  
    36.     }  
    37.     // 更新全局状态  
    38.     requested_suspend_state = new_state;  
    39.     spin_unlock_irqrestore(&state_lock, irqflags);  
    40. }  

    函数首先打印出当前状态变化的log,然后判断新状态,如果是休眠状态则置位SUSPEND_REQUESTED标志,然后将同步缓存、浅度休眠工作队列加入相应的内核线程执行;如果新状态是唤醒则首先将main_wake_lock激活,然后再将浅度唤醒工作队列加入内核线程执行;最后更新全局状态变量,因为提供了一个内核空间接口用于获取当前休眠唤醒状态:

    [cpp] view plaincopy
     
    1. // 返回系统状态值  
    2. suspend_state_t get_suspend_state(void)  
    3. {  
    4.     return requested_suspend_state;  
    5. }  

    5、early_suspend_work、late_resume_work 和 early_sys_sync

    [cpp] view plaincopy
     
    1. static void early_suspend(struct work_struct *work)  
    2. {  
    3.     struct early_suspend *pos;  
    4.     unsigned long irqflags;  
    5.     int abort = 0;  
    6.   
    7.     mutex_lock(&early_suspend_lock);  
    8.     spin_lock_irqsave(&state_lock, irqflags);  
    9.     if (state == SUSPEND_REQUESTED)  // 判断当前状态是否在请求浅度休眠  
    10.         state |= SUSPENDED;      // 如果是则置位SUSPENDED  
    11.     else  
    12.         abort = 1;  
    13.     spin_unlock_irqrestore(&state_lock, irqflags);  
    14.   
    15.     if (abort) {  // 取消early_suspend  
    16.         if (debug_mask & DEBUG_SUSPEND)  
    17.             pr_info("early_suspend: abort, state %d ", state);  
    18.         mutex_unlock(&early_suspend_lock);  
    19.         goto abort;  
    20.     }  
    21.   
    22.     if (debug_mask & DEBUG_SUSPEND)  
    23.         pr_info("early_suspend: call handlers ");  
    24.     // 遍历浅度休眠链表并执行其中所有suspend函数  
    25.     // 执行顺序根据优先等级而定,等级越低越先执行  
    26.     list_for_each_entry(pos, &early_suspend_handlers, link) {  
    27.         if (pos->suspend != NULL)  
    28.             pos->suspend(pos);  
    29.     }  
    30.     mutex_unlock(&early_suspend_lock);  
    31.   
    32.     if (debug_mask & DEBUG_SUSPEND)  
    33.         pr_info("early_suspend: sync ");  
    34.   
    35.     /* Remove sys_sync from early_suspend, and use work queue to complete sys_sync */  
    36.     //sys_sync();  
    37. abort:  
    38.     spin_lock_irqsave(&state_lock, irqflags);  
    39.     if (state == SUSPEND_REQUESTED_AND_SUSPENDED)  
    40.         wake_unlock(&main_wake_lock);  
    41.     spin_unlock_irqrestore(&state_lock, irqflags);  
    42. }  

    在suspend流程中首先判断当前状态是否为SUSPEND_REQUESTED,如果是则置位SUSPENDED标志,如果不是则取消suspend流程;然后遍历浅度休眠链表,从链表头部到尾部依次调用各节点的suspend()函数,执行完后判断当前状态是否为SUSPEND_REQUESTED_AND_SUSPENDED,如果是则释放main_wake_lock,当前系统中如果只存在main_wake_lock这个有效锁,则会在wake_unlock()里面启动深度休眠线程,如果还有其他其他wake_lock则保持当前状态。

    [cpp] view plaincopy
     
    1. static void late_resume(struct work_struct *work)  
    2. {  
    3.     struct early_suspend *pos;  
    4.     unsigned long irqflags;  
    5.     int abort = 0;  
    6.   
    7.     mutex_lock(&early_suspend_lock);  
    8.     spin_lock_irqsave(&state_lock, irqflags);  
    9.     if (state == SUSPENDED)  // 清除浅度休眠完成标志  
    10.         state &= ~SUSPENDED;  
    11.     else  
    12.         abort = 1;  
    13.     spin_unlock_irqrestore(&state_lock, irqflags);  
    14.   
    15.     if (abort) {  
    16.         if (debug_mask & DEBUG_SUSPEND)  
    17.             pr_info("late_resume: abort, state %d ", state);  
    18.         goto abort;  
    19.     }  
    20.     if (debug_mask & DEBUG_SUSPEND)  
    21.         pr_info("late_resume: call handlers ");  
    22.     // 反向遍历浅度休眠链表并执行其中所有resume函数  
    23.     // 执行顺序根据优先等级而定,等级越高越先执行  
    24.     list_for_each_entry_reverse(pos, &early_suspend_handlers, link)  
    25.         if (pos->resume != NULL)  
    26.             pos->resume(pos);  
    27.     if (debug_mask & DEBUG_SUSPEND)  
    28.         pr_info("late_resume: done ");  
    29. abort:  
    30.     mutex_unlock(&early_suspend_lock);  
    31. }  

    在resume流程中同样首先判断当前状态是否为SUSPENDED,如果是则清除SUSPENDED标志,然后反向遍历浅度休眠链表,按照优先等级从高到低的顺序执行节点的resume()函数。

  • 相关阅读:
    MySQL 内存溢出
    使用pt-fifo-split 工具往mysql插入海量数据
    Summary: Calculate average where sum exceed double limits
    Pocket Gem OA: Log Parser
    Pocket Gem OA: Path Finder
    Leetcode: Sliding Window Median
    Leetcode: Number Complement
    FB面经 Prepare: Even Tree
    FB面经 Prepare: All Palindromic Substrings
    FB面经 Prepare: Largest Island
  • 原文地址:https://www.cnblogs.com/rzq232/p/3835896.html
Copyright © 2011-2022 走看看