LWN.net Logo

ABS: The guts of Android

By Jake Edge
April 27, 2011

In a fairly fast-paced talk, Karim Yaghmour presented the internals of Android systems at the Android Builders Summit. The talk focused on things like Android's application development model, the parts and pieces that make it up, and its startup sequence. It gave an overall picture of a system that is both familiar and not.

[Karim Yaghmour] Yaghmour is the lead author of Building Embedded Linux Systems and has done Linux kernel development along the way. He developed the original Linux Trace Toolkit (LTT) in 1999, which has since been taken over by another École Polytechnique de Montréal graduate, Mathieu Desnoyers, as LTT next generation (LTTng). Desnoyers is "doing a much better job" with it than he did, he said with a chuckle. He also developed relayfs, which is an efficient way to relay large amounts of data from kernel to user space. He is now doing Android development and training.

Android internals

With a slide showing Kirk and Spock from the original Star Trek, and the line "it's Linux, Jim, but not as we know it", Yaghmour pointed out that Android is a "strange beast" that "looks weird, feels weird, and acts weird". That's because it doesn't have the traditional Linux user space, but instead has its own user space that sits atop a somewhat non-standard Linux kernel.

For example, there is no "entry point" to an application for Android. Developers create components that get bundled together as applications. An application consists of multiple components, some of which may be shared with other applications. Each application is hosted in a Linux process. Any of those components can disappear while the system is running because its application process goes away. That can be because the application is no longer needed or because of memory pressure from other applications being loaded. That means that components need to implement "lifecycle management". Essentially, components need to be able to come back again the way they were before being killed.

Android also uses messages called "intents" that are sent between components. Yaghmour said they are like "polymorphic Unix signals" that can be sent to a specific component or service, but can also be broadcast. Applications can register their interest in various intents by specifying Intent Filters in their manifest files.

Remote procedure calls (RPCs) (or inter-process communication aka IPC) are done using the "Binder" in Android because "System V IPC sucks", at least according to comments in the Android code. The Binder allows components to talk to services and for services to talk to each other. The Binder is not used directly, however, and instead interfaces are defined using an interface definition language (IDL). Those IDL definitions are fed to a tool that generates Java code to handle the communication.

The development environment for Android is "fantastic", Yaghmour said. The Android SDK provides everything that is needed to create applications. The problems come when trying to develop a device that runs Android, he said, because the "glue that allows all these APIs to talk to the kernel is not documented anywhere". For a "normal" embedded Linux system, you generally just need the kernel, a C library, and BusyBox, which is generally enough to allow you to build any custom applications, but for Android, the picture is much more complex.

It is still the Linux kernel at the bottom, but that's about it that is the same as a normal Linux system. The kernel has numerous patches applied to it for things like wakelocks and the Binder, but it is recognizably Linux. Above that, things start to change. There are a number of libraries available, some of which appear in other systems (Linux, BSD, etc.), like WebKit and SQLite, but some are Android-specific, like the libc replacement, Bionic.

Android has its own init, which is not based on either System V init, or on BusyBox's, partly because it doesn't use the latter. Instead of BusyBox, Android has something called Toolbox that fills the same role, but not as well. Yaghmour said that the first thing he does on an Android system is to replace Toolbox with BusyBox. It was a political decision not to use BusyBox, rather than a technical one, he said. There are also various libraries to support hardware like audio devices, cameras, GPS devices, and so on, all of which are implemented in C or C++.

Android uses Java Native Interface (JNI) to talk to any of that lower level code from the Java-based code that makes up (most of) the rest of user space. The Dalvik virtual machine uses JNI to call those libraries. The system classes (in the android.* namespace), as well as the Apache Harmony-based standard Java classes (in java.*) sit atop of Dalvik, as does the all-important System Server. Above those are the stock Android applications along with any other applications installed by the user (from the Market or elsewhere).

Android replaces the Java virtual machine with Dalvik, and the JDK with classes from Apache Harmony. To create .dex files for Dalvik, the Java is first processed by the Java tools to create .class files, which are then post-processed by dx to produce the files used by Dalvik. One interesting thing noted by Yaghmour is that .dex files are typically half the size of the equivalent .class files.

