A User Space Interface to the Android Runtime
Total Page:16
File Type:pdf, Size:1020Kb
Amniote: A User Space Interface to the Android Runtime Zachary Yannes a and Gary Tyson Department of Computer Science, Florida State University, Tallahassee, U.S.A. Keywords: Android, Zygote, Runtime, Dalvik, Virtual Machine, Preloading, ClassLoader, Library. Abstract: The Android Runtime (ART) executes apps in a dedicated virtual machine called the Dalvik VM. The Dalvik VM creates a Zygote instance when the device first boots which is responsible for sharing Android runtime libraries to new applications. New apps rely heavily on external libraries in addition to the runtime libraries for everything from graphical user interfaces to remote databases. We propose an extension to the Zygote, aptly named Amniote, which exposes the Zygote to the user space. Amniote allows developers to sideload common third-party libraries to reduce application boot time and memory. Just like the Android runtime libraries, apps would share the address to the library and generate a local copy only when one app writes to a page. In this paper, we will address three points. First, we will demonstrate that many third-party libraries are used across the majority of Android applications. Second, execution of benchmark apps show that most page accesses are before copy-on-write operations, which indicates that pages from preloaded classes will infrequently be duplicated. Third, we will provide a solution, the Amniote framework, and detail the benefits over the traditional Zygote framework. 1 INTRODUCTION which is started after the kernel is loaded, preloads common runtime classes and resource files from the Recent studies have shown that Android devices have Android and javax packages into a read-only boot im- maintained substantially higher market shares com- age. When a new application is ready to launch, zy- pared to iOS, Windows, Blackberry, and other oper- gote forks a new VM and starts the process. The new ating systems (Katariya, 2017). With the increase of app contains a reference to the boot image rather than Android devices, application development has also in- a full copy. This allows multiple applications to share creased. A recent report by AppBrain shows that as of common classes and resource files, reducing memory January 1, 2018, there are roughly 3.5 million appli- utilization. cations in the Google Play Store. The growing trend The Android kernel employs a copy-on-write me- of Android development indicates that it is an ideal chanism for the boot image. Whenever an application candidate for research. writes to an address mapped to the shared boot im- Android applications contain a combination of age, the kernel copies that page to that application’s Java bytecode, native code, and resource files. Un- address space. This allows each application to main- like traditional Java applications, Android apps exe- tain a local copy of written pages. cute in a dedicated virtual machine called DalvikVM. DalvikVM has several unique features tailored to- Since the initial Android version release in 2009, wards mobile devices which have less resources than Google has released 14 versions, with the newest, Pie, those of a laptop or desktop computer. For example, currently in beta (Wikipedia, 2018). This is a rapid Dalvik VMs utilize a register-based construct rather development cycle for a relatively new operating sys- the stack-based execution of traditional Java VMs. tem. To keep up with the rapid changes to the An- This improves execution speed and reduces the com- droid API, Google released a dedicated integrated de- plexity of switching between applications. velopment environment (IDE), Android Studio. An- The primary difference between DalvikVM and droid Studio provides support for Maven reposito- traditional VMs is the Zygote. The zygote process, ries, which simplifies the process of linking to exter- nal libraries. This caused a paradigm shift of rely- a https://orcid.org/0000-0003-3098-0807 ing on third-party libraries for common Android util- 59 Yannes, Z. and Tyson, G. Amniote: A User Space Interface to the Android Runtime. DOI: 10.5220/0007715400590067 In Proceedings of the 14th International Conference on Evaluation of Novel Approaches to Software Engineering (ENASE 2019), pages 59-67 ISBN: 978-989-758-375-9 Copyright c 2019 by SCITEPRESS – Science and Technology Publications, Lda. All rights reserved ENASE 2019 - 14th International Conference on Evaluation of Novel Approaches to Software Engineering ities such as graphic user interfaces (GUI) (Android, 2 THE JAVA ClassLoader 2018a; Android, 2018e; Android, 2018f), data stor- age mechanisms (Google, 2018b; Google, 2018c; An- Java VMs rely on a boot classpath which con- droid, 2018c), and code annotations (Android, 2018d; tains paths to all Java class files. This allows Java Google, 2018a; Android, 2018b). to support lazy loading, where classes are loaded There has been widespread adoption of many on-demand via a ClassLoader (Liang and Bracha, third-party libraries which are utilized by a large num- 1998). ClassLoaders also provide support for mul- ber of Android applications. Not only does this re- tiple namespaces, which allows a VM to have non- duce coding time but it also provides an opportu- unique class names which are resolved by unique nity optimization. Just as the Android/javax runtime package namespaces. For example, the Android SDK classes and resource files are referenced by many ap- provides the class android.app.Application contain- plications, so are the classes and resource files in a ing generic app data that is frequently extended to number of third-party libraries. contain app-specific data. In this case, the system The other option is to extend the functionality of ClassLoader will resolve the Android SDK’s Appli- the zygote process to the user space. The ability to ap- cation while a second application ClassLoader will pend classes and resource files to the zygote process’s resolve the memory space from user space would open up many As shown in (Liang and Bracha, 1998), Class- avenues for optimization. Therefore, we introduce a Loaders are typically implemented with a cache of framework called Amniote which provides methods loaded class data referenced by the class name. For of managing the zygote’s boot image from the user example, when the application requires a specific space. The Amniote framework also contains utilities class for the first time, a ClassLoader searches the for calculating common classes between a large set of classpath for the class file, loads the class file into applications. memory, and adds an entry into the ClassLoader’s The Amniote framework could also benefit mo- cache. Each subsequent use of that class then utilizes bile device vendors and Android Things developers. the class data found in the ClassLoader’s cache. Mobile vendors often fork Android with customized launchers, stock applications, and power monitoring (Peters, 2017). The applications in these forked An- 3 PREFETCHING droid versions likely share proprietary code which would benefit from adding to the boot image. An ad- ditional benefit would be quicker deployment of new ClassLoaders demonstrate a common cache problem Android version. Instead of vendors implementing where compulsory (or cold) misses accumulate since optimizations on the operating system, “profile” ap- the cache is initially empty and the first reference of plications could be written which optimize the zygote a class results in a cache miss. To circumvent this boot image depending on the type of applications the problem, prefetching fills the cache with classes it will user will be running. Similarly, Android Things de- likely access to avoid costly searches and disk I/O op- velopers deploy applications with a specific suite of erations. Cache prefetching is not a new solution. In required libraries. fact, preloading process works for both hardware and The remainder of this paper is organized as fol- software caches. Yet we limit the scope of this paper lows. In sections 2 and 3 we describe Java’s Class- to software caches. Loader and how it can benefit from prefetching in both traditional Java VMs and in Android’s Dalvik 3.1 Java VM Prefetching VMs. In section 4, we further explain the zygote pro- cess. In section 5, we analyze the commonly used An early approach, greedy prefetching, was used in third-party classes across a large sample of open- (Luk and Mowry, 1996) for recursive C program source applications. In Section 6, we evaluate the data structures. This was later extended in (Cahoon executions of applications in the Agave benchmark and McKinley, 1999) to Java programs, using intra- suite and stock Android applications. In Section 7, procedural data flow analysis to detect objects to we describe the Amniote framework. In Section 10, prefetch. Their approach demonstrates the effective- we conclude our findings and discuss future work on ness of greedy prefetching which relies upon inlining this topic. to remove unnecessary method calls. We will use a similar approach in Amniote to prefetch third-party library classes. Zaparanuks et. al present a Markov predictor for speculative class-preloading (Zaparanuks et al., 60 Amniote: A User Space Interface to the Android Runtime 2007). Using a Markov predictor allows accurate is constantly changing based on the user’s application prediction of which classes should be preloaded and preferences, it is difficult to determine which library when is the best time to do so. A similar approach is classes should be preloaded. Therefore, Amniote pro- taken by (Fulton, 2016). While these are novel and vides a dynamic preloading mechanism to user space robust methods, they only preload classes available so developers can exploit app category profiles. Our in the application. Android apps have the capability profiles are constructed based on app categories such of linking to third-party libraries in external jar files as games, internet, and navigation, but could be based which would have to be located and extracted first.