Android Os: CSE120 (FA10)
Android Os: CSE120 (FA10)
CSE120 (FA10)
Xiao Ma ([email protected])
WHY ANDROID?
the denition of open: "mkdir android ; cd android ; repo init -u git://android.git.kernel.org/platform/ manifest.git ; repo sync ; make"
OUTLINE
Android platform architecture OS kernel, libraries and devices Android programming model Delvik Virtual Machine Energy efciency How to write efcient code
ARCHITECTURE
ANDROID
ANDROID
ANDROID
BASED ON LINUX
Android uses Linux 2.6 kernel as the hardware abstraction What are the essences an OS should provide? Memory management, process management, IPC No virtual memory; specially implemented IPC Drivers and architecture support How to port Android to a new device? Using Linux vs. Writing a new OS from scratch Do all Linux kernel implementations work well on mobile devices?
APPLICATION LIBRARY
GNU libs (glibc) is too big and complicated for mobile phones, so Android implements its own special version of libc - Bionic libc: Smaller size - 200K (glibc is more than 400K) Strip out some complicated C++ features, the most signicant one - no C++ exception! Very special and small pthread implementation, heavily based on kernel futexes Bionic libc does not fully support POSIX and is not compatible with glibc which means ...?
PROCESS MANAGEMENT
Whats the difference between mobile apps cycle and desktop apps cycle? Two key principles Android usually do not kill an app, i.e. apps keep running even after you switch to other apps Android kills apps when the memory usage goes too high, but it saves app state for quick restart later on Do they make sense to mobile apps?
EXAMPLE
System
Home
Home
Home
EXAMPLE
System
Home List
Home Mail
Home List
EXAMPLE
System
Home List Message
Home Mail
EXAMPLE
System
Home List Message Browser
EXAMPLE
System
Home List Message Browser
Home Browser
Home Browser
EXAMPLE
System
Home List Message Browser Map
EXAMPLE
System
Home List Message Browser
Home Browser
EXAMPLE
System
Home List Message
Home
List
Message
EXAMPLE
System
Home List
Home
List
EXAMPLE
System
Home
Home
DEBATE
Swapping model VS. Androids life-cycle model
DISK I/O
Flash Random access File fragment impact Total power Reliability Write longevity Capacity Price ~0.1ms No 1/2 to 1/3 of HDD Reliable
Limited number of writes
LIMITED WRITES?
Flash drives have the well-known problem of limited number of writes in the life time - 10,000~100,000 times. Solution? What can applications do? How about operating system? Controllers? Hardware?
MEMORY MANAGEMENT
Linux kernel does most of the job Page-based memory management Virtual address to physical address mapping NO virtual memory
Why do we still need virtual to physical address mapping? Why does Android not support virtual memory?
POWER MANAGEMENT
DALVIK VM
DALVIK VM
A special Java virtual machine (VM) designed to run with limited system resource Memory efciency Register machine vs. Stack machine (modern JVM) fewer instructions, faster execution why does the number of instructions matter? Running multiple VMs more efciently
DEX FILE
Java class les are converted into .dex les that Dalvik executes Java byte-code is converted into Dalvik byte-code during this process
MEMORY EFFICIENCY
Shared constant string pool Share clean (even some dirty) memory between processes as much as possible .dex les are mapped as read-only by mmap() Memory efcient JIT implementation JIT itself is about 100K Code cache and supporting data structure takes another 100K for each application
SHARED MEMORY
PROGRAMMING MODEL
Each application is running in its own process An application can have one or more components: activities, services, broadcast receivers and content providers A task (an application from users point of view) consists of several activities from one or multiple applications An application keeps running until the system kills it because of memory shortage
POWER SAVING
Picture is from Google I/O 09 talk - Coding for Life -- Battery Life, That Is
UPDATE BIN
Use setInexactRepeating() so the system can bin your update together with others
Picture is from Google I/O 09 talk - Coding for Life -- Battery Life, That Is
WORK OFFLOADING
Naive ofoading Speech-to-text, OCR More sophisticated ofoading - ne-grained ofoading MAUI: Making Smartphones Last Longer with Code Ofoad (MobiSys 10) Running two versions of the app on the mobile device and a powerful server Decide when/what to ofoad on the y
EFFICIENT CODE
for (int i = initializer; i >= 0; i--) int limit = calculate limit; for (int i = 0; i < limit; i++) Type[] array = get array; for (Type obj : array) for (int i = 0; i < array.length; i++) for (int i = 0; i < this.var; i++) Iterable<Type> list = get list; for (Type obj : list)
EFFICIENT CODE
Try to rest for the most of the time be nice to other processes Avoid allocation short-lived objects need to be garbaged collected long-lived objects take precious memory Make a method static if it does not access member variables Avoid internal getter/setters Use oating point numbers only when you have to Prefer int over enum Use static final for constants