The layout of the native Android user space is very different than standard Linux as well. There is no /bin or /etc, which nearly every standard Linux tool expects to find. The two main directories in Android are /data (for user data) and /system (for the core system components). But some of the expected directories are present, like /dev and /proc.

Android startup

After that relatively high-level overview of the Android system, Yaghmour looked at the Android startup sequence, starting with the bootloader. That bootloader implements a protocol called "fastboot" that is used to control the boot process over USB using a tool of the same name on a host system. The bootloader contains code to copy various portions of the code around on the flash (for returning to a stock image for example), and allows users to boot from a special partition that contains a recovery program (via a magic key sequence at boot time). Some of these are features that might make their way into U-Boot or other bootloaders, he said.

The flash layout of a typical device has multiple partitions to support Android, including "boot" (which is where the kernel resides), "system", "userdata", and "cache" (the latter three corresponding the mounted /system, /data, and /cache filesystems on a running Android system). Yaghmour noted that Android does not follow the Filesystem Hierarchy Standard (FHS) with its filesystems, but it also doesn't conflict with that standard, which allows folks to install FHS filesystems alongside Android's.

Newer Android releases are using the ext4 filesystem, rather than the yaffs2 filesystems used on earlier devices. That's because the latest devices are not using a classic NAND flash chip, and instead are using something that looks like an SD card to the processor. The kernel treats it like a block device. Because the newer devices have multicore processors, Google wanted a filesystem that is optimized for SMP, he said, thus the move from yaffs2 to ext4.

