Android休眠唤醒机制简介(二)

作者:sean

修改历史:

******************************************************************

接上一节,结合code来分析一下:

具体流程

     第一部分:获得wakelock唤醒锁

   比如在应用程序中,当获得wakelock唤醒锁的时候,它首先是调用/android/frameworks/base/core/java/

   android/os/PowerManager类中的public void acquire()方法,而该方法通过android特有的通讯机制,会接着调用到PowerManagerService类中的public void acquireWakeLock。

public void acquireWakeLock(int flags, IBinder lock, String tag, WorkSource ws) {
            int uid = Binder.getCallingUid();
            int pid = Binder.getCallingPid();
            if (uid != Process.myUid()) {
                  mContext.enforceCallingOrSelfPerm ission(android.Manifest.permission.WAKE_LOCK, null);
            }     
            if (ws != null) {
                  enforceWakeSourcePermiss ion(uid, pid);
            }     
            long ident = Binder.clearCallingIdentity();
            try {
                  synchronized (mLocks) {
                        acquireWakeLockLocked(flags, lock, uid, pid, tag, ws); 
                  }     

            } finally {
                  Binder.restoreCallingIdentity(ident);
            }     
}    

而 public void acquireWakeLock方法又调用了acquireWakeLockLocked。

public void acquireWakeLockLocked(int flags, IBinder lock, int uid, int pid, String tag,
                  WorkSource ws)
  {
      if (mSpew) {
        Slog.d(TAG, "acquireWakeLock flags=0x" + Integer.toHexString(flags) + " tag=" + tag); }
            if (ws != null && ws.size() == 0) {ws = null;}
            int index = mLocks.getIndex(lock);
            WakeLock wl;
            boolean newlock;
            boolean diffsource;
            WorkSource oldsource;
          .
                          中间代码省略
         .    
               Power.acquireWakeLock(Power.PARTIAL_WAKE_LOCK,PARTIAL_NAME);
            }
            if (diffsource) {
                  // If the lock sources have changed, need to first release the
                  // old ones.
                  noteStopWakeLocked(wl, oldsource);
            }
            if (newlock || diffsource) {
                  noteStartWakeLocked(wl, ws);
            }
}

我们先看一下android/kernel/kernel/power/main.c中的一段代码,我将会做简单的分析,之后你就会明白刚才上面所产生的疑问了。

#ifdef CONFIG_USER_WAKELOCK
power_attr(wake_lock);
power_attr(wake_unlock);
#endif

static struct attribute * g[] = {
&state_attr.attr,
#ifdef CONFIG_PM_TRACE
&pm_trace_attr.attr,
#endif

#ifdef CONFIG_PM_SLEEP
&pm_async_attr.attr,
#ifdef CONFIG_PM_DEBUG
&pm_test_attr.attr,
#endif

#ifdef CONFIG_USER_WAKELOCK
&wake_lock_attr.attr,
&wake_unlock_attr.attr,
#endif

#endif
NULL,
};

static struct   attribute_group   attr_group = {
.attrs = g,
};

#ifdef CONFIG_PM_RUNTIME
struct workqueue_struct *pm_wq;
EXPORT_SYMBOL_GPL(pm_wq);

static int __init pm_start_workqueue(void)
{
pm_wq = create_freezeable_workqueue("pm");
return pm_wq ? 0 : -ENOMEM;
}
#else
static inline int pm_start_workqueue(void) { return 0; }
#endif
static int __init pm_init(void)
{
int error = pm_start_workqueue();
if (error)
return error;
power_kobj = kobject_create_and_add("power", NULL);
if (!power_kobj)
return -ENOMEM;
return sysfs_create_group(power_kobj, &attr_group);
}
core_initcall(pm_init);

    

#define power_attr(_name) \
static struct kobj_attribute _name##_attr = {\
.attr = { \
.name = __stringify(_name),\
.mode = 0644, \
}, \
.show = _name##_show,\
.store = _name##_store,\
}

在该函数中##的作用通俗点讲就是“连接”的意思,比如power_attr(wake_lock),

static struct kobj_attribute   wake_lock_attr = {\
.attr = { \
.name = __stringify(wake_lock),\
.mode = 0644, \
}, \
.show = wake_lock_show, \
.store = wake_lock_store, \
}

     函数wake_lock_store和wake_lock_show就定义在android/kernel/kernel/power/userwakelock.c 中。因此当我们对/sys/power/wake_lock进行操作的时候就会调用到userwakelock.c中定义的wake_lock_store()函数。

       好了,我们该回到原来我们产生疑问的地方了,在 power.c中我们将重新研究一下这这段代码,这时我们还得关注其中的另一个函数initialize_fds()。

