ProcessList 可以看做是 服务于 AMS 的工具类,用于 管理进程。
例如在启动 Activity 过程中,Activity 所在应用未启动时,将委托它先启动进程。
基于API 31 源码, 学习一下 ProcessList
1. system_server 内部调用 AMI 接口(实际是抽象类)
Framework 内部通过 ActivityManagerInternal 接口(在这里简称为AMI) 访问 AMS.
如在 ATMS(ActivityTaskManagerService) 里
void startProcessAsync(ActivityRecord activity, boolean knownToBeDead, boolean isTop,
String hostingType) {
try {
if (Trace.isTagEnabled(TRACE_TAG_WINDOW_MANAGER)) {
Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "dispatchingStartProcess:"
+ activity.processName);
}
// Post message to start process to avoid possible deadlock of calling into AMS with the
// ATMS lock held.
final Message m = PooledLambda.obtainMessage(ActivityManagerInternal::startProcess,
mAmInternal, activity.processName, activity.info.applicationInfo, knownToBeDead,
isTop, hostingType, activity.intent.getComponent());
mH.sendMessage(m);
} finally {
Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
}
}
2. AMS 实现 AMI
LocalService 实现了 AMI 里的函数,包括 startProcess 用于启动进程。
AMS 里创建 LocalService,并把它添加到 system_server 里的 LocalServices 里。
后续则可以通过 getService 获取到:
mInternal = new LocalService();
LocalServices.addService(ActivityManagerInternal.class, mInternal);
实际上 LocalService 它会再交给 ProcessList 处理,这里先调用AMS的 内部方法 startProcessLocked:
@VisibleForTesting
public final class LocalService extends ActivityManagerInternal
implements ActivityManagerLocal {
....
@Override
public void startProcess(String processName, ApplicationInfo info, boolean knownToBeDead,
boolean isTop, String hostingType, ComponentName hostingName) {
try {
if (Trace.isTagEnabled(Trace.TRACE_TAG_ACTIVITY_MANAGER)) {
Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "startProcess:"
+ processName);
}
synchronized (ActivityManagerService.this) {
// If the process is known as top app, set a hint so when the process is
// started, the top priority can be applied immediately to avoid cpu being
// preempted by other processes before attaching the process of top app.
startProcessLocked(processName, info, knownToBeDead, 0 /* intentFlags */,
new HostingRecord(hostingType, hostingName, isTop),
ZYGOTE_POLICY_FLAG_LATENCY_SENSITIVE, false /* allowWhileBooting */,
false /* isolated */);
}
} finally {
Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
}
}
注意这个 startProcessLocked 是用上了 同步锁的,需要等待锁,避免并发引起的问题。
实际我们会发现,多次调用了 startProcess, 但是 startProcessLocked有可能只调用了一次
3. AMS 调用 ProcessList
AMS不直接启动,而是委托调用 ProcessList.startProcessLocked,
AMS 的 startProcessLocked
@GuardedBy("this")
final ProcessRecord startProcessLocked(String processName,
ApplicationInfo info, boolean knownToBeDead, int intentFlags,
HostingRecord hostingRecord, int zygotePolicyFlags, boolean allowWhileBooting,
boolean isolated) {
return mProcessList.startProcessLocked(processName, info, knownToBeDead, intentFlags,
hostingRecord, zygotePolicyFlags, allowWhileBooting, isolated, 0 /* isolatedUid */,
null /* ABI override */, null /* entryPoint */,
null /* entryPointArgs */, null /* crashHandler */);
}
可以看出 ProcessList 的同名函数 startProcessLocked, 实际上也是受 AMS 里的锁作用的
网友评论