Once the kernel has booted, it "starts one thing and one thing only" and that is the init process. Init parses /init.rc and runs what it finds there. Typically that means it creates mount points and mounts filesystems, starts a low-memory handler that is specific to Android (and runs before the kernel's out-of-memory (OOM) killer), starts a bunch of services (including the servicemanager which manages Binder contexts), and starts the "root" of the Java process tree, Zygote (aka app_process).

Zygote is the parent process of all application processes in the system. It "warms up the cache of classes" so that applications start quickly, and starts the System Service. The System Service is a key part of the Android system, but one that is not very well documented. "Anything that is important that is running in the system is housed inside the System Service", Yaghmour said. That includes services for various hardware devices (battery, lights, vibrator, audio, sensors, etc.), as well as managers for things like alarms, notifications, activities, and windows (i.e. the window manager).

The System Service starts the ActivityManager to do what its name implies, manage activities. It is "one of the most important services" in Android, he said. It handles starting activities, broadcasting events, and more. He likened it to the kernel's scheduler. When an activity needs to be started, for example, the ActivityManager asks Zygote over a socket to do so.

The hardware devices are generally accessed via their services, which call into an underlying library that is typically provided by a vendor. For things like LEDs, Android provides a .h file that describes the interface and vendors create a C program that implements it. It is similar for other devices like GPS, audio, camera, and so on. For WiFi, wpasupplicant is used, while Bluetooth support comes from BlueZ.

Because of GPL concerns, Android talks to BlueZ via D-Bus, which may be controversial in some quarters, Yaghmour said. In answer to a question from an audience member, he noted that Google wanted to avoid having its partners have to explain licensing to their engineers. So, it chose not to use GPL-covered software other than the kernel to keep user space "free" of licensing concerns. That gets a little blurrier with the inclusion of BlueZ, but the "theory" is that the GPL does not apply to code that talks to it via D-Bus. Some may disagree with that theory, he said.

It must have been hard to pull together a reasonable look at Android's guts that would fit into a 50-minute slot, but Yaghmour largely succeeded in doing so. There were undoubtedly lots of details passed over, but attendees definitely got a good feel for what goes on inside the phone that resides in many of their pockets.


(Log in to post comments)

ABS: The guts of Android

Posted Apr 28, 2011 4:46 UTC (Thu) by ikm (subscriber, #493) [Link]

Is there a video available for the talk?

ABS: The guts of Android

Posted Apr 28, 2011 5:25 UTC (Thu) by swetland (subscriber, #63414) [Link]

Interesting. Some of this is fascinating speculation, but a lot of it is certainly on the money. Regarding some of not-quite-how-it-is pieces:

The lack of including sysv ipc and the use of the Binder actually have nothing (directly) to do with each other.

sysv ipc is avoided because it allows for resource leakage beyond the life of a process. For the same reason, a /tmp filesystem is avoided, instead ashmem is provided which can give you the equivalent of anonymous shareable regions (passable by fd) that you'd create by open/truncate/unlink without needing to worry about leaking there either.

The toolbox was never intended to be a full commandline environment replacement, just an absolute minimum for state inspection and debugging -- /data/local/{bin,lib,etc} exist to give you somewhere to install "unix commandline goodies" on a typical android system.

The selection of ext4 had nothing to do with SMP -- emmc devices are traditional block devices, not raw nand, and thus need a traditional filesystem on top of the block layer. There's no real way (or reason) to use yaffs2 on emmc devices -- ext4 gets the job done.

/etc does exist (typically a symlink to /system/etc) and is a bit of a dumping ground for random configuration files, sadly.

ABS: The guts of Android

Posted Apr 28, 2011 13:53 UTC (Thu) by karim (subscriber, #114) [Link]

Hi Brian,

Thanks for taking the time to look at this, and for dotting the I's and crossing the T's. Ideally, it would have been great to have you with us then at the event (or anyone from the Android team for that matter.) This actually was one of Jake's earlier points regarding the ABS: http://lwn.net/Articles/439337/. If nothing else, JIT auto-correct would have been possible ;) Maybe next year ... ?

If my memory serves me right, I did mention something to the effect of SysV IPC being open to DOS issues, but your explanation makes the issue clearer for sure. My reading of the SysV IPC matter with regards to Android is based on the [aosp]/ndk/docs/system/libc/SYSV-IPC.html file.

Regarding ext4 and SMP/multi-core, that one actually comes straight from Google's own Ted T'so (original LWN story: http://lwn.net/Articles/421117/ and original blog post by Ted T'so on ext4 and Android: http://www.linuxfoundation.org/news-media/blogs/browse/20...). So, in all honesty, I'm confused on this one. Maybe ext4 doesn't have anything to do with SMP, but doesn't the choice of a block device (for which there exists a lot of multi-core-optimized filesystems) instead of a flash device (for which most filesystems are not typically optimized for multi-core) have something to do with the increasing use of multi-core?

To an extent, I guess this kind'a brings into focus the issue of Android's development model. It is in fact very hard to fully understand the decisions behind Android's design or any significant change from one AOSP version to the next because we (non-members of the Android dev team) are not privy to the conversations amongst Android's developers. That said, I fully understand and respect the business logic behind this and, most importantly, appreciate your personal effort (and that of other Android developers) in participating in the wider community on forums such as this one. However, it remains that so long as key design decisions are taken behind closed doors without the involvement of the wider set of platform developers who have aligned their work with Android, speculation and misunderstandings are bound to continue. I wish I had a solution for this, but I don't. I just think this issue needs to be put out there. And maybe we just need to live with it. After all, what you guys are doing is nothing short of amazing. So it's hard to fault anyone on this. At the end of the day, though, it would have been great if I (and others) would have had a mailing list archive to grep instead of having to look at the code and sometimes have to guess at why this or that is being done.

Thanks again for having taken a look at this and for your involvement. I hope the above makes some sense.

Karim

ABS: The guts of Android: ext4 vs yaffs2

Posted Apr 29, 2011 16:27 UTC (Fri) by csimmonds (subscriber, #3130) [Link]

I think the choice of file system is dependent on the storage technology: Android devices with raw NAND flash will continue to use yaffs2, I imagine, but those using eMMC, which is basically a micro SD card soldered on to the main board, have to use a "normal" file system. In that case, ext4 seems a reasonable choice. Coincidentally, ext4 is likely to be more multi-threaded than yaffs2. So, I don't think it is a straight yaffs2 vs ext4 debate, it is more a question of horses for courses.

ABS: The guts of Android: ext4 vs yaffs2

Posted Apr 29, 2011 16:48 UTC (Fri) by karim (subscriber, #114) [Link]

Agreed. I've never thought about this as an ext4 vs. yaffs2 thing. But I am wondering if the shift to emmc is due to multi-core (as one would conclude if Ted's line of thought is followed) or due to the fact that hardware manufacturers just wanted their customers to be able to use standard filesystems on their devices (or the customers asking for it.) In all likeliness, the decision was made by hardware design guys ...

nand to eMMC - pin counts

Posted Apr 29, 2011 22:01 UTC (Fri) by pflugstad (subscriber, #224) [Link]

I believe it's about pin counts. Bare nand requires a lot of pins. eMMC is essentially a serial interface, with variable width data path (8 bit max). So by going with eMMC, you reduce your required pin counts, which equals cheaper boards.

ABS: The guts of Android

Posted Apr 30, 2011 7:35 UTC (Sat) by swetland (subscriber, #63414) [Link]

Much respect to Ted, who is awesome, but he's definitely incorrect about SMP being a major reason for the ext4 decision. SMP is also not the driving force for eMMC storage device choice -- that's more of the usual cost/performance/density decision making typical to storage parts selection for embedded devices.

As far as making platform design decisions more visible, that is something that we do want to do, and we are also working to move more of the core platform development "out in the open" over time -- for example the SDK tools development now happens entirely against the git repositories on android.git.kernel.org.

More documentation, aimed at lower level platform developers, OEMs, and integrators is also something that we're working on, especially around the HAL modules and other points of integration with specific devices.

ABS: The guts of Android

Posted Apr 30, 2011 13:46 UTC (Sat) by karim (subscriber, #114) [Link]

Very cool! Thanks.

ABS: The guts of Android

Posted Apr 28, 2011 13:55 UTC (Thu) by karim (subscriber, #114) [Link]

I know the Free Electrons folks filmed it, thought I don't think it's out yet. If past is prelude, there should be a post by LWN when the videos become available.

ABS: The guts of Android

Posted Jun 7, 2011 21:19 UTC (Tue) by mkaehlcke (guest, #61834) [Link]

The video and the slides of the talk are available on the website of Free Electrons:

http://free-electrons.com/blog/abs-2011-videos/

ABS: The guts of Android

Posted Apr 28, 2011 7:00 UTC (Thu) by dlang (✭ supporter ✭, #313) [Link]

is the toolbox that android ships the busybox replacement that Rob Landley is developing?

ABS: The guts of Android

Posted Apr 29, 2011 20:34 UTC (Fri) by vapier (subscriber, #15768) [Link]

his is called "toybox" iirc, and using that over busybox wouldnt provide any advantage in the area concerned. it's still GPL-2 for example.

ABS: The guts of Android

Posted Apr 25, 2012 17:47 UTC (Wed) by tbird20d (subscriber, #1901) [Link]

As of March 2012 or so, it's been moved over to a BSD license, specifically to allow use on Android without increasing the amount of GPL in user-space.

ABS: The guts of Android

Posted Apr 28, 2011 16:02 UTC (Thu) by paravoid (subscriber, #32869) [Link]

A bit off-topic but what's even more weird (at least to people coming from the GNU/Linux world) than the Android system is the ecosystem around it.

Hundreds of shift-zips-around-and-embed-software-illegaly "ROMs" out there, with names like Japanese Jellyfish, Cyanogenmod etc. along with required "mods" like Clockworkmod, Amon_Ra's etc.

Applications that have weird names (often based on the author's name), usually of doubtful quality, with no way to check on their license (or if they're ad-ridden or not for that matter) or file bug reports on them.

My experience with Android is brief but it feels like "modding" rather than hacking and it and it certainly feels more like a Windows Mobile phone than a Linux phone…

[ N900 /was/ my phone of choice ]

ABS: The guts of Android

Posted Apr 28, 2011 16:32 UTC (Thu) by jubal (subscriber, #67202) [Link]

well, at least there are usable contact/calendaring applications (I had a N810; nice piece of hardware, but the quality of the only barely usable PIM application set was abysmal).

(I'm using my Nexus S phone more like a palmtop with a network connectivity than a phone, though.)

ABS: The guts of Android

Posted Apr 29, 2011 20:37 UTC (Fri) by vapier (subscriber, #15768) [Link]

eh ? Cyanogenmod isnt shipping anything illegal. it's a fairly good community android image.

http://www.cyanogenmod.com/

ABS: The guts of Android

Posted Apr 29, 2011 20:57 UTC (Fri) by paravoid (subscriber, #32869) [Link]

Oh that didn't came out right. I knew that and I wasn't referring to that specifically when I was talking on the illegal ROMs; Cyanogen is actually quite good both on a quality level and on a project level (they actually build from source, have repositories, bug trackers etc.).

ABS: The guts of Android

Posted Apr 30, 2011 7:28 UTC (Sat) by swetland (subscriber, #63414) [Link]

I think a lot of the ROM/Modder community (which often operates on the "dump rom, hack on rom, push it back" model) originated from the wince/winmobile ROM/Modder community -- something that the xda-developers site seemed focused on pre-Android.

Early on (around G1 launch) it was often puzzling to watch folks try to reverse engineer file formats that there was full source for manipulating published. I think that some of this community was so used to not having source available and only being able to fiddle with whatever files/images ship on devices that it never occurred to people to look at the source code.

ABS: The guts of Android

Posted May 5, 2011 3:57 UTC (Thu) by Hausvib6 (guest, #70606) [Link]

Android is a major OS in mobile devices which is much more open. The modder community is not used to the usual development way in FLOSS (there are always exceptions). It would be great if these modders starts embracing the FLOSS way: providing the source code of the binary, host it on sourceforge/github if it's a bit big, build scripts (if needed), license, copyright notice, etc.

For the illegal mod... well....

ABS: The guts of Android

Posted May 5, 2011 4:02 UTC (Thu) by swetland (subscriber, #63414) [Link]

You see some of the adoption of more typical open source development techniques among the Cyanogenmod folks, who host repo/git repositories, etc:
https://github.com/cyanogenmod
http://review.cyanogenmod.com/#q,status:open,n,z

ABS: The guts of Android

Posted May 5, 2011 19:35 UTC (Thu) by njs (guest, #40338) [Link]

Now if they could just figure out package management, instead of this lovely "upgrade by unzipping a new filesystem on top of the old one" system... (Obviously this is far from trivial technically, since android doesn't come divided up into separately developed packages, but it would still be a huge improvement.)

ABS: The guts of Android

Posted May 5, 2011 19:39 UTC (Thu) by karim (subscriber, #114) [Link]

Not directly in line with this but Magnus Back (Sony-Ericsson) gave a very good talk at the Android Builders Summit on how they've been using Debian packages to put together phones for all their customers (carriers) without having to rebuild everything from scratch for every single phone variant:
http://events.linuxfoundation.org/events/android-builders...
This isn't exactly what you're requesting, but relevant still.

"it's Linux, Jim, but not as we know it"

Posted Apr 29, 2011 16:18 UTC (Fri) by csimmonds (subscriber, #3130) [Link]

He he! That was from my tutorial at at ELC-E 2010 (slides and video available at Free Electrons). Or maybe it is so obvious that we just converged on the same idea. Nice talk, Karim, wish I had been there.

"it's Linux, Jim, but not as we know it"

Posted Apr 29, 2011 16:21 UTC (Fri) by jake (editor, #205) [Link]

> Or maybe it is so obvious that we just converged on the same idea.

IIRC, he did credit someone with that slide ... I didn't catch the name (sorry!) but it could well have been you ...

jake

"it's Linux, Jim, but not as we know it"

Posted Apr 29, 2011 16:37 UTC (Fri) by karim (subscriber, #114) [Link]

That slide is priceless.

Typically, here's how I jokingly introduce it: "This isn't mine and I won't give it to you as I suspect whoever put this together is violating someone's copyright, but this is exactly how Android feels to anyone coming from a Linux background."

And indeed, that's from your ELC-E presentation. I wasn't there at your presentation either, but that slide is one of the nuggets you find when looking at slides from past events. I can tell you it sets a very good mood with the audience :)

Thanks for your kind words. Hopefully we get to meet in person at a future event.

Karim

ABS: The guts of Android

Posted Jun 10, 2011 11:14 UTC (Fri) by akilan (guest, #75614) [Link]

>That's because it doesn't have the traditional Linux user space, but instead has its own user space that sits atop a somewhat non-standard Linux kernel.

Great to know that Linux has/had user space. That is why some people insist on calling the full OS as GNU/Linux so that people won't forget the name of the user space they are using and write correctly in the articles they are writing!

PS: I have seen FSF websites writing Android/Linux acknowledging the fact that Android has its own user space and Linux Kernel.

Copyright © 2011, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds