Android Process Grooming

Source: Internet
Author: User

First, summarize

System Boot Architecture diagram:

In the Android system-the beginning of the explanation, from the point of view of the Android system boot, this article is from the process/threading perspective to analyze the problem.

1.1 Parent Process

In all processes, a process that exists in the posture of the parent process (that is, the light red item in the diagram), as follows:

    • kthreadd进程: is the parent process of all kernel processes
    • init进程: Is the parent process (or parent process) of all user processes
    • zygote进程: is the parent process for all upper-level Java processes, and zygote the other parent process is a init process.
1.2 Heavyweight processes

In the Android process, there are 3 very important processes (that is, the dark purple items in the figure), as follows:

    • system_server: It was hatched by Zygote, the chief disciple of Zygote, who lifted all the service from the entire Java framework, such as Activitymanagerservice, Powermanagerservice, etc.

    • mediaserver: It was hatched by Init, and all the service of the entire C + + framework was lifted up, such as Audioflinger, Mediaplayerservice and so on.

    • servicemanager: It was hatched by Init, the big butler of the entire Binder architecture (IPC), and all service large and small need to be consulted first ServiceManager.

Ii. process

The Android process is divided into large classes, which can be divided into kernel processes and user processes.

2.1 Kthreadd Child Process

kthreaddProcess (process 2nd), which is the kernel process of a Linux system, is the originator of all kernel processes.

A kernel daemon hatched by Kthreadd that is located in a dark blue block of kernel in the system boot schema diagram. The following is a list of common kernel processes:

Process Name explain
ksoftirqd/0  ,
kworkder/0:0h  
migration/0  
watchdog/0  
binder< /td>  
rcu_sched  
perf  
netns  ,
rpm-smd  
mpm  
writeback  
system  
irq/261-msm_iom  ,
mdss_dsi_event  
kgsl-events  
SPI  
therm_core:noti  
msm_thermal:hot  
... ...

There are no child and child threads in the kernel process, and the user of all kernel processes is root.

The role of each kernel process is then re-

2.2 Init child process

initProcess (process 1th), which is the user-space process of the Linux system, or the first user-space process of Android.

The following is a list of common user processes that are hatched by the INIT process:

Process Name Process Files function
Zygote /system/bin/app_process The first process in the Java world, divided into 32-bit and 64-bit
ServiceManager /system/bin/servicemanager Binder's Daemon
Media /system/bin/mediaserver The process of multimedia services
Ueventd /sbin/ueventd Uevent Daemon Process
Healthd /sbin/healthd The Battery daemon
Logd /system/bin/logd The daemon of log
CD D /sbin/adbd ADBD process (Socket IPC)
Lmkd /system/bin/lmkd Lowmemorykiller Daemon Process
Console /system/bin/sh Control Desk
Vold /system/bin/vold Volume daemon Process
Netd /system/bin/netd Network Daemon
Debuggerd /system/bin/debuggerd For debugger Exception exit
Debuggerd64 /system/bin/debuggerd64 For debugger Exception exit
Ril-daemon /system/bin/rild Radio Interface Layer Daemon
Installd /system/bin/installd Installed Daemons
Surfaceflinger /system/bin/surfaceflinger UI frame-related processes
... ...

ServiceManager, as a big steward of the binder architecture, all registration services, access services, need to go through ServiceManager, more about ServiceManager View Binder series articles.

2.3 Zygote Child Process

Zygote itself is a native application, the first name is "App_process", in the process of running, through the system call to change their name to Zygote. is the parent process for all upper-level Java processes, and there is another Zygote64 process in the Android system to incubate 64-bit application processes.

The red line in the figure is the process of zygote fork, and all the app processes are generated by the zygote fork.

Below Zyogte进程 is a list of hatching parts of the subprocess

Process Name explain
System_server The various services of the Java framework depend on this process
Com.android.phone Telephony Application Process
Android.process.acore Address Book Process
Android.process.media Multimedia application Process
Com.android.settings Set process
Com.android.wifi WiFi app Process
... ...
Third, thread 3.1 Zygote child thread

In the adb shell terminal, enter:

grep -E "NAME| 497 "

Explanation: -E "NAME| 497 " is the output can be displayed more than the NAME row, easy to see the specific meaning of each column represents, 497 is the zygote process number.

The address space of the shared parent process is a child thread, that is, the vsize is necessarily the same, otherwise it is a child process, such as:

