Android12开机各阶段总结

最近公司启动android12的项目,新项目开机启动的问题比较多,在此记录下开机各个阶段的关键日志。

发布日期 2021-10-15

前言

之前对android10上的开机流程有过详细的分析学习,感兴趣的可以点击底部的链接学习,最近公司启动android12的项目,新项目开机启动的问题比较多,在此记录下开机各个阶段的关键日志,方便在分析问题的时候快速定位问题。

关键log

我们先用命令: adb logcat -b events|grep boot过滤出启动阶段的主要事件。可以看到关键log基本把开机的每一步都打印了出来


01-01 13:38:52.139   391   391 I boot_progress_start: 15452
01-01 13:38:53.329   391   391 I boot_progress_preload_start: 16641
01-01 13:38:56.675   391   391 I boot_progress_preload_end: 19989
01-01 13:38:57.020  1729  1729 I boot_progress_system_run: 20333
01-01 13:38:57.824  1729  1729 I boot_progress_pms_start: 21137
01-01 13:38:58.865  1729  1729 I boot_progress_pms_system_scan_start: 22179
01-01 13:39:08.852  1729  1729 I boot_progress_pms_data_scan_start: 32166
01-01 13:39:08.907  1729  1729 I boot_progress_pms_scan_end: 32221
01-01 13:39:10.109  1729  1729 I boot_progress_pms_ready: 33422
01-01 13:39:12.557  1729  1729 I boot_progress_ams_ready: 35871
01-01 13:39:15.189  1729  1782 I boot_progress_enable_screen: 38503
01-01 13:39:17.973   290   321 I sf_stop_bootanim: 41287
01-01 13:39:18.887  1729  1961 I wm_boot_animation_done: 42201

下表是开机各个阶段的描述

阶段 描述
boot_progress_start 系统进入用户空间,标志着kernel启动完成
boot_progress_preload_start Zygote启动
boot_progress_preload_end Zygote结束
boot_progress_system_run SystemServer ready,开始启动Android系统服务
boot_progress_pms_start PMS开始扫描安装的应用
boot_progress_pms_system_scan_start PMS先行扫描/system目录下的安装包
boot_progress_pms_data_scan_start PMS扫描/data目录下的安装包
boot_progress_pms_scan_end PMS扫描结束
boot_progress_pms_ready PMS就绪
boot_progress_ams_ready AMS就绪
boot_progress_enable_screen AMS启动完成后开始激活屏幕,从此以后屏幕才能响应用户的触摸,它在WindowManagerService发出退出开机动画的时间节点之前
sf_stop_bootanim SF设置service.bootanim.exit属性值为1,标志系统要结束开机动画了
wm_boot_animation_done 开机动画结束,这一步用户能直观感受到开机结束

代码梳理

boot_progress_start

frameworks/base/core/jni/AndroidRuntime.cpp


void AndroidRuntime::start(const char* className, const Vector<String8>& options, bool zygote)
{
    ...
    /*
     * 'startSystemServer == true' means runtime is obsolete and not run from
     * init.rc anymore, so we print out the boot start event here.
     */
    for (size_t i = 0; i < options.size(); ++i) {
        if (options[i] == startSystemServer) {
            primary_zygote = true;
           /* track our progress through the boot sequence */
           const int LOG_BOOT_PROGRESS_START = 3000;
           LOG_EVENT_LONG(LOG_BOOT_PROGRESS_START,  ns2ms(systemTime(SYSTEM_TIME_MONOTONIC)));
           addBootEvent("boot_progress_start");  //输出日志
        }
    }
    ...
}

boot_progress_preload_start

boot_progress_preload_end

