安卓中的system service

系统服务的代码路径: framework/base/services/java/com/android/server 
这些系统服务大部分都have a JNI counterpart, through which these services talk to lower layers like the kernel.
用adb shell模式下的service list命令可以得到服务列表。
 
System services start-up
After boot-up, the Linux kernel starts the init process, which is the grand-daddy of all processes in the system, with a PID = 1. The following steps explain the start-up procedure until the Home screen shows up:
1.     A set of native services (written in C/C++) like vold, netd, installd and debuggerd are started. These services have root permission.
2.     Then the Service Manager and Media Server are started. These run with the system’s permission.
3.     init then starts the app_process  (frameworks/base/cmds/app_process/app_main.cpp). This launches the zygote process, in a Dalvik VM via frameworks/base/core/jni/AndroidRuntime.cpp and frameworks/base/core/java/com/android/internal/os/ZygoteInit.java. This zygote process then becomes the parent of every application.
a.     When the zygote starts, it has an active instance of the Dalvik VM that contains the classes required by the applications pre-loaded in its address space. In Android, each application runs its own Dalvik VM. When an application is started, zygote forks a child process (which is the app process); both the zygote and the app processes use the same VM instance until the child does a write [modifies the data], in which case a copy of the Dalvik VM is created, for the child. This ensures a faster start-up time for the applications. Thus the zygote becomes the parent for all the applications.
安卓中每个应用运行在自己的Dalvik VM中,当一个应用启动时,zygote新建一个子进程,开始时zygote和子进程(app的进程)使用同一个VM实例,直到子进程有写操作,这时会为子进程新建一个DVM实例。这样可以保证应用的快速启动。如此zygnote成为所有应用的parent。
b.     We can actually check this in the adb shell using the ps commands.
ps | grep zygote and ps | grep com.android
 
…will reveal this relationship.
zygote启动system_server进程,system_server进程启动其他系统服务。
4.     The zygote starts the system_server process. This code is in the /init.rc file in an Android phone

5. This system_server process then starts all the system services. These services run as threads inside the system_server process. This can be verified by running the following command in the adb shell. This command lists the names of the threads running as part of the system_server process.

$: cat /proc/`pidof system_server`/task/*/comm
 
The system server code is in frameworks/base/services/java/com/android/server/, named asSystem_server.java.
 
6.     One of the services is the ActivityManagerService, which as a final step of its initialisation, starts the Launcher application (which brings up the Home Screen) and sends the BOOT_COMPLETE Intent. This intent signals to the rest of the platform that the system has completed boot up.    
ActivityManagerService启动Launcher,之后进入home screen。
====================================================================
system_server这个进程先启动,之后由它来启动其他所有的系统服务,it also bootstraps file infrastructure, with critical directories like /data/user。
它启动每个服务,并将每个服务加入到Service Manager中。
 
 
system server的部分代码(SystemServer.java)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
class ServerThread extends Thread {
    private static final String TAG = "SystemServer";
    private static final String ENCRYPTING_STATE = "trigger_restart_min_framework";
    private static final String ENCRYPTED_STATE = "1";
 
    ContentResolver mContentResolver;
 
    void reportWtf(String msg, Throwable e) {
        Slog.w(TAG, "***********************************************");
        Log.wtf(TAG, "BOOT FAILURE " + msg, e);
    }
 
    @Override
    public void run() {
        EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_SYSTEM_RUN,
        SystemClock.uptimeMillis());
 
        Looper.prepareMainLooper();
 
        android.os.Process.setThreadPriority(
        android.os.Process.THREAD_PRIORITY_FOREGROUND);
 
...
 
        Slog.i(TAG, "Battery Service");
        battery = new BatteryService(context, lights);
        ServiceManager.addService("battery", battery);
 
        Slog.i(TAG, "Vibrator Service");
        vibrator = new VibratorService(context);
        ServiceManager.addService("vibrator", vibrator);
 
...
 
        Slog.i(TAG, "Window Manager");
        wm = WindowManagerService.main(context, power, display, inputManager, uiHandler, wmHandler,
                               factoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL,
                               !firstBoot, onlyCore);
 
        ServiceManager.addService(Context.WINDOW_SERVICE, wm);
        ServiceManager.addService(Context.INPUT_SERVICE, inputManager);
 
        ActivityManagerService.self().setWindowManager(wm);
 
        inputManager.setWindowManagerCallbacks(wm.getInputMonitor());
        inputManager.start();
 
        display.setWindowManager(wm);
        display.setInputManager(inputManager);
 
...
 
===========================================================
Service Manager:
ServiceManager在其他service之前启动,由init启动。
 If Service Manager is the index to all the other System Service it makes sense that is tarted before any other. For that to happen there is only one place for this to happen, and that is in *init.rc*.  After it, follow Zygote, meda, surfaceflinger and drm. These are the first and most basic services started and they are natively implemented. The reason as we said in the first post, they have to be highly CPU efficient.
1
2
3
4
5
service servicemanager /system/bin/servicemanager class core user system group system critical
onrestart restart zygote
onrestart restart media
onrestart restart surfaceflinger
onrestart restart drm
 
 
 
 
 
 
 
 
 
 
 
 

posted on 2016-01-11 09:20  CarrieSmile  阅读(666)  评论(0)    收藏  举报