The red circle in the figure is the child thread, and the others are child processes.

The child threads of the visible zygote are as follows:

Thread Name explain
referencequeued The daemon thread that references the queue
Finalizerdaemon destructor Thread for destructors
Finalizerwatchd The daemon thread of the destructor monitoring
Heaptrimmerdaem The daemon thread of the heap grooming
Gcdaemon The daemon thread that executes the GC

These 5 threads are all threads that are closely related to the virtual machine, and then all child processes that are hatched directly or indirectly by Zygote will contain these 5 threads, and in their thread descriptions they are not repeated, but instead are represented by the words "for GC". Subsequent free discussions will be conducted specifically for Android virtual machines.

3.2 System_server Child thread

The service in the Java framework runs in the System_server process, with a lot of sub-threads in the System_server, and a system_server with 122 threads on the phone around her. Some sub-threads are listed below:

Thread Name explain
System_server Contains 4 threads of this name
Heap thread Poo Asynchronous Heapworker, containing 5
Signal Catcher Capture kernel signals, such as Signal_quit
JDWP Threads for virtual machine debugging
referencequeued For GC
Finalizerdaemon For GC
Finalizerwatchd For GC
Heaptrimmerdaem For GC
Gcdaemon For GC
Binder_ IPC Thread, containing 16
Thread_ A normal thread that contains several
Asynctask # An asynchronous task that contains several
Renderthread A rendering thread that can contain several
Activitymanager Activitymanagerservice Threads
Performanacecont System_server Proprietary
Fileobserver System_server Proprietary
Cputracker Statistics Process CPU Information
Powermanagerser System_server Proprietary
Packagemanager System_server Proprietary
Watchdog System_server Proprietary
Wifimonitor System_server Proprietary
Ueventobserver System_server Proprietary
... ...

The Activitymanagerservice thread is a serverthread thread. The Comm field of the process structure body task_struct is a char type of length 16, so the process name is up to 15 characters long.

3.3 MediaServer Child Thread

MediaServer child threads, as follows:

Thread Name
MediaServer
Apmtone
Apmaudio
Apmoutput
Safe Speaker Th
Audioout_2
Fastmixer
Audioout_4
Fastmixer
Audioout_6
Binder_1
Binder_2

The role of each thread, the subsequent re-fill on

3.4 App Sub-threading

Here take settings as an example

Thread Name explain
Com.android.settings Settings process
Heap thread Poo Asynchronous Heapworker, containing 5
Signal Catcher Capture kernel signals, such as Signal_quit
JDWP Threads for virtual machine debugging
referencequeued For GC
Finalizerdaemon For GC
Finalizerwatchd For GC
Heaptrimmerdaem For GC
Gcdaemon For GC
Binder_1 For IPC
Binder_2 For IPC
Pool-m-thread-n The nth thread in the thread pool m, containing a number of
Asynctask #1 Exception task
Renderthread There will be a number of
Wifimanager Threads that manage WiFi

In general, each apk will produce 2 or 3 binder threads, and the activity or service that the APK runs will generate 2 binder threads.

About binder Issues

    • The main thread is generated by the zygote matrix;
    • Thread pool: The first time you create a binder thread A, then listen for the Br_spawn_looper event, after you receive the second binder thread B, thread B continues to listen for the Br_spawn_looper event, and then creates a third binder thread C after it is received. Create a total of 3 Bindr threads, which is the binder protocol decision. Depending on the number of system processors and the load strength of the application, the number of threads in the thread pool can be dynamically adjusted, which is a binder optimization consideration.
Iv. Process Statistics

The following is an example of a mobile phone based on Android 5.1.1, counting the number of processes with "parent process" as Ppid:

Parent Process number explain
0 2 Init, Kthreadd, respectively
Init 55 User process
Kthreadd 303 Kernel process
Zygote64 41 64-bit Zygote
Zygote 3 32-bit Zygote
Qseecomd 1 High-pass secure execution Environment
CD D 2 2 adb windows open
Sh 2 PS, grep, respectively

The parent process of ZYGOTE64/ZYGOTE/QSEECOMD/ADBD in the diagram is the init process, and the parent process of SH is adbd, and the parent process of ADB and QSEECOMD is the init process.

Phone Total: 407 a process, 1575 a thread. (This data is for reference only, so that you have a general sense of the current process and the number of threads of the phone)

Android Process Grooming

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.