Android Suspend/resume 過程分析.

來源:互聯網
上載者:User

 

當系統未處於 Suspend 狀態下使用者按下Power鍵時會在 /dev/input/event0 節點中產生一個訊號, 上層的 WindowManager 會收到這個上節點的變化而得知當前應該進入休眠狀態, 通知PowerManagerService, 它會做如下調用,

private int setScreenStateLocked(boolean on) {        int err = Power.setScreenState(on);

setScreenState 最終會調用到.

intset_screen_state(int on){enum {    ACQUIRE_PARTIAL_WAKE_LOCK = 0,    RELEASE_WAKE_LOCK,    REQUEST_STATE,    OUR_FD_COUNT};const char * const OLD_PATHS[] = {    "/sys/android_power/acquire_partial_wake_lock",    "/sys/android_power/release_wake_lock",    "/sys/android_power/request_state"};const char * const NEW_PATHS[] = {    "/sys/power/wake_lock",    "/sys/power/wake_unlock",    "/sys/power/state"};    QEMU_FALLBACK(set_screen_state(on));    LOGI("*** set_screen_state %d", on);    initialize_fds();    //LOGI("go_to_sleep eventTime=%lld now=%lld g_error=%s\n", eventTime,      //      systemTime(), strerror(g_error));    if (g_error) return g_error;    char buf[32];    int len;    if(on)        len = sprintf(buf, on_state);    else        len = sprintf(buf, off_state);    len = write(g_fds[REQUEST_STATE], buf, len);    if(len < 0) {        LOGE("Failed setting last user activity: g_error=%d\n", g_error);    }    return 0;}

這裡向 /sys/power/state 結點寫入了 on 或 mem, 或都有使用者直接操作 # echo standby > /sys/power/state 時. 核心調用 state_store 函數在(具體過程未查) ./kernel/power/main.c 檔案中. 在其中有如下代碼

#ifdef CONFIG_EARLYSUSPEND  if (state == PM_SUSPEND_ON || valid_state(state)) {   error = 0;   request_suspend_state(state);  }#else  error = enter_state(state);#endif

可以看出.如果定義了 CONFIG_EARLYSUSPEND 的話. 會調用 request_suspend_state 函數在 ./kernel/power/earlysuspend.c 檔案中. 其中有關鍵代碼如下

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 (!old_sleep && new_state != PM_SUSPEND_ON) {  state |= SUSPEND_REQUESTED;  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);}

這雷根據不同的 new_state 的不同而將不同的任務添加到工作隊列中. 現在分析 suspend 的情況 early_suspend 主要工作如下

suspend_state_t requested_suspend_state = PM_SUSPEND_MEM;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; else  abort = 1; spin_unlock_irqrestore(&state_lock, irqflags); if (abort) {  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"); 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"); 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);}

在這裡調用了事先註冊的 early_syspend , 同步, 釋放 main_wake_lock, 在釋放 main_wake_lock 時.如下操作.

