Home | History | Annotate | Download | only in dalvik
      1 <html devsite>
      2   <head>
      3     <title>Configuring ART</title>
      4     <meta name="project_path" value="/_project.yaml" />
      5     <meta name="book_path" value="/_book.yaml" />
      6   </head>
      7   <body>
      8   <!--
      9       Copyright 2017 The Android Open Source Project
     10 
     11       Licensed under the Apache License, Version 2.0 (the "License");
     12       you may not use this file except in compliance with the License.
     13       You may obtain a copy of the License at
     14 
     15           http://www.apache.org/licenses/LICENSE-2.0
     16 
     17       Unless required by applicable law or agreed to in writing, software
     18       distributed under the License is distributed on an "AS IS" BASIS,
     19       WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     20       See the License for the specific language governing permissions and
     21       limitations under the License.
     22   -->
     23 
     24 
     25 
     26 <p>This page discusses how to configure ART and its compilation options. Topics addressed here
     27 include configuration of pre-compilation of the system image, dex2oat compilation options,
     28 and how to trade off system partition space, data partition space, and performance.</p>
     29 
     30 <p>See <a href="http://source.android.com/devices/tech/dalvik/index.html">ART
     31 and Dalvik</a>, the <a
     32 href="http://source.android.com/devices/tech/dalvik/dex-format.html">Dalvik
     33 Executable format</a>, and the remaining pages on source.android.com to work
     34 with ART. See <a
     35 href="http://developer.android.com/guide/practices/verifying-apps-art.html">Verifying
     36 App Behavior on the Android Runtime (ART)</a> to ensure your apps work
     37 properly.</p>
     38 
     39 <h2 id=how_art_works>How ART works</h2>
     40 
     41 <p>ART uses ahead-of-time (AOT) compilation, and starting in Android 7.0
     42 (Nougat or N), it uses a hybrid combination of AOT, just-in-time (JIT)
     43 compilation, and profile-guided compilation.  The combination of all these
     44 compilation modes is configurable and will be discussed in this section. As an
     45 example, Pixel devices are configured with the following compilation flow:</p>
     46 <ol>
     47 <li>An application is initially installed without any AOT compilation. The
     48   first few times the application runs, it will be interpreted, and methods
     49   frequently executed will be JIT compiled.</li>
     50 <li>When the device is idle and charging, a compilation daemon runs to
     51   AOT-compile frequently used code based on a profile generated during the
     52   first runs.</li>
     53 <li>The next restart of an application will use the profile-guided code and
     54   avoid doing JIT compilation at runtime for methods already compiled. Methods
     55   that get JIT-compiled during the new runs will be added to the profile, which
     56   will then be picked up by the compilation daemon.</li>
     57 </ol>
     58 
     59 <p>ART comprises a compiler (the <code>dex2oat</code> tool) and a runtime
     60 (<code>libart.so</code>) that is loaded for starting the Zygote. The
     61 <code>dex2oat</code> tool takes an APK file and generates one or more
     62 compilation artifact files that the runtime loads. The number of files, their
     63 extensions, and names are subject to change across releases, but as of the
     64 Android O release, the files being generated are:</p>
     65 <ul>
     66 <li><code>.vdex</code>: contains the uncompressed DEX code of the
     67   APK, with some additional metadata to speed up verification.</li>
     68 <li><code>.odex</code>: contains AOT compiled code for methods in the
     69   APK.</li>
     70 <li><code>.art (optional)</code>: contains ART internal
     71   representations of some strings and classes listed in the APK, used to speed
     72   application startup. </li>
     73 </ul>
     74 
     75 <h2 id=compilation_options>Compilation options</h2>
     76 
     77 <p>Compilation options for ART are of two categories:
     78 <ol>
     79 <li>System ROM configuration: what code gets AOT-compiled when building a
     80   system image.</li>
     81 <li>Runtime configuration: how ART compiles and runs applications on a
     82   device.</li>
     83 </ol>
     84 </p>
     85 
     86 <p>One core ART option to configure these two categories is <em>compiler
     87 filters</em>. Compiler filters drive how ART compiles DEX code and is an
     88 option passed to the <code>dex2oat</code> tool. Starting in Android O, there
     89 are four officially supported filters:</p>
     90 <ul>
     91 <li><em>verify</em>: only run DEX code verification.</li>
     92 <li><em>quicken</em>: run DEX code verification and optimize some DEX
     93   instructions to get better interpreter performance.</li>
     94 <li><em>speed</em>: run DEX code verification and AOT-compile all methods.</li>
     95 <li><em>speed-profile</em>: run DEX code verification and AOT-compile methods
     96   listed in a profile file.</li>
     97 </ul>
     98 
     99 <h3 id=system_rom>System ROM configuration</h3>
    100 
    101 <p>There are a number of ART build options available for configuring a system
    102 ROM. How to configure these options depends on the available storage space for
    103 <code>/system</code> and the number of pre-installed applications. The
    104 JARs/APKs that are compiled into a system ROM can be divided in four
    105 categories:</p>
    106 <ul>
    107 <li>Boot classpath code: compiled with the <em>speed</em> compiler filter by
    108     default.</li>
    109 <li>System server code: compiled with the <em>speed</em> compiler filter by
    110   default.</li>
    111 <li>Product-specific core applications: compiled with the <em>speed</em>
    112   compiler filter by default.</li>
    113 <li>All other applications: compiled with the <em>quicken</em> compiler filter
    114   by default.</li>
    115 </ul>
    116 
    117 <h4 id=build_options>Makefile options</h4>
    118 <ul>
    119 
    120 <li><code>WITH_DEXPREOPT</code></li>
    121 <p>
    122 Whether <code>dex2oat</code> is invoked on DEX code installed on the system image. Enabled by default.
    123 </p>
    124 
    125 <li><code>DONT_DEXPREOPT_PREBUILTS</code> (since Android 5.0)</li>
    126 <p>
    127 Enabling <code>DONT_DEXPREOPT_PREBUILTS</code> prevents the prebuilts from being
    128 pre-optimized. These are apps that have <code>include $(BUILD_PREBUILT)</code>
    129 specified in their <code>Android.mk</code>, such as Gmail. Skipping
    130 pre-optimization of prebuilt apps that are likely to be updated via Google Play
    131 saves <code>/system</code> space but does add to first boot time.
    132 </p>
    133 
    134 <li><code>WITH_DEXPREOPT_BOOT_IMG_ONLY</code></li>
    135 
    136 <p>Enabling <code>WITH_DEXPREOPT_BOOT_IMG_ONLY</code> pre-optimizes only the
    137 boot classpath.
    138 
    139 <li><code>LOCAL_DEX_PREOPT</code></li>
    140 
    141 <p>Pre-optimization can also be enabled or disabled on an individual app basis by
    142 specifying the <code>LOCAL_DEX_PREOPT</code> option in the module definition.
    143 This can be useful for disabling pre-optimization of apps that may immediately
    144 receive Google Play updates since the updates would render the pre-optimized
    145 code in the system image obsolete. This is also useful to save space on major
    146 version upgrade OTAs since users may already have newer versions of apps in the
    147 data partition.</p>
    148 
    149 <p><code>LOCAL_DEX_PREOPT</code> supports the values true or false to
    150 enable or disable pre-optimization, respectively. In addition, nostripping can
    151 be specified if pre-optimization should not strip the <code>classes.dex</code>
    152 file from the APK or JAR file. Normally this file is stripped since its no
    153 longer needed after pre-optimization, but this last option is necessary to
    154 allow third-party APK signatures to remain valid.</p>
    155 
    156 <li><code>PRODUCT_DEX_PREOPT_BOOT_FLAGS</code></li>
    157 <p>
    158 Passes options to <code>dex2oat</code> to control how the boot image is
    159 compiled. It can be used to specify customized image classes lists, compiled
    160 classes lists, and compiler filters.
    161 </p>
    162 
    163 <li><code>PRODUCT_DEX_PREOPT_DEFAULT_FLAGS</code></li>
    164 <p>
    165 Passes options to <code>dex2oat</code> to control how everything besides the
    166 boot image is compiled.
    167 </p>
    168 
    169 <li><code>PRODUCT_DEX_PREOPT_MODULE_CONFIGS</code></li>
    170 <p>
    171 Provides the ability to pass <code>dex2oat</code> options for a particular
    172 module and product configuration. It is set in a products
    173 <code>device.mk</code> file by <code>$(call add-product-dex-preopt-module-config,&lt;modules&gt;,&lt;option&gt;)</code>
    174 where <code>&lt;modules&gt;</code> is a list of LOCAL_MODULE and LOCAL_PACKAGE names
    175 for JAR and APK files, respectively.
    176 </p>
    177 
    178 <li><code>PRODUCT_DEXPREOPT_SPEED_APPS (New in Android O)</code></li>
    179 <p>
    180 List of applications that have been identified as core to the products and
    181 which are desirable to compile with the <em>speed</em> compiler filter. For
    182 example, persistent apps such as SystemUI get a chance to use
    183 profile-guided compilation only at the next reboot, so it may be better for the
    184 product to have these apps always AOT-compiled.
    185 </p>
    186 
    187 <li><code>PRODUCT_SYSTEM_SERVER_APPS (New in Android O)</code></li>
    188 <p>
    189 List of applications that are loaded by the system server. These applications
    190 will be compiled by default with the <em>speed</em> compiler filter.
    191 </p>
    192 
    193 <li><code>PRODUCT_ART_TARGET_INCLUDE_DEBUG_BUILD(Post Android O)</code></li>
    194 <p>
    195 Whether to include a debug version of ART on the device. By default, this is
    196 enabled for userdebug and eng builds. The behavior can be overridden by explicitly
    197 setting the option to <em>true</em> or <em>false</em>.
    198 </p>
    199 <p>
    200 By default, the device will use the non-debug version (<em>libart.so</em>).
    201 To switch, set the system property <code>persist.sys.dalvik.vm.lib.2</code> to
    202 <em>libartd.so</em>.
    203 </p>
    204 
    205 <li><code>WITH_DEXPREOPT_PIC (Removed in Android O)</code></li>
    206 
    207 <p>In Android 5.1.0 through Android 6.0.1, <code>WITH_DEXPREOPT_PIC</code> can
    208 be specified to enable position-independent code (PIC). With this, compiled
    209 code from the image doesnt have to be relocated from /system into
    210 /data/dalvik-cache, saving space in the data partition. However, there is a
    211 slight runtime impact because it disables an optimization that takes advantage
    212 of position-dependent code.  Typically, devices wanting to save space in /data
    213 should enable PIC compilation.</p>
    214 
    215 <p>In Android 7.0, PIC compilation was enabled by default.</p>
    216 
    217 </ul>
    218 
    219 
    220 
    221 <h4 id=boot_classpath>Boot classpath configuration</h4>
    222 
    223 <ul>
    224 <li>Preloaded Classes List</li>
    225 
    226 <p>The preloaded classes list is a list of classes the zygote will initialize on
    227 startup. This saves each app from having to run these class initializers
    228 separately, allowing them to start up faster and share pages in memory. The
    229 preloaded classes list file is located at frameworks/base/preloaded-classes by
    230 default, and it contains a list that is tuned for typical phone use. This may
    231 be different for other devices, such as wearables, and should be tuned
    232 accordingly. Be careful when tuning this since adding too many classes wastes
    233 memory loading unused classes; meanwhile, adding too few forces each app to
    234 have to have its own copy, again wasting memory.</p>
    235 
    236 <p>Example usage (in products device.mk):</p>
    237 
    238 <pre class="devsite-click-to-copy">
    239 PRODUCT_COPY_FILES += &lt;filename&gt;:system/etc/preloaded-classes
    240 </pre>
    241 
    242 <p class="note"><strong>Note:</strong> This line must be placed before
    243 inheriting any product configuration makefiles that get the default one from:
    244 <code>build/target/product/base.mk</code></p>
    245 
    246 <li>Image Classes List</li>
    247 
    248 <p>The image classes list is a list of classes that dex2oat initializes ahead of
    249 time and stores in the boot.art file. This allows the zygote to load these
    250 results out of the boot.art file on startup instead of running the initializers
    251 for these classes itself during preloading. A key feature of this is that the
    252 pages loaded from the image and shared between processes can be clean, allowing
    253 them to be swapped out easily in low-memory situations. In L, by default the
    254 image classes list uses the same list as the preloaded classes list. Beginning
    255 post-L in AOSP, a custom image classes list can be specified using:</p>
    256 
    257 <pre class="devsite-click-to-copy">
    258 PRODUCT_DEX_PREOPT_BOOT_FLAGS
    259 </pre>
    260 
    261 <p>Example use (in products <code>device.mk</code>):</p>
    262 
    263 <pre class="devsite-click-to-copy">
    264 PRODUCT_DEX_PREOPT_BOOT_FLAGS += --image-classes=&lt;filename&gt;
    265 </pre>
    266 
    267 <li>Compiled Classes List</li>
    268 
    269 <p>In post-L AOSP, a subset of classes from the boot classpath can be specified to
    270 be compiled during pre-optimization using the compiled classes list. This can
    271 be a useful option for devices that are very tight on space and cant fit the
    272 entire pre-optimized boot image. However, note classes not specified by this
    273 list will not be compiled - not even on the device - and must be interpreted,
    274 potentially affecting runtime performance. By default, dex2oat will look for a
    275 compiled classes list in $OUT/system/etc/compiled-classes, so a custom one can
    276 be copied to that location by the device.mk. A particular file location can
    277 also be specified using:
    278 
    279 <pre class="devsite-click-to-copy">
    280 PRODUCT_DEX_PREOPT_BOOT_FLAGS
    281 </pre>
    282 
    283 <p>Example usage (in products <code>device.mk</code>):</p>
    284 
    285 <pre class="devsite-click-to-copy">
    286 PRODUCT_COPY_FILES += &lt;filename&gt;:system/etc/compiled-classes
    287 </pre>
    288 
    289 <p class="note"><strong>Note:</strong> This line must be placed before
    290 inheriting any product configuration makefiles that get the default one from:
    291 <code>build/target/product/base.mk</code></p>
    292 </ul>
    293 
    294 <h3 id=runtime_configuration>Runtime configuration</h3>
    295 
    296 <h4 id=undefined>Jit options</h4>
    297 
    298 <p>The following options affect Android releases only where the ART JIT compiler
    299 is available.</p>
    300 
    301 <ul>
    302 <li>dalvik.vm.usejit: whether or not the JIT is enabled.</li>
    303 <li>dalvik.vm.jitinitialsize (default 64K): the initial capacity
    304 of the code cache. The code cache will regularly GC and increase if needed.
    305 <li>dalvik.vm.jitmaxsize (default 64M): the maximum capacity of the code cache.
    306 <li>dalvik.vm.jitthreshold: (default 10000) - This
    307 is the threshold that the "hotness" counter of a method needs to pass in order
    308 for the method to be JIT compiled. The "hotness" counter is a metric internal
    309 to the runtime. It includes the number of calls, backward branches, and other
    310 factors.
    311 <li>dalvik.vm.usejitprofiles: whether or not
    312 JIT profiles are enabled; this may be used even if dalvik.vm.usejit is false.
    313 Note that if this is false, the compiler filter <em>speed-profile</em> does
    314 not AOT-compile any method and is equivalent to <em>quicken</em>.
    315 <li>dalvik.vm.jitprithreadweight (default to
    316 dalvik.vm.jitthreshold / 20) - The weight of the JIT "samples"
    317 (see jitthreshold) for the application UI thread. Use to speed up compilation
    318 of methods that directly affect users experience when interacting with the
    319 app.
    320 <li>dalvik.vm.jittransitionweight: (default to dalvik.vm.jitthreshold / 10)
    321 the weight of the method
    322 invocation that transitions between compile code and interpreter. This helps
    323 make sure the methods involved are compiled to minimize transitions (which are
    324 expensive).
    325 </li>
    326 </ul>
    327 
    328 <h4 id=undefined>Package manager options</h4>
    329 
    330 <p>
    331 Since Android 7.0, there's a generic way to specify the level of
    332 compilation/verification that happened at various stages.
    333 The compilation levels can be configured via system properties
    334 with the defaults being:
    335 </p>
    336 
    337 <ul>
    338 <li>pm.dexopt.install=quicken</li>
    339 <p>This is the compilation filter used when installing applications through Google
    340 Play. For faster installs, try the <em>quicken</em> compiler filter.
    341 </p>
    342 <li>pm.dexopt.bg-dexopt=speed-profile</li>
    343 <p>
    344 This is the compilation filter used when the device is idle, charging and
    345 fully charged.  Try the <em>speed-profile</em> compiler filter
    346 to take advantage of profile-guided compilation and save on storage.
    347 </p>
    348 <li>pm.dexopt.boot=verify</li>
    349 <p>
    350 The compilation filter used after an over-the-air update. We
    351 <strong>strongly</strong> recommend the <em>verify</em> compiler filter for this
    352 option to avoid very long boot times.
    353 </p>
    354 <li>pm.dexopt.first-boot=quicken<li>
    355 <p>
    356 The compilation filter for the first time the device ever boots. The filter
    357 used here will only affect the boot time after factory. We recommend the filter
    358 <em>quicken</em> for it to avoid long times before a user gets to
    359 use the phone for the very first time. Note that if all applications in
    360 <code>/system</code> are already compiled with the <em>quicken</em> compiler
    361 filter or are compiled with the <em>speed</em> or <em>speed-profile</em>
    362 compiler filter, the <code>pm.dexopt.first-boot</code> has no effect.
    363 </p>
    364 
    365 </ul>
    366 
    367 <h4 id=undefined>Dex2oat options</h4>
    368 
    369 
    370 <p>Note that these options affect <code>dex2oat</code>
    371 during on-device compilation as well as during pre-optimization, whereas most
    372 of the options discussed above affect only pre-optimization.</p>
    373 
    374 <p>To control <code>dex2oat</code> while its compiling the boot image:</p>
    375 
    376 <ul>
    377   <li>dalvik.vm.image-dex2oat-Xms: initial heap size
    378   <li>dalvik.vm.image-dex2oat-Xmx: maximum heap size
    379   <li>dalvik.vm.image-dex2oat-filter: compiler filter option
    380   <li>dalvik.vm.image-dex2oat-threads: number of threads to use
    381 </ul>
    382 
    383 <p>To control <code>dex2oat</code> while its compiling everything besides the boot image:</p>
    384 
    385 <ul>
    386   <li>dalvik.vm.dex2oat-Xms: initial heap size
    387   <li>dalvik.vm.dex2oat-Xmx: maximum heap size
    388   <li>dalvik.vm.dex2oat-filter: compiler filter option
    389 </ul>
    390 
    391 <p>On releases through Android 6.0, one additional option is provided for compiling everything
    392 besides the boot image:</p>
    393 <ul>
    394   <li>dalvik.vm.dex2oat-threads: number of threads to use
    395 </ul>
    396 
    397 <p>Starting with Android 6.1, this becomes two additional options for compiling everything besides
    398 the boot image:</p>
    399 <ul>
    400   <li>dalvik.vm.boot-dex2oat-threads: number of threads to use during boot time
    401   <li>dalvik.vm.dex2oat-threads: number of threads to use after boot time
    402 </ul>
    403 
    404 <p>Starting with Android 7.1, two options are provided for controlling how memory is used when
    405 compiling everything besides the boot image:</p>
    406 <ul>
    407   <li>dalvik.vm.dex2oat-very-large: minimum total dex file size in bytes to disable AOT compilation
    408   <li>dalvik.vm.dex2oat-swap: use dex2oat swap file (for low-memory devices)
    409 </ul>
    410 
    411 <p>The options that control initial and maximum heap size for
    412 <code>dex2oat</code> should not be reduced since they could limit what
    413 applications can be compiled.</p>
    414 
    415 <h2 id=other_odex>A/B specific configuration</h2>
    416 
    417 <h3 id=undefined>ROM configuration</h3>
    418 
    419 <p>Starting in Android 7.0, devices may use two system partitions to enable
    420 <a href="/devices/tech/ota/ab_updates.html">A/B system updates</a>.
    421 To save on the system partition size, the preopted files can be installed in
    422 the unused second system partition. They are then copied to the data partition
    423 on first boot.</p>
    424 
    425 <p>Example usage (in <code>device-common.mk</code>):</p>
    426 
    427 <pre class="devsite-click-to-copy">
    428 PRODUCT_PACKAGES += \
    429      cppreopts.sh
    430 PRODUCT_PROPERTY_OVERRIDES += \
    431      ro.cp_system_other_odex=1
    432 </pre>
    433 
    434 <p>And in device's <code>BoardConfig.mk</code>:</p>
    435 
    436 <pre class="devsite-click-to-copy">
    437 BOARD_USES_SYSTEM_OTHER_ODEX := true
    438 </pre>
    439 
    440 <p>
    441 Note that boot classpath code, system server code, and product-specific core
    442 applications always compile to the system partition. By default, all other
    443 applications get compiled to the unused second system partition. This can be
    444 controlled with the <code>SYSTEM_OTHER_ODEX_FILTER</code>, which has a value by
    445 default of:</p>
    446 
    447 <pre class="devsite-click-to-copy">
    448 SYSTEM_OTHER_ODEX_FILTER ?= app/% priv-app/%
    449 </pre>
    450 
    451 <h3 id=undefined>Background dexopt OTA</h3>
    452 
    453 <p>With A/B enabled devices, applications can be compiled in the background for
    454 updating to the new system image. See <a
    455 href="/devices/tech/ota/ab_updates.html#compilation">App compilation in
    456 background</a> to optionally include the compilation script and
    457 binaries in the system image. The compilation filter used for this compilation
    458 is controlled with:</p>
    459 <pre class="devsite-click-to-copy">
    460 pm.dexopt.ab-ota=speed-profile
    461 </pre>
    462 
    463 <p>
    464 We recommend using <em>speed-profile</em> to take advantage of profile guided
    465 compilation and save on storage.
    466 </p>
    467 
    468 
    469 
    470 
    471   </body>
    472 </html>
    473