【Java精品源码栏目提醒】:网学会员鉴于大家对Java精品源码十分关注,论文会员在此为大家搜集整理了“android开机启动流程说明 - 其它资料”一文,供大家参考学习
android 开机启动流程说明第一步:启动 linux1.Bootloader2.Kernel第二步 android 系统启动:入口为 init.rcsystemcorerootdir1./system/bin/service manager: Binder 守护进程;2.Runtime3.Zygote :app-process/app-main4.Start VM5.Start server6.Start android service:Register to service Manager7.Start Launcher第三步:应用程序启动:运行 package Manager l Init 进程 Android 系统在启动时首先会启动 Linux 系统,引导加载 Linux Kernel 并启动init 进程。
Init 进程是一个由内核启动的用户级进程,是 Android 系统的第一个进程。
该进程的相关代码在 platformsystemcoreinitinit.c。
在 main 函数中,有如下代码: open_devnull_stdio log_init INFOquotreading config filenquot init_parse_config_filequot/init.rcquot / pull the kernel commandline and ramdisk properties file in / import_kernel_cmdline0 get_hardware_namehardware amprevision snprintftmp sizeoftmp quot/init.s.rcquot hardware init_parse_config_filetmp 这里会加载解析 init.rc 和 init.hardware.rc 两个初始化脚本。
.rc 文件定义了在 init 进程中需要启动哪些进程服务和执行哪些动作。
其详细说明参见platformsystemcoreinitreademe.txt。
init.rc 见如下定义: service servicemanager /system/bin/servicemanager user system critical onrestart restart zygote onrestart restart media service vold /system/bin/vold socket vold stream 0660 root mount ioprio be 2 service netd /system/bin/netd socket netd stream 0660 root system socket dnsproxyd stream 0660 root inet service debuggerd /system/bin/debuggerd service ril-daemon /system/bin/rild socket rild stream 660 root radio socket rild-debug stream 660 radio system user root group radio cache inet misc audio sdcard_rw service zygote /system/bin/app_process -Xzygote /system/bin --zygote--start-system-server socket zygote stream 666 onrestart write /sys/android_power/request_state wake onrestart write /sys/power/state on onrestart restart media onrestart restart netd service drm /system/bin/drmserver user drm group system root inet 具体解析过程见 platformsystemcoreinitInit_parser.c。
解析所得服务添加到 service_list 中,动作添加到 action_list 中。
接下来在 main 函数中执行动作和启动进程服务: execute_one_command restart_processes 通常 init 过程需要创建一些系统文件夹并启动 USB 守护进程、Android DebugBridge 守护进程、Debug 守护进程、ServiceManager 进程、Zygote 进程等。
l ServiceManager 进程 ServiceManager 进程是所有服务的管理器。
由 init.rc 对 ServiceManager 的描述 service servicemanager /system/bin/servicemanager 可知 servicemanager进程从 platformframeworksbasecmdservicemanagerService_manager.cpp 启动。
在 main 函数中有如下代码: int mainint argc char argv struct binder_state bs void svcmgr BINDER_SERVICE_MANAGER bs binder_open1281024 if binder_become_context_managerbs LOGEquotcannot become context manager snquot strerrorerrno return -1 svcmgr_handle svcmgr binder_loopbs svcmgr_handler return 0 首先调用 binder_open打开 Binder 设备/dev/binder,调用binder_become_context_manager把当前进程设置为 ServiceManager。
ServiceManager 本身就是一个服务。
int binder_become_context_managerstruct binder_state bs return ioctlbs-gtfd BINDER_SET_CONTEXT_MGR 0 最后 binder_loop进入循环状态,并设置 svcmgr_handler 回调函数等待添加、查询、获取服务等请求。
l Zygote 进程 由 Zygote 进程用于产生其他进程。
init.rc 对 zygote 的描述 servicezygot /system/bin/app_process 可知 zygote 进程从platfromframeworksbasecmdsapp_processApp_main.cpp 启动。
在 main 函数中有如下代码: if 0 strcmpquot--zygotequot arg bool startSystemServer i lt argc strcmpargvi quot--start-system-serverquot 0 :false setArgv0argv0 quotzygotequot set_process_namequotzygotequot runtime.startquotcom.android.internal.os.ZygoteInitquot startSystemServer else set_process_nameargv0 runtime.mClassName arg // Remainder of args get passed to startup class main runtime.mArgC argc-i runtime.mArgV argvi LOGVquotApp process is starting with pidd classs.nquot getpid runtime.getClassName runtime.start 首先创建 AppRuntime,即 AndroidRuntime,建立了一个 Dalvik 虚拟机。
从而由 Dalvik通过这个 runtime 传递 com.android.internal.os.ZygoteInit 参数,虚拟机运行 ZygoteInit.
java 的 main,开始创建 Zygote 进程。
在其 main中,如下所示: registerZygoteSocket EventLog.writeEventLOG_BOOT_PROGRESS_PRELOAD_START SystemClock.uptimeMillis preloadClasses //cacheRegisterMaps preloadResources EventLog.writeEventLOG_BOOT_PROGRESS_PRELOAD_END SystemClock.uptimeMillis // Finish profiling the zygote initialization. SamplingProfilerIntegration.writeZygoteSnapshot // Do an initial gc to clean up after startup gc // If requested start system server directly from Zygote if argv.length 2 throw new RuntimeExceptionargv0 USAGE_STRING if argv1.equalsquottruequot startSystemServer else if argv1.equalsquotfalsequot throw new RuntimeExceptionargv0 USAGE_STRING 首先通过 registerZygoteSocket登记端口,接着 preloadClasses装载相关类。
这里大概要装载 1000 多个类,具体装载类见platformframeworksbasepreloaded-classes。
这个文件有WritePreloadedClassFile 类自动生成。
分析该类的 main 函数,有如下一段筛选类的代码: // Preload classes that were loaded by at least 2 processes.Hopefully // the memory associated with these classes will be shared. for LoadedClass loadedClass : root.loadedClasses.values SetltStringgt names loadedClass.processNames if Policy.isPreloadableloadedClass continue if names.size gt MIN_PROCESSES loadedClass.medianTimeMicros gtMIN_LOAD_TIME_MICROS ampamp names.size gt 1 toPreload.addloadedClass int initialSize toPreload.size System.out.printlninitialSize quot classses were loaded by more than one app.quot // Preload eligable classes from applications not long-running // services. for Proc proc : root.processes.values if proc.fromZygote ampamp Policy.isServiceproc.name for Operation operation : proc.operations LoadedClass loadedClass operation.loadedClass if shouldPreloadloadedClass toPreload.addloadedClass 其中 MIN_LOAD_TIME_MICROS 等于 1250,当类的装载时间大于 1.25ms,则需要预装载。
Policy.isPreloadable定于如下: /Reports if the given class should be preloaded. / public static boolean isPreloadableLoadedClass clazz return clazz.systemClassampamp EXCLUDED_CLASSES.containsclazz.name 其中 EXCLUDED_CLASSES 如下定义: / Classes which we shouldnt load from the Zygote. / private static final SetltStringgt EXCLUDED_CLASSES new HashSetltStringgtArrays.asList // Binders quotandroid.app.AlarmManagerquot quotandroid.app.SearchManagerquot quotandroid.os.FileObserverquot quotcom.android.server.PackageManagerServiceAppDirObserverquot // Threads quotandroid.os.AsyncTaskquot quotandroid.pim.ContactsAsyncHelperquot quotjava.lang.ProcessManagerquot 这几个 Binders 和 Thread 是不会被预加载的。
另外还有一些 application 需要装载,要求满足条件 proc.fromZygote且不是属于常驻内存的服务。
SERVICES 定义如下: / Long running services. These are restricted in their contributionto the preloader because their launch time is less critical. / // TODO: Generate this automatically from package manager. private static final SetltStringgt SERVICES newHashSetltStringgtArrays.asList quotsystem_serverquot quotcom.google.process.contentquot quotandroid.process.mediaquot quotcom.android.bluetoothquot quotcom.android.calendarquot quotcom.android.inputmethod.latinquot quotcom.android.phonequot quotcom.google.android.apps.maps.FriendServicequot // pre froyo quotcom.google.android.apps.maps:FriendServicequot // froyo quotcom.google.android.apps.maps.LocationFriendServicequot quotcom.google.android.deskclockquot quotcom.google.process.gappsquot quotandroid.ttsquot preloaded-classes 是在下载
源码的时候生成,WritePreloadedClassFile 类并没有被用到,但可以通过这个类了解 Android 系统对预加载类的默认要求,参考修改 preloaded-classes 文件,减少开机初始化时要预加载的类,提高开机速度。
最后来通过 startSystemServer启动 SystemServer 进程。
见如下代码: / Hardcoded command line to start the system server / String args quot--setuid1000quot quot--setgid1000quot quot--setgroups10011002100310041005100610071008100910101003quot quot--capabilities0104352quot quot--runtime-initquot quot--nice-namesystem_serverquot quotcom.android.server.SystemServerquot ZygoteConnection.Arguments parsedArgs null int pid try parsedArgs new ZygoteConnection.Argumentsargs / Enable debugging of the system process if either thecommand line flags indicate it should be debuggable or the ro.debuggablesystem property is set to quot1quot / int debugFlags parsedArgs.debugFlags if quot1quot.equalsSystemProperties.getquotro.debuggablequot debugFlags Zygote.DEBUG_ENABLE_DEBUGGER / Request to fork the system server process / pid Zygote.forkSystemServer parsedArgs.uid parsedArgs.gid parsedArgs.gids debugFlags null parsedArgs.permittedCapabilities parsedArgs.effectiveCapabilities Zygote 包装了 Linux 的 fork。
forkSystemServer调用forkAndSpecialize,最终穿过虚拟机调用platformdalvikvmnativedalvik_system_Zygote.c 中 由Dalvik_dalvik_system_Zygote_forkAndSpecialize。
dalvik 完成 fork 新的进程。
main最后会调用 runSelectLoopMode,进入 while 循环,由 peers创建新的进程。
l SystemService 进程 SystemService 用于创建 init.rc 定义的服务之外的所有服务。
在main的最后有如下代码: // The system server has to run all of the time so it needs tobe // as efficient as possible with its memory usage. VMRuntime.getRuntime.setTargetHeapUtilization0.8f System.loadLibraryquotandroid_serversquot init1args Init1是在 native 空间实现的,用于启动 native 空间的服务,其实现在com_android_server_SystemServer.cpp 中的android_server_SystemServer_init1: static void android_server_SystemServer_init1JNIEnv env jobjectclazz system_init 而 system_init服务初始化创建 native 层的各个服务:// Start the sensor serviceSensorService::instantiate // On the simulator audioflinger et al dont get started the // same way as on the device and we need to start them here if proc-gtsupportsProcesses // Start the AudioFlinger AudioFlinger::instantiate // Start the media playback service MediaPlayerService::instantiate // Start the camera service CameraService::instantiate // Start the audio policy service AudioPolicyService::instantiate 最后通过如下代码:LOGIquotSystem server: starting Android services.nquot runtime-gtcallStaticquotcom/android/server/SystemServerquot quotinit2quot 回到 SystemServer.
java,调用 init2: public static final void init2 Slog.iTAG quotEntered the Android system serverquot Thread thr new ServerThread thr.setNamequotandroid.server.ServerThreadquot thr.start Init2 启动一个线程,专门用来启动
java 空间的所有服务。
如下代码所示启动部分服务: Slog.iTAG quotContent Managerquot ContentService.maincontext factoryTest SystemServer.FACTORY_TEST_LOW_LEVEL Slog.iTAG quotSystem Content Providersquot ActivityManagerService.installSystemProviders Slog.iTAG quotBattery Servicequot battery new BatteryServicecontext ServiceManager.addServicequotbatteryquot battery Slog.iTAG quotLights Servicequot lights new LightsServicecontext Slog.iTAG quotVibrator Servicequot ServiceManager.addServicequotvibratorquot newVibratorServicecontext // only initialize the power service after we have startedthe // lights service content providers and the batteryservice. power.initcontext lightsActivityManagerService.getDefault battery Slog.iTAG quotAlarm Managerquot AlarmManagerService alarm newAlarmManagerServicecontext ServiceManager.addServiceContext.ALARM_SERVICE alarm 并且把这些服务添加到 ServiceManager 中,以便管理和进程间通讯。
在该线程后半部分,ActivityManagerService 会等待 AppWidget、WallPaper、IMM 等 systemReady 后调用自身的 systemReady。
ActivityManagerServiceServiceManager.getServicequotactivityquot .setWindowManagerwm // Skip Bluetooth if we have an emulator kernel // TODO: Use a more reliable check to see if this productshould // support Bluetooth - see bug 988521 if SystemProperties.getquotro.kernel.qemuquot.equalsquot1quot Slog.iTAG quotRegistering null Bluetooth Serviceemulatorquot ServiceManager.addServiceBluetoothAdapter.BLUETOOTH_SERVICE null else if factoryTest SystemServer.FACTORY_TEST_.
上一篇:
毕业设计:网站程序+论文——梦之道影音工作室网站
下一篇:
缅怀先烈 铭记历史