/frameworks/base/core/java/com/android/internal/os/ZygoteInit.java


 public static void main(String[] argv) {
       ...
    // In some configurations, we avoid preloading resources and classes eagerly.
             // In such cases, we will preload things prior to our first fork.
          if (!enableLazyPreload) {
                 bootTimingsTraceLog.traceBegin("ZygotePreload");
                 EventLog.writeEvent(LOG_BOOT_PROGRESS_PRELOAD_START,SystemClock.uptimeMillis());
                 /// M: Added for BOOTPROF //输出日志
                 addBootEvent("Zygote:Preload Start");
                 /// @}
                preload(bootTimingsTraceLog); //输出日志
                EventLog.writeEvent(LOG_BOOT_PROGRESS_PRELOAD_END, SystemClock.uptimeMillis());
                 bootTimingsTraceLog.traceEnd(); // ZygotePreload
             }
             // Do an initial gc to clean up after startup
              bootTimingsTraceLog.traceBegin("PostZygoteInitGC");
              gcAndFinalize();
              bootTimingsTraceLog.traceEnd(); // PostZygoteInitGC
              bootTimingsTraceLog.traceEnd(); // ZygoteInit
              Zygote.initNativeState(isPrimaryZygote);
             /// M: Added for BOOTPROF
        addBootEvent("Zygote:Preload End");
		...
	}

boot_progress_system_run

frameworks/base/services/java/com/android/server/SystemServer.java

    private void run() {
        ...
            // Here we go!
            Slog.i(TAG, "Entered the Android system server!");
            final long uptimeMillis = SystemClock.elapsedRealtime();
            EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_SYSTEM_RUN, uptimeMillis); //输出日志
            if (!mRuntimeRestart) {
                FrameworkStatsLog.write(FrameworkStatsLog.BOOT_TIME_EVENT_ELAPSED_TIME_REPORTED,
                        FrameworkStatsLog.BOOT_TIME_EVENT_ELAPSED_TIME__EVENT__SYSTEM_SERVER_INIT_START,
                        uptimeMillis);
            }
        ...
    }

boot_progress_pms_start

frameworks/base/services/core/java/com/android/server/pm/PackageManagerService.java

    public PackageManagerService(Injector injector, boolean onlyCore, boolean factoryTest) {
        ...
        mInjector = injector;
        mInjector.bootstrap(this);
        mLock = injector.getLock();
        mInstallLock = injector.getInstallLock();
        LockGuard.installLock(mLock, LockGuard.INDEX_PACKAGES);
        EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_PMS_START,SystemClock.uptimeMillis());//输出日志
        ...
    }

boot_progress_pms_system_scan_start

frameworks/base/services/core/java/com/android/server/pm/PackageManagerService.java

    public PackageManagerService(Injector injector, boolean onlyCore, boolean factoryTest) {
        ...
            long startTime = SystemClock.uptimeMillis();

            EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_PMS_SYSTEM_SCAN_START, startTime); //输出日志
            final String bootClassPath = System.getenv("BOOTCLASSPATH");
            final String systemServerClassPath = System.getenv("SYSTEMSERVERCLASSPATH");
            if (bootClassPath == null) {
                Slog.w(TAG, "No BOOTCLASSPATH found!");
            }
            if (systemServerClassPath == null) {
                Slog.w(TAG, "No SYSTEMSERVERCLASSPATH found!");
            }
        ...
    }

boot_progress_pms_data_scan_start

frameworks/base/services/core/java/com/android/server/pm/PackageManagerService.java

    public PackageManagerService(Injector injector, boolean onlyCore, boolean factoryTest) {
        ...
            // Remove any shared userIDs that have no associated packages
            mSettings.pruneSharedUsersLPw();
            final long systemScanTime = SystemClock.uptimeMillis() - startTime;
            final int systemPackagesCount = mPackages.size();
            Slog.i(TAG, "Finished scanning system apps. Time: " + systemScanTime
                    + " ms, packageCount: " + systemPackagesCount
                    + " , timePerPackage: "
                    + (systemPackagesCount == 0 ? 0 : systemScanTime / systemPackagesCount)
                    + " , cached: " + cachedSystemApps);
            if (mIsUpgrade && systemPackagesCount > 0) {
                //CHECKSTYLE:OFF IndentationCheck
                FrameworkStatsLog.write(FrameworkStatsLog.BOOT_TIME_EVENT_DURATION_REPORTED,
                    BOOT_TIME_EVENT_DURATION__EVENT__OTA_PACKAGE_MANAGER_SYSTEM_APP_AVG_SCAN_TIME,
                    systemScanTime / systemPackagesCount);
                //CHECKSTYLE:ON IndentationCheck
            }
            if (!mOnlyCore) {
                EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_PMS_DATA_SCAN_START,
                        SystemClock.uptimeMillis()); //输出日志
                scanDirTracedLI(sAppInstallDir, 0, scanFlags | SCAN_REQUIRE_KNOWN, 0,
                        packageParser, executorService);

            }
        ...
    }

