blob: be336abf0412950815b328fa298d7626ee4c1579 [file] [log] [blame]
Clay Murphy2cf66112015-01-13 19:01:18 -08001page.title=Configuring ART
2@jd:body
3
4<!--
5 Copyright 2014 The Android Open Source Project
6
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
10
11 http://www.apache.org/licenses/LICENSE-2.0
12
13 Unless required by applicable law or agreed to in writing, software
14 distributed under the License is distributed on an "AS IS" BASIS,
15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 See the License for the specific language governing permissions and
17 limitations under the License.
18-->
19
20
21<div id="qv-wrapper">
22<div id="qv">
23 <h2 id="Contents">In this document</h2>
24 <ol id="auto-toc">
25 </ol>
26</div>
27</div>
28
29<h2 id=introduction>Introduction</h2>
30
31<p>This document is intended to discuss how to configure ART and its compilation
32options. Topics addressed here include configuration of pre-compilation of the
33system image, dex2oat compilation options at first boot (and post-OTA), and how
34to trade off system partition space, data partition space, and performance.</p>
35
36<p>See <a href="http://source.android.com/devices/tech/dalvik/index.html">ART
37and Dalvik</a>, the <a
38href="http://source.android.com/devices/tech/dalvik/dex-format.html">Dalvik
39Executable format</a>, and the remaining pages on source.android.com to work
40with ART. See <a
41href="http://developer.android.com/guide/practices/verifying-apps-art.html">Verifying
42App Behavior on the Android Runtime (ART)</a> to ensure your apps work
43properly.</p>
44
45<h2 id=how_art_works>How ART works</h2>
46
47<p>ART is the new Android runtime for the Android 5.0 (Lollipop or L) release and
48beyond. Dalvik is no longer available. </p>
49
50<p>Please note, this section merely summarizes ART’s configuration. For an
51in-depth description, see the <a
52href="https://www.google.com/events/io/io14videos/b750c8da-aebe-e311-b297-00155d5066d7">Android
53runtime</a> presentation conducted at Google I/O 2014. </p>
54
55<p>ART uses ahead-of-time (AOT) compilation. This means that, at installation, dex
56code is compiled to native code in OAT files, which replace Dalvik’s odex
57files. This has several implications:</p>
58
59<ul>
60 <li> Performance is improved over Dalvik. There is also a commensurate improvement
61in power consumption measured in the lab.
62 <li> There is no runtime code cache. The OAT files are mapped to memory (and are
63thus page-able). The RAM memory footprint for OAT files might seem larger in
64terms of Proportional Set Size (PSS, or the memory shared across processes
65divided evenly between the processes). But because they are pageable we have
66found the system impact is improved in terms of real memory pressure effects as
67the Dalvik JIT cache was not pageable.
68 <li> Similar to preloaded classes in the zygote, ART attempts to pre-initialize a
69set of classes at compile time. This creates a ‘boot.art’ file that comprises
70an image of the compacted heap of pre-initialized classes and related objects.
71This file is mapped into memory upon zygote startup. While this consumes
72additional storage (typically 10MB), it speeds zygote startup and creates
73opportunities for the system to swap out some preloaded classes under memory
74pressure. This also contributes to improved <a
75href="http://source.android.com/devices/low-ram.html">low-RAM</a> performance
76for ART, since in Dalvik much of this class information would have
77been stored in dirty pages in the linear alloc space.
78 <li> Dex file compilation uses a tool called dex2oat and takes more time than
79dexopt. The increase in time varies, but 2-3x increases in compile time are not
80unusual. For example, apps that typically take a second to install using dexopt
81might take 2-3 seconds.
82 <li> OAT files are larger than odex files if full compilation is enabled. We discuss
83options to mitigate this cost later in this document.
84</ul>
85
86<h2 id=compilation_options>Compilation options</h2>
87
88<p>Dex file compilation takes more time than dexopt, which can be noticeable when
89all of a user’s apps must be compiled during first boot (after factory reset or
90after receiving an OTA). To reduce the amount of compilation needed, ART
91supports the option of pre-optimizing libraries and applications in the system
92partition. Including the pre-optimized dex files takes space in the system
93image, so these options trade first boot time for system image size. Note that
94OTAs are relatively infrequent and subsequent boot times should be the same
95with or without pre-optimization.</p>
96
97<h3 id=undefined>WITH_DEXPREOPT</h3>
98
99<p>Pre-optimization is controlled by the build option
100<code>WITH_DEXPREOPT</code>. Before the L release, this was enabled by default
101in “user” builds. Starting in L, this option is opt-in and needs to be enabled
102in the product configuration such as a device’s BoardConfig.mk file.</p>
103
104<p>Enabling <code>WITH_DEXPREOPT</code> causes everything in the system image to be
105pre-optimized. If this makes the system image too large, additional options can
106be specified to reduce the amount of pre-optimization. Note that all the
107following build options with “PREOPT” in the name must have <code>WITH_DEXPREOPT</code>
108enabled to work.</p>
109
110<p>Example usage (in product’s BoardConfig.mk):</p>
111
112<pre><code>WITH_DEXPREOPT := true</code></pre>
113
114<h3 id=dont_dexpreopt_prebuilts>DONT_DEXPREOPT_PREBUILTS</h3>
115
116<p>Enabling <code>DONT_DEXPREOPT_PREBUILTS</code> prevents the prebuilts from being
117pre-optimized. These are apps that have <code>include $(BUILD_PREBUILT)</code> specified
118in their Android.mk, such as Gmail. Skipping pre-optimization of prebuilt apps
119that are likely to be updated via Google Play saves /system space but does add
120to first boot time.</p>
121
122<p>Example usage (in product’s BoardConfig.mk):</p>
123
124<pre><code>WITH_DEXPREOPT := true
125DONT_DEXPREOPT_PREBUILTS := true</code></pre>
126
127<h3 id=with_dexpreopt_boot_img_only>WITH_DEXPREOPT_BOOT_IMG_ONLY</h3>
128
129<p>Enabling <code>WITH_DEXPREOPT_BOOT_IMG_ONLY</code> only pre-optimizes the
130boot image, which consists of boot.art with the image classes and boot.oat with
131code for the boot classpath. Enabling this saves significant /system space but
132means all apps will be optimized at first boot. Typically it is better to
133selectively disable app pre-optimization via
134<code>DONT_DEXPREOPT_PREBUILTS</code> or add-product-dex-preopt-module-config.</p>
135
136<p>Example usage (in product’s BoardConfig.mk):</p>
137
138<pre><code>WITH_DEXPREOPT := true
139WITH_DEXPREOPT_BOOT_IMG_ONLY := true</code></pre>
140
141<h3 id=local_dex_preopt>LOCAL_DEX_PREOPT</h3>
142
143<p>Pre-optimization can also be enabled or disabled on an individual app basis by
144specifying the <code>LOCAL_DEX_PREOPT</code> option in the module definition.
145This can be useful for disabling pre-optimization of apps that may immediately
146receive Google Play updates since the updates would render the pre-optimized
147code in the system image obsolete. This is also useful to save space on major
148version upgrade OTAs since users may already have newer versions of apps in the
149data partition.</p>
150
151<p><code>LOCAL_DEX_PREOPT</code> supports the values ‘true’ or ‘false’ to
152enable or disable pre-optimization respectively. In addition, ‘nostripping’ can
153be specified if pre-optimization should not strip the classes.dex file from the
154apk or jar file. Normally this file is stripped since it’s no longer needed
155after pre-optimization, but this last option is necessary to allow third-party
156APK signatures to remain valid.</p>
157
158<p>Example usage (in app’s Android.mk):</p>
159
160<pre><code>LOCAL_DEX_PREOPT := false</code></pre>
161
162<h3 id=product_dex_preopt_*>PRODUCT_DEX_PREOPT_*</h3>
163
164<p>Beginning post-L release in the Android Open Source Project (AOSP), a number of
165flags have been added that give further control to how pre-optimization is
166done. <code>PRODUCT_DEX_PREOPT_BOOT_FLAGS</code> passes options to dex2oat to control how
167the boot image is compiled. It can be used to specify customized image classes
168lists, compiled classes lists, and compiler filters, all of which are described
169in later sections. Similarly, <code>PRODUCT_DEX_PREOPT_DEFAULT_FLAGS</code>
170controls default flags to pass to dex2oat for compilation of everything besides
171the boot image, namely jar and apk files.</p>
172
173<p><code>PRODUCT_DEX_PREOPT_MODULE_CONFIGS</code> provides the ability to pass
174dex2oat options for a particular module and product configuration. It is set in
175a product’s device.mk file by <code>$(call
176add-product-dex-preopt-module-config,&lt;modules&gt;,&lt;option&gt;)</code>
177where &lt;modules&gt; is a list of <code>LOCAL_MODULE</code> and
178<code>LOCAL_PACKAGE</code> names for jar and apk files respectively. Through
179this flag, it is possible to have fine-grained control of pre-optimization for
180each dex file and a specific device. Such tuning allows /system space to be
181maximally used to improve first boot time.</p>
182
183<p>Example usage (in product’s device.mk):</p>
184
185<pre><code>PRODUCT_DEX_PREOPT_DEFAULT_FLAGS := --compiler-filter=interpret-only
186$(call add-product-dex-preopt-module-config,services,--compiler-filter=space)</code></pre>
187
Jeff Hao6d1548f2015-08-21 11:39:16 -0700188<p>These flags can also be used to selectively disable pre-optimization of a
189particular module or package by specifying <code>$(call
190add-product-dex-preopt-module-config,&lt;modules&gt;,disable)</code> in a
191product's device.mk file.</p>
192
193<p>Example usage (in product’s device.mk):</p>
194
195<pre><code>$(call add-product-dex-preopt-module-config,Calculator,disable)</code></pre>
196
Clay Murphy2cf66112015-01-13 19:01:18 -0800197<h2 id=preloaded_classes_list>Preloaded Classes List</h2>
198
199<p>The preloaded classes list is a list of classes the zygote will initialize on
200startup. This saves each app from having to run these class initializers
201separately, allowing them to start up faster and share pages in memory. The
202preloaded classes list file is located at frameworks/base/preloaded-classes by
203default, and it contains a list that is tuned for typical phone use. This may
204be different for other devices, such as wearables, and should be tuned
205accordingly. Be careful when tuning this since adding too many classes wastes
206memory loading unused classes; meanwhile, adding too few forces each app to
207have to have its own copy, again wasting memory.</p>
208
209<p>Example usage (in product’s device.mk):</p>
210
211<pre><code>PRODUCT_COPY_FILES += &lt;filename&gt;:system/etc/preloaded-classes</code></pre>
212
213<p class="note"><strong>Note:</strong> This line must be placed before
214inheriting any product configuration makefiles that get the default one from
215build/target/product/base.mk.</p>
216
217<h2 id=image_classes_list>Image Classes List</h2>
218
219<p>The image classes list is a list of classes that dex2oat initializes ahead of
220time and stores in the boot.art file. This allows the zygote to load these
221results out of the boot.art file on startup instead of running the initializers
222for these classes itself during preloading. A key feature of this is that the
223pages loaded from the image and shared between processes can be clean, allowing
224them to be swapped out easily in low-memory situations. In L, by default the
225image classes list uses the same list as the preloaded classes list. Beginning
226post-L in AOSP, a custom image classes list can be specified using
227<code>PRODUCT_DEX_PREOPT_BOOT_FLAGS</code>.</p>
228
229<p>Example usage (in product’s device.mk):</p>
230
231<pre><code>PRODUCT_DEX_PREOPT_BOOT_FLAGS += --image-classes=&lt;filename&gt;</code></pre>
232
233<h2 id=compiled_classes_list>Compiled Classes List</h2>
234
235<p>In post-L AOSP, a subset of classes from the boot classpath can be specified to
236be compiled during pre-optimization using the compiled classes list. This can
237be a useful option for devices that are very tight on space and can’t fit the
238entire pre-optimized boot image. However, note classes not specified by this
239list will not be compiled - not even on the device - and must be interpreted,
240potentially affecting runtime performance. By default, dex2oat will look for a
241compiled classes list in $OUT/system/etc/compiled-classes, so a custom one can
242be copied to that location by the device.mk. A particular file location can
243also be specified using <code>PRODUCT_DEX_PREOPT_BOOT_FLAGS</code>.</p>
244
245<p>Example usage (in product’s device.mk):</p>
246
247<pre><code>PRODUCT_COPY_FILES += &lt;filename&gt;:system/etc/compiled-classes</code></pre>
248
249<p class="note"><strong>Note:</strong> This line must be placed before
250inheriting any product configuration makefiles that get the default one from
251build/target/product/base.mk.</p>
252
253<h2 id=compiler_filters>Compiler Filters</h2>
254
255<p>In L, dex2oat takes a variety of --compiler-filter options to control how it
256compiles. Passing in a compiler filter flag for a particular app specifies how
257it’s pre-optimized. Here’s a description of each available option:</p>
258
259<ul>
260 <li><em>everything</em> - compiles almost everything, excluding class initializers and some rare
261methods that are too large to be represented by the compiler’s internal
262representation.
263 <li><em>speed</em> - compiles most methods and maximizes runtime performance, which is the
264default option.
265 <li><em>balanced</em> - attempts to get the best performance return on compilation investment.
266 <li><em>space</em> - compiles a limited number of methods, prioritizing storage space.
267 <li><em>interpret-only</em> - skips all compilation and relies on the interpreter to run code.
268 <li><em>verify-none</em> - special option that skips verification and compilation, should be used only
269for trusted system code.
270</ul>
271
272<h2 id=with_dexpreopt_pic>WITH_DEXPREOPT_PIC</h2>
273
274<p>In post-L AOSP, <code>WITH_DEXPREOPT_PIC</code> can be specified to enable
275position-independent code (PIC). With this, compiled code from the image
276doesn’t have to be relocated from /system into /data/dalvik-cache, saving space
277in the data partition. However, there is a slight runtime impact because it
278disables an optimization that takes advantage of position-dependent code.
279Typically, devices wanting to save space in /data should enable PIC
280compilation.</p>
281
282<p>Example usage (in product’s device.mk):</p>
283
284<pre><code>WITH_DEXPREOPT := true
285WITH_DEXPREOPT_PIC := true</code></pre>
286
287<h2 id=with_art_small_mode>WITH_ART_SMALL_MODE</h2>
288
289<p>For devices with very limited space, <code>WITH_ART_SMALL_MODE</code> can be
290enabled. This option compiles the boot classpath and nothing else, greatly
291reducing first boot time since most compilation is skipped. It also saves on
292storage because there is no compiled code for apps. However, this impacts
293runtime performance since app code has to be interpreted. The impact is limited
294since most performance sensitive code in the framework is still compiled, but
295regressions may appear in benchmarking.</p>
296
297<p>Example usage (in product’s device.mk):</p>
298
299<pre><code>WITH_ART_SMALL_MODE := true</code></pre>
300
301<p>In future releases, this build option will be removed since it can be done with
302this (in product’s device.mk):</p>
303
304<pre><code>PRODUCT_PROPERTY_OVERRIDES += \
305 dalvik.vm.dex2oat-filter=interpret-only \
306 dalvik.vm.image-dex2oat-filter=speed</code></pre>
307
308<h2 id=dalvik_vm_properties>dalvik.vm Properties</h2>
309
310<p>Most dalvik.vm properties in ART are similar to Dalvik, but there are a few
311additional ones as described below. Note that these options affect dex2oat
312during on-device compilation as well as during pre-optimization, whereas most
313of the options discussed above affect only pre-optimization.</p>
314
315<p>To control dex2oat while it’s compiling the boot image:</p>
316
317<ul>
318 <li>dalvik.vm.image-dex2oat-Xms: initial heap size
319 <li>dalvik.vm.image-dex2oat-Xmx: maximum heap size
320 <li>dalvik.vm.image-dex2oat-filter: compiler filter option
321</ul>
322
323<p>To control dex2oat while it’s compiling everything besides the boot image:</p>
324
325<ul>
326 <li> dalvik.vm.dex2oat-Xms: initial heap size
327 <li> dalvik.vm.dex2oat-Xmx: maximum heap size
328 <li> dalvik.vm.dex2oat-filter: compiler filter option
329</ul>
330
331<p>The options that control initial and maximum heap size for dex2oat should not
332be reduced since they could limit what applications can be compiled.</p>
333
334<h2 id=sample_usage>Sample Usage</h2>
335
336<p>The goal of these compiler options is to utilize available space in the system
337and data partition to reduce the amount of dex2oat that must be performed by
338the device. </p>
339
340<p>For devices with ample system and data space, enabling dex pre-optimization is
341all that is necessary.
342
343<p>BoardConfig.mk:</p>
344
345<pre><code>WITH_DEXPREOPT := true</code></pre>
346
347<p>If this causes the system image to become too large, the next thing to try is
348disabling pre-optimization of the prebuilts.
349
350<p>BoardConfig.mk:</p>
351
352<pre><code>WITH_DEXPREOPT := true
353DONT_DEXPREOPT_PREBUILTS := true</code></pre>
354
355<p>Again, if the system image is still too large, try pre-optimizing only the boot
356image.
357
358<p>BoardConfig.mk:</p>
359
360<pre><code>WITH_DEXPREOPT := true
361WITH_DEXPREOPT_BOOT_IMG_ONLY := true</code></pre>
362
363<p>However, limiting to pre-optimizing only the boot-image means all apps will
364have to be optimized on first boot. In order to avoid this, it is possible to
365combine these high level flags with more fine-grained controls to maximize the
366amount of pre-optimized apps.</p>
367
368<p>For instance, if disabling the pre-optimization of the prebuilts almost fits
369into the system partition, compiling the boot classpath with the ‘space’ option
370may make it fit. Note this compiles fewer methods in the boot classpath,
371potentially interpreting more code and impacting runtime performance.
372
373<p>BoardConfig.mk:</p>
374
375<pre><code>WITH_DEXPREOPT := true
376DONT_DEXPREOPT_PREBUILTS := true</code></pre>
377
378<p>device.mk:</p>
379
380<pre><code>PRODUCT_DEX_PREOPT_BOOT_FLAGS := --compiler-filter=space</code></pre>
381
382<p>If a device has very limited system partition space, it’s possible to compile a
383subset of classes in the boot classpath using the compiled classes list. Boot
384classpath methods that aren’t in this list will have to be interpreted, which
385could affect runtime performance.
386
387<p>BoardConfig.mk:</p>
388
389<pre><code>WITH_DEXPREOPT := true
390WITH_DEXPREOPT_BOOT_IMG_ONLY := true</code></pre>
391
392<p>device.mk:</p>
393
394<pre><code>PRODUCT_COPY_FILES += &lt;filename&gt;:system/etc/compiled-classes</code></pre>
395
396<p>If a device has both limited space in the system and data partitions, compiler
397filter flags can be used to disable compilation of certain apps. This will save
398space in both system and data, as there won’t be any compiled code, but these
399apps will have to be interpreted. This example configuration would pre-optimize
400the boot classpath but prevent compilation of other apps that are not
401prebuilts. However, to prevent noticeable performance degradation of
402system_server, the services.jar is still compiled but optimized for space. Note
403that user-installed applications will still use the default compiler filter of
404speed.
405
406<p>BoardConfig.mk:</p>
407
408<pre><code>WITH_DEXPREOPT := true
409DONT_DEXPREOPT_PREBUILTS := true</code></pre>
410
411<p>device.mk:</p>
412
413<pre><code>PRODUCT_DEX_PREOPT_DEFAULT_FLAGS := --compiler-filter=interpret-only
414$(call add-product-dex-preopt-module-config,services,--compiler-filter=space)</code></pre>
415
416<p>For a major version upgrade OTA, it can be useful to blacklist certain apps
417from being pre-optimized since they will likely be out of date. This can be
418done by specifying <code>LOCAL_DEX_PREOPT</code> (for all products) or with
419<code>PRODUCT_DEX_PREOPT_MODULE_CONFIGS</code> (for a particular product).
420
421<p>BoardConfig.mk:</p>
422
423<pre><code>WITH_DEXPREOPT := true</code></pre>
424
425<p>Android.mk (of blacklisted apps):</p>
426
427<pre><code>LOCAL_DEX_PREOPT := false</code></pre>