Posts SystemServer启动流程
Post
Cancel

SystemServer启动流程

SystemServer进程主要用于创建系统服务,我们熟知的AMS、WMS和PMS都是由它来创建的。

一旦在init.rc中为Zygote制定了启动参数--start-system-server,那么ZygoteInit就会调用startSystemServer来启动SystemServer集成。

SystemServer

main()

1
2
3
4
5
//frameworks/base/services/java/com/android/server/SystemServer.java
public static void main(String[] args) {
  //创建SystemServer对象,并调用run方法
  new SystemServer().run();
}

run()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
//frameworks/base/services/java/com/android/server/SystemServer.java
private void run() {
  TimingsTraceAndSlog t = new TimingsTraceAndSlog();
  try {
    t.traceBegin("InitBeforeStartServices");

    // Record the process start information in sys props.
    SystemProperties.set(SYSPROP_START_COUNT, String.valueOf(mStartCount));
    SystemProperties.set(SYSPROP_START_ELAPSED, String.valueOf(mRuntimeStartElapsedTime));
    SystemProperties.set(SYSPROP_START_UPTIME, String.valueOf(mRuntimeStartUptime));

    EventLog.writeEvent(EventLogTags.SYSTEM_SERVER_START,
                        mStartCount, mRuntimeStartUptime, mRuntimeStartElapsedTime);

    //
    // Default the timezone property to GMT if not set.
    //
    String timezoneProperty = SystemProperties.get("persist.sys.timezone");
    if (!isValidTimeZoneId(timezoneProperty)) {
      Slog.w(TAG, "persist.sys.timezone is not valid (" + timezoneProperty
             + "); setting to GMT.");
      SystemProperties.set("persist.sys.timezone", "GMT");
    }

    // If the system has "persist.sys.language" and friends set, replace them with
    // "persist.sys.locale". Note that the default locale at this point is calculated
    // using the "-Duser.locale" command line flag. That flag is usually populated by
    // AndroidRuntime using the same set of system properties, but only the system_server
    // and system apps are allowed to set them.
    //
    // NOTE: Most changes made here will need an equivalent change to
    // core/jni/AndroidRuntime.cpp
    if (!SystemProperties.get("persist.sys.language").isEmpty()) {
      final String languageTag = Locale.getDefault().toLanguageTag();

      SystemProperties.set("persist.sys.locale", languageTag);
      SystemProperties.set("persist.sys.language", "");
      SystemProperties.set("persist.sys.country", "");
      SystemProperties.set("persist.sys.localevar", "");
    }

    // The system server should never make non-oneway calls
    Binder.setWarnOnBlocking(true);
    // The system server should always load safe labels
    PackageItemInfo.forceSafeLabels();

    // Default to FULL within the system server.
    SQLiteGlobal.sDefaultSyncMode = SQLiteGlobal.SYNC_MODE_FULL;

    // Deactivate SQLiteCompatibilityWalFlags until settings provider is initialized
    SQLiteCompatibilityWalFlags.init(null);

    // Here we go!
    Slog.i(TAG, "Entered the Android system server!");
    final long uptimeMillis = SystemClock.elapsedRealtime();
    EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_SYSTEM_RUN, uptimeMillis);
    if (!mRuntimeRestart) {
      FrameworkStatsLog.write(FrameworkStatsLog.BOOT_TIME_EVENT_ELAPSED_TIME_REPORTED,
                              FrameworkStatsLog
                              .BOOT_TIME_EVENT_ELAPSED_TIME__EVENT__SYSTEM_SERVER_INIT_START,
                              uptimeMillis);
    }

    // In case the runtime switched since last boot (such as when
    // the old runtime was removed in an OTA), set the system
    // property so that it is in sync. We can't do this in
    // libnativehelper's JniInvocation::Init code where we already
    // had to fallback to a different runtime because it is
    // running as root and we need to be the system user to set
    // the property. http://b/11463182
    SystemProperties.set("persist.sys.dalvik.vm.lib.2", VMRuntime.getRuntime().vmLibrary());

    // Mmmmmm... more memory!
    VMRuntime.getRuntime().clearGrowthLimit();

    // Some devices rely on runtime fingerprint generation, so make sure
    // we've defined it before booting further.
    Build.ensureFingerprintProperty();

    // Within the system server, it is an error to access Environment paths without
    // explicitly specifying a user.
    Environment.setUserRequired(true);

    // Within the system server, any incoming Bundles should be defused
    // to avoid throwing BadParcelableException.
    BaseBundle.setShouldDefuse(true);

    // Within the system server, when parceling exceptions, include the stack trace
    Parcel.setStackTraceParceling(true);

    // Ensure binder calls into the system always run at foreground priority.
    BinderInternal.disableBackgroundScheduling(true);

    // Increase the number of binder threads in system_server
    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();
    Looper.getMainLooper().setSlowLogThresholdMs(
      SLOW_DISPATCH_THRESHOLD_MS, SLOW_DELIVERY_THRESHOLD_MS);

    SystemServiceRegistry.sEnableServiceNotFoundWtf = true;

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

    // Allow heap / perf profiling.
    initZygoteChildHeapProfiling();

    // Debug builds - spawn a thread to monitor for fd leaks.
    if (Build.IS_DEBUGGABLE) {
      spawnFdLeakCheckThread();
    }

    // Check whether we failed to shut down last time we tried.
    // This call may not return.
    performPendingShutdown();

    // Initialize the system context.
    createSystemContext();

    // Call per-process mainline module initialization.
    ActivityThread.initializeMainlineModules();

    // Create the system service manager.
    mSystemServiceManager = new SystemServiceManager(mSystemContext);
    mSystemServiceManager.setStartInfo(mRuntimeRestart,
                                       mRuntimeStartElapsedTime, mRuntimeStartUptime);
    LocalServices.addService(SystemServiceManager.class, mSystemServiceManager);
    // Prepare the thread pool for init tasks that can be parallelized
    SystemServerInitThreadPool.start();
    // Attach JVMTI agent if this is a debuggable build and the system property is set.
    if (Build.IS_DEBUGGABLE) {
      // Property is of the form "library_path=parameters".
      String jvmtiAgent = SystemProperties.get("persist.sys.dalvik.jvmtiagent");
      if (!jvmtiAgent.isEmpty()) {
        int equalIndex = jvmtiAgent.indexOf('=');
        String libraryPath = jvmtiAgent.substring(0, equalIndex);
        String parameterList =
          jvmtiAgent.substring(equalIndex + 1, jvmtiAgent.length());
        // Attach the agent.
        try {
          Debug.attachJvmtiAgent(libraryPath, parameterList, null);
        } catch (Exception e) {
          Slog.e("System", "*************************************************");
          Slog.e("System", "********** Failed to load jvmti plugin: " + jvmtiAgent);
        }
      }
    }
  } finally {
    t.traceEnd();  // InitBeforeStartServices
  }

  // Setup the default WTF handler
  RuntimeInit.setDefaultApplicationWtfHandler(SystemServer::handleEarlySystemWtf);

  // Start services.
  try {
    t.traceBegin("StartServices");
    startBootstrapServices(t);
    startCoreServices(t);
    startOtherServices(t);
  } catch (Throwable ex) {
    Slog.e("System", "******************************************");
    Slog.e("System", "************ Failure starting system services", ex);
    throw ex;
  } finally {
    t.traceEnd(); // StartServices
  }

  StrictMode.initVmDefaults(null);

  if (!mRuntimeRestart && !isFirstBootOrUpgrade()) {
    final long uptimeMillis = SystemClock.elapsedRealtime();
    FrameworkStatsLog.write(FrameworkStatsLog.BOOT_TIME_EVENT_ELAPSED_TIME_REPORTED,
                            FrameworkStatsLog.BOOT_TIME_EVENT_ELAPSED_TIME__EVENT__SYSTEM_SERVER_READY,
                            uptimeMillis);
    final long maxUptimeMillis = 60 * 1000;
    if (uptimeMillis > maxUptimeMillis) {
      Slog.wtf(SYSTEM_SERVER_TIMING_TAG,
               "SystemServer init took too long. uptimeMillis=" + uptimeMillis);
    }
  }

  // Loop forever.
  Looper.loop();
  throw new RuntimeException("Main thread loop unexpectedly exited");
}

