System Services

The System Services is a set of about 60-80 services that provide the user applications with the information and capabilities necessary to work. All these services relay on the Binder to communicate and collaborate with each other. Also, applications can instantiate these services through Binder. Most are written in Java except for a couple of them, which are written in C/C++. The reason is that those C/C++ services relay on hardware acceleration or are in need or a high performance implementation. A good example is the sensor service or the surface flinger. Other services like camera service or media player, make an extensive use of Native Code through JNI calls.

System Server

Among all the services we have to highlight one of them, the System Server. What does make so special to this guy? This guy is the ruler, I like to call it, the Darth Vader of the System. This service starts all the other services, but not only that, it also bootstraps the file infrastructure, with critical directories like /data/user. It creates each service and add it to the Service Manager. Most of those services are Java-built as you can see in the picture below. This services are JNI friendly 🙂 , so don’t be surprise to find JNI calls to Native code.

System Services

System Services (from Embedded Android)

If you want your android to boot faster, you only have to comment out some of those services. Also, you might want to substitute some of those services for your own implementation. If you want your own Wallpaper Service for example, you can modify the service itself or substitute the whole thing. Modify it would be easer, though. You can also make some Java service Native.

Following is a little snippet of actions that take place in System Server (SystemServer.java)

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);

...

I will not talk about Media Service by now, that requires an extensive posts and I don’t want to make this too heavy.
Next day we will talk about the Services Manager.

Advertisements

2 Trackbacks to “System Services”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: