文件目录 java/com/Android/server/systemserver/
函数入口
/**
* The main entry point from zygote.
*/
public static void main(String[] args) {
new SystemServer().run();
}
构造了一个新的对象后调用了方法run();
public SystemServer() {
// Check for factory test mode.
mFactoryTestMode = FactoryTest.getMode();
// Remember if it's runtime restart(when sys.boot_completed is already set) or reboot
mRuntimeRestart = "1".equals(SystemProperties.get("sys.boot_completed"));
}
private void run() {
try {
traceBeginAndSlog("InitBeforeStartServices");//log,标记位
// 时区相关设置
。。。。。
//语言
。。。。。
Binder.setWarnOnBlocking(true);//事物阻塞时候进程发出警告
Slog.i(TAG, "Entered the Android system server!");
int uptimeMillis = (int) SystemClock.elapsedRealtime();
EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_SYSTEM_RUN, uptimeMillis);
if (!mRuntimeRestart) {
MetricsLogger.histogram(null, "boot_system_server_init", uptimeMillis);
}
SystemProperties.set("persist.sys.dalvik.vm.lib.2", VMRuntime.getRuntime().vmLibrary());
VMRuntime.getRuntime().clearGrowthLimit();//清理内存获取最大的内存
//system server应该一直处于运行中,需要让它尽可能高效的使用内存,强程序堆内存的处理效率,干预GC
VMRuntime.getRuntime().setTargetHeapUtilization(0.8f);
Build.ensureFingerprintProperty();//某些设备依赖于指纹,所以在booting 引导之前就进行定义初始化
Environment.setUserRequired(true);//在system server中,没有指定一个用户就去访问环境目录是错误的
BaseBundle.setShouldDefuse(true);//任何传入的bundle应该避免被抛出BadParcelableException异常
BinderInternal.disableBackgroundScheduling(true);//保证binder的调用在后台
BinderInternal.setMaxThreads(sMaxBinderThreads);//设置binder线程最大数字,增加数量
// 准备looper
android.os.Process.setThreadPriority(
android.os.Process.THREAD_PRIORITY_FOREGROUND);//设置前台进程
android.os.Process.setCanSelfBackground(false);//不能变成后台进程
Looper.prepareMainLooper();
//加载本地方法线程
System.loadLibrary("android_servers");
performPendingShutdown();//检查上次关机是否正常
createSystemContext();//初始化上下文
// 创建service manager.主要用来管理service
mSystemServiceManager = new SystemServiceManager(mSystemContext);
mSystemServiceManager.setRuntimeRestarted(mRuntimeRestart);
LocalServices.addService(SystemServiceManager.class, mSystemServiceManager);
SystemServerInitThreadPool.get();// 为可以并行化的init任务准备线程池
} finally {
traceEnd(); // InitBeforeStartServices
}
// 启动service
try {
traceBeginAndSlog("StartServices");
startBootstrapServices();
startCoreServices();
startOtherServices();//下面分析
SystemServerInitThreadPool.shutdown();
} catch (Throwable ex) {
Slog.e("System", "******************************************");
Slog.e("System", "************ Failure starting system services", ex);
throw ex;
} finally {
traceEnd();
}
// For debug builds, log event loop stalls to dropbox for analysis.
if (StrictMode.conditionallyEnableDebugLogging()) {
Slog.i(TAG, "Enabled StrictMode for system server main thread.");
}
if (!mRuntimeRestart && !isFirstBootOrUpgrade()) {
int uptimeMillis = (int) SystemClock.elapsedRealtime();
MetricsLogger.histogram(null, "boot_system_server_ready", uptimeMillis);
final int MAX_UPTIME_MILLIS = 60 * 1000;
if (uptimeMillis > MAX_UPTIME_MILLIS) {
Slog.wtf(SYSTEM_SERVER_TIMING_TAG,
"SystemServer init took too long. uptimeMillis=" + uptimeMillis);
}
}
// Loop f一直在轮询.
Looper.loop();
throw new RuntimeException("Main thread loop unexpectedly exited");
}
-startOtherServices
//startOtherServices启动不是特别重要的service,状态栏信息显示,比如电池,wifi信号,3G/4G等icon显示,通知面板,比如系统消息, //第三方应用消息,/近期任务栏显示面板,比如长按近期任务快捷键,显示近期使用的应用,截图服务,壁纸服务
startOtherServices(){
//启动很多service
mActivityManagerService.systemReady(new Runnable() {
@Override
public void run() {
Slog.i(TAG, "Making services ready");
......
Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "StartSystemUI");
//启动一些service TwilightService,Lifecycle等等
try {
startSystemUi(context);//启动SystemUIService启动一系列服
} catch (Throwable e) {
reportWtf("starting System UI", e);
}
Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
......
}
});
}