static void suspend(struct work_struct *work){ int ret; int entry_event_num; if (has_wake_lock(WAKE_LOCK_SUSPEND)) {  if (debug_mask & DEBUG_SUSPEND)   pr_info("suspend: abort suspend\n");  return; } entry_event_num = current_event_num; sys_sync(); if (debug_mask & DEBUG_SUSPEND)  pr_info("suspend: enter suspend\n"); ret = pm_suspend(requested_suspend_state); if (debug_mask & DEBUG_EXIT_SUSPEND) {  struct timespec ts;  struct rtc_time tm;  getnstimeofday(&ts);  rtc_time_to_tm(ts.tv_sec, &tm);  pr_info("suspend: exit suspend, ret = %d "   "(%d-%02d-%02d %02d:%02d:%02d.%09lu UTC)\n", ret,   tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,   tm.tm_hour, tm.tm_min, tm.tm_sec, ts.tv_nsec); } if (current_event_num == entry_event_num) {  if (debug_mask & DEBUG_SUSPEND)   pr_info("suspend: pm_suspend returned with no event\n");  wake_lock_timeout(&unknown_wakeup, HZ / 2); }}static DECLARE_WORK(suspend_work, suspend);void wake_unlock(struct wake_lock *lock){ int type; unsigned long irqflags; spin_lock_irqsave(&list_lock, irqflags); type = lock->flags & WAKE_LOCK_TYPE_MASK;#ifdef CONFIG_WAKELOCK_STAT wake_unlock_stat_locked(lock, 0);#endif if (debug_mask & DEBUG_WAKE_LOCK)  pr_info("wake_unlock: %s\n", lock->name); lock->flags &= ~(WAKE_LOCK_ACTIVE | WAKE_LOCK_AUTO_EXPIRE); list_del(&lock->link); list_add(&lock->link, &inactive_locks); if (type == WAKE_LOCK_SUSPEND) {  long has_lock = has_wake_lock_locked(type);  if (has_lock > 0) {   if (debug_mask & DEBUG_EXPIRE)    pr_info("wake_unlock: %s, start expire timer, "     "%ld\n", lock->name, has_lock);   mod_timer(&expire_timer, jiffies + has_lock);  } else {   if (del_timer(&expire_timer))    if (debug_mask & DEBUG_EXPIRE)     pr_info("wake_unlock: %s, stop expire "      "timer\n", lock->name);   if (has_lock == 0){    queue_work(suspend_work_queue, &suspend_work);   }  }  if (lock == &main_wake_lock) {   if (debug_mask & DEBUG_SUSPEND)    print_active_locks(WAKE_LOCK_SUSPEND);#ifdef CONFIG_WAKELOCK_STAT   update_sleep_wait_stats_locked(0);#endif  } } spin_unlock_irqrestore(&list_lock, irqflags);}

在釋放 main_wake_lock 時, 要判斷 當沒有 這個類型 鎖的時候 要將 suspend_work 放到工作隊列中. 在 supsend 函數中 調用 了正常 suspend 的入口函數.ret = pm_suspend(requested_suspend_state);
在 pm_suspend 函數中

/** * suspend_enter - enter the desired system sleep state. * @state:  state to enter * * This function should be called after devices have been suspended. */static int suspend_enter(suspend_state_t state){ int error; if (suspend_ops->prepare) {  error = suspend_ops->prepare();  if (error)   return error; } error = dpm_suspend_noirq(PMSG_SUSPEND); if (error) {  printk(KERN_ERR "PM: Some devices failed to power down\n");  goto Platfrom_finish; } if (suspend_ops->prepare_late) {  error = suspend_ops->prepare_late();  if (error)   goto Power_up_devices; } if (suspend_test(TEST_PLATFORM))  goto Platform_wake; error = disable_nonboot_cpus(); if (error || suspend_test(TEST_CPUS))  goto Enable_cpus; arch_suspend_disable_irqs(); BUG_ON(!irqs_disabled()); error = sysdev_suspend(PMSG_SUSPEND); if (!error) {  if (!suspend_test(TEST_CORE))   error = suspend_ops->enter(state);  sysdev_resume(); } arch_suspend_enable_irqs(); BUG_ON(irqs_disabled()); Enable_cpus: enable_nonboot_cpus(); Platform_wake: if (suspend_ops->wake)  suspend_ops->wake(); Power_up_devices: dpm_resume_noirq(PMSG_RESUME); Platfrom_finish: if (suspend_ops->finish)  suspend_ops->finish(); return error;}/** * suspend_prepare - Do prep work before entering low-power state. * * This is common code that is called for each state that we're entering. * Run suspend notifiers, allocate a console and stop all processes. */static int suspend_prepare(void){        ... .... if (!suspend_ops || !suspend_ops->enter)  return -EPERM;        ... .... error = pm_notifier_call_chain(PM_SUSPEND_PREPARE); if (error)  goto Finish; error = usermodehelper_disable(); if (error)  goto Finish; error = suspend_freeze_processes(); if (!error)  return 0; suspend_thaw_processes(); usermodehelper_enable(); Finish: pm_notifier_call_chain(PM_POST_SUSPEND); pm_restore_console(); return error;}/** * suspend_devices_and_enter - suspend devices and enter the desired system *        sleep state. * @state:    state to enter */int suspend_devices_and_enter(suspend_state_t state){ int error; if (!suspend_ops)  return -ENOSYS; if (suspend_ops->begin) {  error = suspend_ops->begin(state);  if (error)   goto Close; } //suspend_console(); suspend_test_start(); error = dpm_suspend_start(PMSG_SUSPEND); if (error) {  printk(KERN_ERR "PM: Some devices failed to suspend\n");  goto Recover_platform; } suspend_test_finish("suspend devices"); if (suspend_test(TEST_DEVICES)){  goto Recover_platform; } suspend_enter(state); Resume_devices: suspend_test_start(); dpm_resume_end(PMSG_RESUME); suspend_test_finish("resume devices"); resume_console(); Close: if (suspend_ops->end){  suspend_ops->end(); } return error; Recover_platform: if (suspend_ops->recover){  suspend_ops->recover(); } goto Resume_devices;}/** * suspend_finish - Do final work before exiting suspend sequence. * * Call platform code to clean up, restart processes, and free the * console that we've allocated. This is not called for suspend-to-disk. */static void suspend_finish(void){ suspend_thaw_processes(); usermodehelper_enable(); pm_notifier_call_chain(PM_POST_SUSPEND); pm_restore_console();}/** * enter_state - Do common work of entering low-power state. * @state:  pm_state structure for state we're entering. * * Make sure we're the only ones trying to enter a sleep state. Fail * if someone has beat us to it, since we don't want anything weird to * happen when we wake up. * Then, do the setup for suspend, enter the state, and cleaup (after * we've woken up). */int enter_state(suspend_state_t state){ int error; if (!valid_state(state))  return -ENODEV; if (!mutex_trylock(&pm_mutex))  return -EBUSY; printk(KERN_INFO "PM: Syncing filesystems ... 1"); sys_sync(); printk("done.\n"); pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]); error = suspend_prepare(); if (error)  goto Unlock; if (suspend_test(TEST_FREEZER))  goto Finish; pr_debug("PM: Entering %s sleep\n", pm_states[state]); error = suspend_devices_and_enter(state); Finish: pr_debug("PM: Finishing wakeup.\n"); suspend_finish(); Unlock: mutex_unlock(&pm_mutex); return error;}/** * pm_suspend - Externally visible function for suspending system. * @state:  Enumerated value of state to enter. * * Determine whether or not value is within range, get state * structure, and enter (above). */int pm_suspend(suspend_state_t state){ if (state > PM_SUSPEND_ON && state <= PM_SUSPEND_MAX)  return enter_state(state); return -EINVAL;}

pm_suspend -> enter_state(之後和標準 Linux 過程一致) -> suspend_prepare/suspend_devices_and_enter/suspend_finish

在 suspend_prepare 函數中 分別通知上層已經進入 "PM_SUSPEND_PREPARE" 過程讓上層做一些處理. 之後凍結使用者層所有應用程式及服務進程.

suspend_devices_and_enter 函數中 進程休眠, 裝置(驅動)休眠. cpu進入休眠. 停止在 suspend_ops->enter(state);
在arch/xxx/mach-xxx/pm.c 中的pm_cpu_sleep。來電 cpu上電後, 使能中斷.使能非活動的Cpu. 複位驅動, 啟用進程.

suspend_finish 函數中 同樣通知上層 PM_POST_SUSPEND 這個訊息. 上層收到這個訊息後.會調用 NvddkAudioFxSuspend(NV_FALSE); 然後再幹什麼就沒跟住了...
這裡同進入suspend 一致, 會把一個按鍵事件寫入到 /dev/input/event0 中, 上層應用程式被啟用後會檢測這個事件來源, 發現有按下, 會使能螢幕, 這時同 suspend 過程一樣, 在使用者層的最下端寫一個 "on" 到 /sys/power/state 中, 核心層中調用 state_store -> request_suspend_state 這裡會把一個 resume 的工作加入到 工作隊列中. 調用了 已經註冊的 late_resume 函數. 這裡 eraly_suspend 及
late_resume 都是 android 加的補丁!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.