https://zhuanlan.zhihu.com/p/343233982
https://blog.csdn.net/xingyu19911016/article/details/128619982
https://blog.csdn.net/qq_21154101/article/details/89455884

一、初始化(InitBeforeStartServices)

  • 准备MainLooper线程

    // Prepare the main looper thread (this thread).
    android.os.Process.setThreadPriority(
        android.os.Process.THREAD_PRIORITY_FOREGROUND);
    android.os.Process.setCanSelfBackground(false);
    Looper.prepareMainLooper();
    Looper.getMainLooper().setSlowLogThresholdMs(
        SLOW_DISPATCH_THRESHOLD_MS, SLOW_DELIVERY_THRESHOLD_MS);
  • 加载native库

    // Initialize native services.
    System.loadLibrary("android_servers");
  • createSystemContext

    613     private void createSystemContext() {
    614         ActivityThread activityThread = ActivityThread.systemMain();
    615         mSystemContext = activityThread.getSystemContext();
    616         mSystemContext.setTheme(DEFAULT_SYSTEM_THEME);
    617 
    618         final Context systemUiContext = activityThread.getSystemUiContext();
    619         systemUiContext.setTheme(DEFAULT_SYSTEM_THEME);
    620     }
  • 创建SystemServiceManager实例

    501             // Create the system service manager.
    502             mSystemServiceManager = new SystemServiceManager(mSystemContext);
    503             mSystemServiceManager.setStartInfo(mRuntimeRestart,
    504                     mRuntimeStartElapsedTime, mRuntimeStartUptime);
    505             LocalServices.addService(SystemServiceManager.class, mSystemServiceManager);
    506             // Prepare the thread pool for init tasks that can be parallelized
  • 创建线程池
    507 SystemServerInitThreadPool.get();

二、启动服务

 512         // Start services.
 513         try {
 514             traceBeginAndSlog("StartServices");
 515             startBootstrapServices();
 516             startCoreServices();
 517             startOtherServices();
 518             SystemServerInitThreadPool.shutdown();
 519         } catch (Throwable ex) {
 520             Slog.e("System", "******************************************");
 521             Slog.e("System", "************ Failure starting system services", ex);
 522             throw ex;
 523         } finally {
 524             traceEnd();
 525         }

1.服务的多种创建启动方式

方式一:通过SystemServiceManager.java的startService方法

大多数的系统服务都是通过此方法启动的

SystemServiceManager.java源码位置:frameworks/base/services/core/java/com/android/server/SystemServiceManager.java

startService有3个重载方法,最终调用方法三,所有的服务都是SystemService的子类。

//方法一
public SystemService startService(String className)
//方法二
public <T extends SystemService> T startService(Class<T> serviceClass)

//方法三
124     public void startService(@NonNull final SystemService service) {
125         // Register it.
            //mServices是一个ArrayList
126         mServices.add(service);
127         // Start it.
128         long time = SystemClock.elapsedRealtime();
129         try {
130             service.onStart();
131         } catch (RuntimeException ex) {
132             throw new RuntimeException("Failed to start service " + service.getClass().getName()
133                     + ": onStart threw an exception", ex);
134         }
135         warnIfTooLong(SystemClock.elapsedRealtime() - time, service, "onStart");
136     }

方式二:常规new服务类

  • 静态main方法
    PackageManagerService、OtaDexoptService、WindowManagerService
  • 静态create方法
    NetworkManagementService、IpSecService、NsdService、NetworkStatsService
  • 直接New

2.缓存服务

  • LocalServices.addService
    将服务缓存起来,供system_server进程中的其它服务调用。

SystemServer会调用此方法,其它的服务的构造方法一般都会调用此方法。

将服务的Binder缓存起来,供其它进程(一般是应用进程)跨进程调用。
绝大多数的服务都是在SystemServer类中通过ServiceManager.addService注册,也有部分部分是在本类中调用ServiceManager.addService注册的,如ActivityManagerService与PackageManagerService

3.开机启动执行各个阶段的回调

138     /**
139      * Starts the specified boot phase for all system services that have been started up to
140      * this point.
141      *
142      * @param phase The boot phase to start.
143      */
144     public void startBootPhase(final int phase) {
145         if (phase <= mCurrentPhase) {
146             throw new IllegalArgumentException("Next phase must be larger than previous");
147         }
148         mCurrentPhase = phase;
149 
150         Slog.i(TAG, "Starting phase " + mCurrentPhase);
151         try {
152             Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "OnBootPhase " + phase);
153             final int serviceLen = mServices.size();
154             for (int i = 0; i < serviceLen; i++) {
155                 final SystemService service = mServices.get(i);
156                 long time = SystemClock.elapsedRealtime();
157                 Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, service.getClass().getName());
158                 try {
159                     service.onBootPhase(mCurrentPhase);
160                 } catch (Exception ex) {
161                     throw new RuntimeException("Failed to boot service "
162                             + service.getClass().getName()
163                             + ": onBootPhase threw an exception during phase "
164                             + mCurrentPhase, ex);
165                 }
166                 warnIfTooLong(SystemClock.elapsedRealtime() - time, service, "onBootPhase");
167                 Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
168             }
169         } finally {
170             Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
171         }
172     }

在SystemServiceManager.java的startService方法中mServices会将各个SystemService缓存起来,此处再遍历执行各个SystemService的onBootPhase方法。

3.具体源码分析

3-1)startBootstrapServices

  • 启动WatchDog子线程,监听系统服务。

  • Reading configuration
    SystemServerInitThreadPool.get().submit(SystemConfig::getInstance, TAG_SYSTEM_CONFIG);
    这个代码按道理说会报错,SystemConfig都没有实现runnable接口,怎么能加入线程池执行?


0 条评论

发表回复

您的电子邮箱地址不会被公开。