startBootstrapServices()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
//frameworks/base/services/java/com/android/server/SystemServer.java
private void startBootstrapServices(@NonNull TimingsTraceAndSlog t) {
  t.traceBegin("startBootstrapServices");

  // Start the watchdog as early as possible so we can crash the system server
  // if we deadlock during early boot
  t.traceBegin("StartWatchdog");
  final Watchdog watchdog = Watchdog.getInstance();
  watchdog.start();
  t.traceEnd();

  Slog.i(TAG, "Reading configuration...");
  final String TAG_SYSTEM_CONFIG = "ReadingSystemConfig";
  t.traceBegin(TAG_SYSTEM_CONFIG);
  SystemServerInitThreadPool.submit(SystemConfig::getInstance, TAG_SYSTEM_CONFIG);
  t.traceEnd();

  // Platform compat service is used by ActivityManagerService, PackageManagerService, and
  // possibly others in the future. b/135010838.
  t.traceBegin("PlatformCompat");
  PlatformCompat platformCompat = new PlatformCompat(mSystemContext);
  ServiceManager.addService(Context.PLATFORM_COMPAT_SERVICE, platformCompat);
  ServiceManager.addService(Context.PLATFORM_COMPAT_NATIVE_SERVICE,
                            new PlatformCompatNative(platformCompat));
  AppCompatCallbacks.install(new long[0]);
  t.traceEnd();

  // FileIntegrityService responds to requests from apps and the system. It needs to run after
  // the source (i.e. keystore) is ready, and before the apps (or the first customer in the
  // system) run.
  t.traceBegin("StartFileIntegrityService");
  mSystemServiceManager.startService(FileIntegrityService.class);
  t.traceEnd();

  // Wait for installd to finish starting up so that it has a chance to
  // create critical directories such as /data/user with the appropriate
  // permissions.  We need this to complete before we initialize other services.
  t.traceBegin("StartInstaller");
  Installer installer = mSystemServiceManager.startService(Installer.class);
  t.traceEnd();

  // In some cases after launching an app we need to access device identifiers,
  // therefore register the device identifier policy before the activity manager.
  t.traceBegin("DeviceIdentifiersPolicyService");
  mSystemServiceManager.startService(DeviceIdentifiersPolicyService.class);
  t.traceEnd();

  // Uri Grants Manager.
  t.traceBegin("UriGrantsManagerService");
  mSystemServiceManager.startService(UriGrantsManagerService.Lifecycle.class);
  t.traceEnd();

  // Start MemtrackProxyService before ActivityManager, so that early calls
  // to Memtrack::getMemory() don't fail.
  t.traceBegin("MemtrackProxyService");
  startMemtrackProxyService();
  t.traceEnd();

  // Activity manager runs the show.
  t.traceBegin("StartActivityManager");
  // TODO: Might need to move after migration to WM.
  //启动ActivityTaskManagerService
  ActivityTaskManagerService atm = mSystemServiceManager.startService(
    ActivityTaskManagerService.Lifecycle.class).getService();
  //创建ActivityManagerService
  mActivityManagerService = ActivityManagerService.Lifecycle.startService(
    mSystemServiceManager, atm);
  mActivityManagerService.setSystemServiceManager(mSystemServiceManager);
  mActivityManagerService.setInstaller(installer);
  mWindowManagerGlobalLock = atm.getGlobalLock();
  t.traceEnd();

  // Data loader manager service needs to be started before package manager
  t.traceBegin("StartDataLoaderManagerService");
  mDataLoaderManagerService = mSystemServiceManager.startService(
    DataLoaderManagerService.class);
  t.traceEnd();

  // Incremental service needs to be started before package manager
  t.traceBegin("StartIncrementalService");
  mIncrementalServiceHandle = startIncrementalService();
  t.traceEnd();

  // Power manager needs to be started early because other services need it.
  // Native daemons may be watching for it to be registered so it must be ready
  // to handle incoming binder calls immediately (including being able to verify
  // the permissions for those calls).
  t.traceBegin("StartPowerManager");
  mPowerManagerervice = mSystemServiceManager.startService(PowerManagerService.class);
  t.traceEnd();

  t.traceBegin("StartThermalManager");
  mSystemServiceManager.startService(ThermalManagerService.class);
  t.traceEnd();

  // Now that the power manager has been started, let the activity manager
  // initialize power management features.
  t.traceBegin("InitPowerManagement");
  mActivityManagerService.initPowerManagement();
  t.traceEnd();

  // Bring up recovery system in case a rescue party needs a reboot
  t.traceBegin("StartRecoverySystemService");
  mSystemServiceManager.startService(RecoverySystemService.Lifecycle.class);
  t.traceEnd();

  // Now that we have the bare essentials of the OS up and running, take
  // note that we just booted, which might send out a rescue party if
  // we're stuck in a runtime restart loop.
  RescueParty.registerHealthObserver(mSystemContext);
  PackageWatchdog.getInstance(mSystemContext).noteBoot();

  // Manages LEDs and display backlight so we need it to bring up the display.
  t.traceBegin("StartLightsService");
  mSystemServiceManager.startService(LightsService.class);
  t.traceEnd();

  t.traceBegin("StartSidekickService");
  // Package manager isn't started yet; need to use SysProp not hardware feature
  if (SystemProperties.getBoolean("config.enable_sidekick_graphics", false)) {
    mSystemServiceManager.startService(WEAR_SIDEKICK_SERVICE_CLASS);
  }
  t.traceEnd();

  // Display manager is needed to provide display metrics before package manager
  // starts up.
  t.traceBegin("StartDisplayManager");
  mDisplayManagerService = mSystemServiceManager.startService(DisplayManagerService.class);
  t.traceEnd();

  // We need the default display before we can initialize the package manager.
  t.traceBegin("WaitForDisplay");
  mSystemServiceManager.startBootPhase(t, SystemService.PHASE_WAIT_FOR_DEFAULT_DISPLAY);
  t.traceEnd();

  // Only run "core" apps if we're encrypting the device.
  String cryptState = VoldProperties.decrypt().orElse("");
  if (ENCRYPTING_STATE.equals(cryptState)) {
    Slog.w(TAG, "Detected encryption in progress - only parsing core apps");
    mOnlyCore = true;
  } else if (ENCRYPTED_STATE.equals(cryptState)) {
    Slog.w(TAG, "Device encrypted - only parsing core apps");
    mOnlyCore = true;
  }

  // Start the package manager.
  if (!mRuntimeRestart) {
    FrameworkStatsLog.write(FrameworkStatsLog.BOOT_TIME_EVENT_ELAPSED_TIME_REPORTED,
                            FrameworkStatsLog
                            .BOOT_TIME_EVENT_ELAPSED_TIME__EVENT__PACKAGE_MANAGER_INIT_START,
                            SystemClock.elapsedRealtime());
  }

  t.traceBegin("StartPackageManagerService");
  try {
    Watchdog.getInstance().pauseWatchingCurrentThread("packagemanagermain");
    mPackageManagerService = PackageManagerService.main(mSystemContext, installer,
                                                        mFactoryTestMode != FactoryTest.FACTORY_TEST_OFF, mOnlyCore);
  } finally {
    Watchdog.getInstance().resumeWatchingCurrentThread("packagemanagermain");
  }

  // Now that the package manager has started, register the dex load reporter to capture any
  // dex files loaded by system server.
  // These dex files will be optimized by the BackgroundDexOptService.
  SystemServerDexLoadReporter.configureSystemServerDexReporter(mPackageManagerService);

  mFirstBoot = mPackageManagerService.isFirstBoot();
  mPackageManager = mSystemContext.getPackageManager();
  t.traceEnd();
  if (!mRuntimeRestart && !isFirstBootOrUpgrade()) {
    FrameworkStatsLog.write(FrameworkStatsLog.BOOT_TIME_EVENT_ELAPSED_TIME_REPORTED,
                            FrameworkStatsLog
                            .BOOT_TIME_EVENT_ELAPSED_TIME__EVENT__PACKAGE_MANAGER_INIT_READY,
                            SystemClock.elapsedRealtime());
  }
  // Manages A/B OTA dexopting. This is a bootstrap service as we need it to rename
  // A/B artifacts after boot, before anything else might touch/need them.
  // Note: this isn't needed during decryption (we don't have /data anyways).
  if (!mOnlyCore) {
    boolean disableOtaDexopt = SystemProperties.getBoolean("config.disable_otadexopt",
                                                           false);
    if (!disableOtaDexopt) {
      t.traceBegin("StartOtaDexOptService");
      try {
        Watchdog.getInstance().pauseWatchingCurrentThread("moveab");
        OtaDexoptService.main(mSystemContext, mPackageManagerService);
      } catch (Throwable e) {
        reportWtf("starting OtaDexOptService", e);
      } finally {
        Watchdog.getInstance().resumeWatchingCurrentThread("moveab");
        t.traceEnd();
      }
    }
  }

  t.traceBegin("StartUserManagerService");
  mSystemServiceManager.startService(UserManagerService.LifeCycle.class);
  t.traceEnd();

  // Initialize attribute cache used to cache resources from packages.
  t.traceBegin("InitAttributerCache");
  AttributeCache.init(mSystemContext);
  t.traceEnd();

  // Set up the Application instance for the system process and get started.
  t.traceBegin("SetSystemProcess");
  mActivityManagerService.setSystemProcess();
  t.traceEnd();

  // The package receiver depends on the activity service in order to get registered.
  platformCompat.registerPackageReceiver(mSystemContext);

  // Complete the watchdog setup with an ActivityManager instance and listen for reboots
  // Do this only after the ActivityManagerService is properly started as a system process
  t.traceBegin("InitWatchdog");
  watchdog.init(mSystemContext, mActivityManagerService);
  t.traceEnd();

  // DisplayManagerService needs to setup android.display scheduling related policies
  // since setSystemProcess() would have overridden policies due to setProcessGroup
  mDisplayManagerService.setupSchedulerPolicies();

  // Manages Overlay packages
  t.traceBegin("StartOverlayManagerService");
  mSystemServiceManager.startService(new OverlayManagerService(mSystemContext));
  t.traceEnd();

  t.traceBegin("StartSensorPrivacyService");
  mSystemServiceManager.startService(new SensorPrivacyService(mSystemContext));
  t.traceEnd();

  if (SystemProperties.getInt("persist.sys.displayinset.top", 0) > 0) {
    // DisplayManager needs the overlay immediately.
    mActivityManagerService.updateSystemUiContext();
    LocalServices.getService(DisplayManagerInternal.class).onOverlayChanged();
  }

  // The sensor service needs access to package manager service, app ops
  // service, and permissions service, therefore we start it after them.
  // Start sensor service in a separate thread. Completion should be checked
  // before using it.
  mSensorServiceStart = SystemServerInitThreadPool.submit(() -> {
    TimingsTraceAndSlog traceLog = TimingsTraceAndSlog.newAsyncLog();
    traceLog.traceBegin(START_SENSOR_SERVICE);
    startSensorService();
    traceLog.traceEnd();
  }, START_SENSOR_SERVICE);

  t.traceEnd(); // startBootstrapServices
}

