53
4th Korea Android Seminar 1/53 GeunSik GeunSik Lim Lim [email protected] [email protected] http:// http:// blog.naver.com/invain blog.naver.com/invain 4th 4th Korea Android Seminar Korea Android Seminar 2 2 3 3 - - Oct Oct (Fri) , 2009 (Fri) , 2009 Zygote Anatomy Zygote Anatomy Based on Based on Prelink Prelink & Preload & Preload for Android Platform for Android Platform

Zygote Anatomy Based on Prelink & Preload for Android Platform · 2010-10-16 · Zygote Anatomy Based on Prelink & Preload for Android Platform. 4thKorea Android Seminar Agenda 2/53

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Zygote Anatomy Based on Prelink & Preload for Android Platform · 2010-10-16 · Zygote Anatomy Based on Prelink & Preload for Android Platform. 4thKorea Android Seminar Agenda 2/53

4th Korea Android Seminar 1/53

GeunSikGeunSik [email protected]@gmail.com

http://http://blog.naver.com/invainblog.naver.com/invain

4th4th Korea Android SeminarKorea Android Seminar

2233--OctOct(Fri) , 2009(Fri) , 2009

Zygote Anatomy Zygote Anatomy Based on Based on PrelinkPrelink & Preload & Preload

for Android Platformfor Android Platform

Page 2: Zygote Anatomy Based on Prelink & Preload for Android Platform · 2010-10-16 · Zygote Anatomy Based on Prelink & Preload for Android Platform. 4thKorea Android Seminar Agenda 2/53

4th Korea Android Seminar

AgendaAgenda

2/53

1.1. Dynamic Linking & Static LinkingDynamic Linking & Static Linking2.2. PrelinkPrelink FundFundaammeentntaalsls3.3. Understanding PreloadUnderstanding Preload4.4. Custom LinkerCustom Linker (=Android (=Android PrelinkPrelink))5.5. Zygote Zygote WalkthroughWalkthrough6.6. Relation between process and thread by ZygoteRelation between process and thread by Zygote

Page 3: Zygote Anatomy Based on Prelink & Preload for Android Platform · 2010-10-16 · Zygote Anatomy Based on Prelink & Preload for Android Platform. 4thKorea Android Seminar Agenda 2/53

4th Korea Android Seminar

Main Keywords in This SessionMain Keywords in This Session

3/53

Prelink

Preload

Zygote

Dynamic

LinkingStatic Linking

Custom Linker for Android

Preread

Prefork Dynamic

Loading

Prefetch

Page 4: Zygote Anatomy Based on Prelink & Preload for Android Platform · 2010-10-16 · Zygote Anatomy Based on Prelink & Preload for Android Platform. 4thKorea Android Seminar Agenda 2/53

4th Korea Android Seminar

Location of Location of LinkerLinker in FOSSin FOSS WorldWorld

4/53

Page 5: Zygote Anatomy Based on Prelink & Preload for Android Platform · 2010-10-16 · Zygote Anatomy Based on Prelink & Preload for Android Platform. 4thKorea Android Seminar Agenda 2/53

4th Korea Android Seminar

• Static linking avoids dependency problems. (-static)

• In general cases, static linking can result in a performance improvement.

• Static linking can also allow the application to be contained in a single executable file, simplifying distribution and installation.

• In static linking, the size of the executable becomes greater than in dynamic linking, as the library code is stored within the executable rather than in separate files.

Static LinkingStatic Linking

5/53

Page 6: Zygote Anatomy Based on Prelink & Preload for Android Platform · 2010-10-16 · Zygote Anatomy Based on Prelink & Preload for Android Platform. 4thKorea Android Seminar Agenda 2/53

4th Korea Android Seminar

• Libraries can be integrated into a program once by a linker. Dynamic linking has advantages in code size and management.( -dynamic)

• But, every time a program is run, the loader needs to find the relevant libraries.

• Because 1) the libraries can move around in memory, this causes a performance penalty,2) and the more libraries that need to be resolved, the greater the penalty.

Dynamic LinkingDynamic Linking

6/53

Page 7: Zygote Anatomy Based on Prelink & Preload for Android Platform · 2010-10-16 · Zygote Anatomy Based on Prelink & Preload for Android Platform. 4thKorea Android Seminar Agenda 2/53

4th Korea Android Seminar

Dynamic loading with LinuxDynamic loading with Linux

#include stdio.h , stdlib.h , dlfcn.hint main (int argc, char **argv){ void *handle;

double (*cosine)(double);char *error;

handle = dlopen("libm.so", RTLD_LAZY);if (!handle) { fprintf(stderr, "%s\n", dlerror());

exit(EXIT_FAILURE); }

dlerror(); /* Clear any existing error */ *(void **) (&cosine) = dlsym(handle, "cos");

if ((error = dlerror()) != NULL) {fprintf(stderr, "%s\n", error);exit(EXIT_FAILURE); }

printf(“Consine is %f\n", (*cosine)(2.0));dlclose(handle);exit(EXIT_SUCCESS);

}

• Programming Interface : dlopen( ), dlsym( ), dlclose( ), dlerror( ) • Load the math library, and print the cosine of 2.0

7/53

