上一篇文章,我们已经分析了zygote的进程启动流程和做了什么事情。本篇我们看看zygote启动systemserver和atms、ams做了什么事情。回顾一下zygote在java层做了forksystemserver的进程
1.SystemServer进程创建
//*************2.zygote fork。systemserver 进程
if (startSystemServer) {
//*******************************
//埋下一个伏笔,封装成了一个runnable
Runnable r = forkSystemServer(abiList, zygoteSocketName, zygoteServer);
// {@code r == null} in the parent (zygote) process, and {@code r != null} in the
// child (system_server) process.
//这一步要说明的就是forksystemserver,就是复制了一份zygote的一部分内容。
//所以在这里进行判断执行的是systemserver进程的内容
//r如果是zygote进程,r就为null
if (r != null) {
r.run();
return;
}
}
//frameworks/base/core/java/com/android/internal/os/ZygoteInit.java
private static Runnable forkSystemServer(String abiList, String socketName,
ZygoteServer zygoteServer) {
//一系列的参数设置
//fork systemserver
pid = Zygote.forkSystemServer(
parsedArgs.mUid, parsedArgs.mGid,
parsedArgs.mGids,
parsedArgs.mRuntimeFlags,
null,
parsedArgs.mPermittedCapabilities,
parsedArgs.mEffectiveCapabilities);
/* For child process */
// pid=0代表是Systemserver的进程
if (pid == 0) {
if (hasSecondZygote(abiList)) {
waitForSecondaryZygote(socketName);
}
zygoteServer.closeServerSocket();
return handleSystemServerProcess(parsedArgs);
}
}
//走到Zygote.java的forksystemserver方法,可以看到走到了native方法
static int forkSystemServer(int uid, int gid, int[] gids, int runtimeFlags,
int[][] rlimits, long permittedCapabilities, long effectiveCapabilities) {
ZygoteHooks.preFork();
//执行nativeForkSystemServer
int pid = nativeForkSystemServer(
uid, gid, gids, runtimeFlags, rlimits,
permittedCapabilities, effectiveCapabilities);
// Set the Java Language thread priority to the default value for new apps.
Thread.currentThread().setPriority(Thread.NORM_PRIORITY);
ZygoteHooks.postForkCommon();
return pid;
}
//代码里可以看到实际上调用了
//com_android_internal_os_Zygote.cpp
/创建systemserver的方法
static jint com_android_internal_os_Zygote_nativeForkSystemServer(
JNIEnv* env, jclass, uid_t uid, gid_t gid, jintArray gids,
jint runtime_flags, jobjectArray rlimits, jlong permitted_capabilities,
jlong effective_capabilities) {
//fork进程方法
pid_t pid = ForkCommon(env, true,
fds_to_close,
fds_to_ignore,
true);
}
//ForkCommon方法
static pid_t ForkCommon(JNIEnv* env, bool is_system_server,
const std::vector<int>& fds_to_close,
const std::vector<int>& fds_to_ignore,
bool is_priority_fork) {
//真正调用的fork方法,拿到pid
pid_t pid = fork();
}
可以看到fork方法,真正创建了systemserver进程。Systemserver进程创建的方法就看到这里。
2.SystemServer创建后,做了什么?
首先看下Systemserver创建后
// pid=0代表是Systemserver的进程
if (pid == 0) {
if (hasSecondZygote(abiList)) {
waitForSecondaryZygote(socketName);
}
zygoteServer.closeServerSocket();
//pid=0。执行的方法
return handleSystemServerProcess(parsedArgs);
}
//执行方法
private static Runnable handleSystemServerProcess(ZygoteArguments parsedArgs) {
return ZygoteInit.zygoteInit(parsedArgs.mTargetSdkVersion,
parsedArgs.mDisabledCompatChanges,
parsedArgs.mRemainingArgs, cl);
}
//执行了zygoteInit nativeZygoteInit
//frameworks/base/core/java/com/android/internal/os/ZygoteInit.java
public static final Runnable zygoteInit()
RuntimeInit.commonInit();
//*******************方法1 :nativeZygoteInit
ZygoteInit.nativeZygoteInit();
//*******************方法2 :applicationInit
return RuntimeInit.applicationInit(targetSdkVersion, disabledCompatChanges, argv,
classLoader);
}
2.1可以看到方法1执行的过程
//frameworks/base/core/jni/AndroidRuntime.cpp
//先看第一个方法 调用的native方法,执行到了AndroidRuntime.cpp
static void com_android_internal_os_ZygoteInit_nativeZygoteInit(JNIEnv* env, jobject clazz){
gCurRuntime->onZygoteInit();
}
//frameworks/base/cmds/app_process/app_main.cpp
//启动Binder线程池,执行了app_main中的onZygoteInit方法
virtual void onZygoteInit(){
sp<ProcessState> proc = ProcessState::self();
ALOGV("App process: starting thread pool.\n");
//启动线程池
proc->startThreadPool();
}
//frameworks/native/libs/binder/ProcessState.cpp
void ProcessState::startThreadPool(){
//执行线程方法
spawnPooledThread(true);
}
//ProcessState启动Binder线程池
void ProcessState::spawnPooledThread(bool isMain){
if (mThreadPoolStarted) {
String8 name = makeBinderThreadName();
ALOGV("Spawning new pooled thread, name=%s\n", name.string());
sp<Thread> t = new PoolThread(isMain);
t->run(name.string());
}
}
方法1创建了Binder线程池
2.2 方法2
//方法2执行了applicationInit
RuntimeInit.applicationInit(targetSdkVersion, disabledCompatChanges, argv,
classLoader);
//frameworks/base/core/java/com/android/internal/os/RuntimeInit.java
protected static Runnable applicationInit(int targetSdkVersion, long[] disabledCompatChanges,
return findStaticMain(args.startClass, args.startArgs, classLoader);
}
//执行了findStaticMain 查找staticmain方法
//frameworks/base/core/java/com/android/internal/os/RuntimeInit.java
protected static Runnable findStaticMain(String className, String[] argv,
ClassLoader classLoader) {
//可以看到通过反射查找类,拿到main方法
cl = Class.forName(className, true, classLoader);
m = cl.getMethod("main", new Class[] { String[].class });
//调用MethodAndArgsCaller传入方法
return new MethodAndArgsCaller(m, argv);
}
//可以看到MethodAndArgsCaller是一个runnalbe,执行run方法
static class MethodAndArgsCaller implements Runnable {
//调用方法
mMethod.invoke(null, new Object[] { mArgs });
}
//回到forsystemserver方法
if (startSystemServer) {
Runnable r = forkSystemServer(abiList, zygoteSocketName, zygoteServer);
r.run();
}
//从zygote的启动参数可以看到 传入到ZygoteInit的是Systemserver,所以r.run其实是调用了
//SystemServer.main方法
方法2就是创建SystemServer进程后,调用SystemServer.main方法
3.SystemServer.main方法
main中首先就是调用了run()方法
//frameworks/base/services/java/com/android/server/SystemServer.java
public static void main(String[] args) {
new SystemServer().run();
}
private void run() {
//在sys属性中记录进程启动信息
//一系列的属性设置和检查
//清除一些内存空间
//增加system_server中绑定线程的数量
//设置线程属性
//****重点1.创建系统上下文
createSystemContext();
}
//****重点2.创建SystemServiceManager***************************
mSystemServiceManager = new SystemServiceManager(mSystemContext);
//****重点3.创建引导服务startBootstrapServices**************************
startBootstrapServices(t);
//****重点4.创建核心服务startCoreServices*****************************
startCoreServices(t);
//****重点5.创建其他服务startOtherServices****************
startOtherServices(t);
重点1.创建系统上下文,为什么要创建系统上下文,因为后续启动服务要用到上下文对象
//拿到ActivityThread得到SystemContext
private void createSystemContext() {
ActivityThread activityThread = ActivityThread.systemMain();
mSystemContext = activityThread.getSystemContext();
mSystemContext.setTheme(DEFAULT_SYSTEM_THEME);
final Context systemUiContext = activityThread.getSystemUiContext();
systemUiContext.setTheme(DEFAULT_SYSTEM_THEME);
}
//通过ContextImpl创建systemcontext
public ContextImpl getSystemContext() {
synchronized (this) {
if (mSystemContext == null) {
mSystemContext = ContextImpl.createSystemContext(this);
}
return mSystemContext;
}
}
//通过构造函数创建ContextImpl
static ContextImpl createSystemContext(ActivityThread mainThread) {
LoadedApk packageInfo = new LoadedApk(mainThread);
ContextImpl context = new ContextImpl(null, mainThread, packageInfo, null, null, null, null,0, null, null);
return context;
}
重点2.创建SystemServiceManager,为什么要Manager,需要通过SystemServiceManager来启动系统服务。 可以看出SystemServiceManager的作用: 管理服务的生命周期,执行生命周期的方法。 通过构造函数创建。
4.ATMS、AMS的创建
把ATMS、AMS创建的过程放在这里是因为,他们是在引导服务中创建的。看代码
重点3.创建引导服务startBootstrapServices
//创建ATMS服务
ActivityTaskManagerService atm = mSystemServiceManager.startService(
ActivityTaskManagerService.Lifecycle.class).getService();
//创建AMS服务
mActivityManagerService = ActivityManagerService.Lifecycle.startService(
mSystemServiceManager, atm);
//android 10之后,四大组件的管理分开 ATMS主要负责activity的管理
//看看创建过程frameworks/base/services/core/java/com/android/server/SystemServiceManager.java
//首先通过Servermanager.startservice
public <T extends SystemService> T startService(Class<T> serviceClass) {
startService(service);
}
public void startService(@NonNull final SystemService service) {
ArrayList<SystemService> mServices = new ArrayList<SystemService>();
//添加进系统服务列表,被Servermanager管理
mServices.add(service);
//通过反射等调用onstart
service.onStart();
}
//ATMS内部类实现SystemService,所以调用了onstart方法
static final class ActivityTaskManagerService.Lifecycle extends SystemService {
private final ActivityTaskManagerService mService;
public Lifecycle(Context context) {
super(context);
mService = new ActivityTaskManagerService(context);
}
@Override
public void onStart() {
publishBinderService(Context.ACTIVITY_TASK_SERVICE, mService);
mService.start();
}
}
//就是将当前的ActivityTaskManagerService添加给ServiceManager管理
publishBinderService(Context.ACTIVITY_TASK_SERVICE, mService){
ServiceManager.addService(name, service, allowIsolated, dumpPriority);
}
AMS过程同ATMS差不多。至此AMS、ATMS都创建完成并添加给ServiceManager管理。
遗留: 后续如何调用ATMS的服务流程?等有时间更新一下。