framework——应用进程启动流程

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, Object> sLocalServiceObjects = new ArrayMap, Object>();

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

    public static  void addService(Class 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

 

你可能感兴趣的:(framework,应用进程启动,framework,Android)