Zygote Anatomy Based on Prelink & Preload for Android Platform
Total Page:16
File Type:pdf, Size:1020Kb
23-Oct(Fri) , 2009 ZygoteZygote AnatomyAnatomy BasedBased onon PrelinkPrelink && PreloadPreload forfor AndroidAndroid PlatformPlatform 4th Korea Android Seminar GeunSik Lim [email protected] http://blog.naver.com/invain 4th Korea Android Seminar 1/53 Agenda 1. Dynamic Linking & Static Linking 2. Prelink Fundamentals 3. Understanding Preload 4. Custom Linker (=Android Prelink) 5. Zygote Walkthrough 6. Relation between process and thread by Zygote 4th Korea Android Seminar 2/53 Main Keywords in This Session Dynamic Linking Prelink Prefetch Custom Linker Static Preread for Android Linking P rel oad Zygote Prefork Dynamic Loading 4th Korea Android Seminar 3/53 Location of Linker in FOSS World 4th Korea Android Seminar 4/53 Static Linking • 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. 4th Korea Android Seminar 5/53 Dynamic Linking • 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. 4th Korea Android Seminar 6/53 Dynamic loading with Linux • Programming Interface : dlopen( ), dlsym( ), dlclose( ), dlerror( ) • Load the math library, and print the cosine of 2.0 #include stdio.h , stdlib.h , dlfcn.h int 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); } 4th Korea Android Seminar 7/53 Static vs. dynamic Static Linux libraries Dynamic Loading Shared Dynamic Linking Program X Program Y Static libraries Static libraries Static linking ( *.a ) ( *.a ) Static linking At compile-time (init daemon of Android) Program X Program Y Shared libraries Dynamic linking ( *.so ) Dynamic linking Dynamic Of shared libraries At run-time 4th Korea Android Seminar 8/53 Source Compile Process cpp0 PreProcesser cc1 C Compiler Assembler ld or collect2 Hello.c Linker C Source Hello.i ProProcess Result Hello.s Assembly File Hello.o Object File Hello ELF --save-temps ADT JAVA Compiler DX Utility with .XML & .ARSC Eclipse .java (Source Code) .class file (Byte Code) .dex like exe on Dalvik VM .apk Decompiler (Interpreter) (e.g: Decafe , DJ Java) 4th Korea Android Seminar 9/53 Preload • 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. /lib/libc.so.6 /lib/libcom_err.so.2 /lib/libcrypt.so.1 Preload /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 . rams used prog frequently- 4th Korea Android Seminar 10/5 3 Readahead Daemon for Preload • 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 lsof / | grep -v "^₩(lsof₩|grep₩)" | awk '{ print $4 " " $9 }' | grep ^mem ₩ | awk '{ print $2 }' |grep -v "^.₩(var₩|tmp₩|home₩|root₩)" | grep ^[/] | sort -u if (fd >= 0) { readahead(fd, offset, length); close (fd); } 4th Korea Android Seminar 11/53 What is Prelink? • 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". 4th Korea Android Seminar 12/53 Merits of Prelink • Dynamic linker needs for their relocation at startup significantly decreases. • The run-time memory consumption decreases too according to fewer relocations. ☎ Prelinked system fedora11$> LD_DEBUG=statistics firefox 2>&1 | sed ’s/ˆ *//’ 25733: runtime linker statistics: 25733: total startup time in dynamic loader: 5533696 clock cycles 25733: time needed for relocation: 1941529 clock cycles (35.0%) 25733: number of relocations: 0 25733: number of relocations from cache: 2066 25733: number of relative relocations: 0 25733: time needed to load objects: 3217736 clock cycles (58.1%) 25733: runtime linker statistics: 25733: final number of relocations: 0 25733: final number of relocations from cache: 2066 4th Korea Android Seminar 13/53 Prelink Map Application Process Global Dynamic Loader Linkage Table Offset Table PLT Slot Got Entry dl_runtime_resolve JMP GOT[1] GOT[1] dl_runtime_resolve {{ JMP GOT[2] GOT[2] . .. .. JMP GOT[3] GOT[3] . ④ call func_a(); . call func_a(); } ... JMP GOT[4] GOT[4] ③ } ...... ② ... call func_b(); JMP GOT[5] GOT[5] call func_b();... ① ... JMP GOT[6] GOT[6] ... Symbol Table call func_c();... call func_c(); JMP GOT[7] GOT[7] ... Lazy Binding ...... func_a . ⑦ . func_a call func_d();... 0x12345678 ... call func_d();... ... ... JMP GOT[N-2] GOT[N-2] ... ... ... ... Symbol JMP GOT[N-1] GOT[N-1] func_bfunc_b ...Lookup JMP GOT[N] GOT[N] ⑧ ... ⑤ func_cfunc_c Update GOT LookUp Address ...... (Relocation) ⑥ ss of e Addre Absolut bole 4th Korea Android Seminar Sym 14/53 Prelink on X86 Desktop 1/2 prelink-no#> yum -y install prelink ( ubuntu#> apt-get –y install prelink ) prelink-no#> prelink ./firefox prelink-yes#> time LD_DEBUG=statistics DISPLAY= LD_LIBRARY_PATH=. ./firefox real 7m0.261s user 4m0.026s sys 1m0.082s prelink-yes#> prelink --undo ./firefox prelink-no#> time LD_DEBUG=statistics DISPLAY= LD_LIBRARY_PATH=. ./firefox Speed Up real 9m1.342s user 5m6.024s sys 1m5.052s 4th Korea Android Seminar 15/53 Prelink on X86 Desktop 2/2 • Appended Section Headers After Prelink : .gnu_liblist , .gnu_conflict, .gnu_prelink_undo 4th Korea Android Seminar 16/53 Why do you need Prelink? • 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. 4th Korea Android Seminar 17/53 F11#> export LANG=C F11#> man 2 dlopen F11#> man 3 exec Diff Between Prelink and 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. Zygote ile ob M r Fo Ref) Hybrid related Paper : Performance Characterization of Prelinking and Preloading for Embedded Systems 4th Korea Android Seminar 18/53 Memory Layout of Pre-linked Libraries 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 address is found in the below file. ./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 • #define PRELINK_MIN 0x90000000 • #define PRELINK_MAX 0xB0000000 4th Korea Android Seminar 19/53 Prelinked System Libraries 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 libraries 08. PV opencore libraries 09. Opencore hardware support 10. PV libraries 4th Korea Android Seminar 20/53 Prelinked Application Libraries 01. libraries for specific apps or temporary ( libcam_ipl, libwbxml, libwbxml_jni, libxml2wbxml,