int   acquire_wake_lock(int lock, const char* id)
{
//      LOGI("acquire_wake_lock lock=%d id='%s'\n", lock, id);
 return write(fd, id, strlen(id));
}
initialize_fds(void)
{ 
}
const char * const NEW_PATHS[] = { 
};
ssize_t   wake_lock_store(
{
           struct user_wake_lock *l;        
if (debug_mask & DEBUG_ACCESS)                  
bad_name:      
}
static void wake_lock_internal(struct wake_lock *lock, long timeout, int has_timeout)
{
int type;
unsigned long irqflags;
long expire_in;
spin_lock_irqsave(&list_lock, irqflags);
type = lock->flags & WAKE_LOCK_TYPE_MASK;
BUG_ON(type >= WAKE_LOCK_TYPE_COUNT);
BUG_ON(!(lock->flags & WAKE_LOCK_INITIALIZED));
#ifdef CONFIG_WAKELOCK_STAT
if (type == WAKE_LOCK_SUSPEND && wait_for_wakeup) {
if (debug_mask & DEBUG_WAKEUP)
pr_info("wakeup wake lock: %s\n", lock->name);
wait_for_wakeup = 0;
lock->stat.wakeup_count++;
}

if ((lock->flags & WAKE_LOCK_AUTO_EXPIRE) &&
    
wake_unlock_stat_locked(lock, 0);
lock->stat.last_time = ktime_get();
}
#endif
if (!(lock->flags & WAKE_LOCK_ACTIVE)) {
lock->flags |= WAKE_LOCK_ACTIVE;
#ifdef CONFIG_WAKELOCK_STAT
lock->stat.last_time = ktime_get();
#endif
}
list_del(&lock->link);
if (has_timeout) {
if (debug_mask & DEBUG_WAKE_LOCK)
pr_info("wake_lock: %s, type %d, timeout %ld.lu\n",
lock->name, type, timeout / HZ,
(timeout % HZ) * MSEC_PER_SEC / HZ);
lock->expires = jiffies + timeout;
lock->flags |= WAKE_LOCK_AUTO_EXPIRE;
list_add_tail(&lock->link, &active_wake_locks[type]);
} else {
if (debug_mask & DEBUG_WAKE_LOCK)
pr_info("wake_lock: %s, type %d\n", lock->name, type);
lock->expires = LONG_MAX;
lock->flags &= ~WAKE_LOCK_AUTO_EXPIRE;
list_add(&lock->link, &active_wake_locks[type]);
}
if (type == WAKE_LOCK_SUSPEND) {
current_event_num++;
#ifdef CONFIG_WAKELOCK_STAT
if (lock == &main_wake_lock)
update_sleep_wait_stats_locked(1);
else if (!wake_lock_active(&main_wake_lock))
update_sleep_wait_stats_locked(0);
#endif
if (has_timeout)
expire_in = has_wake_lock_locked(type);
else
expire_in = -1;
if (expire_in > 0) {
if (debug_mask & DEBUG_EXPIRE)
pr_info("wake_lock: %s, start expire timer, "
"%ld\n", lock->name, expire_in);
mod_timer(&expire_timer, jiffies + expire_in);
} else {
if (del_timer(&expire_timer))
if (debug_mask & DEBUG_EXPIRE)
pr_info("wake_lock: %s, stop expire timer\n",
lock->name);
if (expire_in == 0)
queue_work(suspend_work_queue, &suspend_work);
}
}
spin_unlock_irqrestore(&list_lock, irqflags);
}

    到这里为止,我们走的第一条路就到目的地了,这个函数具体做了什么,在这里就不仔细分析了,大家可以自己再跟下或者上网查相关资料,理解这个函数不难。

     第二部分:系统进入睡眠

有了上面第一部分的学习,再看第二部分的话,会容易很多。假如现在我们按了PAD上的power睡眠键,经过一些列的事件处理后,它会调用到PowerManager类中的

  public void goToSleep(long time) 
     {    
            try {
                  mService.goToSleep(time);
            } catch (RemoteException e) {
            }    
      }

而该函数会调用到PowerManagerService类中的public void goToSleep()方法;

         public void goToSleep(long time)
         {
            goToSleepWithReason(time, WindowManagerPolicy.OFF_BECAUSE_OF_USER);
     }

       goToSleepWithReason()会调用goToSleepLocked()方法,接着会调用setPowerState();而setPowerState()方法里会调用setScreenStateLocked(),setScreenStateLocked()又会调用到Power类中的JNI接口setScreenState(),其具体实现是在android_os_Power.cpp文件中; 

         static int setScreenState(JNIEnv *env, jobject clazz, jboolean on) 
        {
               return set_screen_state(on);
        }

 函数中return set_screen_state()的实现是android/hardware/libhardware_legacy/power/power.c

      set_screen_state(int on)
{
      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 = snprintf(buf, sizeof(buf), "%s", on_state);
      else
            len = snprintf(buf, sizeof(buf), "%s", off_state);
      buf[sizeof(buf) - 1] = '\0';
      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(android/hardware/libhardware_legacy/power/power.c)进行读写操作的时候,(linux/kernel/power/main.c)中的state_store()函数会被调用,在该函数中会分成两个分支:

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;
            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);
                        }
#else
                        error = enter_state(state);
#endif
#endif
Exit:
            return error ? error : n;
}  

                                                

Android特有的earlysuspend: request_suspend_state(state)

Linux标准的suspend:         


注意:如果CONFIG_EARLYSUSPEND宏开的话,kernel会先走earlysuspend,反之则直接走suspend;从这里开始就要分两个分支了,如果支持earlysuspend的话就进入 request_suspend_state(state)函数,如果不支持的话就进入标准Linux的enter_state(state)函数。、


这两个函数分别在两个文件中kernel/kernel/power/earlysuspend.c和suspend.c。现在再回过头来看的话,感觉整个android中睡眠唤醒机制还是很清晰的。这两个函数体里又做了什么,在这里就不再做具体分析,大家可以自己对照代码或者上网查资料,因为本文的主旨是带读者从最上层应用层一直到最底层kernel层,把整个android的睡眠唤醒机制给走通。

PowerManager.java                                    
PowerManagerService.java                        
PowerManagerService.java                  
PowerManagerService.java                        
PowerManagerService.java                  
Power.java                                          
android_os_Power.cpp                           
power.c                                                   set_screen_state( )
main.c