Android architecture-performance-based considerations (1)

Source: Internet
Author: User

Special statement: this series of articlesLianlab.orgCopyright ownership. For more information, see the source. By @ song Baohua Barry

The android architecture-software self-healing capability-has come to an end. Next we will discuss Android performance considerations in Series 2. The components of the Android system are complicated and the components are rooted in the wrong disk. If you do not fully consider the performance, I am afraid it will be as slow as a snail bait. Android has a unique Dalvik Virtual Machine. during startup, many resources are loaded so that sub-processes can inherit zygote, and shared memory audioflinger, surfaceflinger, and property service are widely used, the application directly render the graphics, simple and efficient new IPC mode binder, etc. We can talk about it more often.

Today, we will first talk about the highlights of the androidjava world nvwa zygote, and praise its magnificent behavior in the vast blue sky. Readers can still communicate with each other through Sina Weibo's "@ Baohua Barry". Writing technical blogs is a very painful process, you are welcome to have a few drinks. What I want to declare here is that this series does not focus on small knowledge points, but on design ideas.

The "inherent territory" of the Java World"

Comrades, the process is a unit of resource encapsulation. The so-called process refers to the houses and cars of the diaosi. task_struct is the data structure used to describe the process in the Linux kernel, and the process is the resource, therefore, task_struct encapsulates resources and attributes of processes (such as PID). Its definition is as follows:

Struct task_struct {wire name: comm wire ID: PID wire House: mm_struct wire car: fs_struct wire salary: signal_struct... Diaosi status: Sleep, working, botnets, etc}

Therefore, task_struct is born for today's society. A struct includes all your assets.

A thread is a unit of CPU scheduling, although it is a house, mm_struct, a car fs_struct, a person's salary signal_struct, etc, if it is shared by two or more diao (here these struct pointers point to the same, pthread_create achieves this function through clone ), then these silk threads are multiple threads in the same process.

In Linux, each diaosi is a task_struct. The kernel does not distinguish between processes and threads. It only implements threads by mounting a house to two task_struct headers. To put it simply, you and your wife have two task_struct instances, but a real estate certificate, mm_struct, is the same. However, as two threads, the only resource of the restroom (CPU) needs to be rescheduled.

Generally, a Linux Process is generated through fork. At this time, the mm_struct pointer of the child process is not equal to the mm_struct of the parent process, instead, it re-allocates a mm_struct memory and makes it equal to the mm_struct of the negative process. Therefore, the sub-process also shares the resources of the parent process at this time. The reason for this inheritance is very simple, because we had a few diaosi fish there thousands of years ago, which is our inherent resource. These inherited resources are read-only. If you want to write, a "Copy at write time" will be generated. The kernel will re-apply for one page for the write process and copy the old page, write again on the new page.

Generally, after a child process is fork, it will call exec () to replace userspace. Typically, Android init uses this model:

Exec () replaces the user space of the current sub-process with an executable file. Note that after exec () is replaced with userspace, except for ID information such as PID, the FD 0, 1, and 2 indicate the standard input, output, and error output. The FD still retains the original meaning, which enables the android INIT process to start init. when the RC service is running, redirect 0, 1, and 2 to/dev/null to see how Android init starts the service:

static void zap_stdio(void){    int fd;    fd = open("/dev/null", O_RDWR);    dup2(fd, 0);    dup2(fd, 1);    dup2(fd, 2);    close(fd);}void service_start(struct service *svc, const char *dynamic_args){    …    pid = fork();    if (pid == 0) {        …        if (needs_console) {            …        } else {            zap_stdio();        }       …            execve(svc->args[0], (char**) arg_ptrs, (char**) ENV);        }}

We can see that all the services created by init are compiled by fork + exec. In general, the printf stuff is gone, because in Exec () DUP to/dev/null.