boot_progress_pms_scan_end

frameworks/base/services/core/java/com/android/server/pm/PackageManagerService.java

    public PackageManagerService(Injector injector, boolean onlyCore, boolean factoryTest) {
        ...
            // Now that we know all the packages we are keeping,
            // read and update their last usage times.
            mPackageUsage.read(mSettings.mPackages);
            mCompilerStats.read();

            EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_PMS_SCAN_END,
                   SystemClock.uptimeMillis()); //输出日志
            Slog.i(TAG, "Time to scan packages: "
                    + ((SystemClock.uptimeMillis()-startTime)/1000f)
                    + " seconds");
        ...
    }

boot_progress_pms_ready

frameworks/base/services/core/java/com/android/server/pm/PackageManagerService.java

    public PackageManagerService(Injector injector, boolean onlyCore, boolean factoryTest) {
        ...
            // can downgrade to reader
            t.traceBegin("write settings");
            mSettings.writeLPr();
            t.traceEnd();
            EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_PMS_READY,
                    SystemClock.uptimeMillis()); //输出日志
        ...
    }

boot_progress_ams_ready

frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java

    public void systemReady(final Runnable goingCallback, @NonNull TimingsTraceAndSlog t) {
        ...****
        t.traceEnd(); // KillProcesses
        Slog.i(TAG, "System now ready");
        EventLogTags.writeBootProgressAmsReady(SystemClock.uptimeMillis());  //输出日志
        t.traceBegin("updateTopComponentForFactoryTest");
        mAtmInternal.updateTopComponentForFactoryTest();
        t.traceEnd();
        ...
    }

boot_progress_enable_screen

frameworks/base/services/core/java/com/android/server/wm/ActivityTaskManagerService.java

        @Override
        public void enableScreenAfterBoot(boolean booted) {
            writeBootProgressEnableScreen(SystemClock.uptimeMillis()); //输出日志
            mWindowManager.enableScreenAfterBoot();
            synchronized (mGlobalLock) {
                updateEventDispatchingLocked(booted);
            }
        }

在这里插入图片描述

sf_stop_bootanim

frameworks/native/services/surfaceflinger/SurfaceFlinger.cpp

void SurfaceFlinger::bootFinished()
{
    ...
    // stop boot animation
    // formerly we would just kill the process, but we now ask it to exit so it
    // can choose where to stop the animation.
    property_set("service.bootanim.exit", "1");

    const int LOGTAG_SF_STOP_BOOTANIM = 60110;
    LOG_EVENT_LONG(LOGTAG_SF_STOP_BOOTANIM, ns2ms(systemTime(SYSTEM_TIME_MONOTONIC))); //输出日志
    sp<IBinder> input(defaultServiceManager()->getService(String16("inputflinger")));
    ...
}

wm_boot_animation_done

frameworks/base/services/core/java/com/android/server/wm/WindowManagerService.java

    private void performEnableScreen() {
        ...
            try {
                IBinder surfaceFlinger = ServiceManager.getService("SurfaceFlinger");
                if (surfaceFlinger != null) {
                    ProtoLog.i(WM_ERROR, "******* TELLING SURFACE FLINGER WE ARE BOOTED!");
                    Parcel data = Parcel.obtain();
                    data.writeInterfaceToken("android.ui.ISurfaceComposer");
                    surfaceFlinger.transact(IBinder.FIRST_CALL_TRANSACTION, // BOOT_FINISHED
                            data, null, 0);
                    data.recycle();
                }
            } catch (RemoteException ex) {
                ProtoLog.e(WM_ERROR, "Boot completed: SurfaceFlinger is dead!");
            }

            EventLogTags.writeWmBootAnimationDone(SystemClock.uptimeMillis()); //输出日志
            Trace.asyncTraceEnd(TRACE_TAG_WINDOW_MANAGER, "Stop bootanim", 0);
            mDisplayEnabled = true;
            ProtoLog.i(WM_DEBUG_SCREEN_ON, "******************** ENABLING SCREEN!");
        ...
    }

