1.AMS 概述
AMS 是系統(tǒng)的引導(dǎo)服務(wù)衡蚂,應(yīng)用進程的啟動年叮、切換和調(diào)度、四大組件的啟動和管
理都需要 AMS 的支持啸蜜。從這里可以看出 AMS 的功能會十分的繁多裹粤,當(dāng)然它并不
是一個類承擔(dān)這個重責(zé)拇泣,它有一些關(guān)聯(lián)類,這在文章后面會講到。AMS 的涉及
的知識點非常多,這篇文章主要會講解 AMS 的以下幾個知識點:
- AMS 的啟動流程。
- AMS 與進程啟動。
- AMS 主要類。
2.AMS 的啟動流程
整理了一下啟動流程:
開機啟動:
SyetemServer在啟動時做了如下工作:
- 啟動Binder線程池,這樣就可以與其他進程進行通信。
- 創(chuàng)建SystemServiceManager用于對系統(tǒng)的服務(wù)進行創(chuàng)建、啟動和生命周期管理柑司。
- 啟動各種系統(tǒng)服務(wù)蟆湖。
ActivityManagerService的構(gòu)造函數(shù)
public ActivityManagerService(Context systemContext) {
mInjector = new Injector();
// AMS 上下文
mContext = systemContext;
mFactoryTest = FactoryTest.getMode();
// ActivityThread 對象
mSystemThread = ActivityThread.currentActivityThread();
// ContextImpl 對象
mUiContext = mSystemThread.getSystemUiContext();
mPermissionReviewRequired = mContext.getResources().getBoolean(
com.android.internal.R.bool.config_permissionReviewRequired);
// 線程名為 ActivityManager 的前臺線程,ServiceThread 繼承于 HandlerThread
mHandlerThread = new ServiceThread(TAG,
THREAD_PRIORITY_FOREGROUND, false /*allowIo*/);
mHandlerThread.start();
// 獲取 mHandlerThread 的 Handler 對象
mHandler = new MainHandler(mHandlerThread.getLooper());
// 創(chuàng)建名為 android.ui 的線程
mUiHandler = mInjector.getUiHandler(this);
mProcStartHandlerThread = new ServiceThread(TAG + ":procStart",
THREAD_PRIORITY_FOREGROUND, false /* allowIo */);
mProcStartHandlerThread.start();
mProcStartHandler = new Handler(mProcStartHandlerThread.getLooper());
mConstants = new ActivityManagerConstants(this, mHandler);
// 根據(jù)優(yōu)先級 kill 后臺應(yīng)用進程
if (sKillHandler == null) {
sKillThread = new ServiceThread(TAG + ":kill",
THREAD_PRIORITY_BACKGROUND, true /* allowIo */);
sKillThread.start();
sKillHandler = new KillHandler(sKillThread.getLooper());
}
// 前臺廣播隊列喉磁,超時時間為 10 秒
mFgBroadcastQueue = new BroadcastQueue(this, mHandler,
"foreground", BROADCAST_FG_TIMEOUT, false);
// 后臺廣播隊列,超時時間為 60 秒
mBgBroadcastQueue = new BroadcastQueue(this, mHandler,
"background", BROADCAST_BG_TIMEOUT, true);
mBroadcastQueues[0] = mFgBroadcastQueue;
mBroadcastQueues[1] = mBgBroadcastQueue;
// 創(chuàng)建 ActiveServices
mServices = new ActiveServices(this);
mProviderMap = new ProviderMap(this);
// 創(chuàng)建 AppErrors纱皆,用于處理應(yīng)用中的錯誤
mAppErrors = new AppErrors(mUiContext, this);
// 創(chuàng)建 /data/system 目錄
File dataDir = Environment.getDataDirectory();
File systemDir = new File(dataDir, "system");
systemDir.mkdirs();
mAppWarnings = new AppWarnings(this, mUiContext, mHandler, mUiHandler, systemDir);
// TODO: Move creation of battery stats service outside of activity manager service.
// 創(chuàng)建 BatteryStatsService近迁,其信息保存在 /data/system/procstats 中
// 這里有個 TODO搏存,打算把 BatteryStatsService 的創(chuàng)建移除 AMS
mBatteryStatsService = new BatteryStatsService(systemContext, systemDir, mHandler);
mBatteryStatsService.getActiveStatistics().readLocked();
mBatteryStatsService.scheduleWriteToDisk();
mOnBattery = DEBUG_POWER ? true
: mBatteryStatsService.getActiveStatistics().getIsOnBattery();
mBatteryStatsService.getActiveStatistics().setCallback(this);
// 創(chuàng)建 ProcessStatsService矢洲,并將其信息保存在 /data/system/procstats 中
mProcessStats = new ProcessStatsService(this, new File(systemDir, "procstats"));
mAppOpsService = mInjector.getAppOpsService(new File(systemDir, "appops.xml"), mHandler);
// 定義 ContentProvider 訪問指定 Uri 數(shù)據(jù)的權(quán)限
mGrantFile = new AtomicFile(new File(systemDir, "urigrants.xml"), "uri-grants");
// 多用戶管理
mUserController = new UserController(this);
mVrController = new VrController(this);
// 獲取 OpenGL 版本
GL_ES_VERSION = SystemProperties.getInt("ro.opengles.version",
ConfigurationInfo.GL_ES_VERSION_UNDEFINED);
if (SystemProperties.getInt("sys.use_fifo_ui", 0) != 0) {
mUseFifoUiScheduling = true;
}
mTrackingAssociations = "1".equals(SystemProperties.get("debug.track-associations"));
mTempConfig.setToDefaults();
mTempConfig.setLocales(LocaleList.getDefault());
mConfigurationSeq = mTempConfig.seq = 1;
// 創(chuàng)建 ActivityStackSupervisor 璧眠,用于管理 Activity 任務(wù)棧
mStackSupervisor = createStackSupervisor();
mStackSupervisor.onConfigurationChanged(mTempConfig);
mKeyguardController = mStackSupervisor.getKeyguardController();
mCompatModePackages = new CompatModePackages(this, systemDir, mHandler);
mIntentFirewall = new IntentFirewall(new IntentFirewallInterface(), mHandler);
mTaskChangeNotificationController =
new TaskChangeNotificationController(this, mStackSupervisor, mHandler);
// 創(chuàng)建 ActivityStartController 對象,用于管理 Activity 的啟動
mActivityStartController = new ActivityStartController(this);
// 創(chuàng)建最近任務(wù)棧 RecentTask 對象
mRecentTasks = createRecentTasks();
mStackSupervisor.setRecentTasks(mRecentTasks);
mLockTaskController = new LockTaskController(mContext, mStackSupervisor, mHandler);
mLifecycleManager = new ClientLifecycleManager();
// 創(chuàng)建 CpuTracker 線程,追蹤 CPU 狀態(tài)
mProcessCpuThread = new Thread("CpuTracker") {
@Override
public void run() {
synchronized (mProcessCpuTracker) {
mProcessCpuInitLatch.countDown();
mProcessCpuTracker.init(); // 初始化 ProcessCpuTracker责静。注意同步問題
}
while (true) {
try {
try {
synchronized(this) {
final long now = SystemClock.uptimeMillis();
long nextCpuDelay = (mLastCpuTime.get()+MONITOR_CPU_MAX_TIME)-now;
long nextWriteDelay = (mLastWriteTime+BATTERY_STATS_TIME)-now;
//Slog.i(TAG, "Cpu delay=" + nextCpuDelay
// + ", write delay=" + nextWriteDelay);
if (nextWriteDelay < nextCpuDelay) {
nextCpuDelay = nextWriteDelay;
}
if (nextCpuDelay > 0) {
mProcessCpuMutexFree.set(true);
this.wait(nextCpuDelay);
}
}
} catch (InterruptedException e) {
}
// 更新 Cpu 統(tǒng)計信息
updateCpuStatsNow();
} catch (Exception e) {
Slog.e(TAG, "Unexpected exception collecting process stats", e);
}
}
}
};
// hidden api 設(shè)置
mHiddenApiBlacklist = new HiddenApiSettings(mHandler, mContext);
// 設(shè)置 Watchdog 監(jiān)控
Watchdog.getInstance().addMonitor(this);
Watchdog.getInstance().addThread(mHandler);
// bind background thread to little cores
// this is expected to fail inside of framework tests because apps can't touch cpusets directly
// make sure we've already adjusted system_server's internal view of itself first
// 更新進程的 oom_adj 值
updateOomAdjLocked();
try {
Process.setThreadGroupAndCpuset(BackgroundThread.get().getThreadId(),
Process.THREAD_GROUP_BG_NONINTERACTIVE);
} catch (Exception e) {
Slog.w(TAG, "Setting background thread cpuset failed");
}
}
最終調(diào)用ActivityManagerService中的start方法
private void start() {
removeAllProcessGroups(); // 移除所有進程組
mProcessCpuThread.start();//啟動同步CPU的線程袁滥,監(jiān)控 cpu 使用情況
mBatteryStatsService.publish();//訂閱WIFI和mobile電量,統(tǒng)計電池信息
mAppOpsService.publish(mContext);//訂閱app管理服務(wù)(前后臺等)
Slog.d("AppOps", "AppOpsService published");
LocalServices.addService(ActivityManagerInternal.class, new LocalService());//啟動本地服務(wù)
mActivityTaskManager.onActivityManagerInternalAdded();//啟動ActivityManagerInternal灾螃,UriGrantsManagerInternal
mUgmInternal.onActivityManagerInternalAdded();//UriGrantsManagerService啟動本地服務(wù)
mPendingIntentController.onActivityManagerInternalAdded();//pending intent controller啟動本地服務(wù)
// 等待 mProcessCpuThread 線程中的同步代碼塊執(zhí)行完畢题翻。
try {
mProcessCpuInitLatch.await();
} catch (InterruptedException e) {
Slog.wtf(TAG, "Interrupted wait during start", e);
Thread.currentThread().interrupt();
throw new IllegalStateException("Interrupted wait during start");
}
}
相關(guān)代碼與方法:
SystemServer
ActivityManagerService
SystemServiceManager
3.AMS 與進程啟動
Zygote 的 Java 框架層中,會創(chuàng)建一個 Server 端的 Socket腰鬼,這個 Socket 用來等待
AMS 來請求 Zygote 來創(chuàng)建新的應(yīng)用程序進程嵌赠。要啟動一個應(yīng)用程序,首先要保
證這個應(yīng)用程序所需要的應(yīng)用程序進程已經(jīng)被啟動垃喊。AMS 在啟動應(yīng)用程序時會
檢查這個應(yīng)用程序需要的應(yīng)用程序進程是否存在,不存在就會請求 Zygote 進程
將需要的應(yīng)用程序進程啟動袜炕。Service 的啟動過程中會調(diào)用 ActiveServices 的
bringUpServiceLocked 方法本谜,
private String bringUpServiceLocked(ServiceRecord r, int intentFlags, boolean execInFg, boolean whileRestarting, boolean permissionsReviewRequired) throws TransactionTooLargeException {
...
final String procName = r.processName;//1
...
ProcessRecord app;
if (!isolated) {
app = mAm.getProcessRecordLocked(procName, r.appInfo.uid, false);//2
...
if (app != null && app.thread != null) {//3
try {
app.addPackage(r.appInfo.packageName, r.appInfo.longVersionCode, mAm.mProcessStats);
realStartServiceLocked(r, app, execInFg);//4
return null;
}...
}
} else {
...
}
if (app == null && !permissionsReviewRequired) {//5
if ((app=mAm.startProcessLocked(procName, r.appInfo, true, intentFlags,
hostingRecord, false, isolated, false)) == null) {//6
...
}
if (isolated) {
r.isolatedProc = app;
}
}
...
}
代碼:ActiveServices
在注釋 1 處得到 ServiceRecord 的 processName 的值賦值給 procName ,其中
ServiceRecord 用來描述 Service 的 android:process 屬性偎窘。
注釋 2 處將 procName和 Service 的 uid 傳入到 AMS 的 getProcessRecordLocked 方法中乌助,來查詢是否存在一個與 Service 對應(yīng)的 ProcessRecord 類型的對象 app,ProcessRecord 主要用來記錄運行的應(yīng)用程序進程的信息陌知。
注釋 5 處判斷 Service 對應(yīng)的 app 為 null 則說明用來運行 Service 的應(yīng)用程序進程不存在他托,則調(diào)用注釋 6 處的 AMS 的startProcessLocked 方法來創(chuàng)建對應(yīng)的應(yīng)用程序進程,
4.AMS 主要類
其他
補充下SystemServer啟動的服務(wù):