startCoreServices()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
private void startCoreServices(@NonNull TimingsTraceAndSlog t) {
  t.traceBegin("startCoreServices");

  // Service for system config
  t.traceBegin("StartSystemConfigService");
  mSystemServiceManager.startService(SystemConfigService.class);
  t.traceEnd();

  t.traceBegin("StartBatteryService");
  // Tracks the battery level.  Requires LightService.
  mSystemServiceManager.startService(BatteryService.class);
  t.traceEnd();

  // Tracks application usage stats.
  t.traceBegin("StartUsageService");
  mSystemServiceManager.startService(UsageStatsService.class);
  mActivityManagerService.setUsageStatsManager(
    LocalServices.getService(UsageStatsManagerInternal.class));
  t.traceEnd();

  // Tracks whether the updatable WebView is in a ready state and watches for update installs.
  if (mPackageManager.hasSystemFeature(PackageManager.FEATURE_WEBVIEW)) {
    t.traceBegin("StartWebViewUpdateService");
    mWebViewUpdateService = mSystemServiceManager.startService(WebViewUpdateService.class);
    t.traceEnd();
  }

  // Tracks and caches the device state.
  t.traceBegin("StartCachedDeviceStateService");
  mSystemServiceManager.startService(CachedDeviceStateService.class);
  t.traceEnd();

  // Tracks cpu time spent in binder calls
  t.traceBegin("StartBinderCallsStatsService");
  mSystemServiceManager.startService(BinderCallsStatsService.LifeCycle.class);
  t.traceEnd();

  // Tracks time spent in handling messages in handlers.
  t.traceBegin("StartLooperStatsService");
  mSystemServiceManager.startService(LooperStatsService.Lifecycle.class);
  t.traceEnd();

  // Manages apk rollbacks.
  t.traceBegin("StartRollbackManagerService");
  mSystemServiceManager.startService(ROLLBACK_MANAGER_SERVICE_CLASS);
  t.traceEnd();

  // Tracks native tombstones.
  t.traceBegin("StartNativeTombstoneManagerService");
  mSystemServiceManager.startService(NativeTombstoneManagerService.class);
  t.traceEnd();

  // Service to capture bugreports.
  t.traceBegin("StartBugreportManagerService");
  mSystemServiceManager.startService(BugreportManagerService.class);
  t.traceEnd();

  // Serivce for GPU and GPU driver.
  t.traceBegin("GpuService");
  mSystemServiceManager.startService(GpuService.class);
  t.traceEnd();

  t.traceEnd(); // startCoreServices
}

