Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Andreas Gampe | d687e37 | 2015-04-28 23:16:03 -0700 | [diff] [blame] | 17 | #include <inttypes.h> |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 18 | #include <stdio.h> |
| 19 | #include <stdlib.h> |
| 20 | #include <sys/stat.h> |
Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 21 | #include "base/memory_tool.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 22 | |
| 23 | #include <fstream> |
| 24 | #include <iostream> |
| 25 | #include <sstream> |
| 26 | #include <string> |
Andreas Gampe | b1fcead | 2015-04-20 18:53:51 -0700 | [diff] [blame] | 27 | #include <unordered_set> |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 28 | #include <vector> |
| 29 | |
Vladimir Marko | f94b781 | 2014-06-05 15:48:04 +0100 | [diff] [blame] | 30 | #if defined(__linux__) && defined(__arm__) |
| 31 | #include <sys/personality.h> |
| 32 | #include <sys/utsname.h> |
| 33 | #endif |
| 34 | |
Ian Rogers | cf7f191 | 2014-10-22 22:06:39 -0700 | [diff] [blame] | 35 | #define ATRACE_TAG ATRACE_TAG_DALVIK |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 36 | #include <cutils/trace.h> |
Ian Rogers | cf7f191 | 2014-10-22 22:06:39 -0700 | [diff] [blame] | 37 | |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 38 | #include "arch/instruction_set_features.h" |
Andreas Gampe | c5a3ea7 | 2015-01-13 16:41:53 -0800 | [diff] [blame] | 39 | #include "arch/mips/instruction_set_features_mips.h" |
David Srbecky | 4fda4eb | 2016-02-05 13:34:46 +0000 | [diff] [blame] | 40 | #include "art_method-inl.h" |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 41 | #include "base/dumpable.h" |
Andreas Gampe | 794ad76 | 2015-02-23 08:12:24 -0800 | [diff] [blame] | 42 | #include "base/macros.h" |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 43 | #include "base/scoped_flock.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 44 | #include "base/stl_util.h" |
| 45 | #include "base/stringpiece.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 46 | #include "base/time_utils.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 47 | #include "base/timing_logger.h" |
| 48 | #include "base/unix_file/fd_file.h" |
| 49 | #include "class_linker.h" |
Nicolas Geoffray | b34f69a | 2014-03-07 15:28:39 +0000 | [diff] [blame] | 50 | #include "compiler.h" |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 51 | #include "compiler_callbacks.h" |
David Srbecky | 4fda4eb | 2016-02-05 13:34:46 +0000 | [diff] [blame] | 52 | #include "debug/method_debug_info.h" |
Mathieu Chartier | 5bdab12 | 2015-01-26 18:30:19 -0800 | [diff] [blame] | 53 | #include "dex/pass_manager.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 54 | #include "dex/quick/dex_file_to_method_inliner_map.h" |
David Srbecky | 4fda4eb | 2016-02-05 13:34:46 +0000 | [diff] [blame] | 55 | #include "dex/quick_compiler_callbacks.h" |
| 56 | #include "dex/verification_results.h" |
| 57 | #include "dex_file-inl.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 58 | #include "driver/compiler_driver.h" |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 59 | #include "driver/compiler_options.h" |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 60 | #include "elf_file.h" |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 61 | #include "elf_writer.h" |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 62 | #include "elf_writer_quick.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 63 | #include "gc/space/image_space.h" |
| 64 | #include "gc/space/space-inl.h" |
| 65 | #include "image_writer.h" |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 66 | #include "interpreter/unstarted_runtime.h" |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 67 | #include "jit/offline_profiling_info.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 68 | #include "leb128.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 69 | #include "mirror/class-inl.h" |
| 70 | #include "mirror/class_loader.h" |
| 71 | #include "mirror/object-inl.h" |
| 72 | #include "mirror/object_array-inl.h" |
| 73 | #include "oat_writer.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 74 | #include "os.h" |
| 75 | #include "runtime.h" |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 76 | #include "runtime_options.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 77 | #include "ScopedLocalRef.h" |
| 78 | #include "scoped_thread_state_change.h" |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 79 | #include "utils.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 80 | #include "well_known_classes.h" |
| 81 | #include "zip_archive.h" |
| 82 | |
| 83 | namespace art { |
| 84 | |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 85 | static int original_argc; |
| 86 | static char** original_argv; |
| 87 | |
| 88 | static std::string CommandLine() { |
| 89 | std::vector<std::string> command; |
| 90 | for (int i = 0; i < original_argc; ++i) { |
| 91 | command.push_back(original_argv[i]); |
| 92 | } |
| 93 | return Join(command, ' '); |
| 94 | } |
| 95 | |
Andreas Gampe | 046c706 | 2015-05-18 23:22:54 -0700 | [diff] [blame] | 96 | // A stripped version. Remove some less essential parameters. If we see a "--zip-fd=" parameter, be |
| 97 | // even more aggressive. There won't be much reasonable data here for us in that case anyways (the |
| 98 | // locations are all staged). |
| 99 | static std::string StrippedCommandLine() { |
| 100 | std::vector<std::string> command; |
| 101 | |
| 102 | // Do a pre-pass to look for zip-fd. |
| 103 | bool saw_zip_fd = false; |
| 104 | for (int i = 0; i < original_argc; ++i) { |
| 105 | if (StartsWith(original_argv[i], "--zip-fd=")) { |
| 106 | saw_zip_fd = true; |
| 107 | break; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | // Now filter out things. |
| 112 | for (int i = 0; i < original_argc; ++i) { |
| 113 | // All runtime-arg parameters are dropped. |
| 114 | if (strcmp(original_argv[i], "--runtime-arg") == 0) { |
| 115 | i++; // Drop the next part, too. |
| 116 | continue; |
| 117 | } |
| 118 | |
| 119 | // Any instruction-setXXX is dropped. |
| 120 | if (StartsWith(original_argv[i], "--instruction-set")) { |
| 121 | continue; |
| 122 | } |
| 123 | |
| 124 | // The boot image is dropped. |
| 125 | if (StartsWith(original_argv[i], "--boot-image=")) { |
| 126 | continue; |
| 127 | } |
| 128 | |
Mathieu Chartier | 97590cc | 2016-02-03 15:50:29 -0800 | [diff] [blame] | 129 | // The image format is dropped. |
| 130 | if (StartsWith(original_argv[i], "--image-format=")) { |
| 131 | continue; |
| 132 | } |
| 133 | |
Andreas Gampe | 046c706 | 2015-05-18 23:22:54 -0700 | [diff] [blame] | 134 | // This should leave any dex-file and oat-file options, describing what we compiled. |
| 135 | |
| 136 | // However, we prefer to drop this when we saw --zip-fd. |
| 137 | if (saw_zip_fd) { |
Mathieu Chartier | a90c772 | 2015-10-29 15:41:36 -0700 | [diff] [blame] | 138 | // Drop anything --zip-X, --dex-X, --oat-X, --swap-X, or --app-image-X |
Andreas Gampe | 046c706 | 2015-05-18 23:22:54 -0700 | [diff] [blame] | 139 | if (StartsWith(original_argv[i], "--zip-") || |
| 140 | StartsWith(original_argv[i], "--dex-") || |
| 141 | StartsWith(original_argv[i], "--oat-") || |
Mathieu Chartier | a90c772 | 2015-10-29 15:41:36 -0700 | [diff] [blame] | 142 | StartsWith(original_argv[i], "--swap-") || |
| 143 | StartsWith(original_argv[i], "--app-image-")) { |
Andreas Gampe | 046c706 | 2015-05-18 23:22:54 -0700 | [diff] [blame] | 144 | continue; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | command.push_back(original_argv[i]); |
| 149 | } |
| 150 | |
| 151 | // Construct the final output. |
| 152 | if (command.size() <= 1U) { |
| 153 | // It seems only "/system/bin/dex2oat" is left, or not even that. Use a pretty line. |
| 154 | return "Starting dex2oat."; |
| 155 | } |
| 156 | return Join(command, ' '); |
| 157 | } |
| 158 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 159 | static void UsageErrorV(const char* fmt, va_list ap) { |
| 160 | std::string error; |
| 161 | StringAppendV(&error, fmt, ap); |
| 162 | LOG(ERROR) << error; |
| 163 | } |
| 164 | |
| 165 | static void UsageError(const char* fmt, ...) { |
| 166 | va_list ap; |
| 167 | va_start(ap, fmt); |
| 168 | UsageErrorV(fmt, ap); |
| 169 | va_end(ap); |
| 170 | } |
| 171 | |
Andreas Gampe | 794ad76 | 2015-02-23 08:12:24 -0800 | [diff] [blame] | 172 | NO_RETURN static void Usage(const char* fmt, ...) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 173 | va_list ap; |
| 174 | va_start(ap, fmt); |
| 175 | UsageErrorV(fmt, ap); |
| 176 | va_end(ap); |
| 177 | |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 178 | UsageError("Command: %s", CommandLine().c_str()); |
| 179 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 180 | UsageError("Usage: dex2oat [options]..."); |
| 181 | UsageError(""); |
Jean-Philippe Halimi | 3d329d7 | 2015-03-23 14:09:48 +0100 | [diff] [blame] | 182 | UsageError(" -j<number>: specifies the number of threads used for compilation."); |
| 183 | UsageError(" Default is the number of detected hardware threads available on the"); |
| 184 | UsageError(" host system."); |
| 185 | UsageError(" Example: -j12"); |
| 186 | UsageError(""); |
Richard Uhler | e934df2 | 2015-03-17 11:26:16 -0700 | [diff] [blame] | 187 | UsageError(" --dex-file=<dex-file>: specifies a .dex, .jar, or .apk file to compile."); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 188 | UsageError(" Example: --dex-file=/system/framework/core.jar"); |
| 189 | UsageError(""); |
Richard Uhler | e934df2 | 2015-03-17 11:26:16 -0700 | [diff] [blame] | 190 | UsageError(" --dex-location=<dex-location>: specifies an alternative dex location to"); |
| 191 | UsageError(" encode in the oat file for the corresponding --dex-file argument."); |
| 192 | UsageError(" Example: --dex-file=/home/build/out/system/framework/core.jar"); |
| 193 | UsageError(" --dex-location=/system/framework/core.jar"); |
| 194 | UsageError(""); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 195 | UsageError(" --zip-fd=<file-descriptor>: specifies a file descriptor of a zip file"); |
| 196 | UsageError(" containing a classes.dex file to compile."); |
| 197 | UsageError(" Example: --zip-fd=5"); |
| 198 | UsageError(""); |
Brian Carlstrom | 4560248 | 2013-07-21 22:07:55 -0700 | [diff] [blame] | 199 | UsageError(" --zip-location=<zip-location>: specifies a symbolic name for the file"); |
| 200 | UsageError(" corresponding to the file descriptor specified by --zip-fd."); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 201 | UsageError(" Example: --zip-location=/system/app/Calculator.apk"); |
| 202 | UsageError(""); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 203 | UsageError(" --oat-file=<file.oat>: specifies an oat output destination via a filename."); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 204 | UsageError(" Example: --oat-file=/system/framework/boot.oat"); |
| 205 | UsageError(""); |
| 206 | UsageError(" --oat-fd=<number>: specifies the oat output destination via a file descriptor."); |
Wonil Kim | 9cb554a | 2014-04-28 11:26:55 +0900 | [diff] [blame] | 207 | UsageError(" Example: --oat-fd=6"); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 208 | UsageError(""); |
| 209 | UsageError(" --oat-location=<oat-name>: specifies a symbolic name for the file corresponding"); |
| 210 | UsageError(" to the file descriptor specified by --oat-fd."); |
| 211 | UsageError(" Example: --oat-location=/data/dalvik-cache/system@app@Calculator.apk.oat"); |
| 212 | UsageError(""); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 213 | UsageError(" --oat-symbols=<file.oat>: specifies an oat output destination with full symbols."); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 214 | UsageError(" Example: --oat-symbols=/symbols/system/framework/boot.oat"); |
| 215 | UsageError(""); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 216 | UsageError(" --image=<file.art>: specifies an output image filename."); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 217 | UsageError(" Example: --image=/system/framework/boot.art"); |
| 218 | UsageError(""); |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 219 | UsageError(" --image-format=(uncompressed|lz4):"); |
| 220 | UsageError(" Which format to store the image."); |
| 221 | UsageError(" Example: --image-format=lz4"); |
| 222 | UsageError(" Default: uncompressed"); |
| 223 | UsageError(""); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 224 | UsageError(" --image-classes=<classname-file>: specifies classes to include in an image."); |
| 225 | UsageError(" Example: --image=frameworks/base/preloaded-classes"); |
| 226 | UsageError(""); |
| 227 | UsageError(" --base=<hex-address>: specifies the base address when creating a boot image."); |
| 228 | UsageError(" Example: --base=0x50000000"); |
| 229 | UsageError(""); |
| 230 | UsageError(" --boot-image=<file.art>: provide the image file for the boot class path."); |
Kevin Brodsky | 64fff41 | 2015-11-24 14:24:34 +0000 | [diff] [blame] | 231 | UsageError(" Do not include the arch as part of the name, it is added automatically."); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 232 | UsageError(" Example: --boot-image=/system/framework/boot.art"); |
Kevin Brodsky | 64fff41 | 2015-11-24 14:24:34 +0000 | [diff] [blame] | 233 | UsageError(" (specifies /system/framework/<arch>/boot.art as the image file)"); |
Nicolas Geoffray | 9583fbc | 2014-02-28 15:21:07 +0000 | [diff] [blame] | 234 | UsageError(" Default: $ANDROID_ROOT/system/framework/boot.art"); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 235 | UsageError(""); |
| 236 | UsageError(" --android-root=<path>: used to locate libraries for portable linking."); |
| 237 | UsageError(" Example: --android-root=out/host/linux-x86"); |
| 238 | UsageError(" Default: $ANDROID_ROOT"); |
| 239 | UsageError(""); |
Andreas Gampe | 57b3429 | 2015-01-14 15:45:59 -0800 | [diff] [blame] | 240 | UsageError(" --instruction-set=(arm|arm64|mips|mips64|x86|x86_64): compile for a particular"); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 241 | UsageError(" instruction set."); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 242 | UsageError(" Example: --instruction-set=x86"); |
| 243 | UsageError(" Default: arm"); |
| 244 | UsageError(""); |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 245 | UsageError(" --instruction-set-features=...,: Specify instruction set features"); |
| 246 | UsageError(" Example: --instruction-set-features=div"); |
| 247 | UsageError(" Default: default"); |
| 248 | UsageError(""); |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 249 | UsageError(" --compile-pic: Force indirect use of code, methods, and classes"); |
| 250 | UsageError(" Default: disabled"); |
| 251 | UsageError(""); |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 252 | UsageError(" --compiler-backend=(Quick|Optimizing): select compiler backend"); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 253 | UsageError(" set."); |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 254 | UsageError(" Example: --compiler-backend=Optimizing"); |
Nicolas Geoffray | 409e809 | 2015-10-01 10:32:19 +0100 | [diff] [blame] | 255 | UsageError(" Default: Optimizing"); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 256 | UsageError(""); |
Nicolas Geoffray | 88157ef | 2014-09-12 10:29:53 +0100 | [diff] [blame] | 257 | UsageError(" --compiler-filter=" |
| 258 | "(verify-none" |
| 259 | "|interpret-only" |
| 260 | "|space" |
| 261 | "|balanced" |
| 262 | "|speed" |
| 263 | "|everything" |
| 264 | "|time):"); |
Jeff Hao | 4a200f5 | 2014-04-01 14:58:49 -0700 | [diff] [blame] | 265 | UsageError(" select compiler filter."); |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 266 | UsageError(" Example: --compiler-filter=everything"); |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 267 | UsageError(" Default: speed"); |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 268 | UsageError(""); |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 269 | UsageError(" --huge-method-max=<method-instruction-count>: threshold size for a huge"); |
| 270 | UsageError(" method for compiler filter tuning."); |
| 271 | UsageError(" Example: --huge-method-max=%d", CompilerOptions::kDefaultHugeMethodThreshold); |
| 272 | UsageError(" Default: %d", CompilerOptions::kDefaultHugeMethodThreshold); |
| 273 | UsageError(""); |
| 274 | UsageError(" --large-method-max=<method-instruction-count>: threshold size for a large"); |
| 275 | UsageError(" method for compiler filter tuning."); |
| 276 | UsageError(" Example: --large-method-max=%d", CompilerOptions::kDefaultLargeMethodThreshold); |
| 277 | UsageError(" Default: %d", CompilerOptions::kDefaultLargeMethodThreshold); |
| 278 | UsageError(""); |
| 279 | UsageError(" --small-method-max=<method-instruction-count>: threshold size for a small"); |
| 280 | UsageError(" method for compiler filter tuning."); |
| 281 | UsageError(" Example: --small-method-max=%d", CompilerOptions::kDefaultSmallMethodThreshold); |
| 282 | UsageError(" Default: %d", CompilerOptions::kDefaultSmallMethodThreshold); |
| 283 | UsageError(""); |
| 284 | UsageError(" --tiny-method-max=<method-instruction-count>: threshold size for a tiny"); |
| 285 | UsageError(" method for compiler filter tuning."); |
| 286 | UsageError(" Example: --tiny-method-max=%d", CompilerOptions::kDefaultTinyMethodThreshold); |
| 287 | UsageError(" Default: %d", CompilerOptions::kDefaultTinyMethodThreshold); |
| 288 | UsageError(""); |
| 289 | UsageError(" --num-dex-methods=<method-count>: threshold size for a small dex file for"); |
| 290 | UsageError(" compiler filter tuning. If the input has fewer than this many methods"); |
Jeff Hao | 4a200f5 | 2014-04-01 14:58:49 -0700 | [diff] [blame] | 291 | UsageError(" and the filter is not interpret-only or verify-none, overrides the"); |
| 292 | UsageError(" filter to use speed"); |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 293 | UsageError(" Example: --num-dex-method=%d", CompilerOptions::kDefaultNumDexMethodsThreshold); |
| 294 | UsageError(" Default: %d", CompilerOptions::kDefaultNumDexMethodsThreshold); |
| 295 | UsageError(""); |
Calin Juravle | ec74835 | 2015-07-29 13:52:12 +0100 | [diff] [blame] | 296 | UsageError(" --inline-depth-limit=<depth-limit>: the depth limit of inlining for fine tuning"); |
| 297 | UsageError(" the compiler. A zero value will disable inlining. Honored only by Optimizing."); |
Roland Levillain | a215b95 | 2015-08-07 11:38:32 +0100 | [diff] [blame] | 298 | UsageError(" Has priority over the --compiler-filter option. Intended for "); |
| 299 | UsageError(" development/experimental use."); |
Calin Juravle | ec74835 | 2015-07-29 13:52:12 +0100 | [diff] [blame] | 300 | UsageError(" Example: --inline-depth-limit=%d", CompilerOptions::kDefaultInlineDepthLimit); |
| 301 | UsageError(" Default: %d", CompilerOptions::kDefaultInlineDepthLimit); |
| 302 | UsageError(""); |
| 303 | UsageError(" --inline-max-code-units=<code-units-count>: the maximum code units that a method"); |
| 304 | UsageError(" can have to be considered for inlining. A zero value will disable inlining."); |
Roland Levillain | a215b95 | 2015-08-07 11:38:32 +0100 | [diff] [blame] | 305 | UsageError(" Honored only by Optimizing. Has priority over the --compiler-filter option."); |
| 306 | UsageError(" Intended for development/experimental use."); |
Calin Juravle | ec74835 | 2015-07-29 13:52:12 +0100 | [diff] [blame] | 307 | UsageError(" Example: --inline-max-code-units=%d", |
| 308 | CompilerOptions::kDefaultInlineMaxCodeUnits); |
| 309 | UsageError(" Default: %d", CompilerOptions::kDefaultInlineMaxCodeUnits); |
| 310 | UsageError(""); |
Ian Rogers | 4639860 | 2013-08-20 07:50:36 -0700 | [diff] [blame] | 311 | UsageError(" --dump-timing: display a breakdown of where time was spent"); |
| 312 | UsageError(""); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 313 | UsageError(" --include-patch-information: Include patching information so the generated code"); |
| 314 | UsageError(" can have its base address moved without full recompilation."); |
| 315 | UsageError(""); |
| 316 | UsageError(" --no-include-patch-information: Do not include patching information."); |
| 317 | UsageError(""); |
David Srbecky | 8363c77 | 2015-05-28 16:12:43 +0100 | [diff] [blame] | 318 | UsageError(" -g"); |
| 319 | UsageError(" --generate-debug-info: Generate debug information for native debugging,"); |
| 320 | UsageError(" such as stack unwinding information, ELF symbols and DWARF sections."); |
David Srbecky | 3e09eeb | 2016-01-12 14:54:03 +0000 | [diff] [blame] | 321 | UsageError(" If used without --native-debuggable, it will be best-effort only."); |
| 322 | UsageError(" This option does not affect the generated code. (disabled by default)"); |
Alex Light | 78382fa | 2014-06-06 15:45:32 -0700 | [diff] [blame] | 323 | UsageError(""); |
David Srbecky | 8363c77 | 2015-05-28 16:12:43 +0100 | [diff] [blame] | 324 | UsageError(" --no-generate-debug-info: Do not generate debug information for native debugging."); |
David Srbecky | 8dc7324 | 2015-04-12 11:40:39 +0100 | [diff] [blame] | 325 | UsageError(""); |
David Srbecky | 5b1c2ca | 2016-01-25 17:32:41 +0000 | [diff] [blame] | 326 | UsageError(" --generate-mini-debug-info: Generate minimal amount of LZMA-compressed"); |
| 327 | UsageError(" debug information necessary to print backtraces. (disabled by default)"); |
| 328 | UsageError(""); |
| 329 | UsageError(" --no-generate-mini-debug-info: Do do generated backtrace info."); |
| 330 | UsageError(""); |
David Srbecky | 3e09eeb | 2016-01-12 14:54:03 +0000 | [diff] [blame] | 331 | UsageError(" --debuggable: Produce code debuggable with Java debugger."); |
David Srbecky | 0cf4493 | 2015-12-09 14:09:59 +0000 | [diff] [blame] | 332 | UsageError(""); |
| 333 | UsageError(" --native-debuggable: Produce code debuggable with native debugger (like LLDB)."); |
| 334 | UsageError(" Implies --debuggable."); |
| 335 | UsageError(""); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 336 | UsageError(" --runtime-arg <argument>: used to specify various arguments for the runtime,"); |
| 337 | UsageError(" such as initial heap size, maximum heap size, and verbose output."); |
| 338 | UsageError(" Use a separate --runtime-arg switch for each argument."); |
| 339 | UsageError(" Example: --runtime-arg -Xms256m"); |
Jeff Hao | 4a200f5 | 2014-04-01 14:58:49 -0700 | [diff] [blame] | 340 | UsageError(""); |
Dave Allison | d6ed642 | 2014-04-09 23:36:15 +0000 | [diff] [blame] | 341 | UsageError(" --profile-file=<filename>: specify profiler output file to use for compilation."); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 342 | UsageError(""); |
Calin Juravle | 877fd96 | 2016-01-05 14:29:29 +0000 | [diff] [blame] | 343 | UsageError(" --profile-file-fd=<number>: same as --profile-file but accepts a file descriptor."); |
| 344 | UsageError(" Cannot be used together with --profile-file."); |
| 345 | UsageError(""); |
Chao-ying Fu | cd8ce66 | 2014-03-11 14:57:19 -0700 | [diff] [blame] | 346 | UsageError(" --print-pass-names: print a list of pass names"); |
| 347 | UsageError(""); |
| 348 | UsageError(" --disable-passes=<pass-names>: disable one or more passes separated by comma."); |
| 349 | UsageError(" Example: --disable-passes=UseCount,BBOptimizations"); |
| 350 | UsageError(""); |
Razvan A Lupusoru | bd25d4b | 2014-07-02 18:16:51 -0700 | [diff] [blame] | 351 | UsageError(" --print-pass-options: print a list of passes that have configurable options along " |
| 352 | "with the setting."); |
| 353 | UsageError(" Will print default if no overridden setting exists."); |
| 354 | UsageError(""); |
| 355 | UsageError(" --pass-options=Pass1Name:Pass1OptionName:Pass1Option#," |
| 356 | "Pass2Name:Pass2OptionName:Pass2Option#"); |
| 357 | UsageError(" Used to specify a pass specific option. The setting itself must be integer."); |
| 358 | UsageError(" Separator used between options is a comma."); |
| 359 | UsageError(""); |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 360 | UsageError(" --swap-file=<file-name>: specifies a file to use for swap."); |
| 361 | UsageError(" Example: --swap-file=/data/tmp/swap.001"); |
| 362 | UsageError(""); |
| 363 | UsageError(" --swap-fd=<file-descriptor>: specifies a file to use for swap (by descriptor)."); |
| 364 | UsageError(" Example: --swap-fd=10"); |
| 365 | UsageError(""); |
Mathieu Chartier | a90c772 | 2015-10-29 15:41:36 -0700 | [diff] [blame] | 366 | UsageError(" --app-image-fd=<file-descriptor>: specify output file descriptor for app image."); |
| 367 | UsageError(" Example: --app-image-fd=10"); |
| 368 | UsageError(""); |
| 369 | UsageError(" --app-image-file=<file-name>: specify a file name for app image."); |
| 370 | UsageError(" Example: --app-image-file=/data/dalvik-cache/system@app@Calculator.apk.art"); |
| 371 | UsageError(""); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 372 | UsageError(" --multi-image: specify that separate oat and image files be generated for each " |
| 373 | "input dex file."); |
| 374 | UsageError(""); |
Roland Levillain | eb2c741 | 2016-01-28 14:37:01 +0000 | [diff] [blame] | 375 | UsageError(" --force-determinism: force the compiler to emit a deterministic output."); |
| 376 | UsageError(" This option is incompatible with read barriers (e.g., if dex2oat has been"); |
| 377 | UsageError(" built with the environment variable `ART_USE_READ_BARRIER` set to `true`)."); |
| 378 | UsageError(""); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 379 | std::cerr << "See log for usage error information\n"; |
| 380 | exit(EXIT_FAILURE); |
| 381 | } |
| 382 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 383 | // The primary goal of the watchdog is to prevent stuck build servers |
| 384 | // during development when fatal aborts lead to a cascade of failures |
| 385 | // that result in a deadlock. |
| 386 | class WatchDog { |
Brian Carlstrom | 95b033b | 2014-12-03 22:29:37 -0800 | [diff] [blame] | 387 | // WatchDog defines its own CHECK_PTHREAD_CALL to avoid using LOG which uses locks |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 388 | #undef CHECK_PTHREAD_CALL |
| 389 | #define CHECK_WATCH_DOG_PTHREAD_CALL(call, args, what) \ |
| 390 | do { \ |
| 391 | int rc = call args; \ |
| 392 | if (rc != 0) { \ |
| 393 | errno = rc; \ |
| 394 | std::string message(# call); \ |
| 395 | message += " failed for "; \ |
| 396 | message += reason; \ |
| 397 | Fatal(message); \ |
| 398 | } \ |
| 399 | } while (false) |
| 400 | |
| 401 | public: |
Brian Carlstrom | 93ba893 | 2013-07-17 21:31:49 -0700 | [diff] [blame] | 402 | explicit WatchDog(bool is_watch_dog_enabled) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 403 | is_watch_dog_enabled_ = is_watch_dog_enabled; |
| 404 | if (!is_watch_dog_enabled_) { |
| 405 | return; |
| 406 | } |
| 407 | shutting_down_ = false; |
| 408 | const char* reason = "dex2oat watch dog thread startup"; |
Kenny Root | 5131638 | 2014-05-13 14:59:37 -0700 | [diff] [blame] | 409 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_init, (&mutex_, nullptr), reason); |
| 410 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_cond_init, (&cond_, nullptr), reason); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 411 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_attr_init, (&attr_), reason); |
| 412 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_create, (&pthread_, &attr_, &CallBack, this), reason); |
| 413 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_attr_destroy, (&attr_), reason); |
| 414 | } |
| 415 | ~WatchDog() { |
| 416 | if (!is_watch_dog_enabled_) { |
| 417 | return; |
| 418 | } |
| 419 | const char* reason = "dex2oat watch dog thread shutdown"; |
| 420 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_lock, (&mutex_), reason); |
| 421 | shutting_down_ = true; |
| 422 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_cond_signal, (&cond_), reason); |
| 423 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_unlock, (&mutex_), reason); |
| 424 | |
Kenny Root | 5131638 | 2014-05-13 14:59:37 -0700 | [diff] [blame] | 425 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_join, (pthread_, nullptr), reason); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 426 | |
| 427 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_cond_destroy, (&cond_), reason); |
| 428 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_destroy, (&mutex_), reason); |
| 429 | } |
| 430 | |
| 431 | private: |
| 432 | static void* CallBack(void* arg) { |
| 433 | WatchDog* self = reinterpret_cast<WatchDog*>(arg); |
| 434 | ::art::SetThreadName("dex2oat watch dog"); |
| 435 | self->Wait(); |
Kenny Root | 5131638 | 2014-05-13 14:59:37 -0700 | [diff] [blame] | 436 | return nullptr; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 437 | } |
| 438 | |
Andreas Gampe | 794ad76 | 2015-02-23 08:12:24 -0800 | [diff] [blame] | 439 | NO_RETURN static void Fatal(const std::string& message) { |
Andreas Gampe | d687e37 | 2015-04-28 23:16:03 -0700 | [diff] [blame] | 440 | // TODO: When we can guarantee it won't prevent shutdown in error cases, move to LOG. However, |
| 441 | // it's rather easy to hang in unwinding. |
| 442 | // LogLine also avoids ART logging lock issues, as it's really only a wrapper around |
| 443 | // logcat logging or stderr output. |
| 444 | LogMessage::LogLine(__FILE__, __LINE__, LogSeverity::FATAL, message.c_str()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 445 | exit(1); |
| 446 | } |
| 447 | |
| 448 | void Wait() { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 449 | // TODO: tune the multiplier for GC verification, the following is just to make the timeout |
| 450 | // large. |
Andreas Gampe | d687e37 | 2015-04-28 23:16:03 -0700 | [diff] [blame] | 451 | constexpr int64_t multiplier = kVerifyObjectSupport > kVerifyObjectModeFast ? 100 : 1; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 452 | timespec timeout_ts; |
| 453 | InitTimeSpec(true, CLOCK_REALTIME, multiplier * kWatchDogTimeoutSeconds * 1000, 0, &timeout_ts); |
| 454 | const char* reason = "dex2oat watch dog thread waiting"; |
| 455 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_lock, (&mutex_), reason); |
| 456 | while (!shutting_down_) { |
Brian Carlstrom | 95b033b | 2014-12-03 22:29:37 -0800 | [diff] [blame] | 457 | int rc = TEMP_FAILURE_RETRY(pthread_cond_timedwait(&cond_, &mutex_, &timeout_ts)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 458 | if (rc == ETIMEDOUT) { |
Andreas Gampe | d687e37 | 2015-04-28 23:16:03 -0700 | [diff] [blame] | 459 | Fatal(StringPrintf("dex2oat did not finish after %" PRId64 " seconds", |
| 460 | kWatchDogTimeoutSeconds)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 461 | } else if (rc != 0) { |
| 462 | std::string message(StringPrintf("pthread_cond_timedwait failed: %s", |
| 463 | strerror(errno))); |
| 464 | Fatal(message.c_str()); |
| 465 | } |
| 466 | } |
| 467 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_unlock, (&mutex_), reason); |
| 468 | } |
| 469 | |
| 470 | // When setting timeouts, keep in mind that the build server may not be as fast as your desktop. |
Mathieu Chartier | 13b9f43 | 2014-09-09 17:26:58 -0700 | [diff] [blame] | 471 | // Debug builds are slower so they have larger timeouts. |
Andreas Gampe | d687e37 | 2015-04-28 23:16:03 -0700 | [diff] [blame] | 472 | static constexpr int64_t kSlowdownFactor = kIsDebugBuild ? 5U : 1U; |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 473 | |
Andreas Gampe | 540138a | 2015-09-14 15:34:38 -0700 | [diff] [blame] | 474 | // 9.5 minutes scaled by kSlowdownFactor. This is slightly smaller than the Package Manager |
| 475 | // watchdog (PackageManagerService.WATCHDOG_TIMEOUT, 10 minutes), so that dex2oat will abort |
| 476 | // itself before that watchdog would take down the system server. |
| 477 | static constexpr int64_t kWatchDogTimeoutSeconds = kSlowdownFactor * (9 * 60 + 30); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 478 | |
| 479 | bool is_watch_dog_enabled_; |
| 480 | bool shutting_down_; |
| 481 | // TODO: Switch to Mutex when we can guarantee it won't prevent shutdown in error cases. |
| 482 | pthread_mutex_t mutex_; |
| 483 | pthread_cond_t cond_; |
| 484 | pthread_attr_t attr_; |
| 485 | pthread_t pthread_; |
| 486 | }; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 487 | |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 488 | static constexpr size_t kMinDexFilesForSwap = 2; |
| 489 | static constexpr size_t kMinDexFileCumulativeSizeForSwap = 20 * MB; |
| 490 | |
| 491 | static bool UseSwap(bool is_image, std::vector<const DexFile*>& dex_files) { |
| 492 | if (is_image) { |
| 493 | // Don't use swap, we know generation should succeed, and we don't want to slow it down. |
| 494 | return false; |
| 495 | } |
| 496 | if (dex_files.size() < kMinDexFilesForSwap) { |
| 497 | // If there are less dex files than the threshold, assume it's gonna be fine. |
| 498 | return false; |
| 499 | } |
| 500 | size_t dex_files_size = 0; |
| 501 | for (const auto* dex_file : dex_files) { |
| 502 | dex_files_size += dex_file->GetHeader().file_size_; |
| 503 | } |
| 504 | return dex_files_size >= kMinDexFileCumulativeSizeForSwap; |
| 505 | } |
| 506 | |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 507 | class Dex2Oat FINAL { |
| 508 | public: |
| 509 | explicit Dex2Oat(TimingLogger* timings) : |
Nicolas Geoffray | 409e809 | 2015-10-01 10:32:19 +0100 | [diff] [blame] | 510 | compiler_kind_(Compiler::kOptimizing), |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 511 | instruction_set_(kRuntimeISA), |
| 512 | // Take the default set of instruction features from the build. |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 513 | image_file_location_oat_checksum_(0), |
| 514 | image_file_location_oat_data_begin_(0), |
| 515 | image_patch_delta_(0), |
| 516 | key_value_store_(nullptr), |
Andreas Gampe | 3f30e12 | 2015-09-17 14:03:21 -0700 | [diff] [blame] | 517 | verification_results_(nullptr), |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 518 | method_inliner_map_(), |
| 519 | runtime_(nullptr), |
| 520 | thread_count_(sysconf(_SC_NPROCESSORS_CONF)), |
| 521 | start_ns_(NanoTime()), |
| 522 | oat_fd_(-1), |
| 523 | zip_fd_(-1), |
| 524 | image_base_(0U), |
| 525 | image_classes_zip_filename_(nullptr), |
| 526 | image_classes_filename_(nullptr), |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 527 | image_storage_mode_(ImageHeader::kStorageModeUncompressed), |
Andreas Gampe | 4bf3ae9 | 2014-11-11 13:28:29 -0800 | [diff] [blame] | 528 | compiled_classes_zip_filename_(nullptr), |
| 529 | compiled_classes_filename_(nullptr), |
Andreas Gampe | 70bef0d | 2015-04-15 02:37:28 -0700 | [diff] [blame] | 530 | compiled_methods_zip_filename_(nullptr), |
| 531 | compiled_methods_filename_(nullptr), |
Mathieu Chartier | a90c772 | 2015-10-29 15:41:36 -0700 | [diff] [blame] | 532 | app_image_(false), |
| 533 | boot_image_(false), |
Jeff Hao | 436183f | 2016-01-12 16:36:50 -0800 | [diff] [blame] | 534 | multi_image_(false), |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 535 | is_host_(false), |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 536 | class_loader_(nullptr), |
| 537 | elf_writers_(), |
| 538 | oat_writers_(), |
| 539 | rodata_(), |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 540 | image_writer_(nullptr), |
Andreas Gampe | 3f30e12 | 2015-09-17 14:03:21 -0700 | [diff] [blame] | 541 | driver_(nullptr), |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 542 | opened_dex_files_maps_(), |
| 543 | opened_dex_files_(), |
Vladimir Marko | 47496c2 | 2016-01-27 16:15:08 +0000 | [diff] [blame] | 544 | no_inline_from_dex_files_(), |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 545 | dump_stats_(false), |
| 546 | dump_passes_(false), |
| 547 | dump_timing_(false), |
| 548 | dump_slow_timing_(kIsDebugBuild), |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 549 | swap_fd_(kInvalidFd), |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 550 | app_image_fd_(kInvalidFd), |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 551 | profile_file_fd_(kInvalidFd), |
Andreas Gampe | ace0dc1 | 2016-01-20 13:33:13 -0800 | [diff] [blame] | 552 | timings_(timings), |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 553 | force_determinism_(false) |
| 554 | {} |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 555 | |
| 556 | ~Dex2Oat() { |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 557 | // Log completion time before deleting the runtime_, because this accesses |
| 558 | // the runtime. |
| 559 | LogCompletionTime(); |
| 560 | |
Vladimir Marko | 307dac9 | 2015-10-20 11:20:09 +0100 | [diff] [blame] | 561 | if (!kIsDebugBuild && !(RUNNING_ON_MEMORY_TOOL && kMemoryToolDetectsLeaks)) { |
| 562 | // We want to just exit on non-debug builds, not bringing the runtime down |
| 563 | // in an orderly fashion. So release the following fields. |
| 564 | driver_.release(); |
| 565 | image_writer_.release(); |
| 566 | for (std::unique_ptr<const DexFile>& dex_file : opened_dex_files_) { |
| 567 | dex_file.release(); |
| 568 | } |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 569 | for (std::unique_ptr<MemMap>& map : opened_dex_files_maps_) { |
| 570 | map.release(); |
| 571 | } |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 572 | for (std::unique_ptr<File>& oat_file : oat_files_) { |
| 573 | oat_file.release(); |
| 574 | } |
Vladimir Marko | 307dac9 | 2015-10-20 11:20:09 +0100 | [diff] [blame] | 575 | runtime_.release(); |
| 576 | verification_results_.release(); |
| 577 | key_value_store_.release(); |
Vladimir Marko | f94b781 | 2014-06-05 15:48:04 +0100 | [diff] [blame] | 578 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 579 | } |
| 580 | |
Roland Levillain | 9ec5e22 | 2015-08-17 20:18:41 +0100 | [diff] [blame] | 581 | struct ParserOptions { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 582 | std::vector<const char*> oat_symbols; |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 583 | std::string boot_image_filename; |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 584 | bool watch_dog_enabled = true; |
Nicolas Geoffray | 1412dfa | 2015-03-20 14:48:13 +0000 | [diff] [blame] | 585 | bool requested_specific_compiler = false; |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 586 | std::string error_msg; |
Roland Levillain | 9ec5e22 | 2015-08-17 20:18:41 +0100 | [diff] [blame] | 587 | }; |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 588 | |
Roland Levillain | 9ec5e22 | 2015-08-17 20:18:41 +0100 | [diff] [blame] | 589 | void ParseZipFd(const StringPiece& option) { |
Nicolas Geoffray | abbb0f7 | 2015-10-29 18:55:58 +0000 | [diff] [blame] | 590 | ParseUintOption(option, "--zip-fd", &zip_fd_, Usage); |
Roland Levillain | 9ec5e22 | 2015-08-17 20:18:41 +0100 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | void ParseOatFd(const StringPiece& option) { |
Nicolas Geoffray | abbb0f7 | 2015-10-29 18:55:58 +0000 | [diff] [blame] | 594 | ParseUintOption(option, "--oat-fd", &oat_fd_, Usage); |
Roland Levillain | 9ec5e22 | 2015-08-17 20:18:41 +0100 | [diff] [blame] | 595 | } |
| 596 | |
Calin Juravle | 877fd96 | 2016-01-05 14:29:29 +0000 | [diff] [blame] | 597 | void ParseFdForCollection(const StringPiece& option, |
| 598 | const char* arg_name, |
| 599 | std::vector<uint32_t>* fds) { |
| 600 | uint32_t fd; |
| 601 | ParseUintOption(option, arg_name, &fd, Usage); |
| 602 | fds->push_back(fd); |
| 603 | } |
| 604 | |
Roland Levillain | 9ec5e22 | 2015-08-17 20:18:41 +0100 | [diff] [blame] | 605 | void ParseJ(const StringPiece& option) { |
Nicolas Geoffray | abbb0f7 | 2015-10-29 18:55:58 +0000 | [diff] [blame] | 606 | ParseUintOption(option, "-j", &thread_count_, Usage, /* is_long_option */ false); |
Roland Levillain | 9ec5e22 | 2015-08-17 20:18:41 +0100 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | void ParseBase(const StringPiece& option) { |
| 610 | DCHECK(option.starts_with("--base=")); |
| 611 | const char* image_base_str = option.substr(strlen("--base=")).data(); |
| 612 | char* end; |
| 613 | image_base_ = strtoul(image_base_str, &end, 16); |
| 614 | if (end == image_base_str || *end != '\0') { |
| 615 | Usage("Failed to parse hexadecimal value for option %s", option.data()); |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | void ParseInstructionSet(const StringPiece& option) { |
| 620 | DCHECK(option.starts_with("--instruction-set=")); |
| 621 | StringPiece instruction_set_str = option.substr(strlen("--instruction-set=")).data(); |
| 622 | // StringPiece is not necessarily zero-terminated, so need to make a copy and ensure it. |
| 623 | std::unique_ptr<char[]> buf(new char[instruction_set_str.length() + 1]); |
| 624 | strncpy(buf.get(), instruction_set_str.data(), instruction_set_str.length()); |
| 625 | buf.get()[instruction_set_str.length()] = 0; |
| 626 | instruction_set_ = GetInstructionSetFromString(buf.get()); |
| 627 | // arm actually means thumb2. |
| 628 | if (instruction_set_ == InstructionSet::kArm) { |
| 629 | instruction_set_ = InstructionSet::kThumb2; |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | void ParseInstructionSetVariant(const StringPiece& option, ParserOptions* parser_options) { |
| 634 | DCHECK(option.starts_with("--instruction-set-variant=")); |
| 635 | StringPiece str = option.substr(strlen("--instruction-set-variant=")).data(); |
| 636 | instruction_set_features_.reset( |
| 637 | InstructionSetFeatures::FromVariant( |
| 638 | instruction_set_, str.as_string(), &parser_options->error_msg)); |
| 639 | if (instruction_set_features_.get() == nullptr) { |
| 640 | Usage("%s", parser_options->error_msg.c_str()); |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | void ParseInstructionSetFeatures(const StringPiece& option, ParserOptions* parser_options) { |
| 645 | DCHECK(option.starts_with("--instruction-set-features=")); |
| 646 | StringPiece str = option.substr(strlen("--instruction-set-features=")).data(); |
| 647 | if (instruction_set_features_.get() == nullptr) { |
| 648 | instruction_set_features_.reset( |
| 649 | InstructionSetFeatures::FromVariant( |
| 650 | instruction_set_, "default", &parser_options->error_msg)); |
| 651 | if (instruction_set_features_.get() == nullptr) { |
| 652 | Usage("Problem initializing default instruction set features variant: %s", |
| 653 | parser_options->error_msg.c_str()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 654 | } |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 655 | } |
Roland Levillain | 9ec5e22 | 2015-08-17 20:18:41 +0100 | [diff] [blame] | 656 | instruction_set_features_.reset( |
| 657 | instruction_set_features_->AddFeaturesFromString(str.as_string(), |
| 658 | &parser_options->error_msg)); |
| 659 | if (instruction_set_features_.get() == nullptr) { |
| 660 | Usage("Error parsing '%s': %s", option.data(), parser_options->error_msg.c_str()); |
| 661 | } |
| 662 | } |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 663 | |
Roland Levillain | 9ec5e22 | 2015-08-17 20:18:41 +0100 | [diff] [blame] | 664 | void ParseCompilerBackend(const StringPiece& option, ParserOptions* parser_options) { |
| 665 | DCHECK(option.starts_with("--compiler-backend=")); |
| 666 | parser_options->requested_specific_compiler = true; |
| 667 | StringPiece backend_str = option.substr(strlen("--compiler-backend=")).data(); |
| 668 | if (backend_str == "Quick") { |
| 669 | compiler_kind_ = Compiler::kQuick; |
| 670 | } else if (backend_str == "Optimizing") { |
| 671 | compiler_kind_ = Compiler::kOptimizing; |
| 672 | } else { |
| 673 | Usage("Unknown compiler backend: %s", backend_str.data()); |
| 674 | } |
| 675 | } |
| 676 | |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 677 | void ParseImageFormat(const StringPiece& option) { |
| 678 | const StringPiece substr("--image-format="); |
| 679 | DCHECK(option.starts_with(substr)); |
| 680 | const StringPiece format_str = option.substr(substr.length()); |
| 681 | if (format_str == "lz4") { |
| 682 | image_storage_mode_ = ImageHeader::kStorageModeLZ4; |
| 683 | } else if (format_str == "uncompressed") { |
| 684 | image_storage_mode_ = ImageHeader::kStorageModeUncompressed; |
| 685 | } else { |
| 686 | Usage("Unknown image format: %s", format_str.data()); |
| 687 | } |
| 688 | } |
| 689 | |
Jeff Hao | 436183f | 2016-01-12 16:36:50 -0800 | [diff] [blame] | 690 | void ProcessOptions(ParserOptions* parser_options) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 691 | boot_image_ = !image_filenames_.empty(); |
Mathieu Chartier | a90c772 | 2015-10-29 15:41:36 -0700 | [diff] [blame] | 692 | app_image_ = app_image_fd_ != -1 || !app_image_file_name_.empty(); |
| 693 | |
| 694 | if (IsAppImage() && IsBootImage()) { |
| 695 | Usage("Can't have both --image and (--app-image-fd or --app-image-file)"); |
| 696 | } |
| 697 | |
| 698 | if (IsBootImage()) { |
Nicolas Geoffray | 409e809 | 2015-10-01 10:32:19 +0100 | [diff] [blame] | 699 | // We need the boot image to always be debuggable. |
Nicolas Geoffray | abbb0f7 | 2015-10-29 18:55:58 +0000 | [diff] [blame] | 700 | compiler_options_->debuggable_ = true; |
Nicolas Geoffray | 1412dfa | 2015-03-20 14:48:13 +0000 | [diff] [blame] | 701 | } |
Nicolas Geoffray | 9bb492a | 2014-11-25 23:42:00 +0000 | [diff] [blame] | 702 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 703 | if (oat_filenames_.empty() && oat_fd_ == -1) { |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 704 | Usage("Output must be supplied with either --oat-file or --oat-fd"); |
| 705 | } |
| 706 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 707 | if (!oat_filenames_.empty() && oat_fd_ != -1) { |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 708 | Usage("--oat-file should not be used with --oat-fd"); |
| 709 | } |
| 710 | |
Roland Levillain | 9ec5e22 | 2015-08-17 20:18:41 +0100 | [diff] [blame] | 711 | if (!parser_options->oat_symbols.empty() && oat_fd_ != -1) { |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 712 | Usage("--oat-symbols should not be used with --oat-fd"); |
| 713 | } |
| 714 | |
Roland Levillain | 9ec5e22 | 2015-08-17 20:18:41 +0100 | [diff] [blame] | 715 | if (!parser_options->oat_symbols.empty() && is_host_) { |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 716 | Usage("--oat-symbols should not be used with --host"); |
| 717 | } |
| 718 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 719 | if (oat_fd_ != -1 && !image_filenames_.empty()) { |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 720 | Usage("--oat-fd should not be used with --image"); |
| 721 | } |
| 722 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 723 | if (!parser_options->oat_symbols.empty() && |
| 724 | parser_options->oat_symbols.size() != oat_filenames_.size()) { |
| 725 | Usage("--oat-file arguments do not match --oat-symbols arguments"); |
| 726 | } |
| 727 | |
| 728 | if (!image_filenames_.empty() && image_filenames_.size() != oat_filenames_.size()) { |
| 729 | Usage("--oat-file arguments do not match --image arguments"); |
| 730 | } |
| 731 | |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 732 | if (android_root_.empty()) { |
| 733 | const char* android_root_env_var = getenv("ANDROID_ROOT"); |
| 734 | if (android_root_env_var == nullptr) { |
| 735 | Usage("--android-root unspecified and ANDROID_ROOT not set"); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 736 | } |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 737 | android_root_ += android_root_env_var; |
| 738 | } |
| 739 | |
Mathieu Chartier | a90c772 | 2015-10-29 15:41:36 -0700 | [diff] [blame] | 740 | if (!boot_image_ && parser_options->boot_image_filename.empty()) { |
Roland Levillain | 9ec5e22 | 2015-08-17 20:18:41 +0100 | [diff] [blame] | 741 | parser_options->boot_image_filename += android_root_; |
| 742 | parser_options->boot_image_filename += "/framework/boot.art"; |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 743 | } |
Roland Levillain | 9ec5e22 | 2015-08-17 20:18:41 +0100 | [diff] [blame] | 744 | if (!parser_options->boot_image_filename.empty()) { |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 745 | boot_image_filename_ = parser_options->boot_image_filename; |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 746 | } |
| 747 | |
Mathieu Chartier | a90c772 | 2015-10-29 15:41:36 -0700 | [diff] [blame] | 748 | if (image_classes_filename_ != nullptr && !IsBootImage()) { |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 749 | Usage("--image-classes should only be used with --image"); |
| 750 | } |
| 751 | |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 752 | if (image_classes_filename_ != nullptr && !boot_image_filename_.empty()) { |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 753 | Usage("--image-classes should not be used with --boot-image"); |
| 754 | } |
| 755 | |
| 756 | if (image_classes_zip_filename_ != nullptr && image_classes_filename_ == nullptr) { |
| 757 | Usage("--image-classes-zip should be used with --image-classes"); |
| 758 | } |
| 759 | |
Mathieu Chartier | a90c772 | 2015-10-29 15:41:36 -0700 | [diff] [blame] | 760 | if (compiled_classes_filename_ != nullptr && !IsBootImage()) { |
Andreas Gampe | 4bf3ae9 | 2014-11-11 13:28:29 -0800 | [diff] [blame] | 761 | Usage("--compiled-classes should only be used with --image"); |
| 762 | } |
| 763 | |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 764 | if (compiled_classes_filename_ != nullptr && !boot_image_filename_.empty()) { |
Andreas Gampe | 4bf3ae9 | 2014-11-11 13:28:29 -0800 | [diff] [blame] | 765 | Usage("--compiled-classes should not be used with --boot-image"); |
| 766 | } |
| 767 | |
| 768 | if (compiled_classes_zip_filename_ != nullptr && compiled_classes_filename_ == nullptr) { |
| 769 | Usage("--compiled-classes-zip should be used with --compiled-classes"); |
| 770 | } |
| 771 | |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 772 | if (dex_filenames_.empty() && zip_fd_ == -1) { |
| 773 | Usage("Input must be supplied with either --dex-file or --zip-fd"); |
| 774 | } |
| 775 | |
| 776 | if (!dex_filenames_.empty() && zip_fd_ != -1) { |
| 777 | Usage("--dex-file should not be used with --zip-fd"); |
| 778 | } |
| 779 | |
| 780 | if (!dex_filenames_.empty() && !zip_location_.empty()) { |
| 781 | Usage("--dex-file should not be used with --zip-location"); |
| 782 | } |
| 783 | |
| 784 | if (dex_locations_.empty()) { |
| 785 | for (const char* dex_file_name : dex_filenames_) { |
| 786 | dex_locations_.push_back(dex_file_name); |
| 787 | } |
| 788 | } else if (dex_locations_.size() != dex_filenames_.size()) { |
| 789 | Usage("--dex-location arguments do not match --dex-file arguments"); |
| 790 | } |
| 791 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 792 | if (!dex_filenames_.empty() && !oat_filenames_.empty()) { |
| 793 | if (oat_filenames_.size() != 1 && oat_filenames_.size() != dex_filenames_.size()) { |
| 794 | Usage("--oat-file arguments must be singular or match --dex-file arguments"); |
| 795 | } |
| 796 | } |
| 797 | |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 798 | if (zip_fd_ != -1 && zip_location_.empty()) { |
| 799 | Usage("--zip-location should be supplied with --zip-fd"); |
| 800 | } |
| 801 | |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 802 | if (boot_image_filename_.empty()) { |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 803 | if (image_base_ == 0) { |
| 804 | Usage("Non-zero --base not specified"); |
| 805 | } |
| 806 | } |
| 807 | |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 808 | if (!profile_file_.empty() && (profile_file_fd_ != kInvalidFd)) { |
| 809 | Usage("Profile file should not be specified with both --profile-file-fd and --profile-file"); |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 810 | } |
| 811 | |
Roland Levillain | 9ec5e22 | 2015-08-17 20:18:41 +0100 | [diff] [blame] | 812 | if (!parser_options->oat_symbols.empty()) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 813 | oat_unstripped_ = std::move(parser_options->oat_symbols); |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | // If no instruction set feature was given, use the default one for the target |
| 817 | // instruction set. |
| 818 | if (instruction_set_features_.get() == nullptr) { |
| 819 | instruction_set_features_.reset( |
Roland Levillain | 9ec5e22 | 2015-08-17 20:18:41 +0100 | [diff] [blame] | 820 | InstructionSetFeatures::FromVariant( |
| 821 | instruction_set_, "default", &parser_options->error_msg)); |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 822 | if (instruction_set_features_.get() == nullptr) { |
| 823 | Usage("Problem initializing default instruction set features variant: %s", |
Roland Levillain | 9ec5e22 | 2015-08-17 20:18:41 +0100 | [diff] [blame] | 824 | parser_options->error_msg.c_str()); |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 825 | } |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | if (instruction_set_ == kRuntimeISA) { |
| 829 | std::unique_ptr<const InstructionSetFeatures> runtime_features( |
| 830 | InstructionSetFeatures::FromCppDefines()); |
| 831 | if (!instruction_set_features_->Equals(runtime_features.get())) { |
| 832 | LOG(WARNING) << "Mismatch between dex2oat instruction set features (" |
| 833 | << *instruction_set_features_ << ") and those of dex2oat executable (" |
| 834 | << *runtime_features <<") for the command line:\n" |
| 835 | << CommandLine(); |
| 836 | } |
| 837 | } |
| 838 | |
Roland Levillain | a215b95 | 2015-08-07 11:38:32 +0100 | [diff] [blame] | 839 | // It they are not set, use default values for inlining settings. |
| 840 | // TODO: We should rethink the compiler filter. We mostly save |
| 841 | // time here, which is orthogonal to space. |
Nicolas Geoffray | abbb0f7 | 2015-10-29 18:55:58 +0000 | [diff] [blame] | 842 | if (compiler_options_->inline_depth_limit_ == CompilerOptions::kUnsetInlineDepthLimit) { |
| 843 | compiler_options_->inline_depth_limit_ = |
| 844 | (compiler_options_->compiler_filter_ == CompilerOptions::kSpace) |
Roland Levillain | a215b95 | 2015-08-07 11:38:32 +0100 | [diff] [blame] | 845 | // Implementation of the space filter: limit inlining depth. |
| 846 | ? CompilerOptions::kSpaceFilterInlineDepthLimit |
| 847 | : CompilerOptions::kDefaultInlineDepthLimit; |
| 848 | } |
Nicolas Geoffray | abbb0f7 | 2015-10-29 18:55:58 +0000 | [diff] [blame] | 849 | if (compiler_options_->inline_max_code_units_ == CompilerOptions::kUnsetInlineMaxCodeUnits) { |
| 850 | compiler_options_->inline_max_code_units_ = |
| 851 | (compiler_options_->compiler_filter_ == CompilerOptions::kSpace) |
Roland Levillain | a215b95 | 2015-08-07 11:38:32 +0100 | [diff] [blame] | 852 | // Implementation of the space filter: limit inlining max code units. |
| 853 | ? CompilerOptions::kSpaceFilterInlineMaxCodeUnits |
| 854 | : CompilerOptions::kDefaultInlineMaxCodeUnits; |
| 855 | } |
| 856 | |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 857 | // Checks are all explicit until we know the architecture. |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 858 | // Set the compilation target's implicit checks options. |
| 859 | switch (instruction_set_) { |
| 860 | case kArm: |
| 861 | case kThumb2: |
| 862 | case kArm64: |
| 863 | case kX86: |
| 864 | case kX86_64: |
Douglas Leung | 22bb5a2 | 2015-07-02 16:42:08 -0700 | [diff] [blame] | 865 | case kMips: |
| 866 | case kMips64: |
Nicolas Geoffray | abbb0f7 | 2015-10-29 18:55:58 +0000 | [diff] [blame] | 867 | compiler_options_->implicit_null_checks_ = true; |
| 868 | compiler_options_->implicit_so_checks_ = true; |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 869 | break; |
| 870 | |
| 871 | default: |
| 872 | // Defaults are correct. |
| 873 | break; |
| 874 | } |
| 875 | |
Nicolas Geoffray | abbb0f7 | 2015-10-29 18:55:58 +0000 | [diff] [blame] | 876 | compiler_options_->verbose_methods_ = verbose_methods_.empty() ? nullptr : &verbose_methods_; |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 877 | |
Jeff Hao | 436183f | 2016-01-12 16:36:50 -0800 | [diff] [blame] | 878 | if (!IsBootImage() && multi_image_) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 879 | Usage("--multi-image can only be used when creating boot images"); |
| 880 | } |
Jeff Hao | 436183f | 2016-01-12 16:36:50 -0800 | [diff] [blame] | 881 | if (IsBootImage() && multi_image_ && image_filenames_.size() > 1) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 882 | Usage("--multi-image cannot be used with multiple image names"); |
| 883 | } |
| 884 | |
| 885 | // For now, if we're on the host and compile the boot image, *always* use multiple image files. |
| 886 | if (!kIsTargetBuild && IsBootImage()) { |
| 887 | if (image_filenames_.size() == 1) { |
Jeff Hao | 436183f | 2016-01-12 16:36:50 -0800 | [diff] [blame] | 888 | multi_image_ = true; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 889 | } |
| 890 | } |
| 891 | |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 892 | // Done with usage checks, enable watchdog if requested |
Roland Levillain | 9ec5e22 | 2015-08-17 20:18:41 +0100 | [diff] [blame] | 893 | if (parser_options->watch_dog_enabled) { |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 894 | watchdog_.reset(new WatchDog(true)); |
| 895 | } |
| 896 | |
| 897 | // Fill some values into the key-value store for the oat header. |
| 898 | key_value_store_.reset(new SafeMap<std::string, std::string>()); |
Andreas Gampe | ace0dc1 | 2016-01-20 13:33:13 -0800 | [diff] [blame] | 899 | |
Hiroshi Yamauchi | 6af5348 | 2016-01-29 15:38:58 -0800 | [diff] [blame] | 900 | // Automatically force determinism for the boot image in a host build if the default GC is CMS |
| 901 | // or MS and read barriers are not enabled, as the former switches the GC to a non-concurrent |
| 902 | // one by passing the option `-Xgc:nonconcurrent` (see below). |
| 903 | if (!kIsTargetBuild && IsBootImage()) { |
| 904 | if (SupportsDeterministicCompilation()) { |
| 905 | force_determinism_ = true; |
| 906 | } else { |
| 907 | LOG(WARNING) << "Deterministic compilation is disabled."; |
| 908 | } |
Andreas Gampe | ace0dc1 | 2016-01-20 13:33:13 -0800 | [diff] [blame] | 909 | } |
| 910 | compiler_options_->force_determinism_ = force_determinism_; |
Roland Levillain | 9ec5e22 | 2015-08-17 20:18:41 +0100 | [diff] [blame] | 911 | } |
| 912 | |
Hiroshi Yamauchi | 6af5348 | 2016-01-29 15:38:58 -0800 | [diff] [blame] | 913 | static bool SupportsDeterministicCompilation() { |
| 914 | return (gc::kCollectorTypeDefault == gc::kCollectorTypeCMS || |
| 915 | gc::kCollectorTypeDefault == gc::kCollectorTypeMS) && |
| 916 | !kEmitCompilerReadBarrier; |
| 917 | } |
| 918 | |
Jeff Hao | 436183f | 2016-01-12 16:36:50 -0800 | [diff] [blame] | 919 | void ExpandOatAndImageFilenames() { |
| 920 | std::string base_oat = oat_filenames_[0]; |
| 921 | size_t last_oat_slash = base_oat.rfind('/'); |
| 922 | if (last_oat_slash == std::string::npos) { |
| 923 | Usage("--multi-image used with unusable oat filename %s", base_oat.c_str()); |
| 924 | } |
| 925 | // We also need to honor path components that were encoded through '@'. Otherwise the loading |
| 926 | // code won't be able to find the images. |
| 927 | if (base_oat.find('@', last_oat_slash) != std::string::npos) { |
| 928 | last_oat_slash = base_oat.rfind('@'); |
| 929 | } |
| 930 | base_oat = base_oat.substr(0, last_oat_slash + 1); |
| 931 | |
| 932 | std::string base_img = image_filenames_[0]; |
| 933 | size_t last_img_slash = base_img.rfind('/'); |
| 934 | if (last_img_slash == std::string::npos) { |
| 935 | Usage("--multi-image used with unusable image filename %s", base_img.c_str()); |
| 936 | } |
| 937 | // We also need to honor path components that were encoded through '@'. Otherwise the loading |
| 938 | // code won't be able to find the images. |
| 939 | if (base_img.find('@', last_img_slash) != std::string::npos) { |
| 940 | last_img_slash = base_img.rfind('@'); |
| 941 | } |
| 942 | |
| 943 | // Get the prefix, which is the primary image name (without path components). Strip the |
| 944 | // extension. |
| 945 | std::string prefix = base_img.substr(last_img_slash + 1); |
| 946 | if (prefix.rfind('.') != std::string::npos) { |
| 947 | prefix = prefix.substr(0, prefix.rfind('.')); |
| 948 | } |
| 949 | if (!prefix.empty()) { |
| 950 | prefix = prefix + "-"; |
| 951 | } |
| 952 | |
| 953 | base_img = base_img.substr(0, last_img_slash + 1); |
| 954 | |
| 955 | // Note: we have some special case here for our testing. We have to inject the differentiating |
| 956 | // parts for the different core images. |
| 957 | std::string infix; // Empty infix by default. |
| 958 | { |
| 959 | // Check the first name. |
| 960 | std::string dex_file = oat_filenames_[0]; |
| 961 | size_t last_dex_slash = dex_file.rfind('/'); |
| 962 | if (last_dex_slash != std::string::npos) { |
| 963 | dex_file = dex_file.substr(last_dex_slash + 1); |
| 964 | } |
| 965 | size_t last_dex_dot = dex_file.rfind('.'); |
| 966 | if (last_dex_dot != std::string::npos) { |
| 967 | dex_file = dex_file.substr(0, last_dex_dot); |
| 968 | } |
| 969 | if (StartsWith(dex_file, "core-")) { |
| 970 | infix = dex_file.substr(strlen("core")); |
| 971 | } |
| 972 | } |
| 973 | |
| 974 | // Now create the other names. Use a counted loop to skip the first one. |
| 975 | for (size_t i = 1; i < dex_locations_.size(); ++i) { |
| 976 | // TODO: Make everything properly std::string. |
| 977 | std::string image_name = CreateMultiImageName(dex_locations_[i], prefix, infix, ".art"); |
| 978 | char_backing_storage_.push_back(base_img + image_name); |
| 979 | image_filenames_.push_back((char_backing_storage_.end() - 1)->c_str()); |
| 980 | |
| 981 | std::string oat_name = CreateMultiImageName(dex_locations_[i], prefix, infix, ".oat"); |
| 982 | char_backing_storage_.push_back(base_oat + oat_name); |
| 983 | oat_filenames_.push_back((char_backing_storage_.end() - 1)->c_str()); |
| 984 | } |
| 985 | } |
| 986 | |
Andreas Gampe | 8994a04 | 2015-12-30 19:03:17 +0000 | [diff] [blame] | 987 | // Modify the input string in the following way: |
| 988 | // 0) Assume input is /a/b/c.d |
| 989 | // 1) Strip the path -> c.d |
| 990 | // 2) Inject prefix p -> pc.d |
| 991 | // 3) Inject infix i -> pci.d |
| 992 | // 4) Replace suffix with s if it's "jar" -> d == "jar" -> pci.s |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 993 | static std::string CreateMultiImageName(std::string in, |
Andreas Gampe | 8994a04 | 2015-12-30 19:03:17 +0000 | [diff] [blame] | 994 | const std::string& prefix, |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 995 | const std::string& infix, |
Andreas Gampe | 8994a04 | 2015-12-30 19:03:17 +0000 | [diff] [blame] | 996 | const char* replace_suffix) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 997 | size_t last_dex_slash = in.rfind('/'); |
| 998 | if (last_dex_slash != std::string::npos) { |
| 999 | in = in.substr(last_dex_slash + 1); |
| 1000 | } |
Andreas Gampe | 8994a04 | 2015-12-30 19:03:17 +0000 | [diff] [blame] | 1001 | if (!prefix.empty()) { |
| 1002 | in = prefix + in; |
| 1003 | } |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1004 | if (!infix.empty()) { |
| 1005 | // Inject infix. |
| 1006 | size_t last_dot = in.rfind('.'); |
| 1007 | if (last_dot != std::string::npos) { |
| 1008 | in.insert(last_dot, infix); |
| 1009 | } |
| 1010 | } |
| 1011 | if (EndsWith(in, ".jar")) { |
Andreas Gampe | 8994a04 | 2015-12-30 19:03:17 +0000 | [diff] [blame] | 1012 | in = in.substr(0, in.length() - strlen(".jar")) + |
| 1013 | (replace_suffix != nullptr ? replace_suffix : ""); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1014 | } |
| 1015 | return in; |
| 1016 | } |
| 1017 | |
Nicolas Geoffray | abbb0f7 | 2015-10-29 18:55:58 +0000 | [diff] [blame] | 1018 | void InsertCompileOptions(int argc, char** argv) { |
Roland Levillain | 9ec5e22 | 2015-08-17 20:18:41 +0100 | [diff] [blame] | 1019 | std::ostringstream oss; |
| 1020 | for (int i = 0; i < argc; ++i) { |
| 1021 | if (i > 0) { |
| 1022 | oss << ' '; |
| 1023 | } |
| 1024 | oss << argv[i]; |
| 1025 | } |
| 1026 | key_value_store_->Put(OatHeader::kDex2OatCmdLineKey, oss.str()); |
| 1027 | oss.str(""); // Reset. |
| 1028 | oss << kRuntimeISA; |
| 1029 | key_value_store_->Put(OatHeader::kDex2OatHostKey, oss.str()); |
| 1030 | key_value_store_->Put( |
| 1031 | OatHeader::kPicKey, |
Nicolas Geoffray | abbb0f7 | 2015-10-29 18:55:58 +0000 | [diff] [blame] | 1032 | compiler_options_->compile_pic_ ? OatHeader::kTrueValue : OatHeader::kFalseValue); |
Roland Levillain | 9ec5e22 | 2015-08-17 20:18:41 +0100 | [diff] [blame] | 1033 | key_value_store_->Put( |
| 1034 | OatHeader::kDebuggableKey, |
Nicolas Geoffray | abbb0f7 | 2015-10-29 18:55:58 +0000 | [diff] [blame] | 1035 | compiler_options_->debuggable_ ? OatHeader::kTrueValue : OatHeader::kFalseValue); |
David Brazdil | ce4b0ba | 2016-01-28 15:05:49 +0000 | [diff] [blame] | 1036 | key_value_store_->Put( |
| 1037 | OatHeader::kExtractOnlyKey, |
| 1038 | compiler_options_->IsExtractOnly() ? OatHeader::kTrueValue : OatHeader::kFalseValue); |
Roland Levillain | 9ec5e22 | 2015-08-17 20:18:41 +0100 | [diff] [blame] | 1039 | } |
| 1040 | |
| 1041 | // Parse the arguments from the command line. In case of an unrecognized option or impossible |
| 1042 | // values/combinations, a usage error will be displayed and exit() is called. Thus, if the method |
| 1043 | // returns, arguments have been successfully parsed. |
| 1044 | void ParseArgs(int argc, char** argv) { |
| 1045 | original_argc = argc; |
| 1046 | original_argv = argv; |
| 1047 | |
| 1048 | InitLogging(argv); |
| 1049 | |
| 1050 | // Skip over argv[0]. |
| 1051 | argv++; |
| 1052 | argc--; |
| 1053 | |
| 1054 | if (argc == 0) { |
| 1055 | Usage("No arguments specified"); |
| 1056 | } |
| 1057 | |
| 1058 | std::unique_ptr<ParserOptions> parser_options(new ParserOptions()); |
Nicolas Geoffray | abbb0f7 | 2015-10-29 18:55:58 +0000 | [diff] [blame] | 1059 | compiler_options_.reset(new CompilerOptions()); |
Roland Levillain | 9ec5e22 | 2015-08-17 20:18:41 +0100 | [diff] [blame] | 1060 | |
| 1061 | for (int i = 0; i < argc; i++) { |
| 1062 | const StringPiece option(argv[i]); |
| 1063 | const bool log_options = false; |
| 1064 | if (log_options) { |
| 1065 | LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i]; |
| 1066 | } |
| 1067 | if (option.starts_with("--dex-file=")) { |
| 1068 | dex_filenames_.push_back(option.substr(strlen("--dex-file=")).data()); |
| 1069 | } else if (option.starts_with("--dex-location=")) { |
| 1070 | dex_locations_.push_back(option.substr(strlen("--dex-location=")).data()); |
| 1071 | } else if (option.starts_with("--zip-fd=")) { |
| 1072 | ParseZipFd(option); |
| 1073 | } else if (option.starts_with("--zip-location=")) { |
| 1074 | zip_location_ = option.substr(strlen("--zip-location=")).data(); |
| 1075 | } else if (option.starts_with("--oat-file=")) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1076 | oat_filenames_.push_back(option.substr(strlen("--oat-file=")).data()); |
Roland Levillain | 9ec5e22 | 2015-08-17 20:18:41 +0100 | [diff] [blame] | 1077 | } else if (option.starts_with("--oat-symbols=")) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1078 | parser_options->oat_symbols.push_back(option.substr(strlen("--oat-symbols=")).data()); |
Roland Levillain | 9ec5e22 | 2015-08-17 20:18:41 +0100 | [diff] [blame] | 1079 | } else if (option.starts_with("--oat-fd=")) { |
| 1080 | ParseOatFd(option); |
| 1081 | } else if (option == "--watch-dog") { |
| 1082 | parser_options->watch_dog_enabled = true; |
| 1083 | } else if (option == "--no-watch-dog") { |
| 1084 | parser_options->watch_dog_enabled = false; |
| 1085 | } else if (option.starts_with("-j")) { |
| 1086 | ParseJ(option); |
| 1087 | } else if (option.starts_with("--oat-location=")) { |
| 1088 | oat_location_ = option.substr(strlen("--oat-location=")).data(); |
| 1089 | } else if (option.starts_with("--image=")) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1090 | image_filenames_.push_back(option.substr(strlen("--image=")).data()); |
Roland Levillain | 9ec5e22 | 2015-08-17 20:18:41 +0100 | [diff] [blame] | 1091 | } else if (option.starts_with("--image-classes=")) { |
| 1092 | image_classes_filename_ = option.substr(strlen("--image-classes=")).data(); |
| 1093 | } else if (option.starts_with("--image-classes-zip=")) { |
| 1094 | image_classes_zip_filename_ = option.substr(strlen("--image-classes-zip=")).data(); |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 1095 | } else if (option.starts_with("--image-format=")) { |
| 1096 | ParseImageFormat(option); |
Roland Levillain | 9ec5e22 | 2015-08-17 20:18:41 +0100 | [diff] [blame] | 1097 | } else if (option.starts_with("--compiled-classes=")) { |
| 1098 | compiled_classes_filename_ = option.substr(strlen("--compiled-classes=")).data(); |
| 1099 | } else if (option.starts_with("--compiled-classes-zip=")) { |
| 1100 | compiled_classes_zip_filename_ = option.substr(strlen("--compiled-classes-zip=")).data(); |
| 1101 | } else if (option.starts_with("--compiled-methods=")) { |
| 1102 | compiled_methods_filename_ = option.substr(strlen("--compiled-methods=")).data(); |
| 1103 | } else if (option.starts_with("--compiled-methods-zip=")) { |
| 1104 | compiled_methods_zip_filename_ = option.substr(strlen("--compiled-methods-zip=")).data(); |
| 1105 | } else if (option.starts_with("--base=")) { |
| 1106 | ParseBase(option); |
| 1107 | } else if (option.starts_with("--boot-image=")) { |
| 1108 | parser_options->boot_image_filename = option.substr(strlen("--boot-image=")).data(); |
| 1109 | } else if (option.starts_with("--android-root=")) { |
| 1110 | android_root_ = option.substr(strlen("--android-root=")).data(); |
| 1111 | } else if (option.starts_with("--instruction-set=")) { |
| 1112 | ParseInstructionSet(option); |
| 1113 | } else if (option.starts_with("--instruction-set-variant=")) { |
| 1114 | ParseInstructionSetVariant(option, parser_options.get()); |
| 1115 | } else if (option.starts_with("--instruction-set-features=")) { |
| 1116 | ParseInstructionSetFeatures(option, parser_options.get()); |
| 1117 | } else if (option.starts_with("--compiler-backend=")) { |
| 1118 | ParseCompilerBackend(option, parser_options.get()); |
Nicolas Geoffray | abbb0f7 | 2015-10-29 18:55:58 +0000 | [diff] [blame] | 1119 | } else if (option.starts_with("--profile-file=")) { |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 1120 | profile_file_ = option.substr(strlen("--profile-file=")).ToString(); |
Calin Juravle | 877fd96 | 2016-01-05 14:29:29 +0000 | [diff] [blame] | 1121 | } else if (option.starts_with("--profile-file-fd=")) { |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 1122 | ParseUintOption(option, "--profile-file-fd", &profile_file_fd_, Usage); |
Roland Levillain | 9ec5e22 | 2015-08-17 20:18:41 +0100 | [diff] [blame] | 1123 | } else if (option == "--host") { |
| 1124 | is_host_ = true; |
| 1125 | } else if (option == "--runtime-arg") { |
| 1126 | if (++i >= argc) { |
| 1127 | Usage("Missing required argument for --runtime-arg"); |
| 1128 | } |
| 1129 | if (log_options) { |
| 1130 | LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i]; |
| 1131 | } |
| 1132 | runtime_args_.push_back(argv[i]); |
| 1133 | } else if (option == "--dump-timing") { |
| 1134 | dump_timing_ = true; |
| 1135 | } else if (option == "--dump-passes") { |
| 1136 | dump_passes_ = true; |
Roland Levillain | 9ec5e22 | 2015-08-17 20:18:41 +0100 | [diff] [blame] | 1137 | } else if (option == "--dump-stats") { |
| 1138 | dump_stats_ = true; |
Nicolas Geoffray | abbb0f7 | 2015-10-29 18:55:58 +0000 | [diff] [blame] | 1139 | } else if (option.starts_with("--swap-file=")) { |
| 1140 | swap_file_name_ = option.substr(strlen("--swap-file=")).data(); |
| 1141 | } else if (option.starts_with("--swap-fd=")) { |
Mathieu Chartier | a90c772 | 2015-10-29 15:41:36 -0700 | [diff] [blame] | 1142 | ParseUintOption(option, "--swap-fd", &swap_fd_, Usage); |
| 1143 | } else if (option.starts_with("--app-image-file=")) { |
| 1144 | app_image_file_name_ = option.substr(strlen("--app-image-file=")).data(); |
| 1145 | } else if (option.starts_with("--app-image-fd=")) { |
| 1146 | ParseUintOption(option, "--app-image-fd", &app_image_fd_, Usage); |
Roland Levillain | 9ec5e22 | 2015-08-17 20:18:41 +0100 | [diff] [blame] | 1147 | } else if (option.starts_with("--verbose-methods=")) { |
| 1148 | // TODO: rather than switch off compiler logging, make all VLOG(compiler) messages |
| 1149 | // conditional on having verbost methods. |
| 1150 | gLogVerbosity.compiler = false; |
| 1151 | Split(option.substr(strlen("--verbose-methods=")).ToString(), ',', &verbose_methods_); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1152 | } else if (option == "--multi-image") { |
Jeff Hao | 436183f | 2016-01-12 16:36:50 -0800 | [diff] [blame] | 1153 | multi_image_ = true; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1154 | } else if (option.starts_with("--no-inline-from=")) { |
| 1155 | no_inline_from_string_ = option.substr(strlen("--no-inline-from=")).data(); |
Andreas Gampe | ace0dc1 | 2016-01-20 13:33:13 -0800 | [diff] [blame] | 1156 | } else if (option == "--force-determinism") { |
Hiroshi Yamauchi | 6af5348 | 2016-01-29 15:38:58 -0800 | [diff] [blame] | 1157 | if (!SupportsDeterministicCompilation()) { |
| 1158 | Usage("Cannot use --force-determinism with read barriers or non-CMS garbage collector"); |
Roland Levillain | eb2c741 | 2016-01-28 14:37:01 +0000 | [diff] [blame] | 1159 | } |
Andreas Gampe | ace0dc1 | 2016-01-20 13:33:13 -0800 | [diff] [blame] | 1160 | force_determinism_ = true; |
Nicolas Geoffray | abbb0f7 | 2015-10-29 18:55:58 +0000 | [diff] [blame] | 1161 | } else if (!compiler_options_->ParseCompilerOption(option, Usage)) { |
Roland Levillain | 9ec5e22 | 2015-08-17 20:18:41 +0100 | [diff] [blame] | 1162 | Usage("Unknown argument %s", option.data()); |
| 1163 | } |
| 1164 | } |
| 1165 | |
Jeff Hao | 436183f | 2016-01-12 16:36:50 -0800 | [diff] [blame] | 1166 | ProcessOptions(parser_options.get()); |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1167 | |
| 1168 | // Insert some compiler things. |
Nicolas Geoffray | abbb0f7 | 2015-10-29 18:55:58 +0000 | [diff] [blame] | 1169 | InsertCompileOptions(argc, argv); |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1170 | } |
| 1171 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1172 | // Check whether the oat output files are writable, and open them for later. Also open a swap |
| 1173 | // file, if a name is given. |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1174 | bool OpenFile() { |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 1175 | // Prune non-existent dex files now so that we don't create empty oat files for multi-image. |
| 1176 | PruneNonExistentDexFiles(); |
| 1177 | |
Jeff Hao | 436183f | 2016-01-12 16:36:50 -0800 | [diff] [blame] | 1178 | // Expand oat and image filenames for multi image. |
| 1179 | if (IsBootImage() && multi_image_) { |
| 1180 | ExpandOatAndImageFilenames(); |
| 1181 | } |
| 1182 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1183 | bool create_file = oat_fd_ == -1; // as opposed to using open file descriptor |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1184 | if (create_file) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1185 | for (const char* oat_filename : oat_filenames_) { |
| 1186 | std::unique_ptr<File> oat_file(OS::CreateEmptyFile(oat_filename)); |
| 1187 | if (oat_file.get() == nullptr) { |
| 1188 | PLOG(ERROR) << "Failed to create oat file: " << oat_filename; |
| 1189 | return false; |
| 1190 | } |
| 1191 | if (create_file && fchmod(oat_file->Fd(), 0644) != 0) { |
| 1192 | PLOG(ERROR) << "Failed to make oat file world readable: " << oat_filename; |
| 1193 | oat_file->Erase(); |
| 1194 | return false; |
| 1195 | } |
| 1196 | oat_files_.push_back(std::move(oat_file)); |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1197 | } |
| 1198 | } else { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1199 | std::unique_ptr<File> oat_file(new File(oat_fd_, oat_location_, true)); |
| 1200 | oat_file->DisableAutoClose(); |
| 1201 | if (oat_file->SetLength(0) != 0) { |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame] | 1202 | PLOG(WARNING) << "Truncating oat file " << oat_location_ << " failed."; |
| 1203 | } |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1204 | if (oat_file.get() == nullptr) { |
| 1205 | PLOG(ERROR) << "Failed to create oat file: " << oat_location_; |
| 1206 | return false; |
| 1207 | } |
| 1208 | if (create_file && fchmod(oat_file->Fd(), 0644) != 0) { |
| 1209 | PLOG(ERROR) << "Failed to make oat file world readable: " << oat_location_; |
| 1210 | oat_file->Erase(); |
| 1211 | return false; |
| 1212 | } |
| 1213 | oat_filenames_.push_back(oat_location_.c_str()); |
| 1214 | oat_files_.push_back(std::move(oat_file)); |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1215 | } |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 1216 | |
| 1217 | // Swap file handling. |
| 1218 | // |
| 1219 | // If the swap fd is not -1, we assume this is the file descriptor of an open but unlinked file |
| 1220 | // that we can use for swap. |
| 1221 | // |
| 1222 | // If the swap fd is -1 and we have a swap-file string, open the given file as a swap file. We |
| 1223 | // will immediately unlink to satisfy the swap fd assumption. |
| 1224 | if (swap_fd_ == -1 && !swap_file_name_.empty()) { |
| 1225 | std::unique_ptr<File> swap_file(OS::CreateEmptyFile(swap_file_name_.c_str())); |
| 1226 | if (swap_file.get() == nullptr) { |
| 1227 | PLOG(ERROR) << "Failed to create swap file: " << swap_file_name_; |
| 1228 | return false; |
| 1229 | } |
| 1230 | swap_fd_ = swap_file->Fd(); |
| 1231 | swap_file->MarkUnchecked(); // We don't we to track this, it will be unlinked immediately. |
| 1232 | swap_file->DisableAutoClose(); // We'll handle it ourselves, the File object will be |
| 1233 | // released immediately. |
| 1234 | unlink(swap_file_name_.c_str()); |
| 1235 | } |
Jeff Hao | 436183f | 2016-01-12 16:36:50 -0800 | [diff] [blame] | 1236 | |
| 1237 | // If we use a swap file, ensure we are above the threshold to make it necessary. |
| 1238 | if (swap_fd_ != -1) { |
| 1239 | if (!UseSwap(IsBootImage(), dex_files_)) { |
| 1240 | close(swap_fd_); |
| 1241 | swap_fd_ = -1; |
| 1242 | VLOG(compiler) << "Decided to run without swap."; |
| 1243 | } else { |
| 1244 | LOG(INFO) << "Large app, accepted running with swap."; |
| 1245 | } |
| 1246 | } |
| 1247 | // Note that dex2oat won't close the swap_fd_. The compiler driver's swap space will do that. |
| 1248 | |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1249 | return true; |
| 1250 | } |
| 1251 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1252 | void EraseOatFiles() { |
| 1253 | for (size_t i = 0; i < oat_files_.size(); ++i) { |
| 1254 | DCHECK(oat_files_[i].get() != nullptr); |
| 1255 | oat_files_[i]->Erase(); |
| 1256 | oat_files_[i].reset(); |
| 1257 | } |
Andreas Gampe | a650e70 | 2014-12-03 14:28:02 -0800 | [diff] [blame] | 1258 | } |
| 1259 | |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 1260 | void Shutdown() { |
| 1261 | ScopedObjectAccess soa(Thread::Current()); |
| 1262 | for (jobject dex_cache : dex_caches_) { |
| 1263 | soa.Env()->DeleteLocalRef(dex_cache); |
| 1264 | } |
| 1265 | dex_caches_.clear(); |
| 1266 | } |
| 1267 | |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1268 | // Set up the environment for compilation. Includes starting the runtime and loading/opening the |
| 1269 | // boot class path. |
| 1270 | bool Setup() { |
| 1271 | TimingLogger::ScopedTiming t("dex2oat Setup", timings_); |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1272 | art::MemMap::Init(); // For ZipEntry::ExtractToMemMap. |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 1273 | |
| 1274 | if (!PrepareImageClasses() || !PrepareCompiledClasses() || !PrepareCompiledMethods()) { |
| 1275 | return false; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1276 | } |
Brian Carlstrom | d76e083 | 2013-08-29 15:17:42 -0700 | [diff] [blame] | 1277 | |
Vladimir Marko | 307dac9 | 2015-10-20 11:20:09 +0100 | [diff] [blame] | 1278 | verification_results_.reset(new VerificationResults(compiler_options_.get())); |
Andreas Gampe | 4585f87 | 2015-03-27 23:45:15 -0700 | [diff] [blame] | 1279 | callbacks_.reset(new QuickCompilerCallbacks( |
Vladimir Marko | 307dac9 | 2015-10-20 11:20:09 +0100 | [diff] [blame] | 1280 | verification_results_.get(), |
Andreas Gampe | 4585f87 | 2015-03-27 23:45:15 -0700 | [diff] [blame] | 1281 | &method_inliner_map_, |
Mathieu Chartier | a90c772 | 2015-10-29 15:41:36 -0700 | [diff] [blame] | 1282 | IsBootImage() ? |
Andreas Gampe | 4585f87 | 2015-03-27 23:45:15 -0700 | [diff] [blame] | 1283 | CompilerCallbacks::CallbackMode::kCompileBootImage : |
| 1284 | CompilerCallbacks::CallbackMode::kCompileApp)); |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1285 | |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 1286 | RuntimeArgumentMap runtime_options; |
| 1287 | if (!PrepareRuntimeOptions(&runtime_options)) { |
| 1288 | return false; |
Andreas Gampe | 1d00add | 2015-02-27 19:35:46 -0800 | [diff] [blame] | 1289 | } |
| 1290 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 1291 | CreateOatWriters(); |
| 1292 | if (!AddDexFileSources()) { |
| 1293 | return false; |
| 1294 | } |
| 1295 | |
| 1296 | if (IsBootImage() && image_filenames_.size() > 1) { |
| 1297 | // If we're compiling the boot image, store the boot classpath into the Key-Value store. |
| 1298 | // We need this for the multi-image case. |
| 1299 | key_value_store_->Put(OatHeader::kBootClassPath, GetMultiImageBootClassPath()); |
| 1300 | } |
| 1301 | |
| 1302 | if (!IsBootImage()) { |
| 1303 | // When compiling an app, create the runtime early to retrieve |
| 1304 | // the image location key needed for the oat header. |
| 1305 | if (!CreateRuntime(std::move(runtime_options))) { |
| 1306 | return false; |
| 1307 | } |
| 1308 | |
David Brazdil | ce4b0ba | 2016-01-28 15:05:49 +0000 | [diff] [blame] | 1309 | if (compiler_options_->IsExtractOnly()) { |
| 1310 | // ExtractOnly oat files only contain non-quickened DEX code and are |
| 1311 | // therefore independent of the image file. |
| 1312 | image_file_location_oat_checksum_ = 0u; |
| 1313 | image_file_location_oat_data_begin_ = 0u; |
| 1314 | image_patch_delta_ = 0; |
| 1315 | } else { |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 1316 | TimingLogger::ScopedTiming t3("Loading image checksum", timings_); |
| 1317 | std::vector<gc::space::ImageSpace*> image_spaces = |
| 1318 | Runtime::Current()->GetHeap()->GetBootImageSpaces(); |
| 1319 | image_file_location_oat_checksum_ = image_spaces[0]->GetImageHeader().GetOatChecksum(); |
| 1320 | image_file_location_oat_data_begin_ = |
| 1321 | reinterpret_cast<uintptr_t>(image_spaces[0]->GetImageHeader().GetOatDataBegin()); |
| 1322 | image_patch_delta_ = image_spaces[0]->GetImageHeader().GetPatchDelta(); |
| 1323 | // Store the boot image filename(s). |
| 1324 | std::vector<std::string> image_filenames; |
| 1325 | for (const gc::space::ImageSpace* image_space : image_spaces) { |
| 1326 | image_filenames.push_back(image_space->GetImageFilename()); |
| 1327 | } |
| 1328 | std::string image_file_location = Join(image_filenames, ':'); |
| 1329 | if (!image_file_location.empty()) { |
| 1330 | key_value_store_->Put(OatHeader::kImageLocationKey, image_file_location); |
| 1331 | } |
| 1332 | } |
| 1333 | |
| 1334 | // Open dex files for class path. |
| 1335 | const std::vector<std::string> class_path_locations = |
| 1336 | GetClassPathLocations(runtime_->GetClassPathString()); |
| 1337 | OpenClassPathFiles(class_path_locations, &class_path_files_); |
| 1338 | |
| 1339 | // Store the classpath we have right now. |
| 1340 | std::vector<const DexFile*> class_path_files = MakeNonOwningPointerVector(class_path_files_); |
| 1341 | key_value_store_->Put(OatHeader::kClassPathKey, |
| 1342 | OatFile::EncodeDexFileDependencies(class_path_files)); |
| 1343 | } |
| 1344 | |
| 1345 | // Now that we have finalized key_value_store_, start writing the oat file. |
Andreas Gampe | c7ae55d | 2015-09-15 20:04:54 -0700 | [diff] [blame] | 1346 | { |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 1347 | TimingLogger::ScopedTiming t_dex("Writing and opening dex files", timings_); |
| 1348 | rodata_.reserve(oat_writers_.size()); |
| 1349 | for (size_t i = 0, size = oat_writers_.size(); i != size; ++i) { |
| 1350 | rodata_.push_back(elf_writers_[i]->StartRoData()); |
| 1351 | // Unzip or copy dex files straight to the oat file. |
| 1352 | std::unique_ptr<MemMap> opened_dex_files_map; |
| 1353 | std::vector<std::unique_ptr<const DexFile>> opened_dex_files; |
| 1354 | if (!oat_writers_[i]->WriteAndOpenDexFiles(rodata_.back(), |
| 1355 | oat_files_[i].get(), |
| 1356 | instruction_set_, |
| 1357 | instruction_set_features_.get(), |
| 1358 | key_value_store_.get(), |
Andreas Gampe | 3a2bd29 | 2016-01-26 17:23:47 -0800 | [diff] [blame] | 1359 | /* verify */ true, |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 1360 | &opened_dex_files_map, |
| 1361 | &opened_dex_files)) { |
| 1362 | return false; |
| 1363 | } |
| 1364 | dex_files_per_oat_file_.push_back(MakeNonOwningPointerVector(opened_dex_files)); |
| 1365 | if (opened_dex_files_map != nullptr) { |
| 1366 | opened_dex_files_maps_.push_back(std::move(opened_dex_files_map)); |
| 1367 | for (std::unique_ptr<const DexFile>& dex_file : opened_dex_files) { |
| 1368 | dex_file_oat_filename_map_.emplace(dex_file.get(), oat_filenames_[i]); |
| 1369 | opened_dex_files_.push_back(std::move(dex_file)); |
| 1370 | } |
| 1371 | } else { |
| 1372 | DCHECK(opened_dex_files.empty()); |
| 1373 | } |
| 1374 | } |
| 1375 | } |
| 1376 | |
| 1377 | dex_files_ = MakeNonOwningPointerVector(opened_dex_files_); |
| 1378 | if (IsBootImage()) { |
| 1379 | // For boot image, pass opened dex files to the Runtime::Create(). |
| 1380 | // Note: Runtime acquires ownership of these dex files. |
| 1381 | runtime_options.Set(RuntimeArgumentMap::BootClassPathDexList, &opened_dex_files_); |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 1382 | if (!CreateRuntime(std::move(runtime_options))) { |
Andreas Gampe | c7ae55d | 2015-09-15 20:04:54 -0700 | [diff] [blame] | 1383 | return false; |
| 1384 | } |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1385 | } |
| 1386 | |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1387 | // If we're doing the image, override the compiler filter to force full compilation. Must be |
| 1388 | // done ahead of WellKnownClasses::Init that causes verification. Note: doesn't force |
| 1389 | // compilation of class initializers. |
| 1390 | // Whilst we're in native take the opportunity to initialize well known classes. |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 1391 | Thread* self = Thread::Current(); |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1392 | WellKnownClasses::Init(self->GetJniEnv()); |
| 1393 | |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 1394 | ClassLinker* const class_linker = Runtime::Current()->GetClassLinker(); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 1395 | if (!IsBootImage()) { |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1396 | constexpr bool kSaveDexInput = false; |
| 1397 | if (kSaveDexInput) { |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 1398 | SaveDexInput(); |
Brian Carlstrom | f79fccb | 2014-02-20 08:55:10 -0800 | [diff] [blame] | 1399 | } |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 1400 | |
| 1401 | // Handle and ClassLoader creation needs to come after Runtime::Create. |
| 1402 | ScopedObjectAccess soa(self); |
| 1403 | |
| 1404 | // Classpath: first the class-path given. |
| 1405 | std::vector<const DexFile*> class_path_files = MakeNonOwningPointerVector(class_path_files_); |
| 1406 | |
| 1407 | // Then the dex files we'll compile. Thus we'll resolve the class-path first. |
| 1408 | class_path_files.insert(class_path_files.end(), dex_files_.begin(), dex_files_.end()); |
| 1409 | |
| 1410 | class_loader_ = class_linker->CreatePathClassLoader(self, class_path_files); |
Brian Carlstrom | f79fccb | 2014-02-20 08:55:10 -0800 | [diff] [blame] | 1411 | } |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 1412 | |
| 1413 | // Ensure opened dex files are writable for dex-to-dex transformations. |
| 1414 | for (const std::unique_ptr<MemMap>& map : opened_dex_files_maps_) { |
| 1415 | if (!map->Protect(PROT_READ | PROT_WRITE)) { |
| 1416 | PLOG(ERROR) << "Failed to make .dex files writeable."; |
| 1417 | return false; |
Vladimir Marko | 919f553 | 2016-01-20 19:13:01 +0000 | [diff] [blame] | 1418 | } |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 1419 | } |
| 1420 | |
| 1421 | // Ensure that the dex caches stay live since we don't want class unloading |
| 1422 | // to occur during compilation. |
| 1423 | for (const auto& dex_file : dex_files_) { |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 1424 | ScopedObjectAccess soa(self); |
| 1425 | dex_caches_.push_back(soa.AddLocalReference<jobject>( |
Mathieu Chartier | d57d454 | 2015-10-14 10:55:30 -0700 | [diff] [blame] | 1426 | class_linker->RegisterDexFile(*dex_file, Runtime::Current()->GetLinearAlloc()))); |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 1427 | } |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1428 | |
| 1429 | /* |
| 1430 | * If we're not in interpret-only or verify-none mode, go ahead and compile small applications. |
| 1431 | * Don't bother to check if we're doing the image. |
| 1432 | */ |
Mathieu Chartier | a90c772 | 2015-10-29 15:41:36 -0700 | [diff] [blame] | 1433 | if (!IsBootImage() && |
Brian Carlstrom | 95b033b | 2014-12-03 22:29:37 -0800 | [diff] [blame] | 1434 | compiler_options_->IsCompilationEnabled() && |
| 1435 | compiler_kind_ == Compiler::kQuick) { |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1436 | size_t num_methods = 0; |
| 1437 | for (size_t i = 0; i != dex_files_.size(); ++i) { |
| 1438 | const DexFile* dex_file = dex_files_[i]; |
| 1439 | CHECK(dex_file != nullptr); |
| 1440 | num_methods += dex_file->NumMethodIds(); |
| 1441 | } |
| 1442 | if (num_methods <= compiler_options_->GetNumDexMethodsThreshold()) { |
| 1443 | compiler_options_->SetCompilerFilter(CompilerOptions::kSpeed); |
| 1444 | VLOG(compiler) << "Below method threshold, compiling anyways"; |
| 1445 | } |
| 1446 | } |
| 1447 | |
| 1448 | return true; |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 1449 | } |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 1450 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1451 | // If we need to keep the oat file open for the image writer. |
| 1452 | bool ShouldKeepOatFileOpen() const { |
| 1453 | return IsImage() && oat_fd_ != kInvalidFd; |
| 1454 | } |
| 1455 | |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1456 | // Create and invoke the compiler driver. This will compile all the dex files. |
| 1457 | void Compile() { |
| 1458 | TimingLogger::ScopedTiming t("dex2oat Compile", timings_); |
| 1459 | compiler_phases_timings_.reset(new CumulativeLogger("compilation times")); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1460 | |
Vladimir Marko | 47496c2 | 2016-01-27 16:15:08 +0000 | [diff] [blame] | 1461 | // Find the dex files we should not inline from. |
| 1462 | |
| 1463 | std::vector<std::string> no_inline_filters; |
| 1464 | Split(no_inline_from_string_, ',', &no_inline_filters); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1465 | |
| 1466 | // For now, on the host always have core-oj removed. |
Vladimir Marko | 47496c2 | 2016-01-27 16:15:08 +0000 | [diff] [blame] | 1467 | const std::string core_oj = "core-oj"; |
| 1468 | if (!kIsTargetBuild && !ContainsElement(no_inline_filters, core_oj)) { |
| 1469 | no_inline_filters.push_back(core_oj); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1470 | } |
| 1471 | |
Vladimir Marko | 47496c2 | 2016-01-27 16:15:08 +0000 | [diff] [blame] | 1472 | if (!no_inline_filters.empty()) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1473 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 1474 | std::vector<const DexFile*> class_path_files = MakeNonOwningPointerVector(class_path_files_); |
| 1475 | std::vector<const std::vector<const DexFile*>*> dex_file_vectors = { |
| 1476 | &class_linker->GetBootClassPath(), |
| 1477 | &class_path_files, |
| 1478 | &dex_files_ |
| 1479 | }; |
| 1480 | for (const std::vector<const DexFile*>* dex_file_vector : dex_file_vectors) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1481 | for (const DexFile* dex_file : *dex_file_vector) { |
Vladimir Marko | 47496c2 | 2016-01-27 16:15:08 +0000 | [diff] [blame] | 1482 | for (const std::string& filter : no_inline_filters) { |
| 1483 | // Use dex_file->GetLocation() rather than dex_file->GetBaseLocation(). This |
| 1484 | // allows tests to specify <test-dexfile>:classes2.dex if needed but if the |
| 1485 | // base location passes the StartsWith() test, so do all extra locations. |
| 1486 | std::string dex_location = dex_file->GetLocation(); |
| 1487 | if (filter.find('/') == std::string::npos) { |
| 1488 | // The filter does not contain the path. Remove the path from dex_location as well. |
| 1489 | size_t last_slash = dex_file->GetLocation().rfind('/'); |
| 1490 | if (last_slash != std::string::npos) { |
| 1491 | dex_location = dex_location.substr(last_slash + 1); |
| 1492 | } |
| 1493 | } |
| 1494 | |
| 1495 | if (StartsWith(dex_location, filter.c_str())) { |
| 1496 | VLOG(compiler) << "Disabling inlining from " << dex_file->GetLocation(); |
| 1497 | no_inline_from_dex_files_.push_back(dex_file); |
| 1498 | break; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1499 | } |
| 1500 | } |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1501 | } |
Vladimir Marko | 47496c2 | 2016-01-27 16:15:08 +0000 | [diff] [blame] | 1502 | } |
| 1503 | if (!no_inline_from_dex_files_.empty()) { |
| 1504 | compiler_options_->no_inline_from_ = &no_inline_from_dex_files_; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1505 | } |
| 1506 | } |
| 1507 | |
Vladimir Marko | 307dac9 | 2015-10-20 11:20:09 +0100 | [diff] [blame] | 1508 | driver_.reset(new CompilerDriver(compiler_options_.get(), |
| 1509 | verification_results_.get(), |
| 1510 | &method_inliner_map_, |
| 1511 | compiler_kind_, |
| 1512 | instruction_set_, |
| 1513 | instruction_set_features_.get(), |
Mathieu Chartier | a90c772 | 2015-10-29 15:41:36 -0700 | [diff] [blame] | 1514 | IsBootImage(), |
Vladimir Marko | 307dac9 | 2015-10-20 11:20:09 +0100 | [diff] [blame] | 1515 | image_classes_.release(), |
| 1516 | compiled_classes_.release(), |
| 1517 | nullptr, |
| 1518 | thread_count_, |
| 1519 | dump_stats_, |
| 1520 | dump_passes_, |
Vladimir Marko | 307dac9 | 2015-10-20 11:20:09 +0100 | [diff] [blame] | 1521 | compiler_phases_timings_.get(), |
| 1522 | swap_fd_, |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 1523 | &dex_file_oat_filename_map_, |
| 1524 | profile_compilation_info_.get())); |
Vladimir Marko | d1eaf0d | 2015-10-29 12:18:29 +0000 | [diff] [blame] | 1525 | driver_->SetDexFilesForOatFile(dex_files_); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 1526 | driver_->CompileAll(class_loader_, dex_files_, timings_); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1527 | } |
| 1528 | |
Jeff Hao | 436183f | 2016-01-12 16:36:50 -0800 | [diff] [blame] | 1529 | // Notes on the interleaving of creating the images and oat files to |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1530 | // ensure the references between the two are correct. |
| 1531 | // |
| 1532 | // Currently we have a memory layout that looks something like this: |
| 1533 | // |
| 1534 | // +--------------+ |
Jeff Hao | 436183f | 2016-01-12 16:36:50 -0800 | [diff] [blame] | 1535 | // | images | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1536 | // +--------------+ |
Jeff Hao | 436183f | 2016-01-12 16:36:50 -0800 | [diff] [blame] | 1537 | // | oat files | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1538 | // +--------------+ |
| 1539 | // | alloc spaces | |
| 1540 | // +--------------+ |
| 1541 | // |
Jeff Hao | 436183f | 2016-01-12 16:36:50 -0800 | [diff] [blame] | 1542 | // There are several constraints on the loading of the images and oat files. |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1543 | // |
Jeff Hao | 436183f | 2016-01-12 16:36:50 -0800 | [diff] [blame] | 1544 | // 1. The images are expected to be loaded at an absolute address and |
| 1545 | // contain Objects with absolute pointers within the images. |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1546 | // |
Jeff Hao | 436183f | 2016-01-12 16:36:50 -0800 | [diff] [blame] | 1547 | // 2. There are absolute pointers from Methods in the images to their |
| 1548 | // code in the oat files. |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1549 | // |
Jeff Hao | 436183f | 2016-01-12 16:36:50 -0800 | [diff] [blame] | 1550 | // 3. There are absolute pointers from the code in the oat files to Methods |
| 1551 | // in the images. |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1552 | // |
Jeff Hao | 436183f | 2016-01-12 16:36:50 -0800 | [diff] [blame] | 1553 | // 4. There are absolute pointers from code in the oat files to other code |
| 1554 | // in the oat files. |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1555 | // |
| 1556 | // To get this all correct, we go through several steps. |
| 1557 | // |
Jeff Hao | 436183f | 2016-01-12 16:36:50 -0800 | [diff] [blame] | 1558 | // 1. We prepare offsets for all data in the oat files and calculate |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1559 | // the oat data size and code size. During this stage, we also set |
| 1560 | // oat code offsets in methods for use by the image writer. |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1561 | // |
Jeff Hao | 436183f | 2016-01-12 16:36:50 -0800 | [diff] [blame] | 1562 | // 2. We prepare offsets for the objects in the images and calculate |
| 1563 | // the image sizes. |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1564 | // |
Jeff Hao | 436183f | 2016-01-12 16:36:50 -0800 | [diff] [blame] | 1565 | // 3. We create the oat files. Originally this was just our own proprietary |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1566 | // file but now it is contained within an ELF dynamic object (aka an .so |
Jeff Hao | 436183f | 2016-01-12 16:36:50 -0800 | [diff] [blame] | 1567 | // file). Since we know the image sizes and oat data sizes and code sizes we |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1568 | // can prepare the ELF headers and we then know the ELF memory segment |
| 1569 | // layout and we can now resolve all references. The compiler provides |
| 1570 | // LinkerPatch information in each CompiledMethod and we resolve these, |
| 1571 | // using the layout information and image object locations provided by |
| 1572 | // image writer, as we're writing the method code. |
| 1573 | // |
Jeff Hao | 436183f | 2016-01-12 16:36:50 -0800 | [diff] [blame] | 1574 | // 4. We create the image files. They need to know where the oat files |
| 1575 | // will be loaded after itself. Originally oat files were simply |
| 1576 | // memory mapped so we could predict where their contents were based |
| 1577 | // on the file size. Now that they are ELF files, we need to inspect |
| 1578 | // the ELF files to understand the in memory segment layout including |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1579 | // where the oat header is located within. |
| 1580 | // TODO: We could just remember this information from step 3. |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1581 | // |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1582 | // 5. We fixup the ELF program headers so that dlopen will try to |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1583 | // load the .so at the desired location at runtime by offsetting the |
| 1584 | // Elf32_Phdr.p_vaddr values by the desired base address. |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1585 | // TODO: Do this in step 3. We already know the layout there. |
| 1586 | // |
| 1587 | // Steps 1.-3. are done by the CreateOatFile() above, steps 4.-5. |
| 1588 | // are done by the CreateImageFile() below. |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1589 | |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1590 | // Write out the generated code part. Calls the OatWriter and ElfBuilder. Also prepares the |
| 1591 | // ImageWriter, if necessary. |
Andreas Gampe | 10e477d | 2014-11-19 12:57:42 -0800 | [diff] [blame] | 1592 | // Note: Flushing (and closing) the file is the caller's responsibility, except for the failure |
| 1593 | // case (when the file will be explicitly erased). |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 1594 | bool WriteOatFiles() { |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1595 | TimingLogger::ScopedTiming t("dex2oat Oat", timings_); |
| 1596 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 1597 | // Sync the data to the file, in case we did dex2dex transformations. |
| 1598 | for (const std::unique_ptr<MemMap>& map : opened_dex_files_maps_) { |
| 1599 | if (!map->Sync()) { |
| 1600 | PLOG(ERROR) << "Failed to Sync() dex2dex output. Map: " << map->GetName(); |
| 1601 | return false; |
| 1602 | } |
| 1603 | } |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1604 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 1605 | if (IsImage()) { |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1606 | if (app_image_ && image_base_ == 0) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1607 | gc::Heap* const heap = Runtime::Current()->GetHeap(); |
| 1608 | for (gc::space::ImageSpace* image_space : heap->GetBootImageSpaces()) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1609 | image_base_ = std::max(image_base_, RoundUp( |
| 1610 | reinterpret_cast<uintptr_t>(image_space->GetImageHeader().GetOatFileEnd()), |
| 1611 | kPageSize)); |
| 1612 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1613 | // The non moving space is right after the oat file. Put the preferred app image location |
| 1614 | // right after the non moving space so that we ideally get a continuous immune region for |
| 1615 | // the GC. |
| 1616 | const size_t non_moving_space_capacity = heap->GetNonMovingSpace()->Capacity(); |
| 1617 | image_base_ += non_moving_space_capacity; |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1618 | VLOG(compiler) << "App image base=" << reinterpret_cast<void*>(image_base_); |
| 1619 | } |
| 1620 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 1621 | image_writer_.reset(new ImageWriter(*driver_, |
| 1622 | image_base_, |
| 1623 | compiler_options_->GetCompilePic(), |
| 1624 | IsAppImage(), |
| 1625 | image_storage_mode_, |
| 1626 | oat_filenames_, |
| 1627 | dex_file_oat_filename_map_)); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1628 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 1629 | // We need to prepare method offsets in the image address space for direct method patching. |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1630 | TimingLogger::ScopedTiming t2("dex2oat Prepare image address space", timings_); |
| 1631 | if (!image_writer_->PrepareImageAddressSpace()) { |
| 1632 | LOG(ERROR) << "Failed to prepare image address space."; |
| 1633 | return false; |
| 1634 | } |
| 1635 | } |
| 1636 | |
| 1637 | { |
| 1638 | TimingLogger::ScopedTiming t2("dex2oat Write ELF", timings_); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 1639 | for (size_t i = 0, size = oat_files_.size(); i != size; ++i) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1640 | std::unique_ptr<File>& oat_file = oat_files_[i]; |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 1641 | std::unique_ptr<ElfWriter>& elf_writer = elf_writers_[i]; |
| 1642 | std::unique_ptr<OatWriter>& oat_writer = oat_writers_[i]; |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 1643 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 1644 | std::vector<const DexFile*>& dex_files = dex_files_per_oat_file_[i]; |
| 1645 | oat_writer->PrepareLayout(driver_.get(), image_writer_.get(), dex_files); |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 1646 | |
David Srbecky | 0c4572e | 2016-01-22 19:19:25 +0000 | [diff] [blame] | 1647 | // We need to mirror the layout of the ELF file in the compressed debug-info. |
| 1648 | // Therefore we need to propagate the sizes of at least those sections. |
| 1649 | size_t rodata_size = oat_writer->GetOatHeader().GetExecutableOffset(); |
| 1650 | size_t text_size = oat_writer->GetSize() - rodata_size; |
| 1651 | elf_writer->PrepareDebugInfo(rodata_size, text_size, oat_writer->GetMethodDebugInfo()); |
| 1652 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 1653 | OutputStream*& rodata = rodata_[i]; |
| 1654 | DCHECK(rodata != nullptr); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1655 | if (!oat_writer->WriteRodata(rodata)) { |
| 1656 | LOG(ERROR) << "Failed to write .rodata section to the ELF file " << oat_file->GetPath(); |
| 1657 | return false; |
| 1658 | } |
| 1659 | elf_writer->EndRoData(rodata); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 1660 | rodata = nullptr; |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 1661 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1662 | OutputStream* text = elf_writer->StartText(); |
| 1663 | if (!oat_writer->WriteCode(text)) { |
| 1664 | LOG(ERROR) << "Failed to write .text section to the ELF file " << oat_file->GetPath(); |
| 1665 | return false; |
| 1666 | } |
| 1667 | elf_writer->EndText(text); |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 1668 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 1669 | if (!oat_writer->WriteHeader(elf_writer->GetStream(), |
| 1670 | image_file_location_oat_checksum_, |
| 1671 | image_file_location_oat_data_begin_, |
| 1672 | image_patch_delta_)) { |
| 1673 | LOG(ERROR) << "Failed to write oat header to the ELF file " << oat_file->GetPath(); |
| 1674 | return false; |
| 1675 | } |
| 1676 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1677 | elf_writer->SetBssSize(oat_writer->GetBssSize()); |
| 1678 | elf_writer->WriteDynamicSection(); |
| 1679 | elf_writer->WriteDebugInfo(oat_writer->GetMethodDebugInfo()); |
| 1680 | elf_writer->WritePatchLocations(oat_writer->GetAbsolutePatchLocations()); |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 1681 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1682 | if (!elf_writer->End()) { |
| 1683 | LOG(ERROR) << "Failed to write ELF file " << oat_file->GetPath(); |
| 1684 | return false; |
| 1685 | } |
| 1686 | |
| 1687 | // Flush the oat file. |
| 1688 | if (oat_files_[i] != nullptr) { |
| 1689 | if (oat_files_[i]->Flush() != 0) { |
| 1690 | PLOG(ERROR) << "Failed to flush oat file: " << oat_filenames_[i]; |
| 1691 | oat_files_[i]->Erase(); |
| 1692 | return false; |
| 1693 | } |
| 1694 | } |
| 1695 | |
| 1696 | if (IsImage()) { |
| 1697 | // Update oat estimates. |
Vladimir Marko | d8904a5 | 2016-01-29 16:27:27 +0000 | [diff] [blame] | 1698 | DCHECK(image_writer_ != nullptr); |
| 1699 | DCHECK_LT(i, oat_filenames_.size()); |
| 1700 | |
| 1701 | image_writer_->UpdateOatFile(oat_file.get(), oat_filenames_[i]); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1702 | } |
| 1703 | |
| 1704 | VLOG(compiler) << "Oat file written successfully: " << oat_filenames_[i]; |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 1705 | |
| 1706 | oat_writer.reset(); |
| 1707 | elf_writer.reset(); |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1708 | } |
| 1709 | } |
| 1710 | |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1711 | return true; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1712 | } |
| 1713 | |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1714 | // If we are compiling an image, invoke the image creation routine. Else just skip. |
| 1715 | bool HandleImage() { |
Mathieu Chartier | a90c772 | 2015-10-29 15:41:36 -0700 | [diff] [blame] | 1716 | if (IsImage()) { |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1717 | TimingLogger::ScopedTiming t("dex2oat ImageWriter", timings_); |
| 1718 | if (!CreateImageFile()) { |
| 1719 | return false; |
| 1720 | } |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1721 | VLOG(compiler) << "Images written successfully"; |
Brian Carlstrom | 4560248 | 2013-07-21 22:07:55 -0700 | [diff] [blame] | 1722 | } |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1723 | return true; |
| 1724 | } |
| 1725 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1726 | // Create a copy from stripped to unstripped. |
| 1727 | bool CopyStrippedToUnstripped() { |
| 1728 | for (size_t i = 0; i < oat_unstripped_.size(); ++i) { |
| 1729 | // If we don't want to strip in place, copy from stripped location to unstripped location. |
| 1730 | // We need to strip after image creation because FixupElf needs to use .strtab. |
| 1731 | if (strcmp(oat_unstripped_[i], oat_filenames_[i]) != 0) { |
| 1732 | // If the oat file is still open, flush it. |
| 1733 | if (oat_files_[i].get() != nullptr && oat_files_[i]->IsOpened()) { |
| 1734 | if (!FlushCloseOatFile(i)) { |
| 1735 | return false; |
| 1736 | } |
| 1737 | } |
| 1738 | |
| 1739 | TimingLogger::ScopedTiming t("dex2oat OatFile copy", timings_); |
| 1740 | std::unique_ptr<File> in(OS::OpenFileForReading(oat_filenames_[i])); |
| 1741 | std::unique_ptr<File> out(OS::CreateEmptyFile(oat_unstripped_[i])); |
| 1742 | size_t buffer_size = 8192; |
| 1743 | std::unique_ptr<uint8_t[]> buffer(new uint8_t[buffer_size]); |
| 1744 | while (true) { |
| 1745 | int bytes_read = TEMP_FAILURE_RETRY(read(in->Fd(), buffer.get(), buffer_size)); |
| 1746 | if (bytes_read <= 0) { |
| 1747 | break; |
| 1748 | } |
| 1749 | bool write_ok = out->WriteFully(buffer.get(), bytes_read); |
| 1750 | CHECK(write_ok); |
| 1751 | } |
| 1752 | if (out->FlushCloseOrErase() != 0) { |
| 1753 | PLOG(ERROR) << "Failed to flush and close copied oat file: " << oat_unstripped_[i]; |
| 1754 | return false; |
| 1755 | } |
| 1756 | VLOG(compiler) << "Oat file copied successfully (unstripped): " << oat_unstripped_[i]; |
| 1757 | } |
| 1758 | } |
| 1759 | return true; |
| 1760 | } |
| 1761 | |
| 1762 | bool FlushOatFiles() { |
| 1763 | TimingLogger::ScopedTiming t2("dex2oat Flush ELF", timings_); |
| 1764 | for (size_t i = 0; i < oat_files_.size(); ++i) { |
| 1765 | if (oat_files_[i].get() != nullptr) { |
| 1766 | if (oat_files_[i]->Flush() != 0) { |
| 1767 | PLOG(ERROR) << "Failed to flush oat file: " << oat_filenames_[i]; |
| 1768 | oat_files_[i]->Erase(); |
Andreas Gampe | 10e477d | 2014-11-19 12:57:42 -0800 | [diff] [blame] | 1769 | return false; |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame] | 1770 | } |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame] | 1771 | } |
Nicolas Geoffray | ea3fa0b | 2014-02-10 11:59:41 +0000 | [diff] [blame] | 1772 | } |
Andreas Gampe | 10e477d | 2014-11-19 12:57:42 -0800 | [diff] [blame] | 1773 | return true; |
| 1774 | } |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1775 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1776 | bool FlushCloseOatFile(size_t i) { |
| 1777 | if (oat_files_[i].get() != nullptr) { |
| 1778 | std::unique_ptr<File> tmp(oat_files_[i].release()); |
Andreas Gampe | 10e477d | 2014-11-19 12:57:42 -0800 | [diff] [blame] | 1779 | if (tmp->FlushCloseOrErase() != 0) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1780 | PLOG(ERROR) << "Failed to flush and close oat file: " << oat_filenames_[i]; |
Andreas Gampe | 10e477d | 2014-11-19 12:57:42 -0800 | [diff] [blame] | 1781 | return false; |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame] | 1782 | } |
| 1783 | } |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1784 | return true; |
| 1785 | } |
| 1786 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1787 | bool FlushCloseOatFiles() { |
| 1788 | bool result = true; |
| 1789 | for (size_t i = 0; i < oat_files_.size(); ++i) { |
| 1790 | result &= FlushCloseOatFile(i); |
| 1791 | } |
| 1792 | return result; |
| 1793 | } |
| 1794 | |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1795 | void DumpTiming() { |
| 1796 | if (dump_timing_ || (dump_slow_timing_ && timings_->GetTotalNs() > MsToNs(1000))) { |
| 1797 | LOG(INFO) << Dumpable<TimingLogger>(*timings_); |
| 1798 | } |
| 1799 | if (dump_passes_) { |
| 1800 | LOG(INFO) << Dumpable<CumulativeLogger>(*driver_->GetTimingsLogger()); |
| 1801 | } |
| 1802 | } |
| 1803 | |
| 1804 | CompilerOptions* GetCompilerOptions() const { |
| 1805 | return compiler_options_.get(); |
| 1806 | } |
| 1807 | |
Andreas Gampe | 10e477d | 2014-11-19 12:57:42 -0800 | [diff] [blame] | 1808 | bool IsImage() const { |
Mathieu Chartier | a90c772 | 2015-10-29 15:41:36 -0700 | [diff] [blame] | 1809 | return IsAppImage() || IsBootImage(); |
| 1810 | } |
| 1811 | |
| 1812 | bool IsAppImage() const { |
| 1813 | return app_image_; |
| 1814 | } |
| 1815 | |
| 1816 | bool IsBootImage() const { |
| 1817 | return boot_image_; |
Andreas Gampe | 10e477d | 2014-11-19 12:57:42 -0800 | [diff] [blame] | 1818 | } |
| 1819 | |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1820 | bool IsHost() const { |
| 1821 | return is_host_; |
| 1822 | } |
| 1823 | |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 1824 | bool UseProfileGuidedCompilation() const { |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 1825 | return !profile_file_.empty() || (profile_file_fd_ != kInvalidFd); |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 1826 | } |
| 1827 | |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 1828 | bool LoadProfile() { |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 1829 | DCHECK(UseProfileGuidedCompilation()); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 1830 | |
| 1831 | profile_compilation_info_.reset(new ProfileCompilationInfo()); |
| 1832 | ScopedFlock flock; |
| 1833 | bool success = false; |
| 1834 | std::string error; |
| 1835 | if (profile_file_fd_ != -1) { |
| 1836 | // The file doesn't need to be flushed so don't check the usage. |
| 1837 | // Pass a bogus path so that we can easily attribute any reported error. |
| 1838 | File file(profile_file_fd_, "profile", /*check_usage*/ false, /*read_only_mode*/ true); |
| 1839 | if (flock.Init(&file, &error)) { |
| 1840 | success = profile_compilation_info_->Load(profile_file_fd_); |
| 1841 | } |
Calin Juravle | 877fd96 | 2016-01-05 14:29:29 +0000 | [diff] [blame] | 1842 | } else { |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 1843 | if (flock.Init(profile_file_.c_str(), O_RDONLY, /* block */ true, &error)) { |
| 1844 | success = profile_compilation_info_->Load(flock.GetFile()->Fd()); |
| 1845 | } |
| 1846 | } |
| 1847 | if (!error.empty()) { |
| 1848 | LOG(WARNING) << "Cannot lock profiles: " << error; |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 1849 | } |
Calin Juravle | 877fd96 | 2016-01-05 14:29:29 +0000 | [diff] [blame] | 1850 | |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 1851 | if (!success) { |
| 1852 | profile_compilation_info_.reset(nullptr); |
| 1853 | } |
Calin Juravle | 877fd96 | 2016-01-05 14:29:29 +0000 | [diff] [blame] | 1854 | |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 1855 | return success; |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 1856 | } |
| 1857 | |
| 1858 | bool ShouldCompileBasedOnProfiles() const { |
| 1859 | DCHECK(UseProfileGuidedCompilation()); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 1860 | // If we are given a profile, compile only if we have some data in it. |
| 1861 | return (profile_compilation_info_ != nullptr) && |
| 1862 | (profile_compilation_info_->GetNumberOfMethods() != 0); |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 1863 | } |
| 1864 | |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1865 | private: |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 1866 | template <typename T> |
| 1867 | static std::vector<T*> MakeNonOwningPointerVector(const std::vector<std::unique_ptr<T>>& src) { |
| 1868 | std::vector<T*> result; |
| 1869 | result.reserve(src.size()); |
| 1870 | for (const std::unique_ptr<T>& t : src) { |
| 1871 | result.push_back(t.get()); |
| 1872 | } |
| 1873 | return result; |
| 1874 | } |
| 1875 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 1876 | std::string GetMultiImageBootClassPath() { |
| 1877 | DCHECK(IsBootImage()); |
| 1878 | DCHECK_GT(oat_filenames_.size(), 1u); |
| 1879 | // If the image filename was adapted (e.g., for our tests), we need to change this here, |
| 1880 | // too, but need to strip all path components (they will be re-established when loading). |
| 1881 | std::ostringstream bootcp_oss; |
| 1882 | bool first_bootcp = true; |
| 1883 | for (size_t i = 0; i < dex_locations_.size(); ++i) { |
| 1884 | if (!first_bootcp) { |
| 1885 | bootcp_oss << ":"; |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1886 | } |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 1887 | |
| 1888 | std::string dex_loc = dex_locations_[i]; |
| 1889 | std::string image_filename = image_filenames_[i]; |
| 1890 | |
| 1891 | // Use the dex_loc path, but the image_filename name (without path elements). |
| 1892 | size_t dex_last_slash = dex_loc.rfind('/'); |
| 1893 | |
| 1894 | // npos is max(size_t). That makes this a bit ugly. |
| 1895 | size_t image_last_slash = image_filename.rfind('/'); |
| 1896 | size_t image_last_at = image_filename.rfind('@'); |
| 1897 | size_t image_last_sep = (image_last_slash == std::string::npos) |
| 1898 | ? image_last_at |
| 1899 | : (image_last_at == std::string::npos) |
| 1900 | ? std::string::npos |
| 1901 | : std::max(image_last_slash, image_last_at); |
| 1902 | // Note: whenever image_last_sep == npos, +1 overflow means using the full string. |
| 1903 | |
| 1904 | if (dex_last_slash == std::string::npos) { |
| 1905 | dex_loc = image_filename.substr(image_last_sep + 1); |
| 1906 | } else { |
| 1907 | dex_loc = dex_loc.substr(0, dex_last_slash + 1) + |
| 1908 | image_filename.substr(image_last_sep + 1); |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1909 | } |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 1910 | |
| 1911 | // Image filenames already end with .art, no need to replace. |
| 1912 | |
| 1913 | bootcp_oss << dex_loc; |
| 1914 | first_bootcp = false; |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1915 | } |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 1916 | return bootcp_oss.str(); |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1917 | } |
| 1918 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 1919 | std::vector<std::string> GetClassPathLocations(const std::string& class_path) { |
| 1920 | // This function is used only for apps and for an app we have exactly one oat file. |
| 1921 | DCHECK(!IsBootImage()); |
| 1922 | DCHECK_EQ(oat_writers_.size(), 1u); |
| 1923 | std::vector<std::string> dex_files_canonical_locations; |
| 1924 | for (const char* location : oat_writers_[0]->GetSourceLocations()) { |
| 1925 | dex_files_canonical_locations.push_back(DexFile::GetDexCanonicalLocation(location)); |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1926 | } |
Vladimir Marko | 625a64a | 2015-11-26 14:44:16 +0000 | [diff] [blame] | 1927 | |
Vladimir Marko | 919f553 | 2016-01-20 19:13:01 +0000 | [diff] [blame] | 1928 | std::vector<std::string> parsed; |
| 1929 | Split(class_path, ':', &parsed); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 1930 | auto kept_it = std::remove_if(parsed.begin(), |
| 1931 | parsed.end(), |
| 1932 | [dex_files_canonical_locations](const std::string& location) { |
| 1933 | return ContainsElement(dex_files_canonical_locations, |
| 1934 | DexFile::GetDexCanonicalLocation(location.c_str())); |
| 1935 | }); |
| 1936 | parsed.erase(kept_it, parsed.end()); |
| 1937 | return parsed; |
| 1938 | } |
| 1939 | |
| 1940 | // Opens requested class path files and appends them to opened_dex_files. |
| 1941 | static void OpenClassPathFiles(const std::vector<std::string>& class_path_locations, |
| 1942 | std::vector<std::unique_ptr<const DexFile>>* opened_dex_files) { |
| 1943 | DCHECK(opened_dex_files != nullptr) << "OpenClassPathFiles out-param is nullptr"; |
| 1944 | for (const std::string& location : class_path_locations) { |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1945 | std::string error_msg; |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 1946 | if (!DexFile::Open(location.c_str(), location.c_str(), &error_msg, opened_dex_files)) { |
| 1947 | LOG(WARNING) << "Failed to open dex file '" << location << "': " << error_msg; |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1948 | } |
| 1949 | } |
| 1950 | } |
| 1951 | |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 1952 | bool PrepareImageClasses() { |
| 1953 | // If --image-classes was specified, calculate the full list of classes to include in the image. |
| 1954 | if (image_classes_filename_ != nullptr) { |
| 1955 | image_classes_ = |
| 1956 | ReadClasses(image_classes_zip_filename_, image_classes_filename_, "image"); |
| 1957 | if (image_classes_ == nullptr) { |
| 1958 | return false; |
| 1959 | } |
| 1960 | } else if (IsBootImage()) { |
| 1961 | image_classes_.reset(new std::unordered_set<std::string>); |
| 1962 | } |
| 1963 | return true; |
| 1964 | } |
| 1965 | |
| 1966 | bool PrepareCompiledClasses() { |
| 1967 | // If --compiled-classes was specified, calculate the full list of classes to compile in the |
| 1968 | // image. |
| 1969 | if (compiled_classes_filename_ != nullptr) { |
| 1970 | compiled_classes_ = |
| 1971 | ReadClasses(compiled_classes_zip_filename_, compiled_classes_filename_, "compiled"); |
| 1972 | if (compiled_classes_ == nullptr) { |
| 1973 | return false; |
| 1974 | } |
| 1975 | } else { |
| 1976 | compiled_classes_.reset(nullptr); // By default compile everything. |
| 1977 | } |
| 1978 | return true; |
| 1979 | } |
| 1980 | |
| 1981 | static std::unique_ptr<std::unordered_set<std::string>> ReadClasses(const char* zip_filename, |
| 1982 | const char* classes_filename, |
| 1983 | const char* tag) { |
| 1984 | std::unique_ptr<std::unordered_set<std::string>> classes; |
| 1985 | std::string error_msg; |
| 1986 | if (zip_filename != nullptr) { |
| 1987 | classes.reset(ReadImageClassesFromZip(zip_filename, classes_filename, &error_msg)); |
| 1988 | } else { |
| 1989 | classes.reset(ReadImageClassesFromFile(classes_filename)); |
| 1990 | } |
| 1991 | if (classes == nullptr) { |
| 1992 | LOG(ERROR) << "Failed to create list of " << tag << " classes from '" |
| 1993 | << classes_filename << "': " << error_msg; |
| 1994 | } |
| 1995 | return classes; |
| 1996 | } |
| 1997 | |
| 1998 | bool PrepareCompiledMethods() { |
| 1999 | // If --compiled-methods was specified, read the methods to compile from the given file(s). |
| 2000 | if (compiled_methods_filename_ != nullptr) { |
| 2001 | std::string error_msg; |
| 2002 | if (compiled_methods_zip_filename_ != nullptr) { |
| 2003 | compiled_methods_.reset(ReadCommentedInputFromZip(compiled_methods_zip_filename_, |
| 2004 | compiled_methods_filename_, |
| 2005 | nullptr, // No post-processing. |
| 2006 | &error_msg)); |
| 2007 | } else { |
| 2008 | compiled_methods_.reset(ReadCommentedInputFromFile(compiled_methods_filename_, |
| 2009 | nullptr)); // No post-processing. |
| 2010 | } |
| 2011 | if (compiled_methods_.get() == nullptr) { |
| 2012 | LOG(ERROR) << "Failed to create list of compiled methods from '" |
| 2013 | << compiled_methods_filename_ << "': " << error_msg; |
| 2014 | return false; |
| 2015 | } |
| 2016 | } else { |
| 2017 | compiled_methods_.reset(nullptr); // By default compile everything. |
| 2018 | } |
| 2019 | return true; |
| 2020 | } |
| 2021 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2022 | void PruneNonExistentDexFiles() { |
| 2023 | DCHECK_EQ(dex_filenames_.size(), dex_locations_.size()); |
| 2024 | size_t kept = 0u; |
| 2025 | for (size_t i = 0, size = dex_filenames_.size(); i != size; ++i) { |
| 2026 | if (!OS::FileExists(dex_filenames_[i])) { |
| 2027 | LOG(WARNING) << "Skipping non-existent dex file '" << dex_filenames_[i] << "'"; |
| 2028 | } else { |
| 2029 | dex_filenames_[kept] = dex_filenames_[i]; |
| 2030 | dex_locations_[kept] = dex_locations_[i]; |
| 2031 | ++kept; |
| 2032 | } |
| 2033 | } |
| 2034 | dex_filenames_.resize(kept); |
| 2035 | dex_locations_.resize(kept); |
| 2036 | } |
| 2037 | |
| 2038 | bool AddDexFileSources() { |
| 2039 | TimingLogger::ScopedTiming t2("AddDexFileSources", timings_); |
| 2040 | if (zip_fd_ != -1) { |
| 2041 | DCHECK_EQ(oat_writers_.size(), 1u); |
| 2042 | if (!oat_writers_[0]->AddZippedDexFilesSource(ScopedFd(zip_fd_), zip_location_.c_str())) { |
| 2043 | return false; |
| 2044 | } |
| 2045 | } else if (oat_writers_.size() > 1u) { |
| 2046 | // Multi-image. |
| 2047 | DCHECK_EQ(oat_writers_.size(), dex_filenames_.size()); |
| 2048 | DCHECK_EQ(oat_writers_.size(), dex_locations_.size()); |
| 2049 | for (size_t i = 0, size = oat_writers_.size(); i != size; ++i) { |
| 2050 | if (!oat_writers_[i]->AddDexFileSource(dex_filenames_[i], dex_locations_[i])) { |
| 2051 | return false; |
| 2052 | } |
| 2053 | } |
| 2054 | } else { |
| 2055 | DCHECK_EQ(oat_writers_.size(), 1u); |
| 2056 | DCHECK_EQ(dex_filenames_.size(), dex_locations_.size()); |
| 2057 | DCHECK_NE(dex_filenames_.size(), 0u); |
| 2058 | for (size_t i = 0; i != dex_filenames_.size(); ++i) { |
| 2059 | if (!oat_writers_[0]->AddDexFileSource(dex_filenames_[i], dex_locations_[i])) { |
| 2060 | return false; |
| 2061 | } |
| 2062 | } |
| 2063 | } |
| 2064 | return true; |
| 2065 | } |
| 2066 | |
| 2067 | void CreateOatWriters() { |
| 2068 | TimingLogger::ScopedTiming t2("CreateOatWriters", timings_); |
| 2069 | elf_writers_.reserve(oat_files_.size()); |
| 2070 | oat_writers_.reserve(oat_files_.size()); |
| 2071 | for (const std::unique_ptr<File>& oat_file : oat_files_) { |
| 2072 | elf_writers_.emplace_back( |
| 2073 | CreateElfWriterQuick(instruction_set_, compiler_options_.get(), oat_file.get())); |
| 2074 | elf_writers_.back()->Start(); |
| 2075 | oat_writers_.emplace_back(new OatWriter(IsBootImage(), timings_)); |
| 2076 | } |
| 2077 | } |
| 2078 | |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 2079 | void SaveDexInput() { |
| 2080 | for (size_t i = 0; i < dex_files_.size(); ++i) { |
| 2081 | const DexFile* dex_file = dex_files_[i]; |
| 2082 | std::string tmp_file_name(StringPrintf("/data/local/tmp/dex2oat.%d.%zd.dex", |
| 2083 | getpid(), i)); |
| 2084 | std::unique_ptr<File> tmp_file(OS::CreateEmptyFile(tmp_file_name.c_str())); |
| 2085 | if (tmp_file.get() == nullptr) { |
| 2086 | PLOG(ERROR) << "Failed to open file " << tmp_file_name |
| 2087 | << ". Try: adb shell chmod 777 /data/local/tmp"; |
| 2088 | continue; |
| 2089 | } |
| 2090 | // This is just dumping files for debugging. Ignore errors, and leave remnants. |
| 2091 | UNUSED(tmp_file->WriteFully(dex_file->Begin(), dex_file->Size())); |
| 2092 | UNUSED(tmp_file->Flush()); |
| 2093 | UNUSED(tmp_file->Close()); |
| 2094 | LOG(INFO) << "Wrote input to " << tmp_file_name; |
| 2095 | } |
| 2096 | } |
| 2097 | |
| 2098 | bool PrepareRuntimeOptions(RuntimeArgumentMap* runtime_options) { |
| 2099 | RuntimeOptions raw_options; |
| 2100 | if (boot_image_filename_.empty()) { |
| 2101 | std::string boot_class_path = "-Xbootclasspath:"; |
| 2102 | boot_class_path += Join(dex_filenames_, ':'); |
| 2103 | raw_options.push_back(std::make_pair(boot_class_path, nullptr)); |
| 2104 | std::string boot_class_path_locations = "-Xbootclasspath-locations:"; |
| 2105 | boot_class_path_locations += Join(dex_locations_, ':'); |
| 2106 | raw_options.push_back(std::make_pair(boot_class_path_locations, nullptr)); |
| 2107 | } else { |
| 2108 | std::string boot_image_option = "-Ximage:"; |
| 2109 | boot_image_option += boot_image_filename_; |
| 2110 | raw_options.push_back(std::make_pair(boot_image_option, nullptr)); |
| 2111 | } |
| 2112 | for (size_t i = 0; i < runtime_args_.size(); i++) { |
| 2113 | raw_options.push_back(std::make_pair(runtime_args_[i], nullptr)); |
| 2114 | } |
| 2115 | |
| 2116 | raw_options.push_back(std::make_pair("compilercallbacks", callbacks_.get())); |
| 2117 | raw_options.push_back( |
| 2118 | std::make_pair("imageinstructionset", GetInstructionSetString(instruction_set_))); |
| 2119 | |
| 2120 | // Only allow no boot image for the runtime if we're compiling one. When we compile an app, |
| 2121 | // we don't want fallback mode, it will abort as we do not push a boot classpath (it might |
| 2122 | // have been stripped in preopting, anyways). |
| 2123 | if (!IsBootImage()) { |
| 2124 | raw_options.push_back(std::make_pair("-Xno-dex-file-fallback", nullptr)); |
| 2125 | } |
| 2126 | // Disable libsigchain. We don't don't need it during compilation and it prevents us |
| 2127 | // from getting a statically linked version of dex2oat (because of dlsym and RTLD_NEXT). |
| 2128 | raw_options.push_back(std::make_pair("-Xno-sig-chain", nullptr)); |
Lin Zang | 97f3f7d | 2016-01-13 10:25:00 +0800 | [diff] [blame] | 2129 | // Disable Hspace compaction to save heap size virtual space. |
| 2130 | // Only need disable Hspace for OOM becasue background collector is equal to |
| 2131 | // foreground collector by default for dex2oat. |
| 2132 | raw_options.push_back(std::make_pair("-XX:DisableHSpaceCompactForOOM", nullptr)); |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 2133 | |
Andreas Gampe | ace0dc1 | 2016-01-20 13:33:13 -0800 | [diff] [blame] | 2134 | // If we're asked to be deterministic, ensure non-concurrent GC for determinism. Also |
| 2135 | // force the free-list implementation for large objects. |
| 2136 | if (compiler_options_->IsForceDeterminism()) { |
| 2137 | raw_options.push_back(std::make_pair("-Xgc:nonconcurrent", nullptr)); |
| 2138 | raw_options.push_back(std::make_pair("-XX:LargeObjectSpace=freelist", nullptr)); |
| 2139 | |
| 2140 | // We also need to turn off the nonmoving space. For that, we need to disable HSpace |
| 2141 | // compaction (done above) and ensure that neither foreground nor background collectors |
| 2142 | // are concurrent. |
| 2143 | raw_options.push_back(std::make_pair("-XX:BackgroundGC=nonconcurrent", nullptr)); |
| 2144 | |
| 2145 | // To make identity hashcode deterministic, set a known seed. |
| 2146 | mirror::Object::SetHashCodeSeed(987654321U); |
| 2147 | } |
| 2148 | |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 2149 | if (!Runtime::ParseOptions(raw_options, false, runtime_options)) { |
| 2150 | LOG(ERROR) << "Failed to parse runtime options"; |
| 2151 | return false; |
| 2152 | } |
| 2153 | return true; |
| 2154 | } |
| 2155 | |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2156 | // Create a runtime necessary for compilation. |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2157 | bool CreateRuntime(RuntimeArgumentMap&& runtime_options) { |
| 2158 | TimingLogger::ScopedTiming t_runtime("Create runtime", timings_); |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 2159 | if (!Runtime::Create(std::move(runtime_options))) { |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2160 | LOG(ERROR) << "Failed to create runtime"; |
| 2161 | return false; |
| 2162 | } |
Vladimir Marko | 307dac9 | 2015-10-20 11:20:09 +0100 | [diff] [blame] | 2163 | runtime_.reset(Runtime::Current()); |
| 2164 | runtime_->SetInstructionSet(instruction_set_); |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2165 | for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) { |
| 2166 | Runtime::CalleeSaveType type = Runtime::CalleeSaveType(i); |
Vladimir Marko | 307dac9 | 2015-10-20 11:20:09 +0100 | [diff] [blame] | 2167 | if (!runtime_->HasCalleeSaveMethod(type)) { |
| 2168 | runtime_->SetCalleeSaveMethod(runtime_->CreateCalleeSaveMethod(), type); |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2169 | } |
| 2170 | } |
Vladimir Marko | 307dac9 | 2015-10-20 11:20:09 +0100 | [diff] [blame] | 2171 | runtime_->GetClassLinker()->FixupDexCaches(runtime_->GetResolutionMethod()); |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 2172 | |
| 2173 | // Initialize maps for unstarted runtime. This needs to be here, as running clinits needs this |
| 2174 | // set up. |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 2175 | interpreter::UnstartedRuntime::Initialize(); |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 2176 | |
Vladimir Marko | 307dac9 | 2015-10-20 11:20:09 +0100 | [diff] [blame] | 2177 | runtime_->GetClassLinker()->RunRootClinits(); |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 2178 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2179 | // Runtime::Create acquired the mutator_lock_ that is normally given away when we |
| 2180 | // Runtime::Start, give it away now so that we don't starve GC. |
| 2181 | Thread* self = Thread::Current(); |
| 2182 | self->TransitionFromRunnableToSuspended(kNative); |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2183 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2184 | return true; |
Vladimir Marko | 919f553 | 2016-01-20 19:13:01 +0000 | [diff] [blame] | 2185 | } |
| 2186 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2187 | // Let the ImageWriter write the image files. If we do not compile PIC, also fix up the oat files. |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2188 | bool CreateImageFile() |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 2189 | REQUIRES(!Locks::mutator_lock_) { |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2190 | CHECK(image_writer_ != nullptr); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2191 | if (!IsBootImage()) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 2192 | CHECK(image_filenames_.empty()); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2193 | image_filenames_.push_back(app_image_file_name_.c_str()); |
| 2194 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 2195 | if (!image_writer_->Write(app_image_fd_, |
| 2196 | image_filenames_, |
| 2197 | oat_fd_, |
| 2198 | oat_filenames_, |
| 2199 | oat_location_)) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2200 | LOG(ERROR) << "Failure during image file creation"; |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2201 | return false; |
| 2202 | } |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2203 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2204 | // We need the OatDataBegin entries. |
| 2205 | std::map<const char*, uintptr_t> oat_data_begins; |
| 2206 | for (const char* oat_filename : oat_filenames_) { |
| 2207 | oat_data_begins.emplace(oat_filename, image_writer_->GetOatDataBegin(oat_filename)); |
| 2208 | } |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2209 | // Destroy ImageWriter before doing FixupElf. |
| 2210 | image_writer_.reset(); |
| 2211 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2212 | for (const char* oat_filename : oat_filenames_) { |
| 2213 | // Do not fix up the ELF file if we are --compile-pic or compiling the app image |
| 2214 | if (!compiler_options_->GetCompilePic() && IsBootImage()) { |
| 2215 | std::unique_ptr<File> oat_file(OS::OpenFileReadWrite(oat_filename)); |
| 2216 | if (oat_file.get() == nullptr) { |
| 2217 | PLOG(ERROR) << "Failed to open ELF file: " << oat_filename; |
| 2218 | return false; |
| 2219 | } |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame] | 2220 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2221 | uintptr_t oat_data_begin = oat_data_begins.find(oat_filename)->second; |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame] | 2222 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2223 | if (!ElfWriter::Fixup(oat_file.get(), oat_data_begin)) { |
| 2224 | oat_file->Erase(); |
| 2225 | LOG(ERROR) << "Failed to fixup ELF file " << oat_file->GetPath(); |
| 2226 | return false; |
| 2227 | } |
| 2228 | |
| 2229 | if (oat_file->FlushCloseOrErase()) { |
| 2230 | PLOG(ERROR) << "Failed to flush and close fixed ELF file " << oat_file->GetPath(); |
| 2231 | return false; |
| 2232 | } |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame] | 2233 | } |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2234 | } |
| 2235 | |
| 2236 | return true; |
| 2237 | } |
| 2238 | |
| 2239 | // Reads the class names (java.lang.Object) and returns a set of descriptors (Ljava/lang/Object;) |
Andreas Gampe | b1fcead | 2015-04-20 18:53:51 -0700 | [diff] [blame] | 2240 | static std::unordered_set<std::string>* ReadImageClassesFromFile( |
| 2241 | const char* image_classes_filename) { |
Andreas Gampe | 70bef0d | 2015-04-15 02:37:28 -0700 | [diff] [blame] | 2242 | std::function<std::string(const char*)> process = DotToDescriptor; |
| 2243 | return ReadCommentedInputFromFile(image_classes_filename, &process); |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2244 | } |
| 2245 | |
| 2246 | // Reads the class names (java.lang.Object) and returns a set of descriptors (Ljava/lang/Object;) |
Andreas Gampe | b1fcead | 2015-04-20 18:53:51 -0700 | [diff] [blame] | 2247 | static std::unordered_set<std::string>* ReadImageClassesFromZip( |
Andreas Gampe | 70bef0d | 2015-04-15 02:37:28 -0700 | [diff] [blame] | 2248 | const char* zip_filename, |
| 2249 | const char* image_classes_filename, |
| 2250 | std::string* error_msg) { |
| 2251 | std::function<std::string(const char*)> process = DotToDescriptor; |
| 2252 | return ReadCommentedInputFromZip(zip_filename, image_classes_filename, &process, error_msg); |
| 2253 | } |
| 2254 | |
| 2255 | // Read lines from the given file, dropping comments and empty lines. Post-process each line with |
| 2256 | // the given function. |
| 2257 | static std::unordered_set<std::string>* ReadCommentedInputFromFile( |
| 2258 | const char* input_filename, std::function<std::string(const char*)>* process) { |
| 2259 | std::unique_ptr<std::ifstream> input_file(new std::ifstream(input_filename, std::ifstream::in)); |
| 2260 | if (input_file.get() == nullptr) { |
| 2261 | LOG(ERROR) << "Failed to open input file " << input_filename; |
| 2262 | return nullptr; |
| 2263 | } |
| 2264 | std::unique_ptr<std::unordered_set<std::string>> result( |
| 2265 | ReadCommentedInputStream(*input_file, process)); |
| 2266 | input_file->close(); |
| 2267 | return result.release(); |
| 2268 | } |
| 2269 | |
| 2270 | // Read lines from the given file from the given zip file, dropping comments and empty lines. |
| 2271 | // Post-process each line with the given function. |
| 2272 | static std::unordered_set<std::string>* ReadCommentedInputFromZip( |
Andreas Gampe | b1fcead | 2015-04-20 18:53:51 -0700 | [diff] [blame] | 2273 | const char* zip_filename, |
Andreas Gampe | 70bef0d | 2015-04-15 02:37:28 -0700 | [diff] [blame] | 2274 | const char* input_filename, |
| 2275 | std::function<std::string(const char*)>* process, |
Andreas Gampe | b1fcead | 2015-04-20 18:53:51 -0700 | [diff] [blame] | 2276 | std::string* error_msg) { |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2277 | std::unique_ptr<ZipArchive> zip_archive(ZipArchive::Open(zip_filename, error_msg)); |
| 2278 | if (zip_archive.get() == nullptr) { |
| 2279 | return nullptr; |
| 2280 | } |
Andreas Gampe | 70bef0d | 2015-04-15 02:37:28 -0700 | [diff] [blame] | 2281 | std::unique_ptr<ZipEntry> zip_entry(zip_archive->Find(input_filename, error_msg)); |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2282 | if (zip_entry.get() == nullptr) { |
Andreas Gampe | 70bef0d | 2015-04-15 02:37:28 -0700 | [diff] [blame] | 2283 | *error_msg = StringPrintf("Failed to find '%s' within '%s': %s", input_filename, |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2284 | zip_filename, error_msg->c_str()); |
| 2285 | return nullptr; |
| 2286 | } |
Andreas Gampe | 70bef0d | 2015-04-15 02:37:28 -0700 | [diff] [blame] | 2287 | std::unique_ptr<MemMap> input_file(zip_entry->ExtractToMemMap(zip_filename, |
| 2288 | input_filename, |
| 2289 | error_msg)); |
| 2290 | if (input_file.get() == nullptr) { |
| 2291 | *error_msg = StringPrintf("Failed to extract '%s' from '%s': %s", input_filename, |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2292 | zip_filename, error_msg->c_str()); |
| 2293 | return nullptr; |
| 2294 | } |
Andreas Gampe | 70bef0d | 2015-04-15 02:37:28 -0700 | [diff] [blame] | 2295 | const std::string input_string(reinterpret_cast<char*>(input_file->Begin()), |
| 2296 | input_file->Size()); |
| 2297 | std::istringstream input_stream(input_string); |
| 2298 | return ReadCommentedInputStream(input_stream, process); |
| 2299 | } |
| 2300 | |
| 2301 | // Read lines from the given stream, dropping comments and empty lines. Post-process each line |
| 2302 | // with the given function. |
| 2303 | static std::unordered_set<std::string>* ReadCommentedInputStream( |
| 2304 | std::istream& in_stream, |
| 2305 | std::function<std::string(const char*)>* process) { |
| 2306 | std::unique_ptr<std::unordered_set<std::string>> image_classes( |
| 2307 | new std::unordered_set<std::string>); |
| 2308 | while (in_stream.good()) { |
| 2309 | std::string dot; |
| 2310 | std::getline(in_stream, dot); |
| 2311 | if (StartsWith(dot, "#") || dot.empty()) { |
| 2312 | continue; |
| 2313 | } |
| 2314 | if (process != nullptr) { |
| 2315 | std::string descriptor((*process)(dot.c_str())); |
| 2316 | image_classes->insert(descriptor); |
| 2317 | } else { |
| 2318 | image_classes->insert(dot); |
| 2319 | } |
| 2320 | } |
| 2321 | return image_classes.release(); |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2322 | } |
| 2323 | |
Mathieu Chartier | 49285c5 | 2014-12-02 15:43:48 -0800 | [diff] [blame] | 2324 | void LogCompletionTime() { |
Andreas Gampe | 1d00add | 2015-02-27 19:35:46 -0800 | [diff] [blame] | 2325 | // Note: when creation of a runtime fails, e.g., when trying to compile an app but when there |
| 2326 | // is no image, there won't be a Runtime::Current(). |
Brian Carlstrom | a11a34c | 2015-03-06 08:44:45 -0800 | [diff] [blame] | 2327 | // Note: driver creation can fail when loading an invalid dex file. |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2328 | LOG(INFO) << "dex2oat took " << PrettyDuration(NanoTime() - start_ns_) |
Mathieu Chartier | ab972ef | 2014-12-03 17:38:22 -0800 | [diff] [blame] | 2329 | << " (threads: " << thread_count_ << ") " |
Andreas Gampe | 3f30e12 | 2015-09-17 14:03:21 -0700 | [diff] [blame] | 2330 | << ((Runtime::Current() != nullptr && driver_ != nullptr) ? |
Andreas Gampe | 1d00add | 2015-02-27 19:35:46 -0800 | [diff] [blame] | 2331 | driver_->GetMemoryUsageString(kIsDebugBuild || VLOG_IS_ON(compiler)) : |
| 2332 | ""); |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2333 | } |
| 2334 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2335 | std::string StripIsaFrom(const char* image_filename, InstructionSet isa) { |
| 2336 | std::string res(image_filename); |
| 2337 | size_t last_slash = res.rfind('/'); |
| 2338 | if (last_slash == std::string::npos || last_slash == 0) { |
| 2339 | return res; |
| 2340 | } |
| 2341 | size_t penultimate_slash = res.rfind('/', last_slash - 1); |
| 2342 | if (penultimate_slash == std::string::npos) { |
| 2343 | return res; |
| 2344 | } |
| 2345 | // Check that the string in-between is the expected one. |
| 2346 | if (res.substr(penultimate_slash + 1, last_slash - penultimate_slash - 1) != |
| 2347 | GetInstructionSetString(isa)) { |
| 2348 | LOG(WARNING) << "Unexpected string when trying to strip isa: " << res; |
| 2349 | return res; |
| 2350 | } |
| 2351 | return res.substr(0, penultimate_slash) + res.substr(last_slash); |
| 2352 | } |
| 2353 | |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2354 | std::unique_ptr<CompilerOptions> compiler_options_; |
| 2355 | Compiler::Kind compiler_kind_; |
| 2356 | |
| 2357 | InstructionSet instruction_set_; |
| 2358 | std::unique_ptr<const InstructionSetFeatures> instruction_set_features_; |
| 2359 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2360 | uint32_t image_file_location_oat_checksum_; |
| 2361 | uintptr_t image_file_location_oat_data_begin_; |
| 2362 | int32_t image_patch_delta_; |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2363 | std::unique_ptr<SafeMap<std::string, std::string> > key_value_store_; |
| 2364 | |
Vladimir Marko | 307dac9 | 2015-10-20 11:20:09 +0100 | [diff] [blame] | 2365 | std::unique_ptr<VerificationResults> verification_results_; |
Andreas Gampe | 3f30e12 | 2015-09-17 14:03:21 -0700 | [diff] [blame] | 2366 | |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2367 | DexFileToMethodInlinerMap method_inliner_map_; |
| 2368 | std::unique_ptr<QuickCompilerCallbacks> callbacks_; |
| 2369 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2370 | std::unique_ptr<Runtime> runtime_; |
| 2371 | |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 2372 | // Ownership for the class path files. |
| 2373 | std::vector<std::unique_ptr<const DexFile>> class_path_files_; |
| 2374 | |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2375 | size_t thread_count_; |
| 2376 | uint64_t start_ns_; |
| 2377 | std::unique_ptr<WatchDog> watchdog_; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2378 | std::vector<std::unique_ptr<File>> oat_files_; |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2379 | std::string oat_location_; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2380 | std::vector<const char*> oat_filenames_; |
| 2381 | std::vector<const char*> oat_unstripped_; |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2382 | int oat_fd_; |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2383 | std::vector<const char*> dex_filenames_; |
| 2384 | std::vector<const char*> dex_locations_; |
| 2385 | int zip_fd_; |
| 2386 | std::string zip_location_; |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 2387 | std::string boot_image_filename_; |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2388 | std::vector<const char*> runtime_args_; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2389 | std::vector<const char*> image_filenames_; |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2390 | uintptr_t image_base_; |
| 2391 | const char* image_classes_zip_filename_; |
| 2392 | const char* image_classes_filename_; |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 2393 | ImageHeader::StorageMode image_storage_mode_; |
Andreas Gampe | 4bf3ae9 | 2014-11-11 13:28:29 -0800 | [diff] [blame] | 2394 | const char* compiled_classes_zip_filename_; |
| 2395 | const char* compiled_classes_filename_; |
Andreas Gampe | 70bef0d | 2015-04-15 02:37:28 -0700 | [diff] [blame] | 2396 | const char* compiled_methods_zip_filename_; |
| 2397 | const char* compiled_methods_filename_; |
Andreas Gampe | b1fcead | 2015-04-20 18:53:51 -0700 | [diff] [blame] | 2398 | std::unique_ptr<std::unordered_set<std::string>> image_classes_; |
| 2399 | std::unique_ptr<std::unordered_set<std::string>> compiled_classes_; |
Andreas Gampe | 70bef0d | 2015-04-15 02:37:28 -0700 | [diff] [blame] | 2400 | std::unique_ptr<std::unordered_set<std::string>> compiled_methods_; |
Mathieu Chartier | a90c772 | 2015-10-29 15:41:36 -0700 | [diff] [blame] | 2401 | bool app_image_; |
| 2402 | bool boot_image_; |
Jeff Hao | 436183f | 2016-01-12 16:36:50 -0800 | [diff] [blame] | 2403 | bool multi_image_; |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2404 | bool is_host_; |
| 2405 | std::string android_root_; |
| 2406 | std::vector<const DexFile*> dex_files_; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2407 | std::string no_inline_from_string_; |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 2408 | std::vector<jobject> dex_caches_; |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2409 | jobject class_loader_; |
Andreas Gampe | 3f30e12 | 2015-09-17 14:03:21 -0700 | [diff] [blame] | 2410 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2411 | std::vector<std::unique_ptr<ElfWriter>> elf_writers_; |
| 2412 | std::vector<std::unique_ptr<OatWriter>> oat_writers_; |
| 2413 | std::vector<OutputStream*> rodata_; |
Vladimir Marko | 307dac9 | 2015-10-20 11:20:09 +0100 | [diff] [blame] | 2414 | std::unique_ptr<ImageWriter> image_writer_; |
| 2415 | std::unique_ptr<CompilerDriver> driver_; |
Andreas Gampe | 3f30e12 | 2015-09-17 14:03:21 -0700 | [diff] [blame] | 2416 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2417 | std::vector<std::unique_ptr<MemMap>> opened_dex_files_maps_; |
| 2418 | std::vector<std::unique_ptr<const DexFile>> opened_dex_files_; |
| 2419 | |
Vladimir Marko | 47496c2 | 2016-01-27 16:15:08 +0000 | [diff] [blame] | 2420 | std::vector<const DexFile*> no_inline_from_dex_files_; |
| 2421 | |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2422 | std::vector<std::string> verbose_methods_; |
| 2423 | bool dump_stats_; |
| 2424 | bool dump_passes_; |
| 2425 | bool dump_timing_; |
| 2426 | bool dump_slow_timing_; |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 2427 | std::string swap_file_name_; |
| 2428 | int swap_fd_; |
Mathieu Chartier | a90c772 | 2015-10-29 15:41:36 -0700 | [diff] [blame] | 2429 | std::string app_image_file_name_; |
| 2430 | int app_image_fd_; |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 2431 | std::string profile_file_; |
| 2432 | int profile_file_fd_; |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 2433 | std::unique_ptr<ProfileCompilationInfo> profile_compilation_info_; |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2434 | TimingLogger* timings_; |
| 2435 | std::unique_ptr<CumulativeLogger> compiler_phases_timings_; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2436 | std::vector<std::vector<const DexFile*>> dex_files_per_oat_file_; |
| 2437 | std::unordered_map<const DexFile*, const char*> dex_file_oat_filename_map_; |
| 2438 | |
| 2439 | // Backing storage. |
| 2440 | std::vector<std::string> char_backing_storage_; |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2441 | |
Andreas Gampe | ace0dc1 | 2016-01-20 13:33:13 -0800 | [diff] [blame] | 2442 | // See CompilerOptions.force_determinism_. |
| 2443 | bool force_determinism_; |
| 2444 | |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2445 | DISALLOW_IMPLICIT_CONSTRUCTORS(Dex2Oat); |
| 2446 | }; |
| 2447 | |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2448 | static void b13564922() { |
| 2449 | #if defined(__linux__) && defined(__arm__) |
| 2450 | int major, minor; |
| 2451 | struct utsname uts; |
| 2452 | if (uname(&uts) != -1 && |
| 2453 | sscanf(uts.release, "%d.%d", &major, &minor) == 2 && |
| 2454 | ((major < 3) || ((major == 3) && (minor < 4)))) { |
| 2455 | // Kernels before 3.4 don't handle the ASLR well and we can run out of address |
| 2456 | // space (http://b/13564922). Work around the issue by inhibiting further mmap() randomization. |
| 2457 | int old_personality = personality(0xffffffff); |
| 2458 | if ((old_personality & ADDR_NO_RANDOMIZE) == 0) { |
| 2459 | int new_personality = personality(old_personality | ADDR_NO_RANDOMIZE); |
| 2460 | if (new_personality == -1) { |
| 2461 | LOG(WARNING) << "personality(. | ADDR_NO_RANDOMIZE) failed."; |
| 2462 | } |
| 2463 | } |
| 2464 | } |
| 2465 | #endif |
| 2466 | } |
| 2467 | |
Andreas Gampe | 10e477d | 2014-11-19 12:57:42 -0800 | [diff] [blame] | 2468 | static int CompileImage(Dex2Oat& dex2oat) { |
| 2469 | dex2oat.Compile(); |
| 2470 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2471 | if (!dex2oat.WriteOatFiles()) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2472 | dex2oat.EraseOatFiles(); |
Andreas Gampe | 10e477d | 2014-11-19 12:57:42 -0800 | [diff] [blame] | 2473 | return EXIT_FAILURE; |
| 2474 | } |
| 2475 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 2476 | // Flush boot.oat. We always expect the output file by name, and it will be re-opened from the |
| 2477 | // unstripped name. Do not close the file if we are compiling the image with an oat fd since the |
| 2478 | // image writer will require this fd to generate the image. |
| 2479 | if (dex2oat.ShouldKeepOatFileOpen()) { |
| 2480 | if (!dex2oat.FlushOatFiles()) { |
| 2481 | return EXIT_FAILURE; |
| 2482 | } |
| 2483 | } else if (!dex2oat.FlushCloseOatFiles()) { |
Andreas Gampe | 10e477d | 2014-11-19 12:57:42 -0800 | [diff] [blame] | 2484 | return EXIT_FAILURE; |
| 2485 | } |
| 2486 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2487 | // Creates the boot.art and patches the oat files. |
Andreas Gampe | 10e477d | 2014-11-19 12:57:42 -0800 | [diff] [blame] | 2488 | if (!dex2oat.HandleImage()) { |
| 2489 | return EXIT_FAILURE; |
| 2490 | } |
| 2491 | |
| 2492 | // When given --host, finish early without stripping. |
| 2493 | if (dex2oat.IsHost()) { |
| 2494 | dex2oat.DumpTiming(); |
| 2495 | return EXIT_SUCCESS; |
| 2496 | } |
| 2497 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2498 | // Copy stripped to unstripped location, if necessary. |
| 2499 | if (!dex2oat.CopyStrippedToUnstripped()) { |
Andreas Gampe | 10e477d | 2014-11-19 12:57:42 -0800 | [diff] [blame] | 2500 | return EXIT_FAILURE; |
| 2501 | } |
| 2502 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2503 | // FlushClose again, as stripping might have re-opened the oat files. |
| 2504 | if (!dex2oat.FlushCloseOatFiles()) { |
Andreas Gampe | 10e477d | 2014-11-19 12:57:42 -0800 | [diff] [blame] | 2505 | return EXIT_FAILURE; |
| 2506 | } |
| 2507 | |
| 2508 | dex2oat.DumpTiming(); |
| 2509 | return EXIT_SUCCESS; |
| 2510 | } |
| 2511 | |
| 2512 | static int CompileApp(Dex2Oat& dex2oat) { |
| 2513 | dex2oat.Compile(); |
| 2514 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2515 | if (!dex2oat.WriteOatFiles()) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2516 | dex2oat.EraseOatFiles(); |
Andreas Gampe | 10e477d | 2014-11-19 12:57:42 -0800 | [diff] [blame] | 2517 | return EXIT_FAILURE; |
| 2518 | } |
| 2519 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2520 | // Do not close the oat files here. We might have gotten the output file by file descriptor, |
Andreas Gampe | 10e477d | 2014-11-19 12:57:42 -0800 | [diff] [blame] | 2521 | // which we would lose. |
Andreas Gampe | 10e477d | 2014-11-19 12:57:42 -0800 | [diff] [blame] | 2522 | |
| 2523 | // When given --host, finish early without stripping. |
| 2524 | if (dex2oat.IsHost()) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2525 | if (!dex2oat.FlushCloseOatFiles()) { |
Andreas Gampe | 10e477d | 2014-11-19 12:57:42 -0800 | [diff] [blame] | 2526 | return EXIT_FAILURE; |
| 2527 | } |
| 2528 | |
| 2529 | dex2oat.DumpTiming(); |
| 2530 | return EXIT_SUCCESS; |
| 2531 | } |
| 2532 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2533 | // Copy stripped to unstripped location, if necessary. This will implicitly flush & close the |
| 2534 | // stripped versions. If this is given, we expect to be able to open writable files by name. |
| 2535 | if (!dex2oat.CopyStrippedToUnstripped()) { |
Andreas Gampe | 10e477d | 2014-11-19 12:57:42 -0800 | [diff] [blame] | 2536 | return EXIT_FAILURE; |
| 2537 | } |
| 2538 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2539 | // Flush and close the files. |
| 2540 | if (!dex2oat.FlushCloseOatFiles()) { |
Andreas Gampe | 10e477d | 2014-11-19 12:57:42 -0800 | [diff] [blame] | 2541 | return EXIT_FAILURE; |
| 2542 | } |
| 2543 | |
| 2544 | dex2oat.DumpTiming(); |
| 2545 | return EXIT_SUCCESS; |
| 2546 | } |
| 2547 | |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2548 | static int dex2oat(int argc, char** argv) { |
| 2549 | b13564922(); |
| 2550 | |
| 2551 | TimingLogger timings("compiler", false, false); |
| 2552 | |
Roland Levillain | 5c7e260 | 2016-02-23 18:08:53 +0000 | [diff] [blame^] | 2553 | // Allocate `dex2oat` on the heap instead of on the stack, as Clang |
| 2554 | // might produce a stack frame too large for this function or for |
| 2555 | // functions inlining it (such as main), that would not fit the |
| 2556 | // requirements of the `-Wframe-larger-than` option. |
| 2557 | std::unique_ptr<Dex2Oat> dex2oat = MakeUnique<Dex2Oat>(&timings); |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2558 | |
| 2559 | // Parse arguments. Argument mistakes will lead to exit(EXIT_FAILURE) in UsageError. |
Roland Levillain | 5c7e260 | 2016-02-23 18:08:53 +0000 | [diff] [blame^] | 2560 | dex2oat->ParseArgs(argc, argv); |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2561 | |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 2562 | // Process profile information and assess if we need to do a profile guided compilation. |
| 2563 | // This operation involves I/O. |
Roland Levillain | 5c7e260 | 2016-02-23 18:08:53 +0000 | [diff] [blame^] | 2564 | if (dex2oat->UseProfileGuidedCompilation()) { |
| 2565 | if (dex2oat->LoadProfile()) { |
| 2566 | if (!dex2oat->ShouldCompileBasedOnProfiles()) { |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 2567 | LOG(INFO) << "Skipped compilation because of insignificant profile delta"; |
| 2568 | return EXIT_SUCCESS; |
| 2569 | } |
| 2570 | } else { |
| 2571 | LOG(WARNING) << "Failed to process profile files"; |
| 2572 | return EXIT_FAILURE; |
| 2573 | } |
| 2574 | } |
| 2575 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2576 | // Check early that the result of compilation can be written |
Roland Levillain | 5c7e260 | 2016-02-23 18:08:53 +0000 | [diff] [blame^] | 2577 | if (!dex2oat->OpenFile()) { |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2578 | return EXIT_FAILURE; |
| 2579 | } |
| 2580 | |
Andreas Gampe | 046c706 | 2015-05-18 23:22:54 -0700 | [diff] [blame] | 2581 | // Print the complete line when any of the following is true: |
| 2582 | // 1) Debug build |
| 2583 | // 2) Compiling an image |
| 2584 | // 3) Compiling with --host |
| 2585 | // 4) Compiling on the host (not a target build) |
| 2586 | // Otherwise, print a stripped command line. |
Roland Levillain | 5c7e260 | 2016-02-23 18:08:53 +0000 | [diff] [blame^] | 2587 | if (kIsDebugBuild || dex2oat->IsBootImage() || dex2oat->IsHost() || !kIsTargetBuild) { |
Andreas Gampe | 046c706 | 2015-05-18 23:22:54 -0700 | [diff] [blame] | 2588 | LOG(INFO) << CommandLine(); |
| 2589 | } else { |
| 2590 | LOG(INFO) << StrippedCommandLine(); |
| 2591 | } |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2592 | |
Roland Levillain | 5c7e260 | 2016-02-23 18:08:53 +0000 | [diff] [blame^] | 2593 | if (!dex2oat->Setup()) { |
| 2594 | dex2oat->EraseOatFiles(); |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2595 | return EXIT_FAILURE; |
| 2596 | } |
| 2597 | |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 2598 | bool result; |
Roland Levillain | 5c7e260 | 2016-02-23 18:08:53 +0000 | [diff] [blame^] | 2599 | if (dex2oat->IsImage()) { |
| 2600 | result = CompileImage(*dex2oat); |
Andreas Gampe | 10e477d | 2014-11-19 12:57:42 -0800 | [diff] [blame] | 2601 | } else { |
Roland Levillain | 5c7e260 | 2016-02-23 18:08:53 +0000 | [diff] [blame^] | 2602 | result = CompileApp(*dex2oat); |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2603 | } |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 2604 | |
Roland Levillain | 5c7e260 | 2016-02-23 18:08:53 +0000 | [diff] [blame^] | 2605 | dex2oat->Shutdown(); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 2606 | return result; |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2607 | } |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 2608 | } // namespace art |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 2609 | |
| 2610 | int main(int argc, char** argv) { |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2611 | int result = art::dex2oat(argc, argv); |
| 2612 | // Everything was done, do an explicit exit here to avoid running Runtime destructors that take |
| 2613 | // time (bug 10645725) unless we're a debug build or running on valgrind. Note: The Dex2Oat class |
| 2614 | // should not destruct the runtime in this case. |
Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 2615 | if (!art::kIsDebugBuild && (RUNNING_ON_MEMORY_TOOL == 0)) { |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2616 | exit(result); |
| 2617 | } |
| 2618 | return result; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 2619 | } |