系统启动,SystemServer

SystemServer创建:

流程:
->ZygoteInit.java: startSystemServer
->Zygote.java:forkSystemServer
->通过JNI调用native函数,nativeForkSystemServer  

上面的步骤就起来了SystemServer进程,后面跟 zygote进程分道扬镳;

SystemServer创建成功后,通过handleSystemServerProcess来进行处理

ZygoteInit.java
if (pid == 0) {
    zygoteServer.closeServerSocket();
    handleSystemServerProcess(parsedArgs);  //通过handleSystemServer来处理
}

private static void handleSystemServerProcess(ZygoteConnection.Arguments parsedArgs){
      //调用到ZygoteInit的 zygoteInit.
      ZygoteInit.zygoteInit(parsedArgs.targetSdkVersion, parsedArgs.remainingArgs, cl);
}

public static final void zygoteInit(int targetSdkVersion, String[] argv,
            ClassLoader classLoader) throws Zygote.MethodAndArgsCaller {
        RuntimeInit.commonInit();    //  (1)  做一些常规的初始化,这里不做细化
        ZygoteInit.nativeZygoteInit(); // (2)  调用native函数
        RuntimeInit.applicationInit(targetSdkVersion, argv, classLoader);//(3)在这里会调用到sytemserver的main函数入口
}

这边着重看下(1)nativeZygoteInit函数,这个函数实现在 AndroidRuntime.cpp中:

static void com_android_internal_os_ZygoteInit_nativeZygoteInit(JNIEnv* env, jobject clazz)
{
    gCurRuntime->onZygoteInit();
}
gCurRuntime的定义:static AndroidRuntime* gCurRuntime = NULL;
onZygoteInit实现在 AndroidRuntime的子类AppRuntime中, Appruntime定义在app_main.c中
virtual void onZygoteInit()
{
        sp<ProcessState> proc = ProcessState::self();
        ALOGV("App process: starting thread pool.\n");
        proc->startThreadPool();  //启动线程,用于 Binder通信
}

接下来看下(2) Runtimeinit.applicationInit这个函数

protected static void applicationInit(int targetSdkVersion, String[] argv, ClassLoader classLoader)
            throws Zygote.MethodAndArgsCaller {
    //这边执行到systemServer的main函数
    invokeStaticMain(args.startClass, args.startArgs, classLoader);
}

看下invokeStaticMain函数~

private static void invokeStaticMain(String className, String[] argv, ClassLoader classLoader)
    throws Zygote.MethodAndArgsCaller {
        //这里居然抛出一个异常,全局搜索下,看那边catch这个异常
        throw new Zygote.MethodAndArgsCaller(m, argv);
}

全局搜索,发现在ZygoteInit->main里面catch了“Zygote.MethodAndArgsCaller”:

public static void main(String argv[]) {
      catch (Zygote.MethodAndArgsCaller caller) {
            caller.run();//居然又回调到Zygote.MethodAndArgsCaller的run,醉了
        } 
}

上述为什么要用异常捕获,而不是直接调用?
继续跟踪:

Zygote.MethodAndArgsCaller
MethodAndArgsCaller是Zygote类的内部类:
public static class MethodAndArgsCaller extends Exception{
    public void run() {
            try {
                mMethod.invoke(null, new Object[] { mArgs }); //反射调用,取到 SystemServer.main
            }
    }
}

截止到上面, 还只是创建SystemServer,真正开始SystemServer是从调用 main函数开始:
-> 调用SystemServer的main函数

    public static void main(String[] args) {
        new SystemServer().run();
    }

->SystemServer().run()

这里主要是启动一些服务~
private void run() {
    //加载 android_servers库
    System.loadLibrary("android_servers");
    //创建上下文关系
    createSystemContext();
    // Create the system service manager.  ->创建service manager
    mSystemServiceManager = new SystemServiceManager(mSystemContext);
    mSystemServiceManager.setRuntimeRestarted(mRuntimeRestart);
    LocalServices.addService(SystemServiceManager.class, mSystemServiceManager);

    //开启services
    startBootstrapServices();    
    startCoreServices();
    startOtherServices();
}

依次来看下开启的几个服务
startBootstrapServices

private void startBootstrapServices() {
    //启动ActivityManagerService
    mActivityManagerService = mSystemServiceManager.startService(
                ActivityManagerService.Lifecycle.class).getService();
    //启动PowerManagerService
    mPowerManagerService = mSystemServiceManager.startService(PowerManagerService.class);
    //启动PackageMangerService
    mPackageManagerService = PackageManagerService.main(mSystemContext, installer,
                mFactoryTestMode != FactoryTest.FACTORY_TEST_OFF, mOnlyCore);
    //启动SensorService
    startSensorService();
}

startCoreServices();

private void startCoreServices() {
        // Tracks the battery level.  Requires LightService.
        mSystemServiceManager.startService(BatteryService.class);

        // Tracks application usage stats.
        mSystemServiceManager.startService(UsageStatsService.class);
        mActivityManagerService.setUsageStatsManager(
                LocalServices.getService(UsageStatsManagerInternal.class));

        // Tracks whether the updatable WebView is in a ready state and watches for update installs.
        mWebViewUpdateService = mSystemServiceManager.startService(WebViewUpdateService.class);
}

startOtherServices->省略掉,反正就是开启services

    原文作者:唐僧不爱洗头_f7b5
    原文地址: https://www.jianshu.com/p/03dada228269
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