android休眠唤醒机制二

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

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

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

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

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

  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.     int error = pm_start_workqueue(); 
  22.     if (error) 
  23.         return error; 
  24.     power_kobj = kobject_create_and_add("power", NULL);  // 创建power节点 
  25.     if (!power_kobj) 
  26.         return -ENOMEM; 
  27.     return sysfs_create_group(power_kobj, &attr_group);  // 创建一组属性节点 
  28.  
  29. core_initcall(pm_init); 

二、early_suspend 实现

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

  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:

  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的使用方式如下:

  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需要用到的一些数据:

  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

  1. void register_early_suspend(struct early_suspend *handler) 
  2.     struct list_head *pos; 
  3.  
  4.     mutex_lock(&early_suspend_lock); 
  5.     // 遍历浅度休眠链表 
  6.     list_for_each(pos, &early_suspend_handlers) { 
  7.         struct early_suspend *e; 
  8.         e = list_entry(pos, struct early_suspend, link); 
  9.         // 判断当前节点的优先等级是否大于handler的优先等级 
  10.         // 以此决定handler在链表中的顺序 
  11.         if (e->level > handler->level) 
  12.             break
  13.     } 
  14.     // 将handler加入当前节点之前,优先等级越低越靠前 
  15.     list_add_tail(&handler->link, pos); 
  16.     if ((state & SUSPENDED) && handler->suspend) 
  17.         handler->suspend(handler); 
  18.     mutex_unlock(&early_suspend_lock); 
  19. EXPORT_SYMBOL(register_early_suspend); 
注册的流程比较简单,首先遍历链表,依次比较每个节点的优先等级,如果遇到优先等级比新节点优先等级高则跳出,然后将新节点加入优先等级较高的节点前面,这样就确保了链表是优先等级低在前高在后的顺序;在将节点加入链表后查看当前状态是否为浅度休眠完成状态,如果是则执行handler的suspend函数。

  1. void unregister_early_suspend(struct early_suspend *handler) 
  2.     mutex_lock(&early_suspend_lock); 
  3.     list_del(&handler->link); 
  4.     mutex_unlock(&early_suspend_lock); 
  5. EXPORT_SYMBOL(unregister_early_suspend); 
注销流程则只是将节点从链表中移除。
4、request_suspend_state

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

  1. void request_suspend_state(suspend_state_t new_state) 
  2.     unsigned long irqflags; 
  3.     int old_sleep; 
  4.  
  5.     spin_lock_irqsave(&state_lock, irqflags); 
  6.     old_sleep = state & SUSPEND_REQUESTED; 
  7.     // 打印当前状态 
  8.     if (debug_mask & DEBUG_USER_STATE) { 
  9.         struct timespec ts; 
  10.         struct rtc_time tm
  11.         getnstimeofday(&ts); 
  12.         rtc_time_to_tm(ts.tv_sec, &tm); 
  13.         pr_info("request_suspend_state: %s (%d->%d) at %lld " 
  14.             "(%d-%02d-%02d %02d:%02d:%02d.%09lu UTC)\n"
  15.             new_state != PM_SUSPEND_ON ? "sleep" : "wakeup"
  16.             requested_suspend_state, new_state, 
  17.             ktime_to_ns(ktime_get()), 
  18.             tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, 
  19.             tm.tm_hour, tm.tm_min, tm.tm_sec, ts.tv_nsec); 
  20.     } 
  21.     // 如果新状态是休眠状态 
  22.     if (!old_sleep && new_state != PM_SUSPEND_ON) { 
  23.         state |= SUSPEND_REQUESTED; 
  24.         pr_info("sys_sync_work_queue early_sys_sync_work.\n"); 
  25.         // 执行缓存同步与浅度休眠的工作队列 
  26.         queue_work(sys_sync_work_queue, &early_sys_sync_work); 
  27.         queue_work(suspend_work_queue, &early_suspend_work); 
  28.     } else if (old_sleep && new_state == PM_SUSPEND_ON) { 
  29.     // 如果新状态是唤醒状态 
  30.         state &= ~SUSPEND_REQUESTED; 
  31.         // 激活内核锁 
  32.         wake_lock(&main_wake_lock); 
  33.         // 执行浅度唤醒的工作队列 
  34.         queue_work(suspend_work_queue, &late_resume_work); 
  35.     } 
  36.     // 更新全局状态 
  37.     requested_suspend_state = new_state; 
  38.     spin_unlock_irqrestore(&state_lock, irqflags); 
函数首先打印出当前状态变化的log,然后判断新状态,如果是休眠状态则置位SUSPEND_REQUESTED标志,然后将同步缓存、浅度休眠工作队列加入相应的内核线程执行;如果新状态是唤醒则首先将main_wake_lock激活,然后再将浅度唤醒工作队列加入内核线程执行;最后更新全局状态变量,因为提供了一个内核空间接口用于获取当前休眠唤醒状态:

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

  1. static void late_resume(struct work_struct *work) 
  2.     struct early_suspend *pos; 
  3.     unsigned long irqflags; 
  4.     int abort = 0; 
  5.  
  6.     mutex_lock(&early_suspend_lock); 
  7.     spin_lock_irqsave(&state_lock, irqflags); 
  8.     if (state == SUSPENDED)  // 清除浅度休眠完成标志 
  9.         state &= ~SUSPENDED; 
  10.     else 
  11.         abort = 1; 
  12.     spin_unlock_irqrestore(&state_lock, irqflags); 
  13.  
  14.     if (abort) { 
  15.         if (debug_mask & DEBUG_SUSPEND) 
  16.             pr_info("late_resume: abort, state %d\n", state); 
  17.         goto abort; 
  18.     } 
  19.     if (debug_mask & DEBUG_SUSPEND) 
  20.         pr_info("late_resume: call handlers\n"); 
  21.     // 反向遍历浅度休眠链表并执行其中所有resume函数 
  22.     // 执行顺序根据优先等级而定,等级越高越先执行 
  23.     list_for_each_entry_reverse(pos, &early_suspend_handlers, link) 
  24.         if (pos->resume != NULL) 
  25.             pos->resume(pos); 
  26.     if (debug_mask & DEBUG_SUSPEND) 
  27.         pr_info("late_resume: done\n"); 
  28. abort: 
  29.     mutex_unlock(&early_suspend_lock); 
在resume流程中同样首先判断当前状态是否为SUSPENDED,如果是则清除SUSPENDED标志,然后反向遍历浅度休眠链表,按照优先等级从高到低的顺序执行节点的resume()函数。

  1. static void early_sys_sync(struct work_struct *work) 
  2.     wake_lock(&sys_sync_wake_lock); 
  3.     sys_sync(); 
  4.     wake_unlock(&sys_sync_wake_lock); 

内核专门为缓存同步建立了一个线程,同时还创建了sys_sync_wake_lock防止在同步缓存时系统进入深度休眠。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值