However, init does not work in the Java World, but the Java World is produced through zygote. In the end, Java programs will not be compiled and connected to an executable file as the C/C ++ native code. The compiled Java source code is not an executable program but an intermediate code, it is interpreted and executed by the Java virtual machine, so it cannot be exec (). Java's performance has declined. However, without exec (), fork () achieves the inheritance of resources. Otherwise, after exec (), userspace will be replaced as a whole.

The systemserver and APK started by zygote are both fork first, and then find and execute the main () function of the corresponding target class. This process does not involve exec (). In this case, in the Java World of Android, some resources may be required by many processes. This opportunity will never be missed by the fishermen of tianchao, make sure you get caught first so that the white rabbit can directly promote it as an "inherent territory". This process mainly involves preloadclasses and preloadresources.

The class to be preloaded is stored in the frameworks/base/Preloaded-classes file. This file is almost 2000 lines. The yundun, which was just a thousand years ago, is used everywhere, huangyan Island and the Diaoyu Islands have all gone, and there are still a lot of reefs and so on. They also ran away and directly turned tianchao into Gao fushuai:

Android. r$ styleableandroid. Accounts. accountandroid. Accounts. Account $1 too many Android. Accounts. accountmanager islands, which is omitted below

Preloadresourcesis used to prepare resources in framework-res.apk. These two preload processes are really slow. You can use bootchart to observe the android startup process. You may find that half of the History of Civilization in 5000 were taken for fishing by zygote:

Some people say that since preload is so slow and seriously affects the boot speed, isn't it good for us to stop preload?

Comrades, the significance of preload is to hold and catch a fish first. After the fork () sub-process occurs, it can be used directly when the sub-process is used. If this process is not done, You need to load it when each sub-process is used by itself. How much memory is consumed and how slow is it? It is obvious that the ancestors went fishing. The result is as follows:

Finally, we want to say that without exec (), the corresponding Java Process will lose a capability. For example, if you want to use valgrind to check native layer Memory leakage, overflow, or native layer Memory leakage and overflow of an APK, you cannot call the following command line: valgrind -- tool = memcheck -- leak-check = Full systemserver?

However, such a demand exists, so Jeff Brown jeffbrown@google.com submitted a patch for the android Java program to be started in EXEC mode, which is distributed in dalvik_system_zygote.c, zygote. java, app_process/app_main.cpp, runtimeinit. java, and added a new wrapperinit. java file, so that we can start the Java program through exec (), so that we can
Before the Java program starts, it is easy to insert wrap (such as valgrind). This process is actually:

    public static void execApplication(String invokeWith, String niceName,            FileDescriptor pipeFd, String[] args) {        StringBuilder command = new StringBuilder(invokeWith);        command.append(" /system/bin/app_process /system/bin --application");        if (niceName != null) {            command.append(" '--nice-name=").append(niceName).append("'");        }        command.append(" com.android.internal.os.WrapperInit ");        command.append(pipeFd != null ? IoUtils.getFd(pipeFd) : 0);        Zygote.appendQuotedShellArgs(command, args);        Zygote.execShell(command.toString());    }

The most critical one is/system/bin/app_process/system/bin-application. In fact, the executable file app_process (which can be Exec) is used to create a process that runs Java. Comrades, zygote actually renamed this app_process. App_process is an authentic Java class starter. zygote can be seen as a vest.

Therefore, valgrind can track systemserver in this way:

adb root adb shell setprop wrap.system_server "logwrapper valgrind" adb shell stop && adb shell start

In fact, the process of starting systemserver is changed to fork-> exec (/system/bin/app_process/system/bin-applicationlogwrapper valgrind system_server.

This is the story of this book. Please let us know how to predict the future.

I would like to dedicate this time to the most lovely Chinese military personnel, whether the national army or the communist army. I hope that we will be committed to defending the land and defending the country in the future, and the Chinese descendants will never meet again.

The sword was suddenly passed to the north of JI, the first smell of tears full of clothes.

But look at the sorrow of his wife.

In the daytime, the song must be accompanied by wine, and the young man will return home.

That is to say, when the bucket passes through the wuxia, it goes down to Xiangyang to Luoyang.


-- Song Baohua

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.