Saturday, May 9, 2009

Android Suspend and resume (the wakelock problem)

I enabled wakelock in my Android kernel today, after the wakelock is enabled on my PC, my machine failed to wake up. It seems that the machine gets back to suspend right after it is waken up.
I spent two hours to read the wakelock code and try to find out the reason for the problem. It seems that the wakelock.c checks a flag called entry_event_num during the machine resume process. The value of entry_event_num is saved before the machine goes into sleep, and wakelock checks it during the machine resume. If the value of entry_event_num did not changed since machine went to sleep, then wakelock will put the machine back to sleep again.
It looks like that the entry_event_num flag is used to verify whether the machnie is waken up from an expected event. If it is not, then machine will continue to sleep. To me this is more like a hack for a hardware bug.
So I hacked it to make my PC to sleep well and wake up on time :-). Here is the code I have changed:
diff --git a/kernel/power/wakelock.c b/kernel/power/wakelock.c
index 7833d28..175f8a3 100644
--- a/kernel/power/wakelock.c
+++ b/kernel/power/wakelock.c
@@ -292,11 +292,13 @@ static void suspend(struct work_struct *work)
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
tm.tm_hour, tm.tm_min, tm.tm_sec, ts.tv_nsec);
+#if 0
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);

Actually, the wakelock is really not a good idea. From my current understanding, if one of the application hold a wakelock without timeout and dead, the machine may not be able to go to sleep forever until you reboot your machine.


  1. I am glad to apprehend the accomplished agreeable of this blog and am actual aflame and blessed to say that the webmaster has done a actual acceptable job actuality to put all the advice agreeable and advice at one place.

    Android app developer

  2. I am quite amazed with your writing skills as well as with the layout on your weblog being all black. Pretty cool! check it out