Page 8: Zygote Anatomy Based on Prelink & Preload for Android Platform · 2010-10-16 · Zygote Anatomy Based on Prelink & Preload for Android Platform. 4thKorea Android Seminar Agenda 2/53

4th Korea Android Seminar

Static vs. dynamicStatic vs. dynamic

Linux libraries

Static

Shared

Dynamic Linking

Dynamic Loading

Program X

Static libraries( *.a )

Program Y

Static libraries( *.a ) Static linking

At compile-time (init daemon of Android)

Program X

Shared libraries( *.so )

Program Y

Dynamic linkingOf shared librariesAt run-time

Stat

ic li

nkin

gD

ynam

ic li

nkin

g

8/53

Page 9: Zygote Anatomy Based on Prelink & Preload for Android Platform · 2010-10-16 · Zygote Anatomy Based on Prelink & Preload for Android Platform. 4thKorea Android Seminar Agenda 2/53

4th Korea Android Seminar

Source Compile ProcessSource Compile Process

Hello.cC Source Hello.i

ProProcess Result Hello.sAssembly File Hello.o

Object FileHelloELF

cc1C Compiler Assembler ld or

collect2Linker

--save-temps

cpp0PreProcesser

Eclipse.java

(Source Code)

JAVA Compiler

.class file(Byte Code) .dex like exe

on Dalvik VM(Interpreter)

DX Utility

.apk

with .XML & .ARSC

ADT

Decompiler(e.g: Decafe , DJ Java)

9/53

Page 10: Zygote Anatomy Based on Prelink & Preload for Android Platform · 2010-10-16 · Zygote Anatomy Based on Prelink & Preload for Android Platform. 4thKorea Android Seminar Agenda 2/53

4th Korea Android Seminar

• Files of more frequently-used programs are, during a computer's spare time, loaded into memory.

• This results in faster (speed up) application startup times as less data needs to be fetched from disk.

PreloadPreload

10/53

frequently-used programs

Preload

/lib/libc.so.6/lib/libcom_err.so.2/lib/libcrypt.so.1/lib/libcrypto.so.6/lib/libdb-4.3.so/lib/libdbus-1.so.3/lib/libdl.so.2/lib/libexpat.so.0/lib/libglib-2.0.so.0/lib/libgmodule-2.0.so.0/lib/libgobject-2.0.so.0/lib/libgthread-2.0.so.0. . . Below Omission . . .

Page 11: Zygote Anatomy Based on Prelink & Preload for Android Platform · 2010-10-16 · Zygote Anatomy Based on Prelink & Preload for Android Platform. 4thKorea Android Seminar Agenda 2/53

4th Korea Android Seminar

• The readahead reads the contents of a list of files into memory, which causes them to be read from cache when they are actually needed.Its goal is to speed up the boot process.

• e.g) Readahead daemon for Linux distribution- An adaptive prefetching daemon- http://sourceforge.net/projects/preload/

#> /usr/sbin/readahead `cat /etc/readahead.early.files` &- A list of files cached in the memory

ReadaheadReadahead Daemon for PreloadDaemon for Preload

lsof / | grep -v "^₩(lsof₩|grep₩)" | awk '{ print $4 " " $9 }' | grep ^mem ₩| awk '{ print $2 }' |grep -v "^.₩(var₩|tmp₩|home₩|root₩)" | grep ^[/] | sort -u

11/53

if (fd >= 0){ readahead(fd, offset, length);close (fd); }

Page 12: Zygote Anatomy Based on Prelink & Preload for Android Platform · 2010-10-16 · Zygote Anatomy Based on Prelink & Preload for Android Platform. 4thKorea Android Seminar Agenda 2/53

4th Korea Android Seminar

What is What is PrelinkPrelink??

• A tool designed to speed up dynamic linking of ELF programs. (e.g: ELF shared libraries and ELF dynamically linked binaries)

• To speed up a system by reducing the time a program needs to begin.

• A FOSS is written by Jakub Jelinek of Red Hat.F11#> svn checkout http://sourceware.org/svn/prelink/trunk prelink

• Process on Mac OS X is called "prebinding".

12/53

Page 13: Zygote Anatomy Based on Prelink & Preload for Android Platform · 2010-10-16 · Zygote Anatomy Based on Prelink & Preload for Android Platform. 4thKorea Android Seminar Agenda 2/53

4th Korea Android Seminar

• Dynamic linker needs for their relocation at startupsignificantly decreases.

• The run-time memory consumption decreases too according to fewer relocations.

Merits of Merits of PrelinkPrelink

☎ Prelinked systemfedora11$> LD_DEBUG=statistics firefox 2>&1 | sed ’s/ˆ *//’25733: runtime linker statistics:25733: total startup time in dynamic loader: 5533696 clock cycles25733: time needed for relocation: 1941529 clock cycles (35.0%)25733: number of relocations: 025733: number of relocations from cache: 206625733: number of relative relocations: 025733: time needed to load objects: 3217736 clock cycles (58.1%)25733: runtime linker statistics:25733: final number of relocations: 025733: final number of relocations from cache: 2066

13/53

Page 14: Zygote Anatomy Based on Prelink & Preload for Android Platform · 2010-10-16 · Zygote Anatomy Based on Prelink & Preload for Android Platform. 4thKorea Android Seminar Agenda 2/53

4th Korea Android Seminar

