inter-process communication of android
TRANSCRIPT
1
Inter-process communicationof Android
Tetsuyuki Kobayashi
2011.6.3 updated
2
Who am I?
20+ years involved in embedded systems
10 years in real time OS, such as iTRON
10 years in embedded Java Virtual Machine
Now GCC, Linux, QEMU, Android, …
Blogs
http://d.hatena.ne.jp/embedded/ (Personal)
http://blog.kmckk.com/ (Corporate)
http://kobablog.wordpress.com/(English)
@tetsu_koba
new
3
Today's topic
Inter process communications in Android
Inter process method invocation using Binder
Ashmem
System Properties
4
Processes running on Android$ ps ...root 37 1 248 156 c00aef2c 0000875c S /sbin/ueventdsystem 42 1 768 260 c022950c afd0b6fc S /system/bin/servicemanagerroot 43 1 3824 564 ffffffff afd0bdac S /system/bin/voldroot 44 1 3796 560 ffffffff afd0bdac S /system/bin/netdroot 45 1 628 264 c02588c0 afd0c0cc S /system/bin/debuggerdradio 46 1 4336 672 ffffffff afd0bdac S /system/bin/rildroot 47 1 62224 27576 c00aef2c afd0b844 S zygotemedia 48 1 16828 3736 ffffffff afd0b6fc S /system/bin/mediaserverbluetooth 49 1 1216 572 c00aef2c afd0c59c S /system/bin/dbus-daemonroot 50 1 776 316 c02a8424 afd0b45c S /system/bin/installdkeystore 51 1 1704 432 c02588c0 afd0c0cc S /system/bin/keystoreshell 52 1 696 336 c0050934 afd0c3ac S /system/bin/shroot 53 1 3356 160 ffffffff 00008294 S /sbin/adbdsystem 67 47 172464 32596 ffffffff afd0b6fc S system_serversystem 115 47 80028 20728 ffffffff afd0c51c S com.android.systemuiapp_24 124 47 80732 20720 ffffffff afd0c51c S com.android.inputmethod.latinradio 135 47 87848 20324 ffffffff afd0c51c S com.android.phoneapp_18 144 47 89136 24160 ffffffff afd0c51c S com.android.launcherapp_7 165 47 86136 22736 ffffffff afd0c51c S android.process.acoreapp_0 197 47 73996 17472 ffffffff afd0c51c S com.android.deskclockapp_14 208 47 75000 18464 ffffffff afd0c51c S android.process.mediaapp_3 219 47 72228 17652 ffffffff afd0c51c S com.android.bluetoothapp_25 234 47 85336 17836 ffffffff afd0c51c S com.android.mmsapp_26 254 47 74656 19080 ffffffff afd0c51c S com.android.emailapp_27 266 47 74912 18100 ffffffff afd0c51c S com.android.providers.calendarapp_1 285 47 71616 16280 ffffffff afd0c51c S com.android.protipsapp_19 293 47 72184 16572 ffffffff afd0c51c S com.android.musicapp_21 301 47 74728 17208 ffffffff afd0c51c S com.android.quicksearchboxapp_28 311 47 75408 18040 ffffffff afd0c51c S com.cooliris.mediashell 323 52 856 316 00000000 afd0b45c R ps$
More than 30 processes (200+ threads).
5
ActivityManager
WindowManager
AlarmManager
Activity
Kernel
Inter Process Communication
6
Abstraction of Inter Process Communication
Binder
AIDL
IntentMore abstract
7
Inter Process Communication of Android
Intent
The highest level abstraction
Inter process method invocationAIDL: Android Interface Definition Language
binder
kernel driver
ashmem
shared memory
8
Intent (1)
The highest level abstraction IPC in Android
Requests are queued and handled sequentially.
Powerful enough between applications
Implemented using inter process method invocation
9
Intent (2)
Loosely coupling
The sender need not choose the receiver
The sender can specify ”action”. Then proper receiver is chosen and handles it.VIEW + http://... → Browser opens the url.VIEW + geo:0,0?q=Tokyo → Map app shows a map of Tokyo.
CALL + tel:01234567 → Phone app dials the number.
10
Inter process method invocation
used for system services
needs multi-thread programming
wrapped by AIDL to hide complexity
implemented using binder
11
Invoking method via interface
caller
callee
In the same process
12
Inter-process invocation
caller
callee
callee
caller
interface
interface
interface
How?
13
Inter-process invocation
caller
callee
Binder in kernel
callee
caller
Proxy
Binder Thread
Stub
interface
interface
interface
14
android.os.Parcel
”flatten” ”unflatten”
transmit
Delivering arguments of method
15
<<interface>>
Proxy Stub
Describing in UML ...
implements
16
<<interface>>
Proxy Stub
Describing in UML ...
caller
callee
calls
implements
extends
17
<<interface>>
Proxy Stub
AIDL
caller
callee
Auto generated from .aidl file
18
ActivityManager
Kernel
Binder Thread #1
Main Thread
Looperqueue
3:”onPause”is called in mainthread
1:Call ”schedulePauseActivity”across process
Use case: Who calls ”onPause” in Activity?
2:Send message
by Handler
Activity
19
Binder
/dev/binder
Not for general purpose. Tuned for specific transaction.
Multi-thread awareHave internal status per thead
(CF. Socket have internal status per fd.)
20
Comparing binder with socket
socket binder
internal status
associated to FD
associated to PID(FD can be shared among threads in the same process)
read & write operation
as a stream done at once by ”ioctl”
network transparency
Yes Noexpects local only
21
Transaction of Binder (1)
if (ioctl(fd, BINDER_WRITE_READ, &bwt ) >= 0) err = NO_ERROR;else err = -errno;
write buffer
read buffer
write_size
write_consumed
write_buffer
read_size
read_consumed
read_buffer
binder_write_read
22
Transaction of Binder (2)
Process A
Process B
Binder
Process A
Binder
Process B
Copy memory by copy_from _user
Copy memory by copy_to_user
Then, wake up process B
Process A and B have different memory space.They can not see each other.
Kernel
Kernel
23
AshmemAndroid / Anonymous SHared MEMory
subsystem
$(ANDROID_TOP)/system/core/cutils/ashmem.hint ashmem_create_region(const char *name, size_t size) → returns
fd
int ashmem_set_prot_region(int fd, int prot)
int ashmem_pin_region(int fd, size_t offset, size_t len)
int ashmem_unpin_region(int fd, size_t offset, size_t len)
kernel reclaim not ‘pin’ ed memory
Similar to weak reference of Java. useful to implement cache.
android.os.MemoryFile from Java program
24
System Properties of Android
'Key-Value store' type data base for basic system information.
Shared among all processes.
Init process has the master data.
The other processes have read-only copy.
Init process handles write request to the master data.
implemented using ashmem before Gingerbread.
At Gingerbread changed not to use ashmem.
25
Example of System Properties ...[ro.bt.bdaddr_path]: [/sys/module/board_mahimahi/parameters/bdaddr][ro.ril.def.agps.mode]: [2][ro.ril.fast.dormancy.rule]: [1][ro.ril.fd.scron.timeout]: [4][ro.ril.fd.scroff.timeout]: [3][ro.ril.gprsclass]: [10][init.svc.adbd]: [running][ril.reload.count]: [1][init.svc.bootanim]: [stopped][hw.keyboards.0.devname]: [mahimahi-keypad][hw.keyboards.65537.devname]: [mahimahi-keypad][hw.keyboards.131075.devname]: [h2w headset][net.hostname]: [android_200142d4dfd0d25c][dev.bootcomplete]: [1][gsm.version.ril-impl]: [HTC-RIL 2.2.1001G (Jul 16 2010,21:52:13)][gsm.sim.operator.numeric]: [][gsm.sim.operator.alpha]: [][gsm.sim.operator.iso-country]: [][gsm.sim.state]: [ABSENT][gsm.current.phone-type]: [1][wlan.driver.status]: [ok][init.svc.wpa_supplicant]: [running] ...
26
Special Key of System Properties
Starts from ”ro.” is read-only.
”ctl.start” / ”ctl.stop” is command to start/stop the service.
property_set("ctl.stop", "bootanim"); stops boot animation service
Starts from ”init.svc.” shows the state of the service.
27
Reading System Property
Kernel
init Other processes
/dev/__properties__(on tmpfs)
mmap (read/write) mmap (read only)
Just read the property in each process.
28
Writing System Property
Kernel
init Other processes
/dev/__properties__(on tmpfs)
mmap (read/write)
mmap (read only)
Send write request to init through socketsocket
29
Actual code of initialize System Properties
fd = open("/dev/__properties__", O_RDWR | O_CREAT, 0600); if (fd < 0) return -1;
if (ftruncate(fd, size) < 0) goto out;
data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if(data == MAP_FAILED) goto out;
close(fd);
fd = open("/dev/__properties__", O_RDONLY); if (fd < 0) return -1;
unlink("/dev/__properties__");
Give this fd to other processes.
No longer access by file name.
$(ANDROID_TOP)/system/core/init/property_service.c
/dev is a tmpfs
30
Resources
3rd session of Yokohama Android Platform clubhttp://www.yokohama.android-pf.org/study/dai-san-kai-benkyou-kai
1st session of Android SDK work grouphttp://www.android-group.jp/index.php?%CA%D9%B6%AF%B2%F1%2FAndroid ...
KMC bloghttp://blog.kmckk.com/archives/3611344.html
http://blog.kmckk.com/archives/3613707.html
http://blog.kmckk.com/archives/3627888.html
http://blog.kmckk.com/archives/3645070.html
http://blog.kmckk.com/archives/3647635.html
http://blog.kmckk.com/archives/3617229.html
31
Q&A and demo
http://www.kmckk.co.jp/eng/kzma9/http://www.kmckk.co.jp/eng/jet_index.html