framework——應用程式啟動流程

little_fat_sheep發表於2020-12-31

1 前言

 

2 原始碼分析

2.1 ActivityManagerInternal 初始化流程

(1)請求啟動應用程式的入口

/frameworks/base/services/core/java/com/android/server/wm/ActivityStackSupervisor.java

void startSpecificActivityLocked(ActivityRecord r, boolean andResume, boolean checkConfig) {
	final WindowProcessController wpc = mService.getProcessController(r.processName, r.info.applicationInfo.uid);
	...
    //判斷應用程式是否已建立,即 IApplicationThread 是否為空
	if (wpc != null && wpc.hasThread()) {
		try {
            //啟動 Activity
			realStartActivityLocked(r, wpc, andResume, checkConfig);
			return;
		}
	}
	try {
		...
		//應用程式不存在,通知建立應用程式,mService 為 ATMS;mService.mAmInternal 為 ActivityManagerInternal 
		final Message msg = PooledLambda.obtainMessage(
				ActivityManagerInternal::startProcess, mService.mAmInternal, r.processName,
				r.info.applicationInfo, knownToBeDead, "activity", r.intent.getComponent());
		mService.mH.sendMessage(msg);
	}
	...
}

ActivityStackSupervisor 物件在 ATMS 的 initialize() 方法中建立,並將 ATMS 物件注入到 ActivityStackSupervisor 中。 

PooledLambda.obtainMessage() 獲取一個 Message,併為其指定 Callback,即執行 mService.mAmInternal 物件的 ActivityManagerInternal::startProcess 函式,函式入參為 r.processName、r.info.applicationInfo 等5個引數。由此可知,ActivityManagerInternal 的 startProcess() 方法是請求啟動應用程式的起點。

ActivityManagerInternal 是抽象類,其實現類是 AMS 的內部類 LocalService。接下來將分析 ActivityManagerInternal 的建立流程和 ATMS 獲取起物件的流程。

(2)ActivityManagerInternal 的建立

/frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java

private void start() {
	...
	LocalServices.addService(ActivityManagerInternal.class, new LocalService());
    //呼叫 ATMS 的 onActivityManagerInternalAdded 方法進一步初始化
	mActivityTaskManager.onActivityManagerInternalAdded();
	...
}

start() 方法被 AMS.Lifecycle 的 onStart() 方法呼叫,onStart() 方法又被 SystemServiceManager 的 startService() 方法呼叫。 

(3)ATMS 獲取 ActivityManagerInternal

/frameworks/base/services/core/java/com/android/server/wm/ActivityTaskManagerService.java

public void onActivityManagerInternalAdded() {
	synchronized (mGlobalLock) {
		mAmInternal = LocalServices.getService(ActivityManagerInternal.class);
		...
	}
}

(4)LocalServices

public final class LocalServices {
    ...

    private static final ArrayMap<Class<?>, Object> sLocalServiceObjects = new ArrayMap<Class<?>, Object>();

    public static <T> T getService(Class<T> type) {
        synchronized (sLocalServiceObjects) {
            return (T) sLocalServiceObjects.get(type);
        }
    }

    public static <T> void addService(Class<T> type, T service) {
        synchronized (sLocalServiceObjects) {
            ...
            sLocalServiceObjects.put(type, service);
        }
    }
	
	...
}

2.2 AMS 到 ZygoteState

(1)startProcess

/frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.LocalService.java

public void startProcess(String processName, ApplicationInfo info, boolean knownToBeDead, String hostingType, ComponentName hostingName) {
	try {
		...
		synchronized (ActivityManagerService.this) {
			startProcessLocked(processName, info, knownToBeDead, 0 , new HostingRecord(hostingType, hostingName), false, false, true);
		}
	}
	...
}

(2)startProcessLocked

/frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java

final ProcessRecord startProcessLocked(String processName, ApplicationInfo info, boolean knownToBeDead, int intentFlags, 
			HostingRecord hostingRecord, boolean allowWhileBooting, boolean isolated, boolean keepIfLarge) {
    //mProcessList 在定義時就被初始化,即:final ProcessList mProcessList = new ProcessList()
	return mProcessList.startProcessLocked(processName, info, knownToBeDead, intentFlags, hostingRecord, 
			allowWhileBooting, isolated, 0, keepIfLarge, null, null, null, null);
}

(3)startProcessLocked

/frameworks/base/services/core/java/com/android/server/am/ProcessList.java