PrelinkPrelink MapMap

call func_a();......

call func_b();......

call func_c();......

call func_d();......

call func_a();......

call func_b();......

call func_c();......

call func_d();......

dl_runtime_resolve{

. . .

. . .}

dl_runtime_resolve{

. . .

. . .}

Dynamic Loader

func_a......

func_b...

func_c...

func_a......

func_b...

func_c...

Application

Symbol Table

Process Linkage Table

GlobalOffset Table

Absolute Address of

Symbole

0x12345678

Lazy Binding

LookUp Address Update GOT(Relocation)

JMP GOT[N]

JMP GOT[N-1]

JMP GOT[N-2]

. . . . . .

JMP GOT[7]

JMP GOT[6]

JMP GOT[5]

JMP GOT[4]

JMP GOT[3]

JMP GOT[2]

JMP GOT[1]

PLT Slot

GOT[N]

GOT[N-1]

GOT[N-2]

. . . . . .

GOT[7]

GOT[6]

GOT[5]

GOT[4]

GOT[3]

GOT[2]

GOT[1]

Got Entry

②③

Symbol Lookup

14/53

Page 15: Zygote Anatomy Based on Prelink & Preload for Android Platform · 2010-10-16 · Zygote Anatomy Based on Prelink & Preload for Android Platform. 4thKorea Android Seminar Agenda 2/53

4th Korea Android Seminar

prelink-no#> yum -y install prelink ( ubuntu#> apt-get –y install prelink )prelink-no#> prelink ./firefoxprelink-yes#> time LD_DEBUG=statistics DISPLAY=

LD_LIBRARY_PATH=. ./firefox

real 7m0.261suser 4m0.026ssys 1m0.082s

prelink-yes#> prelink --undo ./firefoxprelink-no#> time LD_DEBUG=statistics DISPLAY=

LD_LIBRARY_PATH=. ./firefox

real 9m1.342suser 5m6.024ssys 1m5.052s

PrelinkPrelink on X86 Desktop 1/2on X86 Desktop 1/2

15/53

Speed Up

Page 16: Zygote Anatomy Based on Prelink & Preload for Android Platform · 2010-10-16 · Zygote Anatomy Based on Prelink & Preload for Android Platform. 4thKorea Android Seminar Agenda 2/53

4th Korea Android Seminar

PrelinkPrelink on X86 Desktop 2/2on X86 Desktop 2/2

• Appended Section Headers After Prelink : .gnu_liblist , .gnu_conflict, .gnu_prelink_undo

16/53

Page 17: Zygote Anatomy Based on Prelink & Preload for Android Platform · 2010-10-16 · Zygote Anatomy Based on Prelink & Preload for Android Platform. 4thKorea Android Seminar Agenda 2/53

4th Korea Android Seminar

• prelink reduces this penalty by using the system's dynamic linker to reversibly perform this linking in advance by relocating.

• Afterward, the program only needs to spend time finding the relevant libraries on being run if, for some reason (perhaps an upgrade), the libraries have changed since being prelinked.

• In Android Platform, The need for Almost all the local symbols ( 80-90 % of the symbols in .rel.dyn and .rel.plt) and it gives much quicker performance.

In Summary, In order to reduce size and speed up loading.

Why do you need Why do you need PrelinkPrelink??

17/53

Page 18: Zygote Anatomy Based on Prelink & Preload for Android Platform · 2010-10-16 · Zygote Anatomy Based on Prelink & Preload for Android Platform. 4thKorea Android Seminar Agenda 2/53

4th Korea Android Seminar

Diff Between Diff Between PrelinkPrelink and Preload in Mobileand Preload in Mobile

• Prelink for prelinked binary execution model for fork and exec model.• Preload for non-prelinked binary execution model for fork and dlopen model. • Hybrid for pre-linked binary execution model for fork and dlopen model.

Ref) Hybrid related Paper : Performance Characterization of Prelinking and Preloading for Embedded Systems

F11#> export LANG=CF11#> man 2 dlopenF11#> man 3 exec

Zygote

For M

obile

18/53

Page 19: Zygote Anatomy Based on Prelink & Preload for Android Platform · 2010-10-16 · Zygote Anatomy Based on Prelink & Preload for Android Platform. 4thKorea Android Seminar Agenda 2/53

4th Korea Android Seminar

Memory LMemory Layout of ayout of PPrere--linked linked LLibrariesibraries

./build/core/prelink-linux-arm.map# 0xC0000000 - 0xFFFFFFFF Kernel# 0xB0100000 - 0xBFFFFFFF Thread 0 Stack# 0xB0000000 - 0xB00FFFFF Linker# 0xA0000000 - 0xBFFFFFFF Prelinked System Libraries# 0x90000000 - 0x9FFFFFFF Prelinked App Libraries# 0x80000000 - 0x8FFFFFFF Non-prelinked Libraries# 0x40000000 - 0x7FFFFFFF mmap'd stuff# 0x10000000 - 0x3FFFFFFF Thread Stacks# 0x00000000 - 0x0FFFFFFF .text / .data / heap

19/53

• #define PRELINK_MIN 0x90000000

• #define PRELINK_MAX 0xB0000000

Prelinked libraries can only be loaded at the very specific virtual memory address they have been prelinked to (during the build process). The list of prelinked system libraries and their corresponding virtual memory addressis found in the below file.

