【8.0.0_r4】AMS架构与流程分析
AMS主要用来管理应用程序的生命周期,以及其核心组件,包括Activity,Service,Provider,Broadcast,Task等
之前整体架构如下图(O上已经废弃)
新的架构比较直接,简化了很多,直接调用IApplicationThread和IActivityManger这两个接口就可以完成Client和Service端的binder通信
核心record是进程的,各个Record的关系如下图
AMS本身是system_server进程里的的一个服务,在类SystemServer里完成初始化,并为其所在的进程system_server创建Android运行时环境,即主线程ActivityThread和Context具体子类ContextImpl,来满足AMS的运行环境条件;完成AMS的初始化;并将其自身所在的system_server进程其纳入调度中;最后通知其他模块,AMS已经ready了
大体初始化流程如下:
1.
先是在SysemServer的run方法里,在启动任何服务之前,调用createSystemContext方法
270 private void run() {
………………………………
378 // Initialize the system context.
379 createSystemContext();
380
381 // Create the system service manager.
382 mSystemServiceManager = new SystemServiceManager(mSystemContext);
383 mSystemServiceManager.setRuntimeRestarted(mRuntimeRestart);
384 LocalServices.addService(SystemServiceManager.class, mSystemServiceManager);
385 // Prepare the thread pool for init tasks that can be parallelized
386 SystemServerInitThreadPool.get();
387 } finally {
388 traceEnd(); // InitBeforeStartServices
389 }
390
391 // Start services.
392 try {
393 traceBeginAndSlog("StartServices");
394 startBootstrapServices();
395 startCoreServices();
396 startOtherServices();
397 SystemServerInitThreadPool.shutdown();
………………………………
createSystemContext方法
先执行ActivityThread的systemMain方法,再通过获取的线程对象获取SystemContext和systemUiContext,并为其设置主题
475 private void createSystemContext() {
476 ActivityThread activityThread = ActivityThread.systemMain();
477 mSystemContext = activityThread.getSystemContext();
478 mSystemContext.setTheme(DEFAULT_SYSTEM_THEME);
479
480 final Context systemUiContext = activityThread.getSystemUiContext();
481 systemUiContext.setTheme(DEFAULT_SYSTEM_THEME);
482 }
systemMain方法
systemMain方法先判断是不是低端设备,低端设备不用gpu做硬件加速。最后new一个ActivityThread对象并attch当前进程到相应的Instrumentation,Application,Context对象
6444 public static ActivityThread systemMain() {
6445 // The system process on low-memory devices do not get to use hardware
6446 // accelerated drawing, since this can add too much overhead to the
6447 // process.
6448 if (!ActivityManager.isHighEndGfx()) {
6449 ThreadedRenderer.disable(true);
6450 } else {
6451 ThreadedRenderer.enableForegroundTrimming();
6452 }
6453 ActivityThread thread = new ActivityThread();
6454 thread.attach(true);
6455 return thread;
6456 }
attach方法
attach后AMS随后可以通过这个ActivityThread里的ApplicationThread对象接口来调度相应进程里程序组件的执行。
1 我们看看ActivityThread的attach函数:
2
3 //此时,我们传入的参数为true,表示该ActivityThread是系统进程的ActivityThread
4 private void attach(boolean system) {
5 //创建出的ActivityThread保存在类的静态变量sCurrentActivityThread
6 //AMS中的大量操作将会依赖于这个ActivityThread
7 sCurrentActivityThread = this;
8 mSystemThread = system;
9
10 if (!system) {
11 //应用进程的处理流程
12 ..........
13 } else {
14 //系统进程的处理流程,该情况只在SystemServer中处理
15
16 // Don't set application object here -- if the system crashes,
17 // we can't display an alert, we just want to die die die.
18 //设置DDMS(Dalvik Debug Monitor Service)中看到的SystemServer进程的名称为“system_process”
19 android.ddm.DdmHandleAppName.setAppName("system_process",
20 UserHandle.myUserId());
21
22 try {
23 //创建ActivityThread中的重要成员:Instrumentation、Application和Context
24 mInstrumentation = new Instrumentation();
25 ContextImpl context = ContextImpl.createAppContext(
26 this, getSystemContext().mPackageInfo);
27 mInitialApplication = context.mPackageInfo.makeApplication(true, null);
28 mInitialApplication.onCreate();
29 } catch (Exception e) {
30 throw new RuntimeException(
31 "Unable to instantiate Application():" + e.toString(), e);
32 }
33 }
34
35 //以下系统进程和非系统进程均会执行
36 ................
37 //注册Configuration变化的回调通知
38 ViewRootImpl.addConfigCallback(new ComponentCallbacks2() {
39 @Override
40 public void onConfigurationChanged(Configuration newConfig) {
41 //当系统配置发生变化时(例如系统语言发生变化),回调该接口
42 ...............
43 }
44 .............
45 });
46 }
Instrumentation类
因为要监控各个组件的执行情况,所以系统需要使用这个类来创建各个组件,并用来在应用和AMS之间做通信交流
Context类
包含了应用运行环境的全局信息,可以访问资源和相应组件,进而做启动Activity和发送Broadcast等操作
Application类
保存应用的全部状态。上边attach创建的Application对象对应framework-res.apk
关系类图
2.
ActivityManagerService内部静态类Lifecycle继承于SystemService
1 2668 public static final class Lifecycle extends SystemService {
在SystemServer.java里被注册到SystemServiceManager的startService里,调用ActivityManagerService构造方法。ActivityMangerService则通过getService返回并被配置
226 private ActivityManagerService mActivityManagerService;
511 // Activity manager runs the show.
512 traceBeginAndSlog("StartActivityManager");
513 mActivityManagerService = mSystemServiceManager.startService(
514 ActivityManagerService.Lifecycle.class).getService();
515 mActivityManagerService.setSystemServiceManager(mSystemServiceManager);
516 mActivityManagerService.setInstaller(installer);
517 traceEnd();
ActivityManagerService后续被用来
- init Power Manager
527 // Now that the power manager has been started, let the activity manager
528 // initialize power management features.
529 traceBeginAndSlog("InitPowerManagement");
530 mActivityManagerService.initPowerManagement();
531 traceEnd();
- set System Process
613 // Set up the Application instance for the system process and get started.
614 traceBeginAndSlog("SetSystemProcess");
615 mActivityManagerService.setSystemProcess();
616 traceEnd();
- set Usage Stats Manager
654 // Tracks application usage stats.
655 traceBeginAndSlog("StartUsageService");
656 mSystemServiceManager.startService(UsageStatsService.class);
657 mActivityManagerService.setUsageStatsManager(
658 LocalServices.getService(UsageStatsManagerInternal.class));
659 traceEnd();
- install System Provider
787 traceBeginAndSlog("InstallSystemProviders");
788 mActivityManagerService.installSystemProviders();
789 traceEnd();
- 配给watchdog
807 traceBeginAndSlog("InitWatchdog");
808 final Watchdog watchdog = Watchdog.getInstance();
809 watchdog.init(context, mActivityManagerService);
810 traceEnd();
- set Window Manager
842 traceBeginAndSlog("SetWindowManagerService");
843 mActivityManagerService.setWindowManager(wm);
844 traceEnd();
- 配给Network Policy Manager Service
1055 traceBeginAndSlog("StartNetworkPolicyManagerService");
1056 try {
1057 networkPolicy = new NetworkPolicyManagerService(context,
1058 mActivityManagerService, networkStats, networkManagement);
1059 ServiceManager.addService(Context.NETWORK_POLICY_SERVICE, networkPolicy);
1060 } catch (Throwable e) {
1061 reportWtf("starting NetworkPolicy Service", e);
1062 }
1063 traceEnd();
- safe mode下AMS进入safe mode
1513 if (safeMode) {
1514 traceBeginAndSlog("EnterSafeModeAndDisableJitCompilation");
1515 mActivityManagerService.enterSafeMode();
1516 // Disable the JIT for the system_server process
1517 VMRuntime.getRuntime().disableJitCompilation();
1518 traceEnd();
- safe mode下显示相应覆盖界面
1577
1578 if (safeMode) {
1579 mActivityManagerService.showSafeModeOverlay();
1580 }
- 配AMS的App Ops Service给Power Manager Service
1597 traceBeginAndSlog("MakePowerManagerServiceReady");
1598 try {
1599 // TODO: use boot phase
1600 mPowerManagerService.systemReady(mActivityManagerService.getAppOpsService());
1601 } catch (Throwable e) {
1602 reportWtf("making Power Manager Service ready", e);
1603 }
1604 traceEnd();
1605
- 调用AMS的systemReady告诉他可以运行第三方code了,并在真正启动初始程序前通过lambda回调返回到SystemServer,调用其startBootPhase的PHEASE_ACTIVITY_MANAGER_READY通知到各个Service继续初始化。最后AMS start Observing Native Crashes
1641 // We now tell the activity manager it is okay to run third party
1642 // code. It will call back into us once it has gotten to the state
1643 // where third party code can really run (but before it has actually
1644 // started launching the initial applications), for us to complete our
1645 // initialization.
1646 mActivityManagerService.systemReady(() -> {
1647 Slog.i(TAG, "Making services ready");
1648 traceBeginAndSlog("StartActivityManagerReadyPhase");
1649 mSystemServiceManager.startBootPhase(
1650 SystemService.PHASE_ACTIVITY_MANAGER_READY);
1651 traceEnd();
1652 traceBeginAndSlog("StartObservingNativeCrashes");
1653 try {
1654 mActivityManagerService.startObservingNativeCrashes();
1655 } catch (Throwable e) {
1656 reportWtf("observing native crashes", e);
1657 }
1658 traceEnd();
AMS里的handler
kill流程
杀进程的基础工具是framework/base/core/java/android/os/Process.java里的三个方法
流程图:
说明:
- Process.killProcess(int pid): 杀pid进程
- Process.killProcessQuiet(int pid):杀pid进程,且不输出log信息
- Process.killProcessGroup(int uid, int pid):杀同一个uid下同一进程组下的所有进程
AMS里ProcessRecord的kill方法会调用Process里的这三个方法
void kill(String reason, boolean noisy) {
if (!killedByAm) {
if (noisy) {
Slog.i(TAG, "Killing " + toShortString() + " (adj " + setAdj + "): " + reason);
}
//调用该方法,则会输出EventLog, 最后一个参数reason代表是通过何在方法触发kill
EventLog.writeEvent(EventLogTags.AM_KILL, userId, pid, processName, setAdj, reason);
Process.killProcessQuiet(pid);
Process.killProcessGroup(info.uid, pid);
if (!persistent) {
killed = true;
killedByAm = true;
}
}
}
除此之外,lmk也会直接在kernel里杀进程
createSystemContext
【8.0.0_r4】AMS架构与流程分析的更多相关文章
- Android 9.0 默认输入法的设置流程分析
Android 输入法设置文章 Android 9.0 默认输入法的设置流程分析 Android 9.0 添加预置第三方输入法/设置默认输入法(软键盘) 前言 在上一篇文章 Android 9.0 ...
- VLC架构及流程分析
0x00 前置信息 VLC是一个非常庞大的工程,我从它的架构及流程入手进行分析,涉及到一些很细的概念先搁置一边,日后详细分析. 0x01 源码结构(Android Java相关的暂未分析) # bui ...
- Android4.0源码Launcher启动流程分析【android源码Launcher系列一】
最近研究ICS4.0的Launcher,发现4.0和2.3有稍微点区别,但是区别不是特别大,所以我就先整理一下Launcher启动的大致流程. Launcher其实是贯彻于手机的整个系统的,时时刻刻都 ...
- Android7.0 Phone应用源码分析(二) phone来电流程分析
接上篇博文:Android7.0 Phone应用源码分析(一) phone拨号流程分析 今天我们再来分析下Android7.0 的phone的来电流程 1.1TelephonyFramework 当有 ...
- Android7.0 Phone应用源码分析(一) phone拨号流程分析
1.1 dialer拨号 拨号盘点击拨号DialpadFragment的onClick方法会被调用 public void onClick(View view) { int resId = view. ...
- Solr4.8.0源码分析(5)之查询流程分析总述
Solr4.8.0源码分析(5)之查询流程分析总述 前面已经写到,solr查询是通过http发送命令,solr servlet接受并进行处理.所以solr的查询流程从SolrDispatchsFilt ...
- Android7.0 Phone应用源码分析(三) phone拒接流程分析
本文主要分析Android拒接电话的流程,下面先来看一下拒接电话流程时序图 步骤1:滑动按钮到拒接图标,会调用到AnswerFragment的onDecline方法 com.android.incal ...
- Android7.0 Phone应用源码分析(四) phone挂断流程分析
电话挂断分为本地挂断和远程挂断,下面我们就针对这两种情况各做分析 先来看下本地挂断电话的时序图: 步骤1:点击通话界面的挂断按钮,会调用到CallCardPresenter的endCallClicke ...
- [Android6.0][RK3399] 双屏异显代码实现流程分析(一)【转】
本文转载自:http://blog.csdn.net/dearsq/article/details/55049182 Platform: RK3399 OS: Android 6.0 Version: ...
随机推荐
- h5分线程Worker
<!DOCTYPE html><html><head> <meta charset="UTF-8"> <title>Ti ...
- EZOJ #362历史
分析 就是保存前pi-1个数每个ai出现多少次 然后维护这些数当前剩余的最大值 每次和新加进来的比较即可 如果新的大直接取 否则新的最大值一定不大于原来的最大值 因此o(n) 代码 #include& ...
- Mac自带服务器的应用
Mac OS下自带了apache,方便部署一些静态数据(html,css,js,xml,图片等数据) 一.系统默认路径 系统默认是隐藏apache安装目录的,但我们可以通过“命令行”或者“文件夹前往” ...
- springcloud核心组件Eureka、Ribbon、Feign、Hystrix、Zuul
各个组件角色扮演: Eureka:各个服务启动时,Eureka Client都会将服务注册到Eureka Server,并且Eureka Client还可以反过来从Eureka Server拉取 ...
- [转]Hook executed successfully but returned HTTP 403
原文地址:https://www.cnblogs.com/chenglc/p/11174530.html jenkins配置gitlab的webhook,完成配置,测试结果显示 Hook execut ...
- Java8默认方法
Java8引入的接口默认方法实现一个新的概念.此功能是为了向后兼容性增加,使旧接口可用于利用JAVA8. lambda表达式的能力,例如,列表或集合接口不具备forEach方法声明.从而增加了这样的方 ...
- httpclient模拟服务器请求
// 创建默认的httpClient实例. CloseableHttpClient httpclient = HttpClients.createDefault(); // 创建httppost Ht ...
- 自动发现项目中的url
def check_url_exclude(url): """ 判断url是否需要自动被发现,如果不是则移除 :param url: 自动发现的url :return: ...
- P2747 [USACO5.4]周游加拿大Canada Tour
题目描述 你赢得了一场航空公司举办的比赛,奖品是一张加拿大环游机票.旅行在这家航空公司开放的最西边的城市开始,然后一直自西向东旅行,直到你到达最东边的城市,再由东向西返回,直到你回到开始的城市.除了旅 ...
- 爬虫之requests模块的使用
requests模块 概念:基于网络请求的模块 作用:用来模拟浏览器发请求,从而实现爬虫 环境安装:pip install requests 编码流程: 指定url 发起请求 获取响应数据 持久化存储 ...