相关文章链接:
1. Android Framework - 开机启动 SystemServer 进程
相关源码文件:
/frameworks/base/services/java/com/android/server/SystemServer.java
/frameworks/base/services/core/java/com/android/server/SystemServiceManager.java
/frameworks/base/services/core/java/com/android/server/ServiceThread.java
/frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java
ActivityManagerService 是 Android 中非常重要的一个服务,主要功能是管理和启动四大组件。其源代码大约有 2W 多行,这里我们主要来分析其启动过程,后面的文章我们肯定还会分析具体的源码细节。AMS 是由 SystemServer 进程启动的,对于这个有不了解的同学请看这里《Android Framework - 开机启动 SystemServer 进程》
private void startBootstrapServices() {
...
// 启动 AMS 服务
mActivityManagerService = mSystemServiceManager.startService(
ActivityManagerService.Lifecycle.class).getService();
// 设置 AMS 的系统服务管理器
mActivityManagerService.setSystemServiceManager(mSystemServiceManager);
// 设置 AMS 的 APP 安装器
mActivityManagerService.setInstaller(installer);
...
// 设置 SystemServer
mActivityManagerService.setSystemProcess();
}
public static final class Lifecycle extends SystemService {
private final ActivityManagerService mService;
public Lifecycle(Context context) {
super(context);
// 创建 ActivityManagerService
mService = new ActivityManagerService(context);
}
@Override
public void onStart() {
// 调用 start 方法
mService.start();
}
// 获取 ActivityManagerService
public ActivityManagerService getService() {
return mService;
}
}
// Note: This method is invoked on the main thread but may need to attach various
// handlers to other threads. So take care to be explicit about the looper.
public ActivityManagerService(Context systemContext) {
mContext = systemContext;
// 获取 ActivityThread
mSystemThread = ActivityThread.currentActivityThread();
// 创建名为"ActivityManager"的前台 HandlerThread
mHandlerThread = new ServiceThread(TAG,
android.os.Process.THREAD_PRIORITY_FOREGROUND, false /*allowIo*/);
// 启动 ServiceThread
mHandlerThread.start();
// 创建一个 MainHandler 与 mHandlerThread 公用一个 looper
mHandler = new MainHandler(mHandlerThread.getLooper());
// 创建 UiHandler 其内部也会创建一个 HandlerThread
mUiHandler = new UiHandler();
// 前台和后台广播接收队列,分别是 10s 和 60s 放弃执行
mFgBroadcastQueue = new BroadcastQueue(this, mHandler,
"foreground", BROADCAST_FG_TIMEOUT, false);
mBgBroadcastQueue = new BroadcastQueue(this, mHandler,
"background", BROADCAST_BG_TIMEOUT, true);
mBroadcastQueues[0] = mFgBroadcastQueue;
mBroadcastQueues[1] = mBgBroadcastQueue;
// 创建目录/data/system
File dataDir = Environment.getDataDirectory();
File systemDir = new File(dataDir, "system");
systemDir.mkdirs();
// 创建 BatteryStatsService,相当于电表
mBatteryStatsService = new BatteryStatsService(systemDir, mHandler);
mBatteryStatsService.getActiveStatistics().readLocked();
mBatteryStatsService.scheduleWriteToDisk();
mBatteryStatsService.getActiveStatistics().setCallback(this);
// 创建进程统计服务,信息保存在目录 /data/system/procstats,
mProcessStats = new ProcessStatsService(this, new File(systemDir, "procstats"));
...
// 创建 ActivityStackSupervisor 对象
mStackSupervisor = new ActivityStackSupervisor(this, mRecentTasks);
mTaskPersister = new TaskPersister(systemDir, mStackSupervisor, mRecentTasks);
// 创建名为 "CpuTracker" 的线程
mProcessCpuThread = new Thread("CpuTracker") {
@Override
public void run() {
while (true) {
try {
...
updateCpuStatsNow();
} catch (Exception e) {
Slog.e(TAG, "Unexpected exception collecting process stats", e);
}
}
}
};
// 看门狗
Watchdog.getInstance().addMonitor(this);
Watchdog.getInstance().addThread(mHandler);
}
在 SystemService 进程的启动过程中会调用 startBootstrapServices 方法,其内部会启动 AMS 服务,在 ActivityManagerService 的构造函数中会创建三个线程分别是 ActivityManager 、UI 和 CpuTracker。我们接着看 start 方法:
private void start() {
// 移除所有的进程组
Process.removeAllProcessGroups();
// 启动 CpuTracker 线程
mProcessCpuThread.start();
// 把电表服务注册添加到 ServiceManager
mBatteryStatsService.publish(mContext);
mAppOpsService.publish(mContext);
Slog.d("AppOps", "AppOpsService published");
LocalServices.addService(ActivityManagerInternal.class, new LocalService());
}
public void setSystemProcess() {
try {
// 把自己加到 ServiceManager ,然后注册额外的一些服务
ServiceManager.addService(Context.ACTIVITY_SERVICE, this, true);
ServiceManager.addService(ProcessStats.SERVICE_NAME, mProcessStats);
ServiceManager.addService("meminfo", new MemBinder(this));
ServiceManager.addService("gfxinfo", new GraphicsBinder(this));
ServiceManager.addService("dbinfo", new DbBinder(this));
if (MONITOR_CPU_USAGE) {
ServiceManager.addService("cpuinfo", new CpuBinder(this));
}
ServiceManager.addService("permission", new PermissionController(this));
ServiceManager.addService("processinfo", new ProcessInfoService(this));
// 通过 pms 获取 ApplicationInfo 信息
ApplicationInfo info = mContext.getPackageManager().getApplicationInfo(
"android", STOCK_PM_FLAGS);
// 初始化 ApplicationInfo 和 ClassLoader
mSystemThread.installSystemApplicationInfo(info, getClass().getClassLoader());
synchronized (this) {
// 创建 ProcessRecord 对象
ProcessRecord app = newProcessRecordLocked(info, info.processName, false, 0);
app.persistent = true;
app.pid = MY_PID;
app.maxAdj = ProcessList.SYSTEM_ADJ;
app.makeActive(mSystemThread.getApplicationThread(), mProcessStats);
synchronized (mPidsSelfLocked) {
mPidsSelfLocked.put(app.pid, app);
}
updateLruProcessLocked(app, false, null);
updateOomAdjLocked();
}
} catch (PackageManager.NameNotFoundException e) {
throw new RuntimeException(
"Unable to find android system package", e);
}
}
private void startOtherServices() {
...
// 安装系统 Provider
mActivityManagerService.installSystemProviders();
...
mActivityManagerService.systemReady(new Runnable() {
public void run() {
// 启动 WebView
WebViewFactory.prepareWebViewInSystemServer();
// 启动 SystemUi
startSystemUi(context);
// 调用一系列服务的 systemReady 方法
...
// 调用一系列服务的 systemRunning 方法
...
}
}
}
public void systemReady(final Runnable goingCallback) {
...
// 执行 Callback 的 run 方法
if (goingCallback != null) goingCallback.run();
// Start up initial activity.
mBooting = true;
// 启动桌面 Activity 进程
startHomeActivityLocked(mCurrentUserId, "systemReady");
}
static final void startSystemUi(Context context) {
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.android.systemui",
"com.android.systemui.SystemUIService"));
//Slog.d(TAG, "Starting service: " + intent);
context.startServiceAsUser(intent, UserHandle.OWNER);
}
关于 AMS 的启动流程还是很简单的,调用构造函数会初始化三个线程;调用 setSystemProcess 方法中会向 ServcieManager 进程额外发布一些服务:procstats(进程信息)、meminfo(内存信息)、gfxinfo(图形信息)、cpuinfo(cpu信息)、permission(权限)、processinfo(应用使用情况)等;调用 systemReady 方法首先会启动 SystemUIService,然后执行一系列服务的 systemReady 和 systemRunning 方法,最后启动桌面 Activity 进程。
视频地址:https://pan.baidu.com/s/1j_wgzITcgABVbThvO0VBPA
视频密码:jj4b
网友评论