Android 8.0系统启动流程_SystemServer(三)

本系列主要介绍Android8.0系统启动过程中涉及到的init、Zygote、SystemServer和Launcher。
SystemServer 进程主要用于创建系统服务,像AMS、WMS和PMS等,都是由其创建。

一、启动SyetmServer进程

在这里插入图片描述

在Android 8.0系统启动流程_Zygote(二)中讲解到在ZygoteInit中主要作用是启动SystemServer进程,源码如下:

frameworks\base\core\java\com\android\internal\os\ZygoteInit.java
public static void main(String argv[]) {
...
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;
                }
            }
...

private static Runnable forkSystemServer(String abiList, String socketName,
            ZygoteServer zygoteServer) {
            ...
     if (pid == 0) {
            if (hasSecondZygote(abiList)) {
                waitForSecondaryZygote(socketName);
            }

            zygoteServer.closeServerSocket();
            return handleSystemServerProcess(parsedArgs);
        }
        return null;
}
}

由于SystemServer是复制Zygote的进程,因此也会包含Zygote的socket,该socket是服务端socket,对于SystemServer没有其他作用,需要先将其关闭;通过handleSystemServerProcess开启SystemServer进程。

frameworks\base\core\java\com\android\internal\os\ZygoteInit.java
private static Runnable handleSystemServerProcess(ZygoteConnection.Arguments parsedArgs) {
    ...
    ClassLoader cl = null;
            if (systemServerClasspath != null) {
            //创建PathClassLoader
                cl = createPathClassLoader(systemServerClasspath, parsedArgs.targetSdkVersion);
                Thread.currentThread().setContextClassLoader(cl);
            }
            //初始化zygoteInit
            return ZygoteInit.zygoteInit(parsedArgs.targetSdkVersion, parsedArgs.remainingArgs, cl);
            ...
}

public static final Runnable zygoteInit(int targetSdkVersion, String[] argv, ClassLoader classLoader) {
...
Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "ZygoteInit");
        RuntimeInit.redirectLogStreams();
        RuntimeInit.commonInit();
        //启动Binder线程池
        ZygoteInit.nativeZygoteInit();
        //执行SystemServer的main方法     
        return RuntimeInit.applicationInit(targetSdkVersion, argv, classLoader);
}

启动Binder线程池是便于SystemServer与其他进程执行通信操作;
在调用SystemServer的main方法,相对比较复杂,之前版本是通过RuntimeInit通过抛出MethodAndArgsCaller方式跳转至ZygoteInit的main方法中,但在8.0中发现是通过创建MethodAndArgsCaller方式,但最终是执行至MethodAndArgsCaller的invoke中,实现SystemServer运行。流程如下:

frameworks\base\core\java\com\android\internal\os\RuntimeInit.java
 protected static Runnable applicationInit(int targetSdkVersion, String[] argv,
            ClassLoader classLoader) {
... 
        return findStaticMain(args.startClass, args.startArgs, classLoader);
}

private static Runnable findStaticMain(String className, String[] argv,
            ClassLoader classLoader) {
 try {
        //通过反射,获取SystemServer类,
            cl = Class.forName(className, true, classLoader);
        } catch (ClassNotFoundException ex) {
            throw new RuntimeException(
                    "Missing class when invoking static main " + className,
                    ex);
        }
        
        Method m;
        try {
        //通过反射,获取SystemServer类的main方法
            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);
        }
        ...
       
         /*
         * This throw gets caught in ZygoteInit.main(), which responds
         * by invoking the exception's run() method. This arrangement
         * clears up all the stack frames that were required in setting
         * up the process.
         */
          //本版本中,是直接调制至RuntimeInit的静态内部类中。
         return new MethodAndArgsCaller(m, argv);
}

在通过反射获取SystemServer类时,是如何确定该传入的className就是SystemServer?这是由于在上面的ZygoteInit的mian方法时,对于启动的进程做了判断(Zyogte、SystemServer还是application?),上面部分已明确描述加载的是SystemServer类。

  cl = createPathClassLoader(systemServerClasspath, parsedArgs.targetSdkVersion);
  static class MethodAndArgsCaller implements Runnable {
...
 public void run() {
            try {
                mMethod.invoke(null, new Object[] { mArgs });
            } catch (IllegalAccessException ex) {
                throw new RuntimeException(ex);
            } catch (InvocationTargetException ex) {    
...
}

以上过程完成了从Zygote经过RuntimeInit最后完成SystemServer的main方法的运行。下面重点分析SystemServer的main方法。

二、解析SystemServer的main方法

frameworks\base\services\java\com\android\server\SystemServer.java
 public static void main(String[] args) {
        new SystemServer().run();
    }
   private void run() {
        try {
            //创建Looper对象
            Looper.prepareMainLooper();
            // 加载系统声明周期管理的servers的库
            System.loadLibrary("android_servers");
            performPendingShutdown();
            //创建系统的Context
            createSystemContext()
            mSystemServiceManager = new SystemServiceManager(mSystemContext);
            mSystemServiceManager.setRuntimeRestarted(mRuntimeRestart);
            LocalServices.addService(SystemServiceManager.class, mSystemServiceManager);
        SystemServerInitThreadPool.get();
        } finally {
            traceEnd();  // InitBeforeStartServices
        }
  try {
            traceBeginAndSlog("StartServices");
            //启动引导服务
            startBootstrapServices();
            //启动核心服务
            startCoreServices();
            //启动其他服务
            startOtherServices();
            SystemServerInitThreadPool.shutdown();
        } catch (Throwable ex) {
            throw ex;
        } finally {
            traceEnd();
        }
}

该过程主要的作用是启动三种服务(引导、核心和其他服务),分别是指:

  • 引导:AMS(ActivityManagerService)、PMS(PackageManagerService)和UMS(UserManagerService)等;
  • 核心:BatterService、UsageStateService和WebViewService等;
  • 其他: CameraService、InputManagerService和AudioService等。

这些系统服务均来自于ServeryService,但是需要注意在应用层创建的Service不直接属于ServeryService,因为其不是系统服务,其是通过AMS来 管理控制的。

你可能感兴趣的:(Android 8.0系统启动流程_SystemServer(三))