startOtherServices()

1
2
3
4
5
6
private void startOtherServices(@NonNull TimingsTraceAndSlog t) {
  WindowManagerService wm = null;
  wm = WindowManagerService.main(context, inputManager, !mFirstBoot, mOnlyCore,
                    new PhoneWindowManager(), mActivityManagerService.mActivityTaskManager);
  mActivityManagerService.setWindowManager(wm);
}

SystemServiceManager

1
2
3
4
5
//frameworks/base/services/core/java/com/android/server/SystemServiceManager.java
private final Context mContext; 
public SystemServiceManager(Context context) {
   mContext = context;
}

startService()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//frameworks/base/services/core/java/com/android/server/SystemService.java
private final ArrayList<SystemService> mServices = new ArrayList<SystemService>();
@SuppressWarnings("unchecked")
public <T extends SystemService> T startService(Class<T> serviceClass) {
    try {
        final String name = serviceClass.getName();
        Slog.i(TAG, "Starting " + name);
        Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "StartService " + name);

        // Create the service.
        if (!SystemService.class.isAssignableFrom(serviceClass)) {
            throw new RuntimeException("Failed to create " + name
                    + ": service must extend " + SystemService.class.getName());
        }
        final T service;
        try {
            Constructor<T> constructor = serviceClass.getConstructor(Context.class);
            service = constructor.newInstance(mContext);
        } catch (InstantiationException ex) {
            throw new RuntimeException("Failed to create service " + name
                    + ": service could not be instantiated", ex);
        } catch (IllegalAccessException ex) {
            throw new RuntimeException("Failed to create service " + name
                    + ": service must have a public constructor with a Context argument", ex);
        } catch (NoSuchMethodException ex) {
            throw new RuntimeException("Failed to create service " + name
                    + ": service must have a public constructor with a Context argument", ex);
        } catch (InvocationTargetException ex) {
            throw new RuntimeException("Failed to create service " + name
                    + ": service constructor threw an exception", ex);
        }

        // Register it.
        mServices.add(service);

        // Start it.
        try {
            service.onStart();
        } catch (RuntimeException ex) {
            throw new RuntimeException("Failed to start service " + name
                    + ": onStart threw an exception", ex);
        }
        return service;
    } finally {
        Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
    }
}

