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