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