Page 20: Zygote Anatomy Based on Prelink & Preload for Android Platform · 2010-10-16 · Zygote Anatomy Based on Prelink & Preload for Android Platform. 4thKorea Android Seminar Agenda 2/53

4th Korea Android Seminar

01. Core System Libraries (libdl, libc, libstdc++, libm, liblog, libcutils, libthread_db, libz, libevent, libssl, libcrypto, libsysutils)

02. Bluetooth (liba2dp, audio, input, libhcid, libbluedroid, libbluetooth, libdbus)

03. Extended system libraries ( libril, libreference-ril, libwpa_client, libnetutils)

04. Core dalvik runtime support (libicuuc, libicui18n, libandroid-runtime, libnativehelper, libdvm-ARM, libdvm)

05. Graphics ( libpixelflinger, libcorecg, libsurfaceflinger, libagl, libGLESv1_CM, libGLESv2, libOpenVG_CM, libOpenVGU_CM, libEGL, libexif, libui, libsgl)

06. Audio 07. Assorted System libraries08. PV opencore libraries 09. Opencore hardware support10. PV libraries

PrelinkedPrelinked System LibrariesSystem Libraries

20/53

Page 21: Zygote Anatomy Based on Prelink & Preload for Android Platform · 2010-10-16 · Zygote Anatomy Based on Prelink & Preload for Android Platform. 4thKorea Android Seminar Agenda 2/53

4th Korea Android Seminar

01. libraries for specific apps or temporary( libcam_ipl, libwbxml, libwbxml_jni, libxml2wbxml, libaes, libdrm1,

libdrm1_jni, libwapcore, libstreetview, libwapbrowsertest, libminiglobe, libearth, libembunit, libneon, and so on)

PrelinkedPrelinked Application LibrariesApplication Libraries

21/53

Page 22: Zygote Anatomy Based on Prelink & Preload for Android Platform · 2010-10-16 · Zygote Anatomy Based on Prelink & Preload for Android Platform. 4thKorea Android Seminar Agenda 2/53

4th Korea Android Seminar

How to use How to use PrelinkPrelink Module for Android 1/2Module for Android 1/2

• It should be updated each time that a new system library is added to the system.

• The prelink step happens at build time, and uses the 'soslim' and 'apriori‘ tools:

1. 'soslim(./build/tools/soslim/*)' is used to find symbols in an executable ELF file and generate a list that can be passed to 'apriori'.

2. 'apriori(./build/tools/apriori/*)' is the real prelink tool which removes relocations from the shared object, however, it must be given a list of symbols to remove from the file.

22/53

Page 23: Zygote Anatomy Based on Prelink & Preload for Android Platform · 2010-10-16 · Zygote Anatomy Based on Prelink & Preload for Android Platform. 4thKorea Android Seminar Agenda 2/53

4th Korea Android Seminar

Difference between Difference between PrelinkPrelink and Android and Android PrelinkPrelink??

