1. 程式人生 > >android原始碼4.4.2----系統啟動過程分析

android原始碼4.4.2----系統啟動過程分析

public class SystemServer {
   private static final String TAG = "SystemServer";

   public static final int FACTORY_TEST_OFF = 0;
   public static final int FACTORY_TEST_LOW_LEVEL = 1;
   public static final int FACTORY_TEST_HIGH_LEVEL = 2;

   static Timer timer;
   static final long SNAPSHOT_INTERVAL = 60 * 60 * 1000; // 1hr

   // The earliest supported time.  We pick one day into 1970, to
   // give any timezone code room without going into negative time.
   private static final long EARLIEST_SUPPORTED_TIME = 86400 * 1000;

   /**
    * Called to initialize native system services.
    */
   private static native void nativeInit();

   public static void main(String[] args) {

       /*
        * In case the runtime switched since last boot (such as when
        * the old runtime was removed in an OTA), set the system
        * property so that it is in sync. We can't do this in
        * libnativehelper's JniInvocation::Init code where we already
        * had to fallback to a different runtime because it is
        * running as root and we need to be the system user to set
        * the property. http://b/11463182
        */
       SystemProperties.set("persist.sys.dalvik.vm.lib",
                            VMRuntime.getRuntime().vmLibrary());

       if (System.currentTimeMillis() < EARLIEST_SUPPORTED_TIME) {
           // If a device's clock is before 1970 (before 0), a lot of
           // APIs crash dealing with negative numbers, notably
           // java.io.File#setLastModified, so instead we fake it and
           // hope that time from cell towers or NTP fixes it
           // shortly.
           Slog.w(TAG, "System clock is before 1970; setting to 1970.");
           SystemClock.setCurrentTimeMillis(EARLIEST_SUPPORTED_TIME);
       }

       if (SamplingProfilerIntegration.isEnabled()) {
           SamplingProfilerIntegration.start();
           timer = new Timer();
           timer.schedule(new TimerTask() {
               @Override
               public void run() {
                   SamplingProfilerIntegration.writeSnapshot("system_server", null);
               }
           }, SNAPSHOT_INTERVAL, SNAPSHOT_INTERVAL);
       }

       // Mmmmmm... more memory!
       dalvik.system.VMRuntime.getRuntime().clearGrowthLimit();

       // The system server has to run all of the time, so it needs to be
       // as efficient as possible with its memory usage.
       VMRuntime.getRuntime().setTargetHeapUtilization(0.8f);

       Environment.setUserRequired(true);

       System.loadLibrary("android_servers");

       Slog.i(TAG, "Entered the Android system server!");

       // Initialize native services.
       nativeInit();

       // This used to be its own separate thread, but now it is
       // just the loop we run on the main thread.
       ServerThread thr = new ServerThread();
       thr.initAndLoop();
   }
}

1.設定執行環境庫 
SystemProperties.set("persist.sys.dalvik.vm.lib",
                            VMRuntime.getRuntime().vmLibrary());
2.設定系統時間
if (System.currentTimeMillis() < EARLIEST_SUPPORTED_TIME) {
           // If a device's clock is before 1970 (before 0), a lot of
           // APIs crash dealing with negative numbers, notably
           // java.io.File#setLastModified, so instead we fake it and
           // hope that time from cell towers or NTP fixes it
           // shortly.
           Slog.w(TAG, "System clock is before 1970; setting to 1970.");
           SystemClock.setCurrentTimeMillis(EARLIEST_SUPPORTED_TIME);
       }
       
3.效能統計
if (SamplingProfilerIntegration.isEnabled()) {
           SamplingProfilerIntegration.start();
           timer = new Timer();
           timer.schedule(new TimerTask() {
               @Override
               public void run() {
                   SamplingProfilerIntegration.writeSnapshot("system_server", null);
               }
           }, SNAPSHOT_INTERVAL, SNAPSHOT_INTERVAL);
       }

4.設定記憶體上限
// Mmmmmm... more memory!
       dalvik.system.VMRuntime.getRuntime().clearGrowthLimit();

       // The system server has to run all of the time, so it needs to be
       // as efficient as possible with its memory usage.
       VMRuntime.getRuntime().setTargetHeapUtilization(0.8f);
       
5.設定使用者,否則無法使用環境變數
Environment.setUserRequired(true);


6.載入系統服務庫
System.loadLibrary("android_servers");

7.初始化本地服務----SensorService--硬體相關
nativeInit();

static void android_server_SystemServer_nativeInit(JNIEnv* env, jobject clazz) {
   char propBuf[PROPERTY_VALUE_MAX];
   property_get("system_init.startsensorservice", propBuf, "1");
   if (strcmp(propBuf, "1") == 0) {
       // Start the sensor service
       SensorService::instantiate();
   }
}

8.系統服務執行緒--完成系統各種服務的啟動
ServerThread thr = new ServerThread();
        thr.initAndLoop();