final ProcessRecord startProcessLocked(String processName, ApplicationInfo info, boolean knownToBeDead, int intentFlags, 
		HostingRecord hostingRecord, boolean allowWhileBooting, boolean isolated, int isolatedUid, 
		boolean keepIfLarge, String abiOverride, String entryPoint, String[] entryPointArgs, Runnable crashHandler) {
	...
	ProcessRecord app;
	if (!isolated) {
		app = getProcessRecordLocked(processName, info.uid, keepIfLarge);
		...
	}
	...
	if (app == null) {
		...
	} else {
		app.addPackage(info.packageName, info.longVersionCode, mService.mProcessStats);
		...
	}
	...
	final boolean success = startProcessLocked(app, hostingRecord, abiOverride);
	...
	return success ? app : null;
}

(4)startProcessLocked

/frameworks/base/services/core/java/com/android/server/am/ProcessList.java

final boolean startProcessLocked(ProcessRecord app, HostingRecord hostingRecord, String abiOverride) {
	return startProcessLocked(app, hostingRecord, false, false, false, abiOverride);
}

(5)startProcessLocked

/frameworks/base/services/core/java/com/android/server/am/ProcessList.java

boolean startProcessLocked(ProcessRecord app, HostingRecord hostingRecord, boolean disableHiddenApiChecks, 
		boolean disableTestApiChecks, boolean mountExtStorageFull, String abiOverride) {
	...
	try {
		...
		final String entryPoint = "android.app.ActivityThread";
		return startProcessLocked(hostingRecord, entryPoint, app, uid, gids, runtimeFlags, 
			mountExternal, seInfo, requiredAbi, instructionSet, invokeWith, startTime);
	}
	...
}

(6)startProcessLocked

/frameworks/base/services/core/java/com/android/server/am/ProcessList.java

boolean startProcessLocked(HostingRecord hostingRecord, String entryPoint, ProcessRecord app, 
		int uid, int[] gids, int runtimeFlags, int mountExternal, String seInfo, 
		String requiredAbi, String instructionSet, String invokeWith, long startTime) {
	...
	final long startSeq = app.startSeq = ++mProcStartSeqCounter;
	...
	mPendingStarts.put(startSeq, app);

	if (mService.mConstants.FLAG_PROCESS_START_ASYNC) {
		...
		mService.mProcStartHandler.post(() -> {
			try {
				final Process.ProcessStartResult startResult = startProcess(app.hostingRecord,
						entryPoint, app, app.startUid, gids, runtimeFlags, mountExternal,
						app.seInfo, requiredAbi, instructionSet, invokeWith, app.startTime);
				synchronized (mService) {
					handleProcessStartedLocked(app, startResult, startSeq);
				}
			}
			...
		});
		return true;
	} else {
		try {
			final Process.ProcessStartResult startResult = startProcess(hostingRecord, entryPoint, app, uid, gids, 
					runtimeFlags, mountExternal, seInfo, requiredAbi, instructionSet, invokeWith, startTime);
			handleProcessStartedLocked(app, startResult.pid, startResult.usingWrapper, startSeq, false);
		}
		...
		return app.pid > 0;
	}
}

(7)startProcess

/frameworks/base/services/core/java/com/android/server/am/ProcessList.java

private Process.ProcessStartResult startProcess(HostingRecord hostingRecord, String entryPoint,
		ProcessRecord app, int uid, int[] gids, int runtimeFlags, int mountExternal,
		String seInfo, String requiredAbi, String instructionSet, String invokeWith, long startTime) {
	try {
		...
		else {
			startResult = Process.start(entryPoint, app.processName, uid, uid, gids, runtimeFlags, mountExternal,
					app.info.targetSdkVersion, seInfo, requiredAbi, instructionSet, app.info.dataDir, invokeWith, 
					app.info.packageName, app.mDisabledCompatChanges, new String[]{PROC_START_SEQ_IDENT + app.startSeq});
		}
		...
		return startResult;
	}
	...
}

 

(8)ProcessStartResult

/frameworks/base/core/java/android/os/Process.java

public static ProcessStartResult start(final String processClass, final String niceName, int uid, int gid, @Nullable int[] gids,
		int runtimeFlags, int mountExternal, int targetSdkVersion, String seInfo, String abi, String instructionSet,
		String appDataDir, String invokeWith, String packageName, long[] disabledCompatChanges, String[] zygoteArgs) {
	return ZYGOTE_PROCESS.start(processClass, niceName, uid, gid, gids, runtimeFlags, mountExternal, targetSdkVersion, 
			seInfo, abi, instructionSet, appDataDir, invokeWith, packageName, true, disabledCompatChanges, zygoteArgs);
}

(9)ProcessStartResult

/frameworks/base/core/java/android/os/Process.java

z

(10)ProcessStartResult

/frameworks/base/core/java/android/os/Process.java

z

 (11)ProcessStartResult

/frameworks/base/core/java/android/os/Process.java

z

2.3 ZygoteInit 到 ActivityThread

 

相關文章