开机动画流程

定义动画

/frameworks/base/cmds/bootanimation/bootanim.rc

service bootanim /system/bin/bootanimation
    class core animation
    user graphics
    group graphics audio
    disabled
    oneshot
    ioprio rt 0
    task_profiles MaxPerformance

启动动画

frameworks/native/services/surfaceflinger/SurfaceFlinger.cpp

// Do not call property_set on main thread which will be blocked by init
// Use StartPropertySetThread instead.
void SurfaceFlinger::init() {
    ...
    // Inform native graphics APIs whether the present timestamp is supported:
    const bool presentFenceReliable =
            !getHwComposer().hasCapability(hal::Capability::PRESENT_FENCE_IS_NOT_RELIABLE);
    mStartPropertySetThread = getFactory().createStartPropertySetThread(presentFenceReliable);
    if (mStartPropertySetThread->Start() != NO_ERROR) {
        ALOGE("Run StartPropertySetThread failed!");
    }
    ALOGV("Done initializing");
}

frameworks/native/services/surfaceflinger/StartPropertySetThread.cpp

bool StartPropertySetThread::threadLoop() {
    // Set property service.sf.present_timestamp, consumer need check its readiness
    property_set(kTimestampProperty, mTimestampPropertyValue ? "1" : "0");
    // Clear BootAnimation exit flag
    property_set("service.bootanim.exit", "0"); // 设置开机动画标志位为0
    property_set("service.bootanim.progress", "0");
    // Start BootAnimation if not started
    property_set("ctl.start", "bootanim");
    // Exit immediately
    return false;
}

关闭动画

frameworks/native/services/surfaceflinger/SurfaceFlinger.cpp

void SurfaceFlinger::bootFinished()
{
    ...
    // stop boot animation
    // formerly we would just kill the process, but we now ask it to exit so it
    // can choose where to stop the animation.
    property_set("service.bootanim.exit", "1"); // 设置开机动画标志位为1
    ...
}

frameworks/base/cmds/bootanimation/BootAnimation.cpp

bool BootAnimation::playAnimation(const Animation& animation) {
    ...
            for (size_t j=0 ; j<fcount ; j++) {
                ...
                checkExit();
            }
    ...
}

frameworks/base/cmds/bootanimation/BootAnimation.cpp


void BootAnimation::checkExit() {
    // Allow surface flinger to gracefully request shutdown
    char value[PROPERTY_VALUE_MAX];
    property_get(EXIT_PROP_NAME, value, "0");
    int exitnow = atoi(value);
    if (exitnow) {
        requestExit();
    }
}

这里会一直检测 service.bootanim.exit 的值,当属性值为1的时候,则开机动画会requestExit,从而结束开机动画。那是谁给service.bootanim.exit 的属性值设置为1呢?答案就是在WMS的 performEnableScreen()方法里面

/frameworks/base/services/core/java/com/android/server/wm/WindowManagerService.java


  private void performEnableScreen() {
   ...
   if (!mBootAnimationStopped) {
   Trace.asyncTraceBegin(TRACE_TAG_WINDOW_MANAGER, "Stop bootanim", 0);
        // stop boot animation
        // formerly we would just kill the process, but we now ask it to exit so it
        // can choose where to stop the animation.
        SystemProperties.set("service.bootanim.exit", "1"); // 设置标志位为1
        mBootAnimationStopped = true;
	...
 }

到了这里,最终通过设置service.bootanim.exit的值,stop掉了开机动画,接着就是发送开机广播了。

总结

本篇文章简单介绍了android12开机流程的一些关键的日志和开机动画的简要流程,实际的流程要比这个复杂得多,详细流程可以通过阅读android源码去学习,以下附录为android10上开机流程的详细文章,可供借鉴学习

附录

Android10系统启动概述
Android10系统启动之init进程详解
Android10系统启动之Zygote进程详解
Android10系统启动之SystemServer进程详解
Android10系统启动之AMS服务启动详解
Android10系统启动之Launcher启动详解