• Android Prelink is a custom pre-linking tool.• The same thing than GNU-style hash with minor modifications. (e.g.

forcing power-of-2 table sizes to avoid the horribly slow module operation on ARM during lookups, plus selecting smaller table sizesby default.

Q: GNU LD 2.17.50.0.X has the support of GNU-style hash. What is the benefit of this approach???

• Custom pre-linking tool means that remove the need for most of the symbol lookups.

23/53

/* NetBSD's Hash Table */int hash_lookup(Elf *elf, Elf_Data *hash, Elf_Data *symtab, Elf_Data *symstr, const char *symname) {

Elf32_Word *hash_data = (Elf32_Word *)hash->d_buf;Elf32_Word index;Elf32_Word nbuckets = *hash_data++;Elf32_Word *buckets = ++hash_data;Elf32_Word *chains = hash_data + nbuckets;index = buckets[elf_hash(symname) % nbuckets];while (index != STN_UNDEF &&

strcmp((char *)symstr->d_buf +((Elf32_Sym *)symtab->d_buf)[index].st_name, symname)) {index = chains[index];

}return index;

}

Page 24: Zygote Anatomy Based on Prelink & Preload for Android Platform · 2010-10-16 · Zygote Anatomy Based on Prelink & Preload for Android Platform. 4thKorea Android Seminar Agenda 2/53

4th Korea Android Seminar

• By default, these tools are only used to remove internal symbols from libraries, though they have been designed to allow more aggressive optimizations .(e.g. 'global' prelinking and symbol stripping, which prevent replacing individual system libraries though).

• You can disable prelinking at build time by modifying your Android.mk with a line like:

LOCAL_PRELINK_MODULE := false

(e.g. ./bionic/libc/Android.mk , ./bionic/linker/Android.mk, and so on )

How to use How to use PrelinkPrelink Module for Android 2/2Module for Android 2/2

24/53

Page 25: Zygote Anatomy Based on Prelink & Preload for Android Platform · 2010-10-16 · Zygote Anatomy Based on Prelink & Preload for Android Platform. 4thKorea Android Seminar Agenda 2/53

4th Korea Android Seminar

PrelinkPrelink Module for shared librariesModule for shared libraries

• Standard rules for building a normal shared library.• Additional inputs from base_rules.make: None.• LOCAL_PRELINK_MODULE will be set for you.• Android Prelink tool means Android Toolchain and Bionic libc

Optimization of dynamic libraries : Prelink (pre-connected) technology

25/53

./build/core/shared_library.mk

Page 26: Zygote Anatomy Based on Prelink & Preload for Android Platform · 2010-10-16 · Zygote Anatomy Based on Prelink & Preload for Android Platform. 4thKorea Android Seminar Agenda 2/53

4th Korea Android Seminar

What is Zygote ?What is Zygote ?

The "zygote" is a preforked simple process that the system keeps around that makes new application startup faster.

By Dan Morrill <[email protected]>

It sits idle in a neutral state (that is, "unfertilized" by a specificapplication) until it's needed, at which point the system instructs it to exec() the appropriate application.

A new zygote process is then started up in the background to replace the old, waiting for the next process to start.

26/53

Page 27: Zygote Anatomy Based on Prelink & Preload for Android Platform · 2010-10-16 · Zygote Anatomy Based on Prelink & Preload for Android Platform. 4thKorea Android Seminar Agenda 2/53

4th Korea Android Seminar

What is ZygoteWhat is Zygote’’s Role?s Role?

Zygote start Dalvik VM and start system server. It’s the most important process to speed up.The source is in ./device/servers/app.

You absolutely can not create any threads in zygote, since it will be forking (without exec) from itself. By Dianne Hackborn

$> chrome “--renderer-cmd-prefix=gdb –args”Using the --renderer-cmd-prefix option bypasses the

zygote launcher.

27/53

Page 28: Zygote Anatomy Based on Prelink & Preload for Android Platform · 2010-10-16 · Zygote Anatomy Based on Prelink & Preload for Android Platform. 4thKorea Android Seminar Agenda 2/53

4th Korea Android Seminar

Zygote

By init (static)

Fork new VM(Dalvik)

DalvikSystemServer

SystemServer

Runtimelibc

Zygote

Dalvik

Surface Flinger

Audio Flinger

Activie ManagerPackage ManagerWindowManager. . .

System Server

Prelinkedlibc

PrelinkedLibstdc++

Zygote Anatomy for Android PlatformZygote Anatomy for Android Platform

28/53

Page 29: Zygote Anatomy Based on Prelink & Preload for Android Platform · 2010-10-16 · Zygote Anatomy Based on Prelink & Preload for Android Platform. 4thKorea Android Seminar Agenda 2/53

4th Korea Android Seminar

ZygoteZygote’’s location in boot sequences location in boot sequence

29/53

init

daemons Zygote Runtime

dalvik

Linux

Service Manager

Surface Manager

Audio Manager

System Server

Telephony

Active Manager

BlueTooth

Package Manager

. . . . . .

Service Manager

Registration (binder)

Core Engine

• System Server starts the core Android services.e.g) ActivityManager, WindowManager, PackageManager, etc

Core Services

Page 30: Zygote Anatomy Based on Prelink & Preload for Android Platform · 2010-10-16 · Zygote Anatomy Based on Prelink & Preload for Android Platform. 4thKorea Android Seminar Agenda 2/53

4th Korea Android Seminar

It all starts with init…Similar to most Linux-based systems at startup, the bootloader loads the Linux kernel and starts the init process.

Init

Linux Kernel

StartStart--up Walkthrough up Walkthrough

30/53

Page 31: Zygote Anatomy Based on Prelink & Preload for Android Platform · 2010-10-16 · Zygote Anatomy Based on Prelink & Preload for Android Platform. 4thKorea Android Seminar Agenda 2/53

4th Korea Android Seminar

Init starts Linux daemons, including: 1. USB Daemon to manage USB connections 2. Android Debug Bridge to manage ADB connections3. Debugger Daemon to manage debug processes requests (dump

memory, etc.) 4. Radio Interface Layer Daemon to manage communication with

the radio.

usbd adbd debuggerd rild

init

StartStart--up Walkthrough up Walkthrough

31/53

Page 32: Zygote Anatomy Based on Prelink & Preload for Android Platform · 2010-10-16 · Zygote Anatomy Based on Prelink & Preload for Android Platform. 4thKorea Android Seminar Agenda 2/53

4th Korea Android Seminar

Init process starts the zygote process:1. A nascent process which initializes a Dalvik VM instance.

Loads classes and listens on socket for requests to spawn VMs. (Load preload classes: device/java/android/preloaded-classes)

2. Forks on request to create VM instances for managed processes. (Load preload resources.)

3. Copy-on-write to maximize re-use and minimize footprint.

usbdadbddebuggerd

daemons Zygote

Init

StartStart--up Walkthrough up Walkthrough

device/java/android/com/android/internal/os/ZygoteInit.java

32/53

Page 33: Zygote Anatomy Based on Prelink & Preload for Android Platform · 2010-10-16 · Zygote Anatomy Based on Prelink & Preload for Android Platform. 4thKorea Android Seminar Agenda 2/53

4th Korea Android Seminar

