SystemServer
首先看下什么是SystemServer?
SystemServer的进程名实际上叫做“system_server”,通常简称为SS。 是系统中的服务驻留在其中,常见的比如WindowManagerServer(Wms)、ActivityManagerSystemService(AmS)、 PackageManagerServer(PmS)等,这些系统服务都是以一个线程的方式存在于SystemServer进程中。
启动Binder线程池和SystemServiceManager,并且启动各种系统服务
SystemServer.main()
初始化SystemServer对象,然后调用run()
new SystemServer().run()
SystemServer.run()
//其他代码省略
createSystemContext();//加载系统资源
startBootstrapServices(t);//启动引导服务
startCoreServices(t);//启动核心服务
startOtherServices(t);//启动其他服务
SystemServer.createSystemContext()
//系统资源加载
ActivityThread activityThread = ActivityThread.systemMain();
mSystemContext = activityThread.getSystemContext();//ContextImpl
mSystemContext.setTheme(DEFAULT_SYSTEM_THEME);
final Context systemUiContext = activityThread.getSystemUiContext();
systemUiContext.setTheme(DEFAULT_SYSTEM_THEME);
ActivityThread.systemMain()
//ResourcesManager.getInstance()获取资源管理实例
ActivityThread thread = new ActivityThread();
thread.attach(true, 0);
return thread;
thread.attach(true, 0);
mInstrumentation = new Instrumentation();
mInstrumentation.basicInit(this);
/**
getSystemContext()单例模式创建ContextImpl对象mSystemContext-->createSystemContext-->创建LoadedApk对象(创建ApplicationInfo(),创建ClassLoader)
createAppContext()利用刚创建的LoadedApk对象创建新的ContextImpl对象
**/
ContextImpl context = ContextImpl.createAppContext(this,getSystemContext().mPackageInfo);
/**
initializeJavaContextClassLoader() 设置当前的线程ContextClassLoader
newApplication()
public Application newApplication(ClassLoader cl, String className, Context context)throws InstantiationException, IllegalAccessException, ClassNotFoundException {
//创建Application对象
Application app = getFactory(context.getPackageName())
.instantiateApplication(cl, className);
//将新创建的ContextImpl对象保存到Application父类成员变量mBase
//将新创建的LoadedApk对象保存到Application的成员变量mLoadedApk
app.attach(context);
return app;
}
**/
mInitialApplication = context.mPackageInfo.makeApplication(true, null);
mInitialApplication.onCreate();
SystemServer.startBootstrapServices()
// SystemServiceManager 专门管理各种服务启动(java层各种服务)
ActivityTaskManagerService atm = mSystemServiceManager.startService(
ActivityTaskManagerService.Lifecycle.class).getService();
// 在SystemServiceManager.startService()中new Lifecycle()-->new ActivityManagerService(),且回调Lifecycle.onStart()
mActivityManagerService = ActivityManagerService.Lifecycle.startService(
mSystemServiceManager, atm);
mActivityManagerService.setSystemServiceManager(mSystemServiceManager);
//设置AMS的APP安装器
mActivityManagerService.setInstaller(installer);
//开启PMS服务
mPowerManagerService = mSystemServiceManager.startService(PowerManagerService.class);
//初始化AMS相关的PMS服务
mActivityManagerService.initPowerManagement();
//添加C/C++各种服务
mActivityManagerService.setSystemProcess();
new ActivityManagerService(context, sAtm)
- 启动相关服务
- 创建UI线程
- 创建ActiveServices
- 创建CpuTracker线程
Lifecycle.start()
//移除所有的进程组
removeAllProcessGroups();
//启动CpuTracker线程
mProcessCpuThread.start();
//启动电池统计服务
mBatteryStatsService.publish();
//启动APP操作信息服务
mAppOpsService.publish();
//添加到LocalServices中
LocalServices.addService(ActivityManagerInternal.class, mInternal);
ActivityManagerService.setSystemProcess();
/**
ServiceManager c/c++服务
activity AMS
procstats 进程统计
meminfo 内存信息
gfxinfo 图像信息
dbinfo 数据库
cpuinfo
permission
processinfo 进程信息
cacheinfo 缓存信息
**/
ServiceManager.addService(Context.ACTIVITY_SERVICE, this, /* allowIsolated= */ true,DUMP_FLAG_PRIORITY_CRITICAL | DUMP_FLAG_PRIORITY_NORMAL | DUMP_FLAG_PROTO);
ServiceManager.addService(ProcessStats.SERVICE_NAME, mProcessStats);
ServiceManager.addService("meminfo", new MemBinder(this), /* allowIsolated= */ false, DUMP_FLAG_PRIORITY_HIGH);
ServiceManager.addService("gfxinfo", new GraphicsBinder(this));
ServiceManager.addService("dbinfo", new DbBinder(this));
if (MONITOR_CPU_USAGE) {
ServiceManager.addService("cpuinfo", new CpuBinder(this),
/* allowIsolated= */ false, DUMP_FLAG_PRIORITY_CRITICAL);
}
ServiceManager.addService("permission", new PermissionController(this));
ServiceManager.addService("processinfo", new ProcessInfoService(this));
ServiceManager.addService("cacheinfo", new CacheBinder(this));
/**
getSystemContext().installSystemApplicationInfo(info, classLoader);
getSystemUiContext().installSystemApplicationInfo(info, classLoader);
mProfiler = new Profiler();
**/
mSystemThread.installSystemApplicationInfo(info, getClass().getClassLoader());
//创建ProcessRecord对象
ProcessRecord app = mProcessList.newProcessRecordLocked(info, info.processName,
false,
0,
new HostingRecord("system"));
ActivityThread.installSystemApplicationInfo(info,getClass().getClassLoader());
//最终调用LoadedApk的installSystemApplicationInfo(),加载名为android的包
getSystemContext().installSystemApplicationInfo(info, classLoader);
getSystemUiContext().installSystemApplicationInfo(info, classLoader);
//创建用于性能统计Profiler对象
mProfiler = new Profiler();
SystemServer.startOtherServices(t);
//与AMS相关,其他代码省略
/**
安装系统Provider
创建CoreSettingsObserver,用于监控Settings的改变
**/
mActivityManagerService.installSystemProviders();
//
wm = WindowManagerService.main(context, inputManager, !mFirstBoot, mOnlyCore,new PhoneWindowManager(),mActivityManagerService.mActivityTaskManager);
//加入到底层服务中
ServiceManager.addService(Context.WINDOW_SERVICE, wm, /* allowIsolated= */ false, DUMP_FLAG_PRIORITY_CRITICAL | DUMP_FLAG_PROTO);
//WMS管理
mActivityManagerService.setWindowManager(wm);
/**
startSystemUi()启动系统UI
执行一系列服务的systemReady()
**/
mActivityManagerService.systemReady();
//至此80多个服务初始化完成
以上就是framework中底层服务学习的ams技术板块的部分。framework板块分布很多,想要更深入学习《Framework全家桶》可以前往获取方式。
文末
AMS的启动
- 系统启动后Zygote进程第一个fork出SystemServer进程
- SystemServer->run()->createSystemContext():创建了系统的ActivityThread对象,运行环境mSystemContext、systemUiContext。
- SystemServer->run()->startBootstrapServices()->ActivityManagerService.Lifecycle.startService():AMS在引导服务启动方法中,通过构造函数new ActivityManagerService()进行了一些对象创建和初始化(除activity外3大组件的管理和调度对象创建;内存、电池、权限、性能、cpu等的监控等相关对象创建),start()启动服务(移除进程组、启动cpu线程、注册权限、电池等服务)。
- SystemServer->run()->startBootstrapServices()->setSystemServiceManager()、setInstaller()、initPowerManagement()、setSystemProcess():AMS创建后进行了一系列相关的初始化和设置。 setSystemProcess():将framework-res.apk的信息加入到SystemServer进程的LoadedApk中,并创建了SystemServer进程的ProcessRecord,加入到mPidsSelfLocked,由AMS统一管理。
- SystemServer->run()->startOtherServices():AMS启动后的后续工作,主要调用systemReady()和运行调用时传入的goingCallback。 systemReady()/goingCallback:各种服务或进程等AMS启动完成后需进一步完成的工作及系统相关初始化。 桌面应用在systemReady()方法中启动,systemui在goingCallback中完成。当桌面应用启动完成后,发送开机广播ACTION_BOOT_COMPLETED,到此为止。
网友评论