本文轉(zhuǎn)載自Gityuan的博客,原文地址為:http://gityuan.com/2016/02/01/android-booting/ 轉(zhuǎn)載請(qǐng)注明作者及原文鏈接。
基于Android 6.0的源碼剖析, Android啟動(dòng)過(guò)程概述
Android系統(tǒng)底層基于Linux Kernel, 當(dāng)Kernel啟動(dòng)過(guò)程會(huì)創(chuàng)建init進(jìn)程, 該進(jìn)程是所有用戶空間的鼻祖, init進(jìn)程會(huì)啟動(dòng)servicemanager(binder服務(wù)管家), Zygote進(jìn)程(Java進(jìn)程的鼻祖). Zygote進(jìn)程會(huì)創(chuàng)建 system_server進(jìn)程以及各種app進(jìn)程,下圖是這幾個(gè)系統(tǒng)重量級(jí)進(jìn)程之間的層級(jí)關(guān)系。
init是Linux系統(tǒng)中用戶空間的第一個(gè)進(jìn)程(pid=1), Kerner啟動(dòng)后會(huì)調(diào)用/system/core/init/Init.cpp的main()方法.
int main(int argc, char** argv) { ... klog_init(); //初始化kernel log property_init(); //創(chuàng)建一塊共享的內(nèi)存空間,用于屬性服務(wù) signal_handler_init(); //初始化子進(jìn)程退出的信號(hào)處理過(guò)程 property_load_boot_defaults(); //加載/default.prop文件 start_property_service(); //啟動(dòng)屬性服務(wù)器(通過(guò)socket通信) init_parse_config_file("/init.rc"); //解析init.rc文件 //執(zhí)行rc文件中觸發(fā)器為 on early-init的語(yǔ)句 action_for_each_trigger("early-init", action_add_queue_tail); //執(zhí)行rc文件中觸發(fā)器為 on init的語(yǔ)句 action_for_each_trigger("init", action_add_queue_tail); //執(zhí)行rc文件中觸發(fā)器為 on late-init的語(yǔ)句 action_for_each_trigger("late-init", action_add_queue_tail); while (true) { if (!waiting_for_exec) { execute_one_command(); restart_processes(); } int timeout = -1; if (process_needs_restart) { timeout = (process_needs_restart - gettime()) * 1000; if (timeout < 0) timeout = 0; } if (!action_queue_empty() || cur_action) { timeout = 0; } epoll_event ev; //循環(huán) 等待事件發(fā)生 int nr = TEMP_FAILURE_RETRY(epoll_wait(epoll_fd, &ev, 1, timeout)); if (nr == -1) { ERROR("epoll_wait failed: %s\n", strerror(errno)); } else if (nr == 1) { ((void (*)()) ev.data.ptr)(); } } return 0;}
init進(jìn)程的主要功能點(diǎn):
分析和運(yùn)行所有的init.rc文件;
生成設(shè)備驅(qū)動(dòng)節(jié)點(diǎn); (通過(guò)rc文件創(chuàng)建)
處理子進(jìn)程的終止(signal方式);
提供屬性服務(wù)property service。
當(dāng)init解析到下面這條語(yǔ)句,便會(huì)啟動(dòng)Zygote進(jìn)程
service zygote /system/bin/app_process -Xzygote /system/bin --zygote --start-system-server class main //伴隨著main class的啟動(dòng)而啟動(dòng) socket zygote stream 660 root system //創(chuàng)建socket onrestart write /sys/android_power/request_state wake onrestart write /sys/power/state on onrestart restart media //當(dāng)zygote重啟時(shí),則會(huì)重啟media onrestart restart netd // 當(dāng)zygote重啟時(shí),則會(huì)重啟netd
當(dāng)init子進(jìn)程(Zygote)退出時(shí),會(huì)產(chǎn)生SIGCHLD信號(hào),并發(fā)送給init進(jìn)程,通過(guò)socket套接字傳遞數(shù)據(jù),調(diào)用到wait_for_one_process()方法,根據(jù)是否是oneshot,來(lái)決定是重啟子進(jìn)程,還是放棄啟動(dòng)。由于缺省模式oneshot=false,因此Zygote一旦被殺便會(huì)再次由init進(jìn)程拉起.
接下來(lái),便是進(jìn)入了Zygote進(jìn)程.
當(dāng)Zygote進(jìn)程啟動(dòng)后, 便會(huì)執(zhí)行到frameworks/base/cmds/app_process/App_main.cpp文件的main()方法. 整個(gè)調(diào)用流程:
App_main.main AndroidRuntime.start AndroidRuntime.startVm AndroidRuntime.startReg ZygoteInit.main (首次進(jìn)入Java世界) registerZygoteSocket preload startSystemServer runSelectLoop
int main(int argc, char* const argv[]){ AppRuntime runtime(argv[0], computeArgBlockSize(argc, argv)); while (i < argc) { ...//參數(shù)解析 } //設(shè)置進(jìn)程名 if (!niceName.isEmpty()) { runtime.setArgv0(niceName.string()); set_process_name(niceName.string()); } if (zygote) { // 啟動(dòng)AppRuntime,見(jiàn)小節(jié)[3.2] runtime.start("com.android.internal.os.ZygoteInit", args, zygote); } else if (className) { runtime.start("com.android.internal.os.RuntimeInit", args, zygote); }}
在app_process進(jìn)程啟動(dòng)過(guò)程,有兩個(gè)分支:
當(dāng)zygote為true時(shí),則執(zhí)行ZygoteInit.main()
當(dāng)zygote為false時(shí),則執(zhí)行RuntimeInit.main()
[-> AndroidRuntime.cpp]
void AndroidRuntime::start(const char* className, const Vector<String8>& options){ ... // 虛擬機(jī)創(chuàng)建 if (startVm(&mJavaVM, &env, zygote) != 0) { return; } onVmCreated(env); // JNI方法注冊(cè) if (startReg(env) < 0) { return; } ... // 調(diào)用ZygoteInit.main()方法[見(jiàn)小節(jié)3.3] env->CallStaticVoidMethod(startClass, startMeth, strArray);
[–>ZygoteInit.java]
public static void main(String argv[]) { try { ... registerZygoteSocket(socketName); //為Zygote注冊(cè)socket preload(); // 預(yù)加載類和資源[見(jiàn)小節(jié)3.4] ... if (startSystemServer) { startSystemServer(abiList, socketName);//啟動(dòng)system_server[見(jiàn)小節(jié)3.5] } Log.i(TAG, "Accepting command socket connections"); runSelectLoop(abiList); //進(jìn)入循環(huán)模式[見(jiàn)小節(jié)3.6] ... } catch (MethodAndArgsCaller caller) { caller.run(); //啟動(dòng)system_server中會(huì)講到。 } ...}
[–>ZygoteInit.java]
static void preload() { Log.d(TAG, "begin preload"); preloadClasses(); preloadResources(); preloadOpenGL(); preloadSharedLibraries(); WebViewFactory.prepareWebViewInZygote(); Log.d(TAG, "end preload");}
[–>ZygoteInit.java]
private static boolean startSystemServer(String abiList, String socketName) throws MethodAndArgsCaller, RuntimeException { ... // fork子進(jìn)程system_server pid = Zygote.forkSystemServer( parsedArgs.uid, parsedArgs.gid, parsedArgs.gids, parsedArgs.debugFlags, null, parsedArgs.permittedCapabilities, parsedArgs.effectiveCapabilities); ... if (pid == 0) { if (hasSecondZygote(abiList)) { waitForSecondaryZygote(socketName); } //進(jìn)入system_server進(jìn)程[見(jiàn)小節(jié)4.1] handleSystemServerProcess(parsedArgs); } return true;}
[–>ZygoteInit.java]
private static void runSelectLoop(String abiList) throws MethodAndArgsCaller { ArrayList<FileDescriptor> fds = new ArrayList<FileDescriptor>(); ArrayList<ZygoteConnection> peers = new ArrayList<ZygoteConnection>(); //sServerSocket是socket通信中的服務(wù)端,即zygote進(jìn)程 fds.add(sServerSocket.getFileDescriptor()); peers.add(null); while (true) { StructPollfd[] pollFds = new StructPollfd[fds.size()]; for (int i = 0; i < pollFds.length; ++i) { pollFds[i] = new StructPollfd(); pollFds[i].fd = fds.get(i); pollFds[i].events = (short) POLLIN; } ... Os.poll(pollFds, -1); for (int i = pollFds.length - 1; i >= 0; --i) { //采用I/O多路復(fù)用機(jī)制,當(dāng)客戶端發(fā)出 連接請(qǐng)求或者數(shù)據(jù)處理請(qǐng)求時(shí),則執(zhí)行continue if ((pollFds[i].revents & POLLIN) == 0) { continue; } if (i == 0) { //創(chuàng)建客戶端連接 ZygoteConnection newPeer = acceptCommandPeer(abiList); peers.add(newPeer); fds.add(newPeer.getFileDesciptor()); } else { //處理客戶端數(shù)據(jù)事務(wù) boolean done = peers.get(i).runOnce(); if (done) { peers.remove(i); fds.remove(i); } } } }}
Zygote進(jìn)程創(chuàng)建Java虛擬機(jī),并注冊(cè)JNI方法, 真正成為Java進(jìn)程的母體,用于孵化Java進(jìn)程. 在創(chuàng)建完system_server進(jìn)程后,zygote功成身退,調(diào)用runSelectLoop(),隨時(shí)待命,當(dāng)接收到請(qǐng)求創(chuàng)建新進(jìn)程請(qǐng)求時(shí)立即喚醒并執(zhí)行相應(yīng)工作。
Zygote通過(guò)fork后創(chuàng)建system_server進(jìn)程,在小節(jié)[3.5]執(zhí)行完startSystemServer()方法后,進(jìn)入到了handleSystemServerProcess()方法,如下所示。
[–>ZygoteInit.java]
private static void handleSystemServerProcess( ZygoteConnection.Arguments parsedArgs) throws ZygoteInit.MethodAndArgsCaller { ... if (parsedArgs.niceName != null) { //設(shè)置當(dāng)前進(jìn)程名為"system_server" Process.setArgV0(parsedArgs.niceName); } final String systemServerClasspath = Os.getenv("SYSTEMSERVERCLASSPATH"); if (systemServerClasspath != null) { //執(zhí)行dex優(yōu)化操作,比如services.jar performSystemServerDexOpt(systemServerClasspath); } if (parsedArgs.invokeWith != null) { ... } else { ClassLoader cl = null; if (systemServerClasspath != null) { cl = new PathClassLoader(systemServerClasspath, ClassLoader.getSystemClassLoader()); Thread.currentThread().setContextClassLoader(cl); } //[見(jiàn)小節(jié)4.2] RuntimeInit.zygoteInit(parsedArgs.targetSdkVersion, parsedArgs.remainingArgs, cl); }}
system_server進(jìn)程創(chuàng)建PathClassLoader類加載器.
[–> RuntimeInit.java]
public static final void zygoteInit(int targetSdkVersion, String[] argv, ClassLoader classLoader) throws ZygoteInit.MethodAndArgsCaller { Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "RuntimeInit"); redirectLogStreams(); //重定向log輸出 commonInit(); // 通用的一些初始化 nativeZygoteInit(); // zygote初始化 applicationInit(targetSdkVersion, argv, classLoader); // [見(jiàn)小節(jié)3.4]}
nativeZygoteInit()方法經(jīng)過(guò)層層調(diào)用,會(huì)進(jìn)入app_main.cpp中的onZygoteInit()方法, Binder線程池的創(chuàng)建也是在這個(gè)過(guò)程,如下:
virtual void onZygoteInit() { sp<ProcessState> proc = ProcessState::self(); proc->startThreadPool(); //啟動(dòng)新binder線程池}
applicationInit()方法經(jīng)過(guò)層層調(diào)用,會(huì)拋出異常ZygoteInit.MethodAndArgsCaller(m, argv), 具體過(guò)程如下:
protected static Runnable applicationInit(int targetSdkVersion, String[] argv, ClassLoader classLoader) { ... VMRuntime.getRuntime().setTargetHeapUtilization(0.75f); VMRuntime.getRuntime().setTargetSdkVersion(targetSdkVersion); final Arguments args = new Arguments(argv); //找到目標(biāo)類的靜態(tài)main()方法 invokeStaticMain(args.startClass, args.startArgs, classLoader);}private static void invokeStaticMain(String className, String[] argv, ClassLoader classLoader) throws ZygoteInit.MethodAndArgsCaller { //此處的className等于SystemServer Class<?> cl = Class.forName(className, true, classLoader); Method m = cl.getMethod("main", new Class[] { String[].class }); //拋出異常Runnable對(duì)象 throw new ZygoteInit.MethodAndArgsCaller(m, argv);}
設(shè)置虛擬機(jī)的堆利用率0.75和置TargetSdk版本;并拋出異常,然后由ZygoteInit.main()捕獲該異常, 見(jiàn)下文
[–>ZygoteInit.java]
public static void main(String argv[]) { try { startSystemServer(abiList, socketName); //拋出MethodAndArgsCaller異常 .... } catch (MethodAndArgsCaller caller) { caller.run(); //此處通過(guò)反射,會(huì)調(diào)用SystemServer.main()方法 [見(jiàn)小節(jié)4.4] } catch (RuntimeException ex) { ... }}static class MethodAndArgsCaller implements Runnable { private final Method mMethod; private final String[] mArgs; public MethodAndArgsCaller(Method method, String[] args) { mMethod = method; mArgs = args; } public void run() { //執(zhí)行SystemServer.main() mMethod.invoke(null, new Object[] { mArgs }); }}
采用拋出異常的方式,用于棧幀清空,提供利用率, 以至于現(xiàn)在大家看到的每個(gè)Java進(jìn)程的調(diào)用棧如下:
... at com.android.server.SystemServer.main(SystemServer.java:175) at java.lang.reflect.Method.invoke!(Native method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)
[–>SystemServer.java]
public final class SystemServer { ... public static void main(String[] args) { //先初始化SystemServer對(duì)象,再調(diào)用對(duì)象的run()方法 new SystemServer().run(); }}
[–>SystemServer.java]
private void run() { if (System.currentTimeMillis() < EARLIEST_SUPPORTED_TIME) { Slog.w(TAG, "System clock is before 1970; setting to 1970."); SystemClock.setCurrentTimeMillis(EARLIEST_SUPPORTED_TIME); } ... Slog.i(TAG, "Entered the Android system server!"); EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_SYSTEM_RUN, SystemClock.uptimeMillis()); Looper.prepareMainLooper();// 準(zhǔn)備主線程looper //加載android_servers.so庫(kù),該庫(kù)包含的源碼在frameworks/base/services/目錄下 System.loadLibrary("android_servers"); //檢測(cè)上次關(guān)機(jī)過(guò)程是否失敗,該方法可能不會(huì)返回 performPendingShutdown(); createSystemContext(); //初始化系統(tǒng)上下文 //創(chuàng)建系統(tǒng)服務(wù)管理 mSystemServiceManager = new SystemServiceManager(mSystemContext); LocalServices.addService(SystemServiceManager.class, mSystemServiceManager); //啟動(dòng)各種系統(tǒng)服務(wù) try { startBootstrapServices(); // 啟動(dòng)引導(dǎo)服務(wù) startCoreServices(); // 啟動(dòng)核心服務(wù) startOtherServices(); // 啟動(dòng)其他服務(wù)[見(jiàn)小節(jié)4.6] } catch (Throwable ex) { Slog.e("System", "************ Failure starting system services", ex); throw ex; } //一直循環(huán)執(zhí)行 Looper.loop(); throw new RuntimeException("Main thread loop unexpectedly exited");}
public final class SystemServer { private void startBootstrapServices() { ... //phase100 mSystemServiceManager.startBootPhase(SystemService.PHASE_WAIT_FOR_DEFAULT_DISPLAY); ... } private void startOtherServices() { ... //phase480 和phase500 mSystemServiceManager.startBootPhase(SystemService.PHASE_LOCK_SETTINGS_READY); mSystemServiceManager.startBootPhase(SystemService.PHASE_SYSTEM_SERVICES_READY); ... //[見(jiàn)小節(jié)4.7] mActivityManagerService.systemReady(new Runnable() { @Override public void run() { //phase550 mSystemServiceManager.startBootPhase( SystemService.PHASE_ACTIVITY_MANAGER_READY); ... //phase600 mSystemServiceManager.startBootPhase( SystemService.PHASE_THIRD_PARTY_APPS_CAN_START); } } }}
start: 創(chuàng)建AMS, PMS, LightsService, DMS.
phase100: 進(jìn)入Phase100, 創(chuàng)建PKMS, WMS, IMS, DBMS, LockSettingsService, JobSchedulerService, MmsService等服務(wù);
phase480 && 500: 進(jìn)入Phase480, 調(diào)用WMS, PMS, PKMS, DisplayManagerService這4個(gè)服務(wù)的systemReady();
Phase550: 進(jìn)入phase550, 執(zhí)行AMS.systemReady(), 啟動(dòng)SystemUI, WebViewFactory, Watchdog.
Phase600: 進(jìn)入phase600, 執(zhí)行AMS.systemReady(), 執(zhí)行各種服務(wù)的systemRunning().
Phase1000: 進(jìn)入1000, 執(zhí)行finishBooting, 啟動(dòng)啟動(dòng)on-hold進(jìn)程.
public final class ActivityManagerService extends ActivityManagerNative implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback { public void systemReady(final Runnable goingCallback) { ... //update相關(guān) mSystemReady = true; //殺掉所有非persistent進(jìn)程 removeProcessLocked(proc, true, false, "system update done"); mProcessesReady = true; goingCallback.run(); //[見(jiàn)小節(jié)1.6.2] addAppLocked(info, false, null); //啟動(dòng)所有的persistent進(jìn)程 mBooting = true; //啟動(dòng)home startHomeActivityLocked(mCurrentUserId, "systemReady"); //恢復(fù)棧頂?shù)腁ctivity mStackSupervisor.resumeTopActivitiesLocked(); }}
System_server主線程的啟動(dòng)工作,總算完成, 進(jìn)入Looper.loop()狀態(tài),等待其他線程通過(guò)handler發(fā)送消息再處理.
對(duì)于普通的app進(jìn)程,跟system_server進(jìn)程的啟動(dòng)過(guò)來(lái)有些類似.不同的是app進(jìn)程是向發(fā)消息給system_server進(jìn)程, 由system_server向zygote發(fā)出創(chuàng)建進(jìn)程的請(qǐng)求.
理解Android進(jìn)程創(chuàng)建流程, 可知進(jìn)程創(chuàng)建后 接下來(lái)會(huì)進(jìn)入ActivityThread.main()過(guò)程。
public static void main(String[] args) { ... Environment.initForCurrentUser(); ... Process.setArgV0("<pre-initialized>"); //創(chuàng)建主線程looper Looper.prepareMainLooper(); ActivityThread thread = new ActivityThread(); thread.attach(false); //attach到系統(tǒng)進(jìn)程 if (sMainThreadHandler == null) { sMainThreadHandler = thread.getHandler(); } //主線程進(jìn)入循環(huán)狀態(tài) Looper.loop(); throw new RuntimeException("Main thread loop unexpectedly exited");}
App進(jìn)程的主線程調(diào)用棧的棧底如下:
... at android.app.ActivityThread.main(ActivityThread.java:5442) at java.lang.reflect.Method.invoke!(Native method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)
跟前面介紹的system_server進(jìn)程調(diào)用棧對(duì)比:
at com.android.server.SystemServer.main(SystemServer.java:175) at java.lang.reflect.Method.invoke!(Native method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)
以下列舉啟動(dòng)部分重要進(jìn)程以及關(guān)鍵節(jié)點(diǎn)會(huì)打印出的log
/system/bin/vold: 383 /system/bin/lmkd: 432 /system/bin/surfaceflinger: 434 /system/bin/debuggerd64: 537 /system/bin/mediaserver: 540 /system/bin/installd: 541 /system/vendor/bin/thermal-engine: 552 zygote64: 557 zygote: 558 system_server: 1274
//啟動(dòng)vold, 再列舉當(dāng)前系統(tǒng)所支持的文件系統(tǒng). 執(zhí)行到system/vold/main.cpp的main()11-23 14:36:47.474 383 383 I vold : Vold 3.0 (the awakening) firing up 11-23 14:36:47.475 383 383 V vold : Detected support for: ext4 vfat //使用內(nèi)核的lmk策略11-23 14:36:47.927 432 432 I lowmemorykiller: Using in-kernel low memory killer interface//啟動(dòng)SurfaceFlinger11-23 14:36:48.041 434 434 I SurfaceFlinger: SurfaceFlinger is starting11-23 14:36:48.042 434 434 I SurfaceFlinger: SurfaceFlinger's main thread ready to run. Initializing graphics H/W...// 開(kāi)機(jī)動(dòng)畫(huà)11-23 14:36:48.583 508 508 I BootAnimation: bootanimation launching ...// debuggerd11-23 14:36:50.306 537 537 I : debuggerd: starting// installd啟動(dòng)11-23 14:36:50.311 541 541 I installd: installd firing up// thermal守護(hù)進(jìn)程11-23 14:36:50.369 552 552 I ThermalEngine: Thermal daemon started
// Zygote64進(jìn)程(Zygote): AndroidRuntime::start11-23 14:36:51.260 557 557 D AndroidRuntime: >>>>>> START com.android.internal.os.ZygoteInit uid 0 <<<<<<// Zygote64進(jìn)程: AndroidRuntime::startVm11-23 14:36:51.304 557 557 D AndroidRuntime: CheckJNI is OFF// 執(zhí)行ZygoteInit.preload()11-23 14:36:52.134 557 557 D Zygote : begin preload// 執(zhí)行ZygoteInit.preloadClasses(), 預(yù)加載3860個(gè)classes, 花費(fèi)時(shí)長(zhǎng)746ms11-23 14:36:52.134 557 557 I Zygote : Preloading classes...11-23 14:36:52.881 557 557 I Zygote : ...preloaded 3860 classes in 746ms.// 執(zhí)行ZygoteInit.preloadClasses(), 預(yù)加載86組資源, 花費(fèi)時(shí)長(zhǎng)179ms11-23 14:36:53.114 557 557 I Zygote : Preloading resources...11-23 14:36:53.293 557 557 I Zygote : ...preloaded 86 resources in 179ms.// 執(zhí)行ZygoteInit.preloadSharedLibraries()11-23 14:36:53.494 557 557 I Zygote : Preloading shared libraries...11-23 14:36:53.503 557 557 D Zygote : end preload// 執(zhí)行com_android_internal_os_Zygote_nativeForkSystemServer(),成功fork出system_server進(jìn)程11-23 14:36:53.544 557 557 I Zygote : System server process 1274 has been created// Zygote開(kāi)始進(jìn)入runSelectLoop()11-23 14:36:53.546 557 557 I Zygote : Accepting command socket connections
//進(jìn)入system_server, 建立跟Zygote進(jìn)程的socket通道 11-23 14:36:53.586 1274 1274 I Zygote : Process: zygote socket opened, supported ABIS: armeabi-v7a,armeabi // 執(zhí)行SystemServer.run() 11-23 14:36:53.618 1274 1274 I SystemServer: Entered the Android system server! <===> boot_progress_system_run // 等待installd準(zhǔn)備就緒 11-23 14:36:53.707 1274 1274 I Installer: Waiting for installd to be ready. //服務(wù)啟動(dòng) 11-23 14:36:53.732 1274 1274 I ActivityManager: Memory class: 192 //phase100 11-23 14:36:53.883 1274 1274 I SystemServiceManager: Starting phase 100 11-23 14:36:53.902 1274 1274 I SystemServer: Package Manager 11-23 14:37:03.816 1274 1274 I SystemServer: User Service ... 11-23 14:37:03.940 1274 1274 I SystemServer: Init Watchdog 11-23 14:37:03.941 1274 1274 I SystemServer: Input Manager 11-23 14:37:03.946 1274 1274 I SystemServer: Window Manager ... 11-23 14:37:04.081 1274 1274 I SystemServiceManager: Starting com.android.server.MountService$Lifecycle 11-23 14:37:04.088 1274 2717 D MountService: Thinking about reset, mSystemReady=false, mDaemonConnected=true 11-23 14:37:04.088 1274 1274 I SystemServiceManager: Starting com.android.server.UiModeManagerService 11-23 14:37:04.520 1274 1274 I SystemServer: NetworkTimeUpdateService //phase480 && 500 11-23 14:37:05.056 1274 1274 I SystemServiceManager: Starting phase 480 11-23 14:37:05.061 1274 1274 I SystemServiceManager: Starting phase 500 11-23 14:37:05.231 1274 1274 I ActivityManager: System now ready <==> boot_progress_ams_ready 11-23 14:37:05.234 1274 1274 I SystemServer: Making services ready 11-23 14:37:05.243 1274 1274 I SystemServer: WebViewFactory preparation //phase550 11-23 14:37:05.234 1274 1274 I SystemServiceManager: Starting phase 550 11-23 14:37:05.237 1274 1288 I ActivityManager: Force stopping com.android.providers.media appid=10010 user=-1: vold reset //Phase600 11-23 14:37:06.066 1274 1274 I SystemServiceManager: Starting phase 600 11-23 14:37:06.236 1274 1274 D MountService: onStartUser 0
通過(guò)adb bugreport抓取log信息.先看zygote是否起來(lái), 再看system_server主線程的運(yùn)行情況,再看ActivityManager情況
adb logcat -s Zygote adb logcat -s SystemServer adb logcat -s SystemServiceManager adb logcat | grep "1359 1359" //system_server情況 adb logcat -s ActivityManager
現(xiàn)場(chǎng)調(diào)試命令
cat proc/[pid]/stack ==> 查看kernel調(diào)用棧
debuggerd -b [pid] ==> 也不可以不帶參數(shù)-b, 則直接輸出到/data/tombstones/目錄
kill -3 [pid] ==> 生成/data/anr/traces.txt文件
lsof [pid] ==> 查看進(jìn)程所打開(kāi)的文件
各大核心進(jìn)程啟動(dòng)后,都會(huì)進(jìn)入各種對(duì)象所相應(yīng)的main()方法,如下
注意app_process進(jìn)程是指通過(guò)/system/bin/app_process啟動(dòng)的進(jìn)程,且后面跟的參數(shù)不帶–zygote,即并非啟動(dòng)zygote進(jìn)程。 比如常見(jiàn)的有通過(guò)adb shell方式來(lái)執(zhí)行am,pm等命令,便是這種方式。
關(guān)于重要進(jìn)程重啟的過(guò)程,會(huì)觸發(fā)哪些關(guān)聯(lián)進(jìn)程重啟名單:
zygote:觸發(fā)media、netd以及子進(jìn)程(包括system_server進(jìn)程)重啟;
system_server: 觸發(fā)zygote重啟;
surfaceflinger:觸發(fā)zygote重啟;
servicemanager: 觸發(fā)zygote、healthd、media、surfaceflinger、drm重啟
所以,surfaceflinger,servicemanager,zygote自身以及system_server進(jìn)程被殺都會(huì)觸發(fā)Zygote重啟。
聯(lián)系客服