07-13 19:52:44.667: INFO/Zygote(563): ...preloaded 15 resources in 23ms.07-13 19:52:44.727: DEBUG/dalvikvm(563): GC freed 117 objects / 8416 bytes in 52ms07-13 19:52:44.786: DEBUG/dalvikvm(563): GC freed 205 objects / 8064 bytes in 47ms07-13 19:52:44.852: DEBUG/dalvikvm(563): GC freed 36 objects / 1384 bytes in 50ms07-13 19:52:44.846: INFO/dalvikvm(563): Splitting out new zygote heap07-13 19:52:44.876: INFO/dalvikvm(563): System server process 582 has been created07-13 19:52:44.876: INFO/Zygote(563): Accepting command socket connections07-13 19:52:44.917: INFO/jdwp(582): received file descriptor 19 from ADB07-13 19:52:45.016: DEBUG/dalvikvm(582): Trying to load lib /system/lib/libandroid_servers.so 0x007-13 19:52:45.186: DEBUG/dalvikvm(582): Added shared lib /system/lib/libandroid_servers.so 0x007-13 19:52:45.206: INFO/sysproc(582): Entered system_init()07-13 19:52:45.206: INFO/sysproc(582): ServiceManager: 0x16c83807-13 19:52:45.206: INFO/SurfaceFlinger(582): SurfaceFlinger is starting07-13 19:52:45.218: INFO/SurfaceFlinger(582): SurfaceFlinger's main thread ready to run. Initializing graphics...

Preloading Classes (Resources)

Preloading Classes (Resources)

• Pre-loaded Class objects do live in the Zygote heap. • Classes(1,167) which are preloaded by com.android.internal.os.ZygoteInit. • Screenshot using LogCat ( e.g: Log.d , Log.e , Log.i , Log.v , Log.w )

Preload is often paired with prelink.33/53

StartStart--up Walkthrough up Walkthrough

Page 34: Zygote Anatomy Based on Prelink & Preload for Android Platform · 2010-10-16 · Zygote Anatomy Based on Prelink & Preload for Android Platform. 4thKorea Android Seminar Agenda 2/53

4th Korea Android Seminar

Zygote Public Class for DevelopersZygote Public Class for Developers

34/53

StartStart--up Walkthrough up Walkthrough

http://developer.android.com/reference/dalvik/system/Zygote.html

Page 35: Zygote Anatomy Based on Prelink & Preload for Android Platform · 2010-10-16 · Zygote Anatomy Based on Prelink & Preload for Android Platform. 4thKorea Android Seminar Agenda 2/53

4th Korea Android Seminar

Init starts runtime process:1. Initializes Service Manager – the context manager for

Binder that handles service registration and lookup.2. Registers Service Manager as default context manager

for Binder services.

Service Manager usbd

adbddebuggerd runtime daemons Zygote

Init

StartStart--up Walkthrough up Walkthrough

35/53

Page 36: Zygote Anatomy Based on Prelink & Preload for Android Platform · 2010-10-16 · Zygote Anatomy Based on Prelink & Preload for Android Platform. 4thKorea Android Seminar Agenda 2/53

4th Korea Android Seminar

Runtime process sends request for Zygote to start System Service.

Service Manager usbd

adbddebuggerd

daemons runtime Zygote

Init

StartStart--up Walkthrough up Walkthrough

36/53

Page 37: Zygote Anatomy Based on Prelink & Preload for Android Platform · 2010-10-16 · Zygote Anatomy Based on Prelink & Preload for Android Platform. 4thKorea Android Seminar Agenda 2/53

4th Korea Android Seminar

1. Runtime process sends request for Zygote to start System Server.frameworks/base/services/java/com/android/server/SystemServer.java

. runtime->callStatic("com/android/server/SystemServer", "init2");

2. Zygote forks a new VM instance for the System Service process and starts the service.

Zygote::forkSystemServer (in device/dalvik/vm/InternalNative.c).com.android.server.SystemServer(in device/java/services/com/android/server).system_init ( in device/servers/system/library/system_init.cpp).

Service System Manager Server usbd

adbddebuggerd Zygote daemons runtime Dalvik VM

Init

StartStart--up Walkthrough up Walkthrough

37/53

Page 38: Zygote Anatomy Based on Prelink & Preload for Android Platform · 2010-10-16 · Zygote Anatomy Based on Prelink & Preload for Android Platform. 4thKorea Android Seminar Agenda 2/53

4th Korea Android Seminar

service servicemanager /system/bin/servicemanageruser systemcriticalonrestart restart zygoteonrestart restart media

service zygote /system/bin/app_process -Xzygote /system/bin --zygote --start-system-serversocket zygote stream 666onrestart write /sys/android_power/request_state wakeonrestart write /sys/power/state on

Zygote for Custom Zygote for Custom PrelinkPrelink in Android in Android

./frameworks/base/cmds/app_process/app_main.cpp

38/53

StartStart--up Walkthrough up Walkthrough

Page 39: Zygote Anatomy Based on Prelink & Preload for Android Platform · 2010-10-16 · Zygote Anatomy Based on Prelink & Preload for Android Platform. 4thKorea Android Seminar Agenda 2/53

4th Korea Android Seminar

System Service starts the native system servers, including: 1. Surface Flinger. 2. Audio Flinger.

Audio Flinger

Surface Flinger

Service System Manager Server

Zygote runtime Dalvik VM

Init

usbdadbd

debuggerddaemons

StartStart--up Walkthrough up Walkthrough

