monkey测试中如果不添加--kill-process-after-error 参数,在系统关键进程存在block的情况导致watchdog的时候会导致定屏而非重启,原因如下:
在watchdog.java里面的run函数里,会做检测,如果controller不为空,说明有monkey,所以要将该信息回报给monkey,所以会调用monkey的systemNotResponding函数
watchdog.java:
synchronized (this) {
controller = mController;
}
if (controller != null) {
Slog.i(TAG, "Reporting stuck state to activity controller");
try {
Binder.setDumpDisabled("Service dumps disabled due to hung system process.");
// 1 = keep waiting, -1 = kill system
int res = controller.systemNotResponding(subject);
if (res >= 0) {
Slog.i(TAG, "Activity controller requested to coninue to wait");
waitedHalf = false;
continue;
}
} catch (RemoteException e) {
}
}
monkey.java里在systemNotResponding这个函数里,如果加了--kill-process-after-error函数后,就会将mKillProcessAfterError设置为true
} else if (opt.equals("--kill-process-after-error")) {
mKillProcessAfterError = true;
如果mKillProcessAfterError = true monkey的 systemNotResponding 的函数就会返回-1, 后面watchdog就可以正常走了。反之,返回1的话走到wathdog的run里面,res就会大于0,然后就会打印"Activity controller requested to coninue to wait"到 system log里,然后将 waitedHalf = false continue了,就不会走watchdog kill的流程了,就会定屏了。
public int systemNotResponding(String message) {
StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskWrites();
System.err.println("// WATCHDOG: " + message);
StrictMode.setThreadPolicy(savedPolicy);
synchronized (Monkey.this) {
if (!mIgnoreCrashes) {
mAbort = true;
}
if (mRequestBugreport) {
mRequestWatchdogBugreport = true;
}
mWatchdogWaiting = true;
}
synchronized (Monkey.this) {
while (mWatchdogWaiting) {
try {
Monkey.this.wait();
} catch (InterruptedException e) {
}
}
}
return (mKillProcessAfterError) ? -1 : 1;
}