SystemServer進(jìn)程啟動過程解析
1、SystemServer進(jìn)程作用
SystemServer進(jìn)程主要是用于創(chuàng)建系統(tǒng)服務(wù)的,例如AMS、WMS、PMS;
SystemService進(jìn)程被創(chuàng)建后,主要的處理如下:
初始化一些系統(tǒng)設(shè)置,虛擬機(jī)配置等;
啟動Binder線程池,這樣就可以與其他進(jìn)程進(jìn)行Binder跨進(jìn)程通信;
創(chuàng)建SystemServiceManager,它用來對系統(tǒng)服務(wù)進(jìn)行創(chuàng)建、啟動和生命周期管理;
創(chuàng)建主線程Looper并進(jìn)入循環(huán)等待消息;
啟動各種系統(tǒng)服務(wù):引導(dǎo)服務(wù)、核心服務(wù)、其他服務(wù),如引導(dǎo)服務(wù)ActivityManagerService、PackageManagerService和其他服務(wù)WindowManagerService、InputManagerService即可;
2、SystemServer進(jìn)程啟動流程
2.1、Zygote進(jìn)程調(diào)用
2.1.1、啟動參數(shù)
在Init進(jìn)程啟動時,解析init.rc文件時,拿到相關(guān)啟動參數(shù),其中參數(shù)中包含“--start-system-server”,表示啟動時要啟動SystemServer進(jìn)程,最終Zygote進(jìn)程拿到相關(guān)參數(shù),所以startSystemServer值為true;
2.1.2、Zygote進(jìn)程fork
在Zygote進(jìn)程啟動后,執(zhí)行ZygoteInit類的main()方法,通過fork的方式啟動SystemServer;
啟動完SystemServer之后會返回一個Runnable對象,在父進(jìn)程Zygote中該Runnable對象為null,子進(jìn)程SystemServer中不為null,會在SystemServer進(jìn)程中執(zhí)行該Runnable對象;
public static void main(String argv[]) { ZygoteServer zygoteServer = new ZygoteServer(); ... boolean startSystemServer = false; for (int i = 1; i < argv.length; i++) { if ("start-system-server".equals(argv[i])) { startSystemServer = true; }... } ... zygoteServer.registerServerSocketFromEnv(socketName); ... if (startSystemServer) { Runnable r = forkSystemServer(abiList, socketName, zygoteServer); // {@code r == null} in the parent (zygote) process, and {@code r != null} in the // child (system_server) process. if (r != null) { r.run(); return; } } ... }
在forkSystemServer()方法中,通過硬編碼的方法寫入啟動參數(shù)數(shù)組,調(diào)用ZygoteConnection.Arguments類去解析該參數(shù)數(shù)組,最后調(diào)用Zygote類的forkSystemServer()方法去請求fork SystemServer進(jìn)程;
如果fork成功,在父進(jìn)程中會返回子進(jìn)程的pid,子進(jìn)程中會返回pid=0,并且子進(jìn)程會繼續(xù)從該處執(zhí)行,判斷pid大于0,如果有兩個Zygote進(jìn)程,則需要等待另一個也完成,然后子進(jìn)程清除調(diào)從父進(jìn)程fork過來的socket信息,繼續(xù)執(zhí)行handleSystemServerProcess()方法;
private static Runnable forkSystemServer(String abiList, String socketName, ZygoteServer zygoteServer) { ... String args[] = { "--setuid=1000", "--setgid=1000", "--setgroups=1001,1002,1003,1004,1005,1006,1007,1008,1009,1010,1018,1021,1023,1024,1032,1065,3001,3002,3003,3006,3007,3009,3010", "--capabilities=" + capabilities + "," + capabilities, "--nice-name=system_server", "--runtime-args", "--target-sdk-version=" + VMRuntime.SDK_VERSION_CUR_DEVELOPMENT, "com.android.server.SystemServer", }; ZygoteConnection.Arguments parsedArgs = null; int pid; try { parsedArgs = new ZygoteConnection.Arguments(args); ... /* Request to fork the system server process */ pid = Zygote.forkSystemServer( parsedArgs.uid, parsedArgs.gid, parsedArgs.gids, parsedArgs.runtimeFlags, null, parsedArgs.permittedCapabilities, parsedArgs.effectiveCapabilities); } catch (IllegalArgumentException ex) { throw new RuntimeException(ex); } /* For child process */ if (pid == 0) { if (hasSecondZygote(abiList)) { waitForSecondaryZygote(socketName); } zygoteServer.closeServerSocket(); return handleSystemServerProcess(parsedArgs); } return null; }
在Zygote的forkSystemServer()方法中,會先重置線程優(yōu)先級,然后調(diào)用native方法去執(zhí)行fork;
public static int forkSystemServer(int uid, int gid, int[] gids, int runtimeFlags, int[][] rlimits, long permittedCapabilities, long effectiveCapabilities) { VM_HOOKS.preFork(); // Resets nice priority for zygote process. resetNicePriority(); int pid = nativeForkSystemServer(uid, gid, gids, runtimeFlags, rlimits, permittedCapabilities, effectiveCapabilities); ... VM_HOOKS.postForkCommon(); return pid; } native private static int nativeForkSystemServer(int uid, int gid, int[] gids, int runtimeFlags, int[][] rlimits, long permittedCapabilities, long effectiveCapabilities);
2.1.3、進(jìn)入Native層方法
Zygote類對應(yīng)的native方法在AndroidRuntime.cpp中注冊的,調(diào)用com_android_internal_os_Zygote.cpp中的register_com_android_internal_os_Zygote()方法建立native方法的映射關(guān)系;
在native方法中又調(diào)用ForkAndSpecializeCommon()方法,創(chuàng)建完成后Zygote進(jìn)程會去檢查SystemServer是否已經(jīng)啟動,如果system_server創(chuàng)建失敗后,會重啟zygote進(jìn)程,Zygote進(jìn)程和SystemServer進(jìn)程是Android系統(tǒng)的兩個重要的進(jìn)程,二者缺一不可,否則就無法正常運(yùn)行;
static jint com_android_internal_os_Zygote_nativeForkSystemServer( JNIEnv* env, jclass, uid_t uid, gid_t gid, jintArray gids, jint runtime_flags, jobjectArray rlimits, jlong permittedCapabilities, jlong effectiveCapabilities) { pid_t pid = ForkAndSpecializeCommon(env, uid, gid, gids, runtime_flags, rlimits, permittedCapabilities, effectiveCapabilities, MOUNT_EXTERNAL_DEFAULT, NULL, NULL, true, NULL, NULL, false, NULL, NULL); ... if (pid > 0) { int status; if (waitpid(pid, &status, WNOHANG) == pid) { ALOGE("System server process %d has died. Restarting Zygote!", pid); RuntimeAbort(env, __LINE__, "System server process has died. Restarting Zygote!"); } } return pid; }
2.1.4、fork進(jìn)程
在ForkAndSpecializeCommon()方法中,調(diào)用fork()函數(shù)去從父進(jìn)程Zygote中fork出子進(jìn)程,即SystemServer進(jìn)程,然后根據(jù)進(jìn)程pid去判斷,做一些初始化工作;
在進(jìn)程fork的時候,操作系統(tǒng)會復(fù)制一個與父進(jìn)程完全相同的子進(jìn)程,共享代碼空間,但是數(shù)據(jù)空間是互相獨(dú)立的,子進(jìn)程數(shù)據(jù)空間中的內(nèi)容是父進(jìn)程的完整拷貝,指令指針也完全相同,子進(jìn)程擁有父進(jìn)程當(dāng)前運(yùn)行到的位置(兩進(jìn)程的程序計數(shù)器pc值相同,也就是說,子進(jìn)程是從fork返回處開始執(zhí)行的),但是兩者返回的pid是不同的,如果fork成功,子進(jìn)程中會返回pid=0,父進(jìn)程Zygote中會返回子進(jìn)程的pid,fork失敗父進(jìn)程中會返回負(fù)數(shù);
子進(jìn)程SystemServer創(chuàng)建成功之后,會將從父進(jìn)程拷貝過來的數(shù)據(jù)做一些初始化操作;
// Utility routine to fork zygote and specialize the child process. static pid_t ForkAndSpecializeCommon(JNIEnv* env, uid_t uid, gid_t gid, jintArray javaGids, jint runtime_flags, jobjectArray javaRlimits, jlong permittedCapabilities, jlong effectiveCapabilities, jint mount_external, jstring java_se_info, jstring java_se_name, bool is_system_server, jintArray fdsToClose, jintArray fdsToIgnore, bool is_child_zygote, jstring instructionSet, jstring dataDir) { SetSignalHandlers(); ... pid_t pid = fork(); if (pid == 0) { // pid = 0 為在子進(jìn)程中,即SystemServer進(jìn)程,然后做一系列初始化工作 ... } else if (pid > 0) { // pid > 0 為在父進(jìn)程中,即Zygote進(jìn)程 ... } return pid; }
2.1.5、Java層獲取到結(jié)果
此時子進(jìn)程SystemServer進(jìn)程fork成功,順著調(diào)用的API返回到ZygoteInit類的forkSystemServer()方法中,此時在Native層fork進(jìn)程完成,結(jié)果返回到Java層,SystemServer進(jìn)程從fork之后開始執(zhí)行,即handleSystemServerProcess();
2.1.6、SystemServer進(jìn)程相關(guān)設(shè)置
初始化SystemServer進(jìn)程名,創(chuàng)建類加載器等,繼續(xù)調(diào)用zygoteInit()方法;
private static Runnable handleSystemServerProcess(ZygoteConnection.Arguments parsedArgs) { ... // 設(shè)置進(jìn)程名 if (parsedArgs.niceName != null) { Process.setArgV0(parsedArgs.niceName); } ... if (parsedArgs.invokeWith != null) { ... } else { ClassLoader cl = null; if (systemServerClasspath != null) { cl = createPathClassLoader(systemServerClasspath, parsedArgs.targetSdkVersion); Thread.currentThread().setContextClassLoader(cl); } /* * Pass the remaining arguments to SystemServer. */ return ZygoteInit.zygoteInit(parsedArgs.targetSdkVersion, parsedArgs.remainingArgs, cl); } }
在該方法中做一些初始化操作,如日志定向,通用初始化即Zygote的初始化,最后調(diào)用applicationInit()方法;
public static final Runnable zygoteInit(int targetSdkVersion, String[] argv, ClassLoader classLoader) { ... // 日志相關(guān) RuntimeInit.redirectLogStreams(); RuntimeInit.commonInit(); ZygoteInit.nativeZygoteInit(); return RuntimeInit.applicationInit(targetSdkVersion, argv, classLoader); } RunTimeInit類中的commonInit()方法主要初始化一些通用配置,如日志、時區(qū)、Http User-agent、socket的tag等; protected static final void commonInit() { ... // 設(shè)置時區(qū) TimezoneGetter.setInstance(new TimezoneGetter() { @Override public String getId() { return SystemProperties.get("persist.sys.timezone"); } }); TimeZone.setDefault(null); ... // 設(shè)置默認(rèn)的HTTP User-agent格式 String userAgent = getDefaultUserAgent(); System.setProperty("http.agent", userAgent); ... }
在applicationInit()方法中初始化程序退出時的設(shè)置,設(shè)置虛擬機(jī)內(nèi)存利用率參數(shù),sdk版本等,隨后繼續(xù)調(diào)用findStaticMain()方法;
protected static Runnable applicationInit(int targetSdkVersion, String[] argv, ClassLoader classLoader) { // 程序退出時相關(guān)設(shè)置 nativeSetExitWithoutCleanup(true); // 設(shè)置虛擬機(jī)的內(nèi)存利用率參數(shù)值為0.75 VMRuntime.getRuntime().setTargetHeapUtilization(0.75f); VMRuntime.getRuntime().setTargetSdkVersion(targetSdkVersion); ... return findStaticMain(args.startClass, args.startArgs, classLoader); }
2.1.7、進(jìn)入SystemServer進(jìn)程main方法
在findStaticMain()方法中通過反射找到SystemServer類的main()方法,將其作為參數(shù)新建MethodAndArgsCaller對象,MethodAndArgsCaller是一個Runnable對象,其run方法里是調(diào)用該傳入的方法,即執(zhí)行SystemServer類的main()方法;
protected static Runnable findStaticMain(String className, String[] argv, ClassLoader classLoader) { Class<?> cl; try { cl = Class.forName(className, true, classLoader); } catch (ClassNotFoundException ex) { throw new RuntimeException("Missing class when invoking static main " + className, ex); } Method m; try { m = cl.getMethod("main", new Class[] { String[].class }); } catch (NoSuchMethodException ex) { throw new RuntimeException("Missing static main on " + className, ex); } catch (SecurityException ex) { throw new RuntimeException( "Problem getting static main on " + className, ex); } ... return new MethodAndArgsCaller(m, argv); }
2.2、SystemServer進(jìn)程工作
SystemServer的run()方法中,做了大量的初始化操作,如設(shè)置系統(tǒng)時間、設(shè)置虛擬機(jī)相關(guān)配置參數(shù)、binder調(diào)用相關(guān)、創(chuàng)建主線程Looper并循環(huán)等待消息、并創(chuàng)建SystemServerManager等;
public final class SystemServer { public static void main(String[] args) { new SystemServer().run(); } private void run() { try { VMRuntime.getRuntime().clearGrowthLimit(); VMRuntime.getRuntime().setTargetHeapUtilization(0.8f); Build.ensureFingerprintProperty(); Environment.setUserRequired(true); BinderInternal.disableBackgroundScheduling(true); BinderInternal.setMaxThreads(sMaxBinderThreads); // Prepare the main looper thread (this thread). android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_FOREGROUND); android.os.Process.setCanSelfBackground(false); Looper.prepareMainLooper(); // Initialize native services. System.loadLibrary("android_servers"); ... // Initialize the system context. createSystemContext(); mSystemServiceManager = new SystemServiceManager(mSystemContext); } finally { ... } // Start services. try { traceBeginAndSlog("StartServices"); startBootstrapServices(); startCoreServices(); startOtherServices(); SystemServerInitThreadPool.shutdown(); } catch (Throwable ex) { ... } ... // Loop forever. Looper.loop(); throw new RuntimeException("Main thread loop unexpectedly exited"); } }
此時SystemServer進(jìn)入自身的Looper循環(huán)中,等待消息處理,SystemServer進(jìn)程正式運(yùn)行起來了;
2.2.1、初始化配置
SystemServer啟動之后,會執(zhí)行一系列初始化操作,如判斷系統(tǒng)時間是否早于1970年,設(shè)置系統(tǒng)時間、虛擬機(jī)內(nèi)存設(shè)置、加載指紋信息、Binder調(diào)用的優(yōu)先級、Binder線程池的最大數(shù)量、創(chuàng)建主線程Looper、加載android_servers庫、初始化系統(tǒng)上下文、創(chuàng)建SystemServerManager等;
2.2.2、創(chuàng)建SystemServerManager
在run()方法中,會先執(zhí)行createSystemContext()方法創(chuàng)建系統(tǒng)上下文對象,mSystemContext對象是從ActivityThread獲取的,調(diào)用ActivityThread的systemMain()方法,執(zhí)行其attach()方法,創(chuàng)建出App的context,及執(zhí)行Application的onCreate()方法,系統(tǒng)上下文對象是通過ActivityThread的getSystemContext()方法獲取,調(diào)用ContextImpl類的createSystemContext()方法創(chuàng)建;
private void createSystemContext() { ActivityThread activityThread = ActivityThread.systemMain(); mSystemContext = activityThread.getSystemContext(); mSystemContext.setTheme(DEFAULT_SYSTEM_THEME); final Context systemUiContext = activityThread.getSystemUiContext(); systemUiContext.setTheme(DEFAULT_SYSTEM_THEME); } # ActivityThread public static ActivityThread systemMain() { ... ActivityThread thread = new ActivityThread(); thread.attach(true, 0); return thread; } private void attach(boolean system, long startSeq) { ... if (!system) { ... } else { ... try { ... ContextImpl context = ContextImpl.createAppContext(this, getSystemContext().mPackageInfo); mInitialApplication = context.mPackageInfo.makeApplication(true, null); mInitialApplication.onCreate(); } catch (Exception e) { throw new RuntimeException("Unable to instantiate Application():" + e.toString(), e); } } ... } public ContextImpl getSystemContext() { synchronized (this) { if (mSystemContext == null) { mSystemContext = ContextImpl.createSystemContext(this); } return mSystemContext; } }
拿到上下文對象,去創(chuàng)建SystemServerManager對象;
mSystemServiceManager = new SystemServiceManager(mSystemContext); // SystemServiceManager public class SystemServiceManager { SystemServiceManager(Context context) { mContext = context; } }
2.2.3、啟動引導(dǎo)服務(wù)
SystemServer調(diào)用startBootstrapServices()方法去啟動一系列的引導(dǎo)服務(wù),如ActivityManagerService、PackageManagerService等;
private void startBootstrapServices() { ... // 啟動AMS mActivityManagerService = mSystemServiceManager.startService(ActivityManagerService.Lifecycle.class).getService(); mActivityManagerService.setSystemServiceManager(mSystemServiceManager); mActivityManagerService.setInstaller(installer); ... // 啟動PMS mPackageManagerService = PackageManagerService.main(mSystemContext, installer, mFactoryTestMode != FactoryTest.FACTORY_TEST_OFF, mOnlyCore); mFirstBoot = mPackageManagerService.isFirstBoot(); mPackageManager = mSystemContext.getPackageManager(); ... }
2.2.4、啟動核心服務(wù)
啟動核心服務(wù),如電量管理服務(wù)、WebViewUpdateService等;
private void startCoreServices() { ... mSystemServiceManager.startService(BatteryService.class); ... if (mPackageManager.hasSystemFeature(PackageManager.FEATURE_WEBVIEW)) { traceBeginAndSlog("StartWebViewUpdateService"); mWebViewUpdateService = mSystemServiceManager.startService(WebViewUpdateService.class); traceEnd(); } ... }
2.2.5、啟動其他服務(wù)
調(diào)用startOtherServices()方法創(chuàng)建其他服務(wù),如NetworkManagementService、WindowManagerService、InputManagerService等;
并且在該方法中會執(zhí)行ActivityManagerService的systemReady()方法,通過調(diào)用該方法會啟動Launcher進(jìn)程,即桌面App,桌面本身就是一個App進(jìn)程;
private void startOtherServices() { ... try { networkManagement = NetworkManagementService.create(context); ServiceManager.addService(Context.NETWORKMANAGEMENT_SERVICE, networkManagement); } ... // WMS wm = WindowManagerService.main(context, inputManager, mFactoryTestMode != FactoryTest.FACTORY_TEST_LOW_LEVEL, !mFirstBoot, mOnlyCore, new PhoneWindowManager()); ServiceManager.addService(Context.WINDOW_SERVICE, wm, /* allowIsolated= */ false, DUMP_FLAG_PRIORITY_CRITICAL | DUMP_FLAG_PROTO); ... }
2.2.6、Looper循環(huán)消息
創(chuàng)建了主線程Looper,并執(zhí)行l(wèi)oop()函數(shù)開啟消息輪訓(xùn)等待消息到來;
以上就是SystemServer進(jìn)程啟動過程解析的詳細(xì)內(nèi)容,更多關(guān)于SystemServer進(jìn)程啟動的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
關(guān)于springboot使用rocketmq?RocketMQMessageListener參數(shù)問題
這篇文章主要介紹了springboot使用rocketmq?RocketMQMessageListener參數(shù)問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值需要的朋友可以參考下2022-11-11如何實(shí)現(xiàn)Java中一個簡單的LinkedList
LinkedList與ArrayList都是List接口的具體實(shí)現(xiàn)類。下面將介紹如何實(shí)現(xiàn)一個簡單的LinkedList,具有很好的參考價值,下面跟著小編一起來看下吧2017-02-02