Page 40: Zygote Anatomy Based on Prelink & Preload for Android Platform · 2010-10-16 · Zygote Anatomy Based on Prelink & Preload for Android Platform. 4thKorea Android Seminar Agenda 2/53

4th Korea Android Seminar

Native system servers register with Service Manager as IPC service targets:

IPCThreadState::self()->joinThreadPool()

usbdadbd

debuggerddaemons

Audio Flinger

Surface Flinger

Service System Manager Server

Zygote runtime Dalvik VM

Init

StartStart--up Walkthrough up Walkthrough

40/53

Page 41: Zygote Anatomy Based on Prelink & Preload for Android Platform · 2010-10-16 · Zygote Anatomy Based on Prelink & Preload for Android Platform. 4thKorea Android Seminar Agenda 2/53

4th Korea Android Seminar

System Service starts the Android managed services:Location Content Telephony Bluetooth Connectivity

Init

Window Activity Package Power Manager Manager Manager Manager

Manager Manager Service Service Service

Service System Manager Server

Zygote runtime Dalvik VM

usbdadbd

debuggerddaemons

Camera

Surface Service

Flinger

StartStart--up Walkthrough up Walkthrough

41/53

Page 42: Zygote Anatomy Based on Prelink & Preload for Android Platform · 2010-10-16 · Zygote Anatomy Based on Prelink & Preload for Android Platform. 4thKorea Android Seminar Agenda 2/53

4th Korea Android Seminar

Android managed Services register with Service Manager:Location Content Telephony Bluetooth Connectivity Manager Manager Service Service Service

Window Activity Package Power …Manager Manager Manager Manager

Service Manager Camera

Surface Service

Flinger

Service System Manager Server usbdadbd

debuggerddaemons Zygote runtime Dalvik VM

Init

StartStart--up Walkthrough up Walkthrough

42/53

Page 43: Zygote Anatomy Based on Prelink & Preload for Android Platform · 2010-10-16 · Zygote Anatomy Based on Prelink & Preload for Android Platform. 4thKorea Android Seminar Agenda 2/53

4th Korea Android Seminar 43/53

Communications Between zygote and Java appCommunications Between zygote and Java app

ForkRuntime

initAndroidRuntime zygoteinit

Dalvik

ApplicationContent Application Application

Thread

ActivityThread Instrumentation

WindowManager

ActivityManager

Packagemanager

ApplicationContent Application Application

Thread

ActivityThread Instrumentation

WindowManager

ActivityManager

Packagemanager

Zygote System Server (Core Engine)

Java Process (Core Services , e.g: android.process.acore)

Fork

socket

Fork Binder IPC

☞Other JAVA process is created from ActivityManagerService(run in system_server process) like this.int pid = Process.start("android.app.ActivityThread", mSimpleProcessManagement ? app.processName : null, uid, uid, gids, ((app.info.flags&ApplicationInfo.FLAG_DEBUGGABLE) != 0), null);

Page 44: Zygote Anatomy Based on Prelink & Preload for Android Platform · 2010-10-16 · Zygote Anatomy Based on Prelink & Preload for Android Platform. 4thKorea Android Seminar Agenda 2/53

4th Korea Android Seminar

…Power Manager

Activity Manager

Camera Service Surface Flinger

Service System Manager Server usbdadbd

debuggerddaemons Zygote runtime Dalvik VM

Init

StartStart--up Walkthrough up Walkthrough

Page 45: Zygote Anatomy Based on Prelink & Preload for Android Platform · 2010-10-16 · Zygote Anatomy Based on Prelink & Preload for Android Platform. 4thKorea Android Seminar Agenda 2/53

4th Korea Android Seminar

After system server loads all services, the system is ready…

Init runtime Zygote SystemDaemon Server Processes

Activity Manager Package Manager Window Manager

Dalvik VM

Surface Flinger

usbdadbd

debuggerd Audio runtime Init daemons Zygote Flinger

StartStart--up Walkthrough up Walkthrough

45/53

Page 46: Zygote Anatomy Based on Prelink & Preload for Android Platform · 2010-10-16 · Zygote Anatomy Based on Prelink & Preload for Android Platform. 4thKorea Android Seminar Agenda 2/53

4th Korea Android Seminar

After system server loads all services, the system is ready…

Init runtime Zygote Home System Daemon

Server Processes Home

Activity Manager Dalvik VM Package Manager Window Manager

Dalvik VM

Surface Flinger

usbd

adbddebuggerd

Audio runtime Init daemons Zygote Flinger

StartStart--up Walkthrough up Walkthrough

46/53

Page 47: Zygote Anatomy Based on Prelink & Preload for Android Platform · 2010-10-16 · Zygote Anatomy Based on Prelink & Preload for Android Platform. 4thKorea Android Seminar Agenda 2/53

4th Korea Android Seminar

After system server loads all services, the system is ready…

Init runtime Zygote Home System Daemon Server Processes

Activity Home Manager Package Dalvik VM Manager Window Manager

Dalvik VM

Surface Flinger

usbdadbd

Audio debuggerdInit daemons runtime Zygote Flinger

libclibc libc libc libc libc

StartStart--up Walkthrough up Walkthrough

47/53

Page 48: Zygote Anatomy Based on Prelink & Preload for Android Platform · 2010-10-16 · Zygote Anatomy Based on Prelink & Preload for Android Platform. 4thKorea Android Seminar Agenda 2/53

