Framework底层服务之使用SystemServer启动AMS

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技术板块的部分。

最后

AMS的启动

  1. 系统启动后Zygote进程第一个fork出SystemServer进程
  2. SystemServer->run()->createSystemContext():创建了系统的ActivityThread对象,运行环境mSystemContext、systemUiContext。
  3. SystemServer->run()->startBootstrapServices()->ActivityManagerService.Lifecycle.startService():AMS在引导服务启动方法中,通过构造函数new ActivityManagerService()进行了一些对象创建和初始化(除activity外3大组件的管理和调度对象创建;内存、电池、权限、性能、cpu等的监控等相关对象创建),start()启动服务(移除进程组、启动cpu线程、注册权限、电池等服务)。
  4. SystemServer->run()->startBootstrapServices()->setSystemServiceManager()、setInstaller()、initPowerManagement()、setSystemProcess():AMS创建后进行了一系列相关的初始化和设置。 setSystemProcess():将framework-res.apk的信息加入到SystemServer进程的LoadedApk中,并创建了SystemServer进程的ProcessRecord,加入到mPidsSelfLocked,由AMS统一管理。
  5. SystemServer->run()->startOtherServices():AMS启动后的后续工作,主要调用systemReady()和运行调用时传入的goingCallback。 systemReady()/goingCallback:各种服务或进程等AMS启动完成后需进一步完成的工作及系统相关初始化。 桌面应用在systemReady()方法中启动,systemui在goingCallback中完成。当桌面应用启动完成后,发送开机广播ACTION_BOOT_COMPLETED,到此为止。

文末福利

如果想要成为架构师或想突破20~30K薪资范畴,那就不要局限在编码,业务,要会选型、扩展,提升编程思维。此外,良好的职业规划也很重要,学习的习惯很重要,但是最重要的还是要能持之以恒,任何不能坚持落实的计划都是空谈。

如果你没有方向,这里给大家分享一套由阿里高级架构师编写的《Android八大模块进阶笔记》,帮大家将杂乱、零散、碎片化的知识进行体系化的整理,让大家系统而高效地掌握Android开发的各个知识点。
在这里插入图片描述
相对于我们平时看的碎片化内容,这份笔记的知识点更系统化,更容易理解和记忆,是严格按照知识体系编排的。

全套视频资料:

一、面试合集

在这里插入图片描述
二、源码解析合集
在这里插入图片描述

三、开源框架合集
在这里插入图片描述
欢迎大家一键三连支持,若需要文中资料,直接扫描文末CSDN官方认证微信卡片免费领取↓↓↓

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值