1. Android Application 如何使用wakelock
a. PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);-->getSystemService@frameworks/base/core/java/android/view/contextthemewrapper.java-->getSystemService和getPowerManager@frameworks/base/core/java/android/app/contextimpl.java
b. mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "SoundRecorder");
PARTIAL_WAKE_LOCK:保持CPU 运转,屏幕和键盘灯有可能是关闭的。
SCREEN_DIM_WAKE_LOCK:保持CPU 运转,允许保持屏幕显示但有可能是灰的,允许关闭键盘灯
SCREEN_BRIGHT_WAKE_LOCK:保持CPU 运转,允许保持屏幕高亮显示,允许关闭键盘灯
FULL_WAKE_LOCK:保持CPU 运转,保持屏幕高亮显示,键盘灯也保持亮度
ACQUIRE_CAUSES_WAKEUP:一旦有请求锁时强制打开Screen和keyboard light
ON_AFTER_RELEASE: 在释放锁时reset activity timer
c. newWakeLock@frameworks/base/core/java/android/os/powermanager.java
d. mWakeLock.acquire();-->acquire@frameworks/base/core/java/android/os/powermanager.java-->acquireWakeLock和acquireWakeLockLocked@frameworks/base/services/java/com/android/server/powermanagerservice.java;
2.
[email protected]和
[email protected] 两者如何关联上???
a. 在framework/base/services/java/com/android/server/systemserver.java中有如下代码
power = new PowerManagerService();
ServiceManager.addService(Context.POWER_SERVICE, power);//把power service添加到servicemanager中管理
b. 在framework/base/core/java/android/app/contextimpl.java中有如下代码
IBinder b = ServiceManager.getService(POWER_SERVICE);
IPowerManager service = IPowerManager.Stub.asInterface(b);
sPowerManager = new PowerManager(service, mMainThread.getHandler());
所以 PowerManager.java中的mService就是PowerManagerService, 具体的实现原理还需要进一步分析java的语法
3. 在PowerManagerService.java中,mLocks保存的是所有WakeLock,该WakeLock不是以tag来区分的,也就是一个SCREEN_DIM_WAKE_LOCK tag,可以对应多个WakeLock;注意PowerManager.java中的WakeLock和PowerManagerService.java中的WakeLock不是一回事
[email protected]
4. 如何go to sleep
5. frameworks/base/core/java/android/os/Power.java中的WakeLock只有PARTIAL_WAKE_LOCK和FULL_WAKE_LOCK两种类型,而powermanager.java中却有PARTIAL_WAKE_LOCK、FULL_WAKE_LOCK、等状态,它们如何对应上?
6. PowerManagerService.java 似乎之关心PARTIAL_WAKE_LOCK类型的WakeLock, 只有代码Power.acquireWakeLock(Power.PARTIAL_WAKE_LOCK,PARTIAL_NAME) = Power.acquireWakeLock(2,"PowerManagerService");另外也说明没有延时wakelock被使用
7. [JNI]acquireWakeLock@frameworks/base/core/jni/android_os_power.cpp-->acquire_wake_lock@hardware/libhardware_legacy/power/power.c; write操作的是"/sys/power/wake_lock"文件
8. [Kernel] kernel/kernel/power/main.c中sysfs_create_group(power_kobj, &attr_group); 会创建sys文件接口[/sys/power/wake_lock],其中write操作对应wake_lock_store@kernel/kernel/power/userwakelock.c-->lookup_wake_lock_name[如果不存在该名称的wakelock,会先创建个在添加到user_wake_locks 队列中,并且添加的都是WAKE_LOCK_SUSPEND类型的wakelock]-->wake_lock和wake_lock_internal@kernel/kernel/power/wakelock.c,在wake_lock_internal函数中会把该wakelock添加到active_wake_locks[type=WAKE_LOCK_SUSPEND]中,WAKE_LOCK_SUSPEND表示不能让手机suspend,这个list链表只增不减??不是,在wake_unlock中有list_del(&lock->link);
8. [Kernel] 如果是有时间延时的wakelock,会启动一个timer,也就是通过一个timer来实现延时wakelock的操作,在wake_lock_internal@kernel/kernel/power/wakelock.c中有mod_timer(&expire_timer, jiffies + expire_in);
9. 系统suspend如何与wakelock关联起来的???
10. kernel/kernel/power/main.c中会创建pm_wq = create_freezeable_workqueue("pm");
11. 谁调用??suspend@kernel/kernel/power/wakelock.c --> has_wake_lock-->has_wake_lock_locked[active_wake_locks只要里面有wakelock就不能suspend],也就是说linux是通过一个list来管理wakelock,然后决定是否suspend.
12. User Space写入standby或者mem到"sys/power/state",会调用state_store@kernel/kernel/power/main.c-->request_suspend_state(state)@kernel/kernel/power/earlysuspend.c{其中state为PM_SUSPEND_STANDBY,PM_SUSPEND_MEM},如果需要suspend则会调用queue_work(suspend_work_queue, &early_suspend_work)-->early_suspend@kernel/kernel/power/earlysuspend.c 和 pos->suspend(pos); -->wake_unlock(&main_wake_lock)@kernel/kernel/power/wakelock.c 其中wake_lock_init(&main_wake_lock, WAKE_LOCK_SUSPEND, "main");如果可以进入suspend状态则会调用suspend(..) -->pm_suspend@kernel/kernel/power/suspend.c-->suspend_prepare和suspend_devices_and_enter-->suspend_console-> 继续分析??????
13. 其中pm_suspend(requested_suspend_state)中的requested_suspend_state 状态是如何确定的??? 在state_show和state_store@kernel/kernel/power/main.c中,查看手机#cat sys/power/state 为“standby mem”只支持这两种模式? {'standby' (Power-On Suspend), 'mem' (Suspend-to-RAM), and'disk' (Suspend-to-Disk).}
14. 所有需要early suspend的设备需要预先用register_early_suspend@kernel/kernel/power/earlysuspend.c 注册到early_suspend_handlers中