SystemServer启动分析

  •  简单来说,systemServer就是系统用来启动各种service的入口,安卓系统在启动的时候, 会初 始化两个重要的部分,一个是zygote进程,另一个是由zygote进程fork出来的SystemServer进程,SystemServer会启动我们系统中所需要的一系列service,下面会做分析。

        SystemServer由zygote进程启动。启动后,会调用SystemServer.main()方法,如下:

    /**

     * The main entry point from zygote.

     */

    public static void main(String[] args) {

        new SystemServer().run();

    }

    其中,创建SystemServer实例并调用run()方法,在run方法中进行了一些初始化的工作,例如调用createSystemContext创建系统Context。如下:

    private void createSystemContext() {

        ActivityThread activityThread = ActivityThread.systemMain();

        mSystemContext = activityThread.getSystemContext();

        mSystemContext.setTheme(android.R.style.Theme_DeviceDefault_Light_DarkActionBar);

    }

也是在run()方法中,创建并启动了各项系统服务,

创建SystemServerManager:

        // Create the system service manager.

        mSystemServiceManager = new SystemServiceManager(mSystemContext);

        LocalServices.addService(SystemServiceManager.class, mSystemServiceManager);

启动各种service:

        try {

            startBootstrapServices();

            startCoreServices();

            startOtherServices();

        } catch (Throwable ex) {

            Slog.e("System", "******************************************");

            Slog.e("System", "************ Failure starting system services", ex);

            throw ex;

        }

从谷歌给我们的注释我们给出以上三个方法简单的解释:

  •  startBootstrapServices() 主要是用来开启一些系统级别的service,这些service具有高度的复杂的相互依赖关系,所以我们需要把他们的初始化放在同一个地方
  •  startCoreServices() 主要是启动一些核心的Service,但是这些service跟我们的bootservice没有相互依赖关系的,是相对独立的服务
  •  startOtherServices() 正如英文所示,这是一些费关键非核心的service

分析startBootstrapServices()方法

    private void startBootstrapServices() {

        // Wait for installd to finish starting up so that it has a chance to

        // create critical directories such as /data/user with the appropriate

        // permissions.  We need this to complete before we initialize other services.

        Installer installer = mSystemServiceManager.startService(Installer.class);

        // Activity manager runs the show.

        mActivityManagerService = mSystemServiceManager.startService(

                ActivityManagerService.Lifecycle.class).getService();

        mActivityManagerService.setSystemServiceManager(mSystemServiceManager);

        mActivityManagerService.setInstaller(installer);

        mPowerManagerService = mSystemServiceManager.startService(PowerManagerService.class);

        // Now that the power manager has been started, let the activity manager

        // initialize power management features.

        mActivityManagerService.initPowerManagement();

        // Manages LEDs and display backlight so we need it to bring up the display.

        mSystemServiceManager.startService(LightsService.class);

        // Display manager is needed to provide display metrics before package manager

        // starts up.

        mDisplayManagerService = mSystemServiceManager.startService(DisplayManagerService.class);

        // We need the default display before we can initialize the package manager.

        mSystemServiceManager.startBootPhase(SystemService.PHASE_WAIT_FOR_DEFAULT_DISPLAY);

        // Start the package manager.

        Slog.i(TAG, "Package Manager");

        mPackageManagerService = PackageManagerService.main(mSystemContext, installer,

                mFactoryTestMode != FactoryTest.FACTORY_TEST_OFF, mOnlyCore);

        mFirstBoot = mPackageManagerService.isFirstBoot();

        mPackageManager = mSystemContext.getPackageManager();

        // Set up the Application instance for the system process and get started.

        mActivityManagerService.setSystemProcess();

        // The sensor service needs access to package manager service, app ops

        // service, and permissions service, therefore we start it after them.

        startSensorService();

    }

这段代码的核心

    traceBeginAndSlog("StartInstaller");

    Installer installer = mSystemServiceManager.startService(Installer.class);

    traceEnd();

    // In some cases after launching an app we need to access device identifiers,

    // therefore register the device identifier policy before the activity manager.

    traceBeginAndSlog("DeviceIdentifiersPolicyService");

    mSystemServiceManager.startService(DeviceIdentifiersPolicyService.class);

    traceEnd();

这段代码的执行,就是为了先启动installer,这样android才有机会去创建一些关键的路径(data/user),这些都需要在其他Service启动前完成。其次,通过mSystemServiceManager.startService(DeviceIdentifiersPolicyService.class);这段代码,android需要注册当前的设备表示,以防有一些特殊的时候需要用到。

我们进入install类的onStart()方法一看

@Override

public void onStart() {

    if (mIsolated) {

        mInstalld = null;

    } else {

        connect();

    }

}  

private void connect() {

    IBinder binder = ServiceManager.getService("installd");

    if (binder != null) {

        try {

            binder.linkToDeath(new DeathRecipient() {

                @Override

                public void binderDied() {

                    Slog.w(TAG, "installd died; reconnecting");

                    connect();

                }

            }, 0);

        } catch (RemoteException e) {

            binder = null;

        }

    }

    if (binder != null) {

        mInstalld = IInstalld.Stub.asInterface(binder);

        try {

            invalidateMounts();

        } catch (InstallerException ignored) {

        }

    } else {

        Slog.w(TAG, "installd not found; trying again");

        BackgroundThread.getHandler().postDelayed(() -> {

            connect();

        }, DateUtils.SECOND_IN_MILLIS);

    }

}

我们可以看到,其实这就是一个递归。install通过调用connect,去判断installer是否被初始化。只有installer被初始化了,才会继续往下掉用,初始化其他的服务。

接下来,启动ActivityManagerService。我们通过代码分析:

    mActivityManagerService = mSystemServiceManager.startService(

            ActivityManagerService.Lifecycle.class).getService();

    mActivityManagerService.setSystemServiceManager(mSystemServiceManager);

    mActivityManagerService.setInstaller(installer);

我们开启ams传入的是 ActivityManagerService.Lifecycle.class,同时把installer传入给ams。我们进入AMS查看代码,可以看到

final void finishBooting() {

    ...

    for (String abi : Build.SUPPORTED_ABIS) {

        zygoteProcess.establishZygoteConnectionForAbi(abi);

        final String instructionSet = VMRuntime.getInstructionSet(abi);

        if (!completedIsas.contains(instructionSet)) {

            try {

                mInstaller.markBootComplete(VMRuntime.getInstructionSet(abi));

            } catch (InstallerException e) {

                Slog.w(TAG, "Unable to mark boot complete for abi: " + abi + " (" +

                        e.getMessage() +")");

            }

            completedIsas.add(instructionSet);

        }

    }

  ...

 }

所以finishBooting是ams和zygote进行通讯的入口。

startBootstrapServices()方法中,还启动了很多其他的services,包括PowerManagerService、RecoverySystemService、LightsService、DisplayManagerService等等,这里就不重复做出分析了。

分析startCoreServices()方法

private void startCoreServices() {

    // Records errors and logs, for example wtf()

    traceBeginAndSlog("StartDropBoxManager");

    mSystemServiceManager.startService(DropBoxManagerService.class);

    traceEnd();

    traceBeginAndSlog("StartBatteryService");

    // Tracks the battery level.  Requires LightService.

    mSystemServiceManager.startService(BatteryService.class);

    traceEnd();

    // Tracks application usage stats.

    traceBeginAndSlog("StartUsageService");

    mSystemServiceManager.startService(UsageStatsService.class);

    mActivityManagerService.setUsageStatsManager(

            LocalServices.getService(UsageStatsManagerInternal.class));

    traceEnd();

    // Tracks whether the updatable WebView is in a ready state and watches for update installs.

    traceBeginAndSlog("St

搜索工具

百度为您找到相关结果约229,000个

artWebViewUpdateService");

    mWebViewUpdateService = mSystemServiceManager.startService(WebViewUpdateService.class);

    traceEnd();

}

这个方法主要启动一些跟bootstrap进程无关的service,这些service是相对独立的服务。

分析startOtherServices()方法:

        这个方法主要是为了整理或者重构一些杂七杂八的包,不太重要,不做分析。

总结:

  • SystemServer进程是android中一个很重要的进程由Zygote进程启动;
  • SystemServer进程主要用于启动系统中的服务;
  • SystemServer进程启动服务的启动函数为main函数;
  • SystemServer在执行过程中首先会初始化一些系统变量,加载类库,创建Context对象,创建SystemServiceManager对象等之后才开始启动系统服务;
  • SystemServer进程将系统服务分为三类:boot服务,core服务和other服务,并逐步启动
  • 创建的系统服务过程中主要通过SystemServiceManager对象来管理,通过调用服务对象的构造方法和onStart方法初始化服务的相关变量;
  • 服务对象都有自己的异步消息对象,并运行在单独的线程中;
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android 12 SystemServer启动流程如下: 1. 引导加载:系统启动时,先加载引导程序,进行硬件初始化、内核加载等操作。 2. Zygote 进程启动:Zygote 是 Android 系统中的一个特殊进程,负责孵化其他应用进程。Zygote 进程会预加载一些常用的类和资源,以加快应用的启动速度。 3. SystemServer 进程启动:Zygote 进程会 fork 出 SystemServer 进程,该进程是 Android 系统中的核心服务进程。SystemServer 进程负责启动和管理系统级别的服务,例如 ActivityManagerService、PackageManagerService、WindowManagerService 等。 4. SystemServer 初始化:SystemServer 进程启动后,会进行一系列的初始化操作。首先会创建 Looper 线程,用于接收消息并处理各个服务的初始化工作。然后依次创建各个系统服务,并调用它们的启动方法。 5. 启动系统服务:SystemServer 进程会按照一定顺序启动各个系统服务。每个系统服务都有自己的初始化流程,例如 PackageManagerService 会加载应用程序列表、数据目录等;ActivityManagerService 会初始化进程间通信机制等。 6. 启动应用进程:在系统服务启动完成后,SystemServer 进程会通过 Zygote 孵化出其他应用进程。应用进程会根据 AndroidManifest.xml 中的配置进行初始化,包括创建 Application、加载资源等。 总结来说,Android 12 SystemServer启动流程包括引导加载、Zygote 进程启动、SystemServer 进程启动、SystemServer 初始化、启动系统服务和启动应用进程等步骤。这些步骤都是为了在系统启动时提供必要的服务和资源。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值