[Android Gingerbread] The system server in Android

Source: Internet
Author: User

Today found some guy wrote a post about system server and I want to discuss more about this topic.

 

Here are the steps:

1. kernel boot and start init and init will run the command in init. RC.

We can find:

Service zygote/system/bin/app_process-xzygote/system/bin -- zygote -- start-system-Server

Zygote will be started as a server and it will start systemserver.

2. in frameworks/base/CORE/Java/COM/Android/Internal/OS/zygoteinit. java, after preloadclasses and preloadresources, it will check the arguments and start system server directly from zygote if requested

Startsystemserver ();

This will fork a PID for systemserver and start systemserver

3. After this, we can look into frameworks/base/services/Java/COM/Android/Server/systemserver. Java

591 public class systemserver

592 {

593 Private Static final string tag = "systemserver ";

594

595 public static final int factory_test_off = 0;

596 public static final int factory_test_low_level = 1;

597 public static final int factory_test_high_level = 2;

598

599 static timer;

600 static final long snapshot_interval = 60*60*1000; // 1hr

601

602 // The earliest supported time. We pick one day into 1970,

603 // give any timezone code room without going into negative time.

604 Private Static final long earliest_supported_time = 86400*1000;

605

606 /**

607 * this method is called from zygote to initialize the system. This will cause the native

608 * services (surfaceflinger, audioflinger, Etc ..) to be started. After that it will call back

609 * up into init2 () to start the android services.

610 */

611 native public static void init1 (string [] ARGs );

612

613 public static void main (string [] ARGs ){

614 if (system. currenttimemillis () <earliest_supported_time ){

615 // If a device's clock is before 1970 (before 0), a lot

616 // APIs crash dealing with negative numbers, notably

617 // java. Io. File # setlastmodified, So instead we fake it and

618 // hope that time from cell towers or NTP fixes it

619 // shortly.

620 slog. W (TAG, "system clock is before 1970; setting to 1970 .");

621 systemclock. setcurrenttimemillis (earliest_supported_time );

622}

623

624 if (samplingprofilerintegration. isenabled ()){

625 samplingprofilerintegration. Start ();

626 timer = new timer ();

627 timer. Schedule (New timertask (){

628 @ override

629 public void run (){

630 samplingprofilerintegration. writesnapshot ("system_server ");

631}

632}, snapshot_interval, snapshot_interval );

633}

634

635 // The system server has to run all of the time, so it needs to be

636 // as efficient as possible with its memory usage.

637 vmruntime. getruntime (). settargetheaputilization (0.8f );

638

639 system. loadlibrary ("android_servers ");

640 init1 (ARGs );

641}

642

643 public static final void init2 (){

644 slog. I (TAG, "entered the Android system server! ");

645 thread thr = new serverthread ();

646 thr. setname ("android. server. serverthread ");

647 thr. Start ();

648}

649}

 

The first thing that happens is that the server will load a native library called android_servers that provides interfaces to native functionality. source files for this lib are placed in frameworks/base/services/JNI /. then the native init method that will setup native services is called, init1 (ARGs), and executed:

In com_android_server_systemserver.cpp

26 static void android_server_systemserver_init1 (jnienv * ENV, jobject clazz)

27 {

28 system_init ();

29}

30

31 /*

32 * JNI registration.

33 */

34 static jninativemethod gmethods [] = {

35/* name, signature, funcptr */

36 {"init1", "([ljava/lang/string;) V", (void *) android_server_systemserver_init1 },

37 };

38

39 int register_android_server_systemserver (jnienv * env)

40 {

41 return jniregisternativemethods (ENV, "com/Android/Server/systemserver ",

42 gmethods, nelem (gmethods ));

43}


The name of the function that implements this is system_init () and the it resides in frameworks/base/cmds/system_server/library/system_init.cpp:

54 extern "C" status_t system_init ()

55 {

56 Logi ("entered system_init ()");

57

58 sp <processstate> proc (processstate: Self ());

59

60 sp <iservicemanager> Sm = defaultservicemanager ();

61 Logi ("servicemanager: % P/N", Sm. Get ());

62

63 sp <grimreaper> grim = new grimreaper ();

64 Sm-> asbinder ()-> linktodeath (grim, Grim. Get (), 0 );

65

66 char propbuf [property_value_max];

67 property_get ("system_init.startsurfaceflinger", propbuf, "1 ");

68 if (strcmp (propbuf, "1") = 0 ){

69 // start the surfaceflinger

70 surfaceflinger: instantiate ();

71}

72

73 // start the sensor Service

74 sensorservice: instantiate ();

75

76 // on the simulator, audioflinger et al don't get started

77 // same way as on the device, and we need to start them here

78 If (! Proc-> supportsprocesses ()){

79

80 // start the audioflinger

81 audioflinger: instantiate ();

82

83 // start the media playback Service

84 mediaplayerservice: instantiate ();

85

86 // start the camera service

87 cameraservice: instantiate ();

88

89 // start the audio Policy Service

90 audiopolicyservice: instantiate ();

91}

92

93 // and now start the android runtime. We have to do this bit

94 // of nastiness because the android runtime initialization requires

95 // some of the core system services to already be started.

96 // all other servers shoshould just start the android runtime

97 // the beginning of their processes's main (), before calling

98 // The init function.

99 Logi ("system server: Starting Android runtime./N ");

100

101 androidruntime * runtime = androidruntime: getruntime ();

102

103 Logi ("system server: Starting Android services./N ");

104 runtime-> callstatic ("com/Android/Server/systemserver", "init2 ");

105

106 // if running in our own process, just go into the thread

107 // pool. Otherwise, call the initialization finished

108 // func to let this process continue its initilization.

109 If (proc-> supportsprocesses ()){

110 Logi ("system server: Entering thread pool./N ");

111 processstate: Self ()-> startthreadpool ();

112 ipcthreadstate: Self ()-> jointhreadpool ();

113 Logi ("system server: exiting thread pool./N ");

114}

115 return no_error;

116}

117

After setting up the native services there is a callback at line 104:

Runtime-> callstatic ("com/Android/Server/systemserver", "init2 ");

To init2 () abve to create the server thread. This thread will start the remaining services in the system according to the necessary start order. A snippet of the initial sequence gives:

In frameworks/base/services/Java/COM/Android/Server/systemserver. Java:

134 try {

135 slog. I (TAG, "entropy service ");

136 servicemanager. addservice ("entropy", new entropyservice ());

137

138 slog. I (TAG, "Power Manager ");

139 power = new powermanagerservice ();

140 servicemanager. addservice (context. power_service, power );

141

142 slog. I (TAG, "activity manager ");

143 context = activitymanagerservice. Main (factorytest );

144

145 slog. I (TAG, "telephony Registry ");

146 servicemanager. addservice ("telephony. Registry", new telephonyregistry (context ));

147

148 attributecache. INIT (context );

149

150 slog. I (TAG, "Package Manager ");

151 PM = packagemanagerservice. Main (context,

152 factorytest! = Systemserver. factory_test_off );

We see that the power manager is started first, followed by the activity manager and the other services. there are a lot more services started after these initial and if you are interested take look in the systemserver. java file. each service is running in a separate Dalvik thread in the systemserver process.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.