启动ActivityManagerService分析

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//frameworks/base/services/core/java/com/android/server/wm/ActivityTaskManagerService.java
public static final class Lifecycle extends SystemService {
    private final ActivityTaskManagerService mService;

    public Lifecycle(Context context) {
        super(context);
        mService = new ActivityTaskManagerService(context);
    }

    @Override
    public void onStart() {
        publishBinderService(Context.ACTIVITY_TASK_SERVICE, mService);
        mService.start();
    }

    @Override
    public void onUnlockUser(int userId) {
        synchronized (mService.getGlobalLock()) {
            mService.mStackSupervisor.onUserUnlocked(userId);
        }
    }

    @Override
    public void onCleanupUser(int userId) {
        synchronized (mService.getGlobalLock()) {
            mService.mStackSupervisor.mLaunchParamsPersister.onCleanupUser(userId);
        }
    }

    public ActivityTaskManagerService getService() {
        return mService;
    }
}
1
2
3
4
5
//frameworks/base/services/core/java/com/android/server/SystemService.java
protected final void publishBinderService(String name, IBinder service,
                                          boolean allowIsolated, int dumpPriority) {
  ServiceManager.addService(name, service, allowIsolated, dumpPriority);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java
public static final class Lifecycle extends SystemService {
  private final ActivityManagerService mService;
  private static ActivityTaskManagerService sAtm;

  public Lifecycle(Context context) {
    super(context);
    mService = new ActivityManagerService(context, sAtm);
  }

  public static ActivityManagerService startService(
    SystemServiceManager ssm, ActivityTaskManagerService atm) {
    sAtm = atm;
    return ssm.startService(ActivityManagerService.Lifecycle.class).getService();
  }

  @Override
  public void onStart() {
    mService.start();
  }

  @Override
  public void onBootPhase(int phase) {
    mService.mBootPhase = phase;
    if (phase == PHASE_SYSTEM_SERVICES_READY) {
      mService.mBatteryStatsService.systemServicesReady();
      mService.mServices.systemServicesReady();
    } else if (phase == PHASE_ACTIVITY_MANAGER_READY) {
      mService.startBroadcastObservers();
    } else if (phase == PHASE_THIRD_PARTY_APPS_CAN_START) {
      mService.mPackageWatchdog.onPackagesReady();
    }
  }

  @Override
  public void onCleanupUser(int userId) {
    mService.mBatteryStatsService.onCleanupUser(userId);
  }

  public ActivityManagerService getService() {
    return mService;
  }
}

WindowManagerService

wms的注册

1
2
3
4
5
6
7
8
9
10
11
//frameworks/base/services/java/com/android/server/SystemServer.java
WindowManagerService wm = null;
private void startOtherServices(@NonNull TimingsTraceAndSlog t) {
    //调用main方法获取WindowManagerService
    wm = WindowManagerService.main(context, inputManager, !mFirstBoot, mOnlyCore,
    new PhoneWindowManager(), mActivityManagerService.mActivityTaskManager);
    //添加服务
ServiceManager.addService(Context.WINDOW_SERVICE, wm, /* allowIsolated= */ false,
    DUMP_FLAG_PRIORITY_CRITICAL | DUMP_FLAG_PROTO);
  
}
1
2
3
4
5
6
7
8
//frameworks/base/services/core/java/com/android/server/wm/WindowManagerService.java
public static WindowManagerService main(final Context context, final InputManagerService im,
        final boolean showBootMsgs, final boolean onlyCore, WindowManagerPolicy policy,
        ActivityTaskManagerService atm) {
   //调用重载方法
    return main(context, im, showBootMsgs, onlyCore, policy, atm,
            SurfaceControl.Transaction::new, Surface::new, SurfaceControl.Builder::new);
}
1
2
3
4
5
6
7
8
9
10
11
12
public static WindowManagerService main(final Context context, final InputManagerService im,
        final boolean showBootMsgs, final boolean onlyCore, WindowManagerPolicy policy,
        ActivityTaskManagerService atm, Supplier<SurfaceControl.Transaction> transactionFactory,
        Supplier<Surface> surfaceFactory,
        Function<SurfaceSession, SurfaceControl.Builder> surfaceControlFactory) {
   
    DisplayThread.getHandler().runWithScissors(() ->
            //创建WindowManagerService
            sInstance = new WindowManagerService(context, im, showBootMsgs, onlyCore, policy,
                    atm, transactionFactory, surfaceFactory, surfaceControlFactory), 0);
    return sInstance;
}
1
2
3
4
public IWindowSession openSession(IWindowSessionCallback callback) {
    //创建Session,并传入WindowManagerService
    return new Session(this, callback);
}

Session

1
2
3
4
5
6
7
8
9
10
11
12
@Override
public int addToDisplayAsUser(IWindow window, int seq, WindowManager.LayoutParams attrs,
        int viewVisibility, int displayId, int userId, Rect outFrame,
        Rect outContentInsets, Rect outStableInsets,
        DisplayCutout.ParcelableWrapper outDisplayCutout, InputChannel outInputChannel,
        InsetsState outInsetsState, InsetsSourceControl[] outActiveControls) {
    //调用WindowManagerService的addWindow
    return mService.addWindow(this, window, seq, attrs, viewVisibility, displayId, outFrame,
            outContentInsets, outStableInsets, outDisplayCutout, outInputChannel,
            outInsetsState, outActiveControls, userId);
}

1
2
3
4
5
6
7
8
//frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java
public void setWindowManager(WindowManagerService wm) {
  synchronized (this) {
    mWindowManager = wm;
    mWmInternal = LocalServices.getService(WindowManagerInternal.class);
    mActivityTaskManager.setWindowManager(wm);
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//frameworks/base/services/core/java/com/android/server/wm/ActivityTaskManagerService.java
public void setWindowManager(WindowManagerService wm) {
  synchronized (mGlobalLock) {
    mWindowManager = wm;
    mRootWindowContainer = wm.mRoot;
    mTempConfig.setToDefaults();
    mTempConfig.setLocales(LocaleList.getDefault());
    mConfigurationSeq = mTempConfig.seq = 1;
    mRootWindowContainer.onConfigurationChanged(mTempConfig);
    mLockTaskController.setWindowManager(wm);
    mStackSupervisor.setWindowManager(wm);
    mRootWindowContainer.setWindowManager(wm);
  }
}

参考

This post is licensed under CC BY 4.0 by the author.