4th Korea Android Seminar

Each subsequent application is launched in it's own process

runtime Zygote System Home Contacts Daemon Server Processes

Activity Home ContactsManager Package Dalvik VM Dalvik VMManager Window Manager

Dalvik VM

Surface Flinger usbd

adbdAudio debuggerd

daemons runtime Zygote Flinger

libc libclibclibc libc libc

StartStart--up Walkthrough up Walkthrough

48/53

Page 49: Zygote Anatomy Based on Prelink & Preload for Android Platform · 2010-10-16 · Zygote Anatomy Based on Prelink & Preload for Android Platform. 4thKorea Android Seminar Agenda 2/53

4th Korea Android Seminar

USER PID PPID VSIZE RSS WCHAN PC NAME

root 1 0 252 164 c0082240 0000ab0c S /init

root 536 1 740 312 c0141bb0 afe0c1bc S /system/bin/sh

system 537 1 808 264 c01654b4 afe0c45c S /system/bin/servicemanager

root 539 1 668 264 c0192c20 afe0cdec S /system/bin/debuggerd

radio 540 1 5392 684 ffffffff afe0cacc S /system/bin/rild

root 541 1 72432 25176 c008e3f4 afe0c584 S zygote

media 542 1 17720 3528 ffffffff afe0c45c S /system/bin/mediaserver

root 543 1 800 324 c01f3b04 afe0c1bc S /system/bin/installd

root 549 1 3332 152 ffffffff 0000e8c4 S /sbin/adbd

system 572 541 181016 24620 ffffffff afe0c45c S system_server

radio 657 541 105856 17724 ffffffff afe0d3e4 S com.android.phone

app_2 673 541 115412 21328 ffffffff afe0d3e4 S android.process.acore

app_15 687 541 94492 13364 ffffffff afe0d3e4 S com.android.mms

app_0 703 541 94296 12656 ffffffff afe0d3e4 S com.android.alarmclock

app_3 709 541 98380 12968 ffffffff afe0d3e4 S com.android.inputmethod.latin

app_4 721 541 95416 13620 ffffffff afe0d3e4 S android.process.media

system 723 541 98332 12536 ffffffff afe0d3e4 S com.android.settings

Relations between PID and TID by zygoteRelations between PID and TID by zygote

49/53

Parent PID

(Forked Process)

Page 50: Zygote Anatomy Based on Prelink & Preload for Android Platform · 2010-10-16 · Zygote Anatomy Based on Prelink & Preload for Android Platform. 4thKorea Android Seminar Agenda 2/53

4th Korea Android Seminar

Relations between PID and TID by zygoteRelations between PID and TID by zygote

50/53

651 WifiService652 WIfiWatchdogThread653 AudioService654 android:unnamed655 android:unnamed656 er$SensorThread661 watchdog671 app_process672 GPSEventThread693 Binder Thread#694 Binder Thread#711 r.MountListener738 Binder Thread#739 Binder Thread#

/proc/572 System Server

572 system_server573 HeapWorker574 Signal Catcher575 JDWP576 Binder Thread #577 Binder Thread #578 Binder Thread #579 SurfaceFlinger587 DisplayEventThr588 er.ServerThread590 ActivityManager591 ProcessStats592 PackageManager604 FileObserver642 SyncHandlerThread643 UEventObserver644 PowerManagerServer645 AlarmManager646 WindowManager647 InputDeviceRead652 WindowManagerPo649 InputDispatcher650 ConnectivityThread

/proc/1init

/proc/531zygote

/proc/572/task/37 threads

Fork

Exec

Create threads

Dalvik System(Native Start)

Page 51: Zygote Anatomy Based on Prelink & Preload for Android Platform · 2010-10-16 · Zygote Anatomy Based on Prelink & Preload for Android Platform. 4thKorea Android Seminar Agenda 2/53

4th Korea Android Seminar

Relations between PID and TID by zygoteRelations between PID and TID by zygote

51/53

/proc/861 app_process

/proc/1init

/proc/531zygote

/proc/861/task/16 threads

Fork

Exec

Create threads

861 app_process (com.android.browser )862 HeapWorker863 Signal Catcher864 JDWP (Java Debug Wire Protocol)

865 Binder Thread #866 Binder Thread #867 Binder Thread #870 CookieSyncManager872 AsyncTask #1 (computation that runs on

a background thread)873 WebViewCoreThread876 WebViewCoreThread877 WebViewCoreThread879 http0880 http1881 http2882 http3

Dalvik System(Native Start)

Page 52: Zygote Anatomy Based on Prelink & Preload for Android Platform · 2010-10-16 · Zygote Anatomy Based on Prelink & Preload for Android Platform. 4thKorea Android Seminar Agenda 2/53

4th Korea Android Seminar

THANKS

52/5352/53

Page 53: Zygote Anatomy Based on Prelink & Preload for Android Platform · 2010-10-16 · Zygote Anatomy Based on Prelink & Preload for Android Platform. 4thKorea Android Seminar Agenda 2/53

4th Korea Android Seminar53/53

Android Prelink? Android prelink means that remove the need for most of the symbol lookups.

Android Zygote? A preforked process that the system keeps around that makes new application startup faster.

MEMO