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 | |
| 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <sys/stat.h> |
Ian Rogers | 2672a9f | 2013-09-05 17:24:22 -0700 | [diff] [blame] | 20 | #include <valgrind.h> |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 21 | |
| 22 | #include <fstream> |
| 23 | #include <iostream> |
| 24 | #include <sstream> |
| 25 | #include <string> |
| 26 | #include <vector> |
| 27 | |
Vladimir Marko | f94b781 | 2014-06-05 15:48:04 +0100 | [diff] [blame] | 28 | #if defined(__linux__) && defined(__arm__) |
| 29 | #include <sys/personality.h> |
| 30 | #include <sys/utsname.h> |
| 31 | #endif |
| 32 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 33 | #include "base/stl_util.h" |
| 34 | #include "base/stringpiece.h" |
| 35 | #include "base/timing_logger.h" |
| 36 | #include "base/unix_file/fd_file.h" |
| 37 | #include "class_linker.h" |
Nicolas Geoffray | b34f69a | 2014-03-07 15:28:39 +0000 | [diff] [blame] | 38 | #include "compiler.h" |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 39 | #include "compiler_callbacks.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 40 | #include "dex_file-inl.h" |
Jean Christophe Beyler | 2469e60 | 2014-05-06 20:36:55 -0700 | [diff] [blame] | 41 | #include "dex/pass_driver_me_opts.h" |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 42 | #include "dex/verification_results.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 43 | #include "dex/quick_compiler_callbacks.h" |
| 44 | #include "dex/quick/dex_file_to_method_inliner_map.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 45 | #include "driver/compiler_driver.h" |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 46 | #include "driver/compiler_options.h" |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 47 | #include "elf_writer.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 48 | #include "gc/space/image_space.h" |
| 49 | #include "gc/space/space-inl.h" |
| 50 | #include "image_writer.h" |
| 51 | #include "leb128.h" |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 52 | #include "mirror/art_method-inl.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 53 | #include "mirror/class-inl.h" |
| 54 | #include "mirror/class_loader.h" |
| 55 | #include "mirror/object-inl.h" |
| 56 | #include "mirror/object_array-inl.h" |
| 57 | #include "oat_writer.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 58 | #include "os.h" |
| 59 | #include "runtime.h" |
| 60 | #include "ScopedLocalRef.h" |
| 61 | #include "scoped_thread_state_change.h" |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 62 | #include "utils.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 63 | #include "vector_output_stream.h" |
| 64 | #include "well_known_classes.h" |
| 65 | #include "zip_archive.h" |
| 66 | |
| 67 | namespace art { |
| 68 | |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 69 | static int original_argc; |
| 70 | static char** original_argv; |
| 71 | |
| 72 | static std::string CommandLine() { |
| 73 | std::vector<std::string> command; |
| 74 | for (int i = 0; i < original_argc; ++i) { |
| 75 | command.push_back(original_argv[i]); |
| 76 | } |
| 77 | return Join(command, ' '); |
| 78 | } |
| 79 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 80 | static void UsageErrorV(const char* fmt, va_list ap) { |
| 81 | std::string error; |
| 82 | StringAppendV(&error, fmt, ap); |
| 83 | LOG(ERROR) << error; |
| 84 | } |
| 85 | |
| 86 | static void UsageError(const char* fmt, ...) { |
| 87 | va_list ap; |
| 88 | va_start(ap, fmt); |
| 89 | UsageErrorV(fmt, ap); |
| 90 | va_end(ap); |
| 91 | } |
| 92 | |
Ian Rogers | 7223d44 | 2014-10-10 20:05:39 -0700 | [diff] [blame] | 93 | [[noreturn]] static void Usage(const char* fmt, ...) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 94 | va_list ap; |
| 95 | va_start(ap, fmt); |
| 96 | UsageErrorV(fmt, ap); |
| 97 | va_end(ap); |
| 98 | |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 99 | UsageError("Command: %s", CommandLine().c_str()); |
| 100 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 101 | UsageError("Usage: dex2oat [options]..."); |
| 102 | UsageError(""); |
| 103 | UsageError(" --dex-file=<dex-file>: specifies a .dex file to compile."); |
| 104 | UsageError(" Example: --dex-file=/system/framework/core.jar"); |
| 105 | UsageError(""); |
| 106 | UsageError(" --zip-fd=<file-descriptor>: specifies a file descriptor of a zip file"); |
| 107 | UsageError(" containing a classes.dex file to compile."); |
| 108 | UsageError(" Example: --zip-fd=5"); |
| 109 | UsageError(""); |
Brian Carlstrom | 4560248 | 2013-07-21 22:07:55 -0700 | [diff] [blame] | 110 | UsageError(" --zip-location=<zip-location>: specifies a symbolic name for the file"); |
| 111 | UsageError(" corresponding to the file descriptor specified by --zip-fd."); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 112 | UsageError(" Example: --zip-location=/system/app/Calculator.apk"); |
| 113 | UsageError(""); |
| 114 | UsageError(" --oat-file=<file.oat>: specifies the oat output destination via a filename."); |
| 115 | UsageError(" Example: --oat-file=/system/framework/boot.oat"); |
| 116 | UsageError(""); |
| 117 | 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] | 118 | UsageError(" Example: --oat-fd=6"); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 119 | UsageError(""); |
| 120 | UsageError(" --oat-location=<oat-name>: specifies a symbolic name for the file corresponding"); |
| 121 | UsageError(" to the file descriptor specified by --oat-fd."); |
| 122 | UsageError(" Example: --oat-location=/data/dalvik-cache/system@app@Calculator.apk.oat"); |
| 123 | UsageError(""); |
| 124 | UsageError(" --oat-symbols=<file.oat>: specifies the oat output destination with full symbols."); |
| 125 | UsageError(" Example: --oat-symbols=/symbols/system/framework/boot.oat"); |
| 126 | UsageError(""); |
| 127 | UsageError(" --bitcode=<file.bc>: specifies the optional bitcode filename."); |
| 128 | UsageError(" Example: --bitcode=/system/framework/boot.bc"); |
| 129 | UsageError(""); |
| 130 | UsageError(" --image=<file.art>: specifies the output image filename."); |
| 131 | UsageError(" Example: --image=/system/framework/boot.art"); |
| 132 | UsageError(""); |
| 133 | UsageError(" --image-classes=<classname-file>: specifies classes to include in an image."); |
| 134 | UsageError(" Example: --image=frameworks/base/preloaded-classes"); |
| 135 | UsageError(""); |
| 136 | UsageError(" --base=<hex-address>: specifies the base address when creating a boot image."); |
| 137 | UsageError(" Example: --base=0x50000000"); |
| 138 | UsageError(""); |
| 139 | UsageError(" --boot-image=<file.art>: provide the image file for the boot class path."); |
| 140 | UsageError(" Example: --boot-image=/system/framework/boot.art"); |
Nicolas Geoffray | 9583fbc | 2014-02-28 15:21:07 +0000 | [diff] [blame] | 141 | UsageError(" Default: $ANDROID_ROOT/system/framework/boot.art"); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 142 | UsageError(""); |
| 143 | UsageError(" --android-root=<path>: used to locate libraries for portable linking."); |
| 144 | UsageError(" Example: --android-root=out/host/linux-x86"); |
| 145 | UsageError(" Default: $ANDROID_ROOT"); |
| 146 | UsageError(""); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 147 | UsageError(" --instruction-set=(arm|arm64|mips|x86|x86_64): compile for a particular"); |
| 148 | UsageError(" instruction set."); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 149 | UsageError(" Example: --instruction-set=x86"); |
| 150 | UsageError(" Default: arm"); |
| 151 | UsageError(""); |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 152 | UsageError(" --instruction-set-features=...,: Specify instruction set features"); |
| 153 | UsageError(" Example: --instruction-set-features=div"); |
| 154 | UsageError(" Default: default"); |
| 155 | UsageError(""); |
Nicolas Geoffray | b34f69a | 2014-03-07 15:28:39 +0000 | [diff] [blame] | 156 | UsageError(" --compiler-backend=(Quick|Optimizing|Portable): select compiler backend"); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 157 | UsageError(" set."); |
Brian Carlstrom | 635733d | 2013-10-30 23:19:31 -0700 | [diff] [blame] | 158 | UsageError(" Example: --compiler-backend=Portable"); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 159 | UsageError(" Default: Quick"); |
| 160 | UsageError(""); |
Nicolas Geoffray | 88157ef | 2014-09-12 10:29:53 +0100 | [diff] [blame] | 161 | UsageError(" --compiler-filter=" |
| 162 | "(verify-none" |
| 163 | "|interpret-only" |
| 164 | "|space" |
| 165 | "|balanced" |
| 166 | "|speed" |
| 167 | "|everything" |
| 168 | "|time):"); |
Jeff Hao | 4a200f5 | 2014-04-01 14:58:49 -0700 | [diff] [blame] | 169 | UsageError(" select compiler filter."); |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 170 | UsageError(" Example: --compiler-filter=everything"); |
| 171 | #if ART_SMALL_MODE |
| 172 | UsageError(" Default: interpret-only"); |
| 173 | #else |
| 174 | UsageError(" Default: speed"); |
| 175 | #endif |
| 176 | UsageError(""); |
| 177 | UsageError(" --huge-method-max=<method-instruction-count>: the threshold size for a huge"); |
| 178 | UsageError(" method for compiler filter tuning."); |
| 179 | UsageError(" Example: --huge-method-max=%d", CompilerOptions::kDefaultHugeMethodThreshold); |
| 180 | UsageError(" Default: %d", CompilerOptions::kDefaultHugeMethodThreshold); |
| 181 | UsageError(""); |
| 182 | UsageError(" --huge-method-max=<method-instruction-count>: threshold size for a huge"); |
| 183 | UsageError(" method for compiler filter tuning."); |
| 184 | UsageError(" Example: --huge-method-max=%d", CompilerOptions::kDefaultHugeMethodThreshold); |
| 185 | UsageError(" Default: %d", CompilerOptions::kDefaultHugeMethodThreshold); |
| 186 | UsageError(""); |
| 187 | UsageError(" --large-method-max=<method-instruction-count>: threshold size for a large"); |
| 188 | UsageError(" method for compiler filter tuning."); |
| 189 | UsageError(" Example: --large-method-max=%d", CompilerOptions::kDefaultLargeMethodThreshold); |
| 190 | UsageError(" Default: %d", CompilerOptions::kDefaultLargeMethodThreshold); |
| 191 | UsageError(""); |
| 192 | UsageError(" --small-method-max=<method-instruction-count>: threshold size for a small"); |
| 193 | UsageError(" method for compiler filter tuning."); |
| 194 | UsageError(" Example: --small-method-max=%d", CompilerOptions::kDefaultSmallMethodThreshold); |
| 195 | UsageError(" Default: %d", CompilerOptions::kDefaultSmallMethodThreshold); |
| 196 | UsageError(""); |
| 197 | UsageError(" --tiny-method-max=<method-instruction-count>: threshold size for a tiny"); |
| 198 | UsageError(" method for compiler filter tuning."); |
| 199 | UsageError(" Example: --tiny-method-max=%d", CompilerOptions::kDefaultTinyMethodThreshold); |
| 200 | UsageError(" Default: %d", CompilerOptions::kDefaultTinyMethodThreshold); |
| 201 | UsageError(""); |
| 202 | UsageError(" --num-dex-methods=<method-count>: threshold size for a small dex file for"); |
| 203 | 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] | 204 | UsageError(" and the filter is not interpret-only or verify-none, overrides the"); |
| 205 | UsageError(" filter to use speed"); |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 206 | UsageError(" Example: --num-dex-method=%d", CompilerOptions::kDefaultNumDexMethodsThreshold); |
| 207 | UsageError(" Default: %d", CompilerOptions::kDefaultNumDexMethodsThreshold); |
| 208 | UsageError(""); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 209 | UsageError(" --host: used with Portable backend to link against host runtime libraries"); |
| 210 | UsageError(""); |
Ian Rogers | 4639860 | 2013-08-20 07:50:36 -0700 | [diff] [blame] | 211 | UsageError(" --dump-timing: display a breakdown of where time was spent"); |
| 212 | UsageError(""); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 213 | UsageError(" --include-patch-information: Include patching information so the generated code"); |
| 214 | UsageError(" can have its base address moved without full recompilation."); |
| 215 | UsageError(""); |
| 216 | UsageError(" --no-include-patch-information: Do not include patching information."); |
| 217 | UsageError(""); |
Alex Light | 78382fa | 2014-06-06 15:45:32 -0700 | [diff] [blame] | 218 | UsageError(" --include-debug-symbols: Include ELF symbols in this oat file"); |
| 219 | UsageError(""); |
| 220 | UsageError(" --no-include-debug-symbols: Do not include ELF symbols in this oat file"); |
| 221 | UsageError(""); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 222 | UsageError(" --runtime-arg <argument>: used to specify various arguments for the runtime,"); |
| 223 | UsageError(" such as initial heap size, maximum heap size, and verbose output."); |
| 224 | UsageError(" Use a separate --runtime-arg switch for each argument."); |
| 225 | UsageError(" Example: --runtime-arg -Xms256m"); |
Jeff Hao | 4a200f5 | 2014-04-01 14:58:49 -0700 | [diff] [blame] | 226 | UsageError(""); |
Dave Allison | d6ed642 | 2014-04-09 23:36:15 +0000 | [diff] [blame] | 227 | UsageError(" --profile-file=<filename>: specify profiler output file to use for compilation."); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 228 | UsageError(""); |
Chao-ying Fu | cd8ce66 | 2014-03-11 14:57:19 -0700 | [diff] [blame] | 229 | UsageError(" --print-pass-names: print a list of pass names"); |
| 230 | UsageError(""); |
| 231 | UsageError(" --disable-passes=<pass-names>: disable one or more passes separated by comma."); |
| 232 | UsageError(" Example: --disable-passes=UseCount,BBOptimizations"); |
| 233 | UsageError(""); |
Razvan A Lupusoru | bd25d4b | 2014-07-02 18:16:51 -0700 | [diff] [blame] | 234 | UsageError(" --print-pass-options: print a list of passes that have configurable options along " |
| 235 | "with the setting."); |
| 236 | UsageError(" Will print default if no overridden setting exists."); |
| 237 | UsageError(""); |
| 238 | UsageError(" --pass-options=Pass1Name:Pass1OptionName:Pass1Option#," |
| 239 | "Pass2Name:Pass2OptionName:Pass2Option#"); |
| 240 | UsageError(" Used to specify a pass specific option. The setting itself must be integer."); |
| 241 | UsageError(" Separator used between options is a comma."); |
| 242 | UsageError(""); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 243 | std::cerr << "See log for usage error information\n"; |
| 244 | exit(EXIT_FAILURE); |
| 245 | } |
| 246 | |
| 247 | class Dex2Oat { |
| 248 | public: |
Brian Carlstrom | 4560248 | 2013-07-21 22:07:55 -0700 | [diff] [blame] | 249 | static bool Create(Dex2Oat** p_dex2oat, |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 250 | const RuntimeOptions& runtime_options, |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 251 | const CompilerOptions& compiler_options, |
Nicolas Geoffray | b34f69a | 2014-03-07 15:28:39 +0000 | [diff] [blame] | 252 | Compiler::Kind compiler_kind, |
Brian Carlstrom | 4560248 | 2013-07-21 22:07:55 -0700 | [diff] [blame] | 253 | InstructionSet instruction_set, |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 254 | InstructionSetFeatures instruction_set_features, |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 255 | VerificationResults* verification_results, |
| 256 | DexFileToMethodInlinerMap* method_inliner_map, |
Brian Carlstrom | 4560248 | 2013-07-21 22:07:55 -0700 | [diff] [blame] | 257 | size_t thread_count) |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 258 | SHARED_TRYLOCK_FUNCTION(true, Locks::mutator_lock_) { |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 259 | CHECK(verification_results != nullptr); |
| 260 | CHECK(method_inliner_map != nullptr); |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 261 | std::unique_ptr<Dex2Oat> dex2oat(new Dex2Oat(&compiler_options, |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 262 | compiler_kind, |
| 263 | instruction_set, |
| 264 | instruction_set_features, |
| 265 | verification_results, |
| 266 | method_inliner_map, |
| 267 | thread_count)); |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 268 | if (!dex2oat->CreateRuntime(runtime_options, instruction_set)) { |
Kenny Root | 5131638 | 2014-05-13 14:59:37 -0700 | [diff] [blame] | 269 | *p_dex2oat = nullptr; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 270 | return false; |
| 271 | } |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 272 | *p_dex2oat = dex2oat.release(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 273 | return true; |
| 274 | } |
| 275 | |
| 276 | ~Dex2Oat() { |
| 277 | delete runtime_; |
Brian Carlstrom | 65c23bb | 2014-02-01 22:12:39 -0800 | [diff] [blame] | 278 | LogCompletionTime(); |
| 279 | } |
| 280 | |
| 281 | void LogCompletionTime() { |
| 282 | LOG(INFO) << "dex2oat took " << PrettyDuration(NanoTime() - start_ns_) |
Brian Carlstrom | 4560248 | 2013-07-21 22:07:55 -0700 | [diff] [blame] | 283 | << " (threads: " << thread_count_ << ")"; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | |
Brian Carlstrom | 4560248 | 2013-07-21 22:07:55 -0700 | [diff] [blame] | 287 | // Reads the class names (java.lang.Object) and returns a set of descriptors (Ljava/lang/Object;) |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 288 | std::set<std::string>* ReadImageClassesFromFile(const char* image_classes_filename) { |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 289 | std::unique_ptr<std::ifstream> image_classes_file(new std::ifstream(image_classes_filename, |
Brian Carlstrom | 4560248 | 2013-07-21 22:07:55 -0700 | [diff] [blame] | 290 | std::ifstream::in)); |
Kenny Root | 5131638 | 2014-05-13 14:59:37 -0700 | [diff] [blame] | 291 | if (image_classes_file.get() == nullptr) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 292 | LOG(ERROR) << "Failed to open image classes file " << image_classes_filename; |
Kenny Root | 5131638 | 2014-05-13 14:59:37 -0700 | [diff] [blame] | 293 | return nullptr; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 294 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 295 | std::unique_ptr<std::set<std::string>> result(ReadImageClasses(*image_classes_file)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 296 | image_classes_file->close(); |
| 297 | return result.release(); |
| 298 | } |
| 299 | |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 300 | std::set<std::string>* ReadImageClasses(std::istream& image_classes_stream) { |
| 301 | std::unique_ptr<std::set<std::string>> image_classes(new std::set<std::string>); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 302 | while (image_classes_stream.good()) { |
| 303 | std::string dot; |
| 304 | std::getline(image_classes_stream, dot); |
| 305 | if (StartsWith(dot, "#") || dot.empty()) { |
| 306 | continue; |
| 307 | } |
| 308 | std::string descriptor(DotToDescriptor(dot.c_str())); |
| 309 | image_classes->insert(descriptor); |
| 310 | } |
| 311 | return image_classes.release(); |
| 312 | } |
| 313 | |
Brian Carlstrom | 4560248 | 2013-07-21 22:07:55 -0700 | [diff] [blame] | 314 | // Reads the class names (java.lang.Object) and returns a set of descriptors (Ljava/lang/Object;) |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 315 | std::set<std::string>* ReadImageClassesFromZip(const char* zip_filename, |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 316 | const char* image_classes_filename, |
| 317 | std::string* error_msg) { |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 318 | std::unique_ptr<ZipArchive> zip_archive(ZipArchive::Open(zip_filename, error_msg)); |
Kenny Root | 5131638 | 2014-05-13 14:59:37 -0700 | [diff] [blame] | 319 | if (zip_archive.get() == nullptr) { |
| 320 | return nullptr; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 321 | } |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 322 | std::unique_ptr<ZipEntry> zip_entry(zip_archive->Find(image_classes_filename, error_msg)); |
Kenny Root | 5131638 | 2014-05-13 14:59:37 -0700 | [diff] [blame] | 323 | if (zip_entry.get() == nullptr) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 324 | *error_msg = StringPrintf("Failed to find '%s' within '%s': %s", image_classes_filename, |
| 325 | zip_filename, error_msg->c_str()); |
Kenny Root | 5131638 | 2014-05-13 14:59:37 -0700 | [diff] [blame] | 326 | return nullptr; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 327 | } |
Brian Carlstrom | 0aa504b | 2014-05-23 02:47:28 -0700 | [diff] [blame] | 328 | std::unique_ptr<MemMap> image_classes_file(zip_entry->ExtractToMemMap(zip_filename, |
| 329 | image_classes_filename, |
| 330 | error_msg)); |
Kenny Root | 5131638 | 2014-05-13 14:59:37 -0700 | [diff] [blame] | 331 | if (image_classes_file.get() == nullptr) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 332 | *error_msg = StringPrintf("Failed to extract '%s' from '%s': %s", image_classes_filename, |
| 333 | zip_filename, error_msg->c_str()); |
Kenny Root | 5131638 | 2014-05-13 14:59:37 -0700 | [diff] [blame] | 334 | return nullptr; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 335 | } |
| 336 | const std::string image_classes_string(reinterpret_cast<char*>(image_classes_file->Begin()), |
| 337 | image_classes_file->Size()); |
| 338 | std::istringstream image_classes_stream(image_classes_string); |
| 339 | return ReadImageClasses(image_classes_stream); |
| 340 | } |
| 341 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 342 | void Compile(const std::string& boot_image_option, |
| 343 | const std::vector<const DexFile*>& dex_files, |
| 344 | const std::string& bitcode_filename, |
| 345 | bool image, |
| 346 | std::unique_ptr<std::set<std::string>>& image_classes, |
| 347 | bool dump_stats, |
| 348 | bool dump_passes, |
| 349 | TimingLogger* timings, |
| 350 | CumulativeLogger* compiler_phases_timings, |
| 351 | const std::string& profile_file) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 352 | // Handle and ClassLoader creation needs to come after Runtime::Create |
Kenny Root | 5131638 | 2014-05-13 14:59:37 -0700 | [diff] [blame] | 353 | jobject class_loader = nullptr; |
Ian Rogers | 3f3d22c | 2013-08-27 18:11:09 -0700 | [diff] [blame] | 354 | Thread* self = Thread::Current(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 355 | if (!boot_image_option.empty()) { |
| 356 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 357 | std::vector<const DexFile*> class_path_files(dex_files); |
| 358 | OpenClassPathFiles(runtime_->GetClassPathString(), class_path_files); |
Ian Rogers | 3f3d22c | 2013-08-27 18:11:09 -0700 | [diff] [blame] | 359 | ScopedObjectAccess soa(self); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 360 | for (size_t i = 0; i < class_path_files.size(); i++) { |
| 361 | class_linker->RegisterDexFile(*class_path_files[i]); |
| 362 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 363 | soa.Env()->AllocObject(WellKnownClasses::dalvik_system_PathClassLoader); |
| 364 | ScopedLocalRef<jobject> class_loader_local(soa.Env(), |
| 365 | soa.Env()->AllocObject(WellKnownClasses::dalvik_system_PathClassLoader)); |
| 366 | class_loader = soa.Env()->NewGlobalRef(class_loader_local.get()); |
| 367 | Runtime::Current()->SetCompileTimeClassPath(class_loader, class_path_files); |
| 368 | } |
| 369 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 370 | driver_.reset(new CompilerDriver(compiler_options_, |
| 371 | verification_results_, |
| 372 | method_inliner_map_, |
| 373 | compiler_kind_, |
| 374 | instruction_set_, |
| 375 | instruction_set_features_, |
| 376 | image, |
| 377 | image_classes.release(), |
| 378 | thread_count_, |
| 379 | dump_stats, |
| 380 | dump_passes, |
| 381 | compiler_phases_timings, |
| 382 | profile_file)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 383 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 384 | driver_->GetCompiler()->SetBitcodeFileName(*driver_, bitcode_filename); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 385 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 386 | driver_->CompileAll(class_loader, dex_files, timings); |
| 387 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 388 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 389 | void PrepareImageWriter(uintptr_t image_base) { |
| 390 | image_writer_.reset(new ImageWriter(*driver_, image_base)); |
| 391 | } |
| 392 | |
| 393 | bool CreateOatFile(const std::vector<const DexFile*>& dex_files, |
| 394 | const std::string& android_root, |
| 395 | bool is_host, |
| 396 | File* oat_file, |
| 397 | const std::string& oat_location, |
| 398 | TimingLogger* timings, |
| 399 | SafeMap<std::string, std::string>* key_value_store) { |
| 400 | CHECK(key_value_store != nullptr); |
| 401 | |
| 402 | TimingLogger::ScopedTiming t2("dex2oat OatWriter", timings); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 403 | std::string image_file_location; |
| 404 | uint32_t image_file_location_oat_checksum = 0; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 405 | uintptr_t image_file_location_oat_data_begin = 0; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 406 | int32_t image_patch_delta = 0; |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 407 | if (!driver_->IsImage()) { |
| 408 | TimingLogger::ScopedTiming t3("Loading image checksum", timings); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 409 | gc::space::ImageSpace* image_space = Runtime::Current()->GetHeap()->GetImageSpace(); |
| 410 | image_file_location_oat_checksum = image_space->GetImageHeader().GetOatChecksum(); |
| 411 | image_file_location_oat_data_begin = |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 412 | reinterpret_cast<uintptr_t>(image_space->GetImageHeader().GetOatDataBegin()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 413 | image_file_location = image_space->GetImageFilename(); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 414 | image_patch_delta = image_space->GetImageHeader().GetPatchDelta(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 415 | } |
| 416 | |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 417 | if (!image_file_location.empty()) { |
| 418 | key_value_store->Put(OatHeader::kImageLocationKey, image_file_location); |
| 419 | } |
| 420 | |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 421 | OatWriter oat_writer(dex_files, image_file_location_oat_checksum, |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 422 | image_file_location_oat_data_begin, |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 423 | image_patch_delta, |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 424 | driver_.get(), |
| 425 | image_writer_.get(), |
| 426 | timings, |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 427 | key_value_store); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 428 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 429 | if (driver_->IsImage()) { |
| 430 | // The OatWriter constructor has already updated offsets in methods and we need to |
| 431 | // prepare method offsets in the image address space for direct method patching. |
| 432 | t2.NewTiming("Preparing image address space"); |
| 433 | if (!image_writer_->PrepareImageAddressSpace()) { |
| 434 | LOG(ERROR) << "Failed to prepare image address space."; |
| 435 | return false; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 436 | } |
| 437 | } |
| 438 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 439 | t2.NewTiming("Writing ELF"); |
| 440 | if (!driver_->WriteElf(android_root, is_host, dex_files, &oat_writer, oat_file)) { |
| 441 | LOG(ERROR) << "Failed to write ELF file " << oat_file->GetPath(); |
| 442 | return false; |
| 443 | } |
| 444 | |
| 445 | // Flush result to disk. |
| 446 | t2.NewTiming("Flushing ELF"); |
| 447 | if (oat_file->Flush() != 0) { |
| 448 | LOG(ERROR) << "Failed to flush ELF file " << oat_file->GetPath(); |
| 449 | return false; |
| 450 | } |
| 451 | |
| 452 | return true; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | bool CreateImageFile(const std::string& image_filename, |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 456 | const std::string& oat_filename, |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 457 | const std::string& oat_location) |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 458 | LOCKS_EXCLUDED(Locks::mutator_lock_) { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 459 | CHECK(image_writer_ != nullptr); |
| 460 | if (!image_writer_->Write(image_filename, oat_filename, oat_location)) { |
| 461 | LOG(ERROR) << "Failed to create image file " << image_filename; |
| 462 | return false; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 463 | } |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 464 | uintptr_t oat_data_begin = image_writer_->GetOatDataBegin(); |
| 465 | |
| 466 | // Destroy ImageWriter before doing FixupElf. |
| 467 | image_writer_.reset(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 468 | |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 469 | std::unique_ptr<File> oat_file(OS::OpenFileReadWrite(oat_filename.c_str())); |
Kenny Root | 5131638 | 2014-05-13 14:59:37 -0700 | [diff] [blame] | 470 | if (oat_file.get() == nullptr) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 471 | PLOG(ERROR) << "Failed to open ELF file: " << oat_filename; |
| 472 | return false; |
| 473 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 474 | if (!ElfWriter::Fixup(oat_file.get(), oat_data_begin)) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 475 | LOG(ERROR) << "Failed to fixup ELF file " << oat_file->GetPath(); |
| 476 | return false; |
| 477 | } |
| 478 | return true; |
| 479 | } |
| 480 | |
| 481 | private: |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 482 | explicit Dex2Oat(const CompilerOptions* compiler_options, |
Nicolas Geoffray | b34f69a | 2014-03-07 15:28:39 +0000 | [diff] [blame] | 483 | Compiler::Kind compiler_kind, |
Brian Carlstrom | 4560248 | 2013-07-21 22:07:55 -0700 | [diff] [blame] | 484 | InstructionSet instruction_set, |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 485 | InstructionSetFeatures instruction_set_features, |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 486 | VerificationResults* verification_results, |
| 487 | DexFileToMethodInlinerMap* method_inliner_map, |
Brian Carlstrom | 0177fe2 | 2013-07-21 12:21:36 -0700 | [diff] [blame] | 488 | size_t thread_count) |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 489 | : compiler_options_(compiler_options), |
Nicolas Geoffray | b34f69a | 2014-03-07 15:28:39 +0000 | [diff] [blame] | 490 | compiler_kind_(compiler_kind), |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 491 | instruction_set_(instruction_set), |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 492 | instruction_set_features_(instruction_set_features), |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 493 | verification_results_(verification_results), |
| 494 | method_inliner_map_(method_inliner_map), |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 495 | runtime_(nullptr), |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 496 | thread_count_(thread_count), |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 497 | start_ns_(NanoTime()), |
| 498 | driver_(nullptr), |
| 499 | image_writer_(nullptr) { |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 500 | CHECK(compiler_options != nullptr); |
| 501 | CHECK(verification_results != nullptr); |
| 502 | CHECK(method_inliner_map != nullptr); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 503 | } |
| 504 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 505 | bool CreateRuntime(const RuntimeOptions& runtime_options, InstructionSet instruction_set) |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 506 | SHARED_TRYLOCK_FUNCTION(true, Locks::mutator_lock_) { |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 507 | if (!Runtime::Create(runtime_options, false)) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 508 | LOG(ERROR) << "Failed to create runtime"; |
| 509 | return false; |
| 510 | } |
| 511 | Runtime* runtime = Runtime::Current(); |
Vladimir Marko | 7624d25 | 2014-05-02 14:40:15 +0100 | [diff] [blame] | 512 | runtime->SetInstructionSet(instruction_set); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 513 | for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) { |
| 514 | Runtime::CalleeSaveType type = Runtime::CalleeSaveType(i); |
| 515 | if (!runtime->HasCalleeSaveMethod(type)) { |
Vladimir Marko | 7624d25 | 2014-05-02 14:40:15 +0100 | [diff] [blame] | 516 | runtime->SetCalleeSaveMethod(runtime->CreateCalleeSaveMethod(type), type); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 517 | } |
| 518 | } |
| 519 | runtime->GetClassLinker()->FixupDexCaches(runtime->GetResolutionMethod()); |
Sebastien Hertz | 4e99b3d | 2014-06-24 14:35:40 +0200 | [diff] [blame] | 520 | runtime->GetClassLinker()->RunRootClinits(); |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 521 | runtime_ = runtime; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 522 | return true; |
| 523 | } |
| 524 | |
| 525 | // Appends to dex_files any elements of class_path that it doesn't already |
| 526 | // contain. This will open those dex files as necessary. |
Brian Carlstrom | 4560248 | 2013-07-21 22:07:55 -0700 | [diff] [blame] | 527 | static void OpenClassPathFiles(const std::string& class_path, |
| 528 | std::vector<const DexFile*>& dex_files) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 529 | std::vector<std::string> parsed; |
| 530 | Split(class_path, ':', parsed); |
| 531 | // Take Locks::mutator_lock_ so that lock ordering on the ClassLinker::dex_lock_ is maintained. |
| 532 | ScopedObjectAccess soa(Thread::Current()); |
| 533 | for (size_t i = 0; i < parsed.size(); ++i) { |
| 534 | if (DexFilesContains(dex_files, parsed[i])) { |
| 535 | continue; |
| 536 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 537 | std::string error_msg; |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 538 | if (!DexFile::Open(parsed[i].c_str(), parsed[i].c_str(), &error_msg, &dex_files)) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 539 | LOG(WARNING) << "Failed to open dex file '" << parsed[i] << "': " << error_msg; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 540 | } |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | // Returns true if dex_files has a dex with the named location. |
Brian Carlstrom | 4560248 | 2013-07-21 22:07:55 -0700 | [diff] [blame] | 545 | static bool DexFilesContains(const std::vector<const DexFile*>& dex_files, |
| 546 | const std::string& location) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 547 | for (size_t i = 0; i < dex_files.size(); ++i) { |
| 548 | if (dex_files[i]->GetLocation() == location) { |
| 549 | return true; |
| 550 | } |
| 551 | } |
| 552 | return false; |
| 553 | } |
| 554 | |
Brian Carlstrom | ae7083d | 2014-02-24 21:56:02 -0800 | [diff] [blame] | 555 | const CompilerOptions* const compiler_options_; |
Nicolas Geoffray | b34f69a | 2014-03-07 15:28:39 +0000 | [diff] [blame] | 556 | const Compiler::Kind compiler_kind_; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 557 | |
| 558 | const InstructionSet instruction_set_; |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 559 | const InstructionSetFeatures instruction_set_features_; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 560 | |
Brian Carlstrom | ae7083d | 2014-02-24 21:56:02 -0800 | [diff] [blame] | 561 | VerificationResults* const verification_results_; |
| 562 | DexFileToMethodInlinerMap* const method_inliner_map_; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 563 | Runtime* runtime_; |
| 564 | size_t thread_count_; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 565 | uint64_t start_ns_; |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 566 | std::unique_ptr<CompilerDriver> driver_; |
| 567 | std::unique_ptr<ImageWriter> image_writer_; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 568 | |
| 569 | DISALLOW_IMPLICIT_CONSTRUCTORS(Dex2Oat); |
| 570 | }; |
| 571 | |
Brian Carlstrom | 3cf59d5 | 2013-11-10 21:04:10 -0800 | [diff] [blame] | 572 | static size_t OpenDexFiles(const std::vector<const char*>& dex_filenames, |
| 573 | const std::vector<const char*>& dex_locations, |
| 574 | std::vector<const DexFile*>& dex_files) { |
| 575 | size_t failure_count = 0; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 576 | for (size_t i = 0; i < dex_filenames.size(); i++) { |
| 577 | const char* dex_filename = dex_filenames[i]; |
| 578 | const char* dex_location = dex_locations[i]; |
Ian Rogers | 740a11d | 2014-01-14 10:11:25 -0800 | [diff] [blame] | 579 | ATRACE_BEGIN(StringPrintf("Opening dex file '%s'", dex_filenames[i]).c_str()); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 580 | std::string error_msg; |
Brian Carlstrom | d5aba59 | 2013-11-12 01:52:44 -0800 | [diff] [blame] | 581 | if (!OS::FileExists(dex_filename)) { |
| 582 | LOG(WARNING) << "Skipping non-existent dex file '" << dex_filename << "'"; |
| 583 | continue; |
| 584 | } |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 585 | if (!DexFile::Open(dex_filename, dex_location, &error_msg, &dex_files)) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 586 | LOG(WARNING) << "Failed to open .dex from file '" << dex_filename << "': " << error_msg; |
Brian Carlstrom | 3cf59d5 | 2013-11-10 21:04:10 -0800 | [diff] [blame] | 587 | ++failure_count; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 588 | } |
Ian Rogers | 740a11d | 2014-01-14 10:11:25 -0800 | [diff] [blame] | 589 | ATRACE_END(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 590 | } |
Brian Carlstrom | 3cf59d5 | 2013-11-10 21:04:10 -0800 | [diff] [blame] | 591 | return failure_count; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | // The primary goal of the watchdog is to prevent stuck build servers |
| 595 | // during development when fatal aborts lead to a cascade of failures |
| 596 | // that result in a deadlock. |
| 597 | class WatchDog { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 598 | // WatchDog defines its own CHECK_PTHREAD_CALL to avoid using Log which uses locks |
| 599 | #undef CHECK_PTHREAD_CALL |
| 600 | #define CHECK_WATCH_DOG_PTHREAD_CALL(call, args, what) \ |
| 601 | do { \ |
| 602 | int rc = call args; \ |
| 603 | if (rc != 0) { \ |
| 604 | errno = rc; \ |
| 605 | std::string message(# call); \ |
| 606 | message += " failed for "; \ |
| 607 | message += reason; \ |
| 608 | Fatal(message); \ |
| 609 | } \ |
| 610 | } while (false) |
| 611 | |
| 612 | public: |
Brian Carlstrom | 93ba893 | 2013-07-17 21:31:49 -0700 | [diff] [blame] | 613 | explicit WatchDog(bool is_watch_dog_enabled) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 614 | is_watch_dog_enabled_ = is_watch_dog_enabled; |
| 615 | if (!is_watch_dog_enabled_) { |
| 616 | return; |
| 617 | } |
| 618 | shutting_down_ = false; |
| 619 | const char* reason = "dex2oat watch dog thread startup"; |
Kenny Root | 5131638 | 2014-05-13 14:59:37 -0700 | [diff] [blame] | 620 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_init, (&mutex_, nullptr), reason); |
| 621 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_cond_init, (&cond_, nullptr), reason); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 622 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_attr_init, (&attr_), reason); |
| 623 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_create, (&pthread_, &attr_, &CallBack, this), reason); |
| 624 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_attr_destroy, (&attr_), reason); |
| 625 | } |
| 626 | ~WatchDog() { |
| 627 | if (!is_watch_dog_enabled_) { |
| 628 | return; |
| 629 | } |
| 630 | const char* reason = "dex2oat watch dog thread shutdown"; |
| 631 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_lock, (&mutex_), reason); |
| 632 | shutting_down_ = true; |
| 633 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_cond_signal, (&cond_), reason); |
| 634 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_unlock, (&mutex_), reason); |
| 635 | |
Kenny Root | 5131638 | 2014-05-13 14:59:37 -0700 | [diff] [blame] | 636 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_join, (pthread_, nullptr), reason); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 637 | |
| 638 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_cond_destroy, (&cond_), reason); |
| 639 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_destroy, (&mutex_), reason); |
| 640 | } |
| 641 | |
| 642 | private: |
| 643 | static void* CallBack(void* arg) { |
| 644 | WatchDog* self = reinterpret_cast<WatchDog*>(arg); |
| 645 | ::art::SetThreadName("dex2oat watch dog"); |
| 646 | self->Wait(); |
Kenny Root | 5131638 | 2014-05-13 14:59:37 -0700 | [diff] [blame] | 647 | return nullptr; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | static void Message(char severity, const std::string& message) { |
| 651 | // TODO: Remove when we switch to LOG when we can guarantee it won't prevent shutdown in error |
| 652 | // cases. |
| 653 | fprintf(stderr, "dex2oat%s %c %d %d %s\n", |
| 654 | kIsDebugBuild ? "d" : "", |
| 655 | severity, |
| 656 | getpid(), |
| 657 | GetTid(), |
| 658 | message.c_str()); |
| 659 | } |
| 660 | |
| 661 | static void Warn(const std::string& message) { |
| 662 | Message('W', message); |
| 663 | } |
| 664 | |
Ian Rogers | 7223d44 | 2014-10-10 20:05:39 -0700 | [diff] [blame] | 665 | [[noreturn]] static void Fatal(const std::string& message) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 666 | Message('F', message); |
| 667 | exit(1); |
| 668 | } |
| 669 | |
| 670 | void Wait() { |
| 671 | bool warning = true; |
| 672 | CHECK_GT(kWatchDogTimeoutSeconds, kWatchDogWarningSeconds); |
| 673 | // TODO: tune the multiplier for GC verification, the following is just to make the timeout |
| 674 | // large. |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 675 | int64_t multiplier = kVerifyObjectSupport > kVerifyObjectModeFast ? 100 : 1; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 676 | timespec warning_ts; |
| 677 | InitTimeSpec(true, CLOCK_REALTIME, multiplier * kWatchDogWarningSeconds * 1000, 0, &warning_ts); |
| 678 | timespec timeout_ts; |
| 679 | InitTimeSpec(true, CLOCK_REALTIME, multiplier * kWatchDogTimeoutSeconds * 1000, 0, &timeout_ts); |
| 680 | const char* reason = "dex2oat watch dog thread waiting"; |
| 681 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_lock, (&mutex_), reason); |
| 682 | while (!shutting_down_) { |
| 683 | int rc = TEMP_FAILURE_RETRY(pthread_cond_timedwait(&cond_, &mutex_, |
| 684 | warning ? &warning_ts |
| 685 | : &timeout_ts)); |
| 686 | if (rc == ETIMEDOUT) { |
| 687 | std::string message(StringPrintf("dex2oat did not finish after %d seconds", |
| 688 | warning ? kWatchDogWarningSeconds |
| 689 | : kWatchDogTimeoutSeconds)); |
| 690 | if (warning) { |
| 691 | Warn(message.c_str()); |
| 692 | warning = false; |
| 693 | } else { |
| 694 | Fatal(message.c_str()); |
| 695 | } |
| 696 | } else if (rc != 0) { |
| 697 | std::string message(StringPrintf("pthread_cond_timedwait failed: %s", |
| 698 | strerror(errno))); |
| 699 | Fatal(message.c_str()); |
| 700 | } |
| 701 | } |
| 702 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_unlock, (&mutex_), reason); |
| 703 | } |
| 704 | |
| 705 | // 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] | 706 | // Debug builds are slower so they have larger timeouts. |
| 707 | static const unsigned int kSlowdownFactor = kIsDebugBuild ? 5U : 1U; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 708 | #if ART_USE_PORTABLE_COMPILER |
Mathieu Chartier | 13b9f43 | 2014-09-09 17:26:58 -0700 | [diff] [blame] | 709 | // 2 minutes scaled by kSlowdownFactor. |
| 710 | static const unsigned int kWatchDogWarningSeconds = kSlowdownFactor * 2 * 60; |
| 711 | // 30 minutes scaled by kSlowdownFactor. |
| 712 | static const unsigned int kWatchDogTimeoutSeconds = kSlowdownFactor * 30 * 60; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 713 | #else |
Mathieu Chartier | 13b9f43 | 2014-09-09 17:26:58 -0700 | [diff] [blame] | 714 | // 1 minutes scaled by kSlowdownFactor. |
| 715 | static const unsigned int kWatchDogWarningSeconds = kSlowdownFactor * 1 * 60; |
| 716 | // 6 minutes scaled by kSlowdownFactor. |
| 717 | static const unsigned int kWatchDogTimeoutSeconds = kSlowdownFactor * 6 * 60; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 718 | #endif |
| 719 | |
| 720 | bool is_watch_dog_enabled_; |
| 721 | bool shutting_down_; |
| 722 | // TODO: Switch to Mutex when we can guarantee it won't prevent shutdown in error cases. |
| 723 | pthread_mutex_t mutex_; |
| 724 | pthread_cond_t cond_; |
| 725 | pthread_attr_t attr_; |
| 726 | pthread_t pthread_; |
| 727 | }; |
| 728 | const unsigned int WatchDog::kWatchDogWarningSeconds; |
| 729 | const unsigned int WatchDog::kWatchDogTimeoutSeconds; |
| 730 | |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 731 | // Given a set of instruction features from the build, parse it. The |
| 732 | // input 'str' is a comma separated list of feature names. Parse it and |
| 733 | // return the InstructionSetFeatures object. |
| 734 | static InstructionSetFeatures ParseFeatureList(std::string str) { |
| 735 | InstructionSetFeatures result; |
| 736 | typedef std::vector<std::string> FeatureList; |
| 737 | FeatureList features; |
| 738 | Split(str, ',', features); |
| 739 | for (FeatureList::iterator i = features.begin(); i != features.end(); i++) { |
| 740 | std::string feature = Trim(*i); |
| 741 | if (feature == "default") { |
| 742 | // Nothing to do. |
| 743 | } else if (feature == "div") { |
| 744 | // Supports divide instruction. |
| 745 | result.SetHasDivideInstruction(true); |
| 746 | } else if (feature == "nodiv") { |
| 747 | // Turn off support for divide instruction. |
| 748 | result.SetHasDivideInstruction(false); |
Vladimir Marko | 674744e | 2014-04-24 15:18:26 +0100 | [diff] [blame] | 749 | } else if (feature == "lpae") { |
| 750 | // Supports Large Physical Address Extension. |
| 751 | result.SetHasLpae(true); |
| 752 | } else if (feature == "nolpae") { |
| 753 | // Turn off support for Large Physical Address Extension. |
| 754 | result.SetHasLpae(false); |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 755 | } else { |
| 756 | Usage("Unknown instruction set feature: '%s'", feature.c_str()); |
| 757 | } |
| 758 | } |
| 759 | // others... |
| 760 | return result; |
| 761 | } |
| 762 | |
Calin Juravle | c1b643c | 2014-05-30 23:44:11 +0100 | [diff] [blame] | 763 | void ParseStringAfterChar(const std::string& s, char c, std::string* parsed_value) { |
| 764 | std::string::size_type colon = s.find(c); |
| 765 | if (colon == std::string::npos) { |
| 766 | Usage("Missing char %c in option %s\n", c, s.c_str()); |
| 767 | } |
| 768 | // Add one to remove the char we were trimming until. |
| 769 | *parsed_value = s.substr(colon + 1); |
| 770 | } |
| 771 | |
| 772 | void ParseDouble(const std::string& option, char after_char, |
| 773 | double min, double max, double* parsed_value) { |
| 774 | std::string substring; |
| 775 | ParseStringAfterChar(option, after_char, &substring); |
| 776 | bool sane_val = true; |
| 777 | double value; |
| 778 | if (false) { |
| 779 | // TODO: this doesn't seem to work on the emulator. b/15114595 |
| 780 | std::stringstream iss(substring); |
| 781 | iss >> value; |
| 782 | // Ensure that we have a value, there was no cruft after it and it satisfies a sensible range. |
| 783 | sane_val = iss.eof() && (value >= min) && (value <= max); |
| 784 | } else { |
| 785 | char* end = nullptr; |
| 786 | value = strtod(substring.c_str(), &end); |
| 787 | sane_val = *end == '\0' && value >= min && value <= max; |
| 788 | } |
| 789 | if (!sane_val) { |
| 790 | Usage("Invalid double value %s for option %s\n", substring.c_str(), option.c_str()); |
| 791 | } |
| 792 | *parsed_value = value; |
| 793 | } |
| 794 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 795 | static int dex2oat(int argc, char** argv) { |
Vladimir Marko | f94b781 | 2014-06-05 15:48:04 +0100 | [diff] [blame] | 796 | #if defined(__linux__) && defined(__arm__) |
| 797 | int major, minor; |
| 798 | struct utsname uts; |
| 799 | if (uname(&uts) != -1 && |
| 800 | sscanf(uts.release, "%d.%d", &major, &minor) == 2 && |
| 801 | ((major < 3) || ((major == 3) && (minor < 4)))) { |
| 802 | // Kernels before 3.4 don't handle the ASLR well and we can run out of address |
| 803 | // space (http://b/13564922). Work around the issue by inhibiting further mmap() randomization. |
| 804 | int old_personality = personality(0xffffffff); |
| 805 | if ((old_personality & ADDR_NO_RANDOMIZE) == 0) { |
| 806 | int new_personality = personality(old_personality | ADDR_NO_RANDOMIZE); |
| 807 | if (new_personality == -1) { |
| 808 | LOG(WARNING) << "personality(. | ADDR_NO_RANDOMIZE) failed."; |
| 809 | } |
| 810 | } |
| 811 | } |
| 812 | #endif |
| 813 | |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 814 | original_argc = argc; |
| 815 | original_argv = argv; |
| 816 | |
Ian Rogers | 5fe9af7 | 2013-11-14 00:17:20 -0800 | [diff] [blame] | 817 | TimingLogger timings("compiler", false, false); |
Nicolas Geoffray | ea3fa0b | 2014-02-10 11:59:41 +0000 | [diff] [blame] | 818 | CumulativeLogger compiler_phases_timings("compilation times"); |
Brian Carlstrom | 4560248 | 2013-07-21 22:07:55 -0700 | [diff] [blame] | 819 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 820 | InitLogging(argv); |
| 821 | |
| 822 | // Skip over argv[0]. |
| 823 | argv++; |
| 824 | argc--; |
| 825 | |
| 826 | if (argc == 0) { |
Brian Carlstrom | e0948e1 | 2013-08-29 09:36:15 -0700 | [diff] [blame] | 827 | Usage("No arguments specified"); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 828 | } |
| 829 | |
| 830 | std::vector<const char*> dex_filenames; |
| 831 | std::vector<const char*> dex_locations; |
| 832 | int zip_fd = -1; |
| 833 | std::string zip_location; |
| 834 | std::string oat_filename; |
| 835 | std::string oat_symbols; |
| 836 | std::string oat_location; |
| 837 | int oat_fd = -1; |
| 838 | std::string bitcode_filename; |
Kenny Root | 5131638 | 2014-05-13 14:59:37 -0700 | [diff] [blame] | 839 | const char* image_classes_zip_filename = nullptr; |
| 840 | const char* image_classes_filename = nullptr; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 841 | std::string image_filename; |
| 842 | std::string boot_image_filename; |
| 843 | uintptr_t image_base = 0; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 844 | std::string android_root; |
| 845 | std::vector<const char*> runtime_args; |
| 846 | int thread_count = sysconf(_SC_NPROCESSORS_CONF); |
Nicolas Geoffray | b34f69a | 2014-03-07 15:28:39 +0000 | [diff] [blame] | 847 | Compiler::Kind compiler_kind = kUsePortableCompiler |
| 848 | ? Compiler::kPortable |
| 849 | : Compiler::kQuick; |
Kenny Root | 5131638 | 2014-05-13 14:59:37 -0700 | [diff] [blame] | 850 | const char* compiler_filter_string = nullptr; |
Igor Murashkin | d6dee67 | 2014-10-16 18:36:16 -0700 | [diff] [blame^] | 851 | bool compile_pic = false; |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 852 | int huge_method_threshold = CompilerOptions::kDefaultHugeMethodThreshold; |
| 853 | int large_method_threshold = CompilerOptions::kDefaultLargeMethodThreshold; |
| 854 | int small_method_threshold = CompilerOptions::kDefaultSmallMethodThreshold; |
| 855 | int tiny_method_threshold = CompilerOptions::kDefaultTinyMethodThreshold; |
| 856 | int num_dex_methods_threshold = CompilerOptions::kDefaultNumDexMethodsThreshold; |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 857 | |
Brian Carlstrom | 1bd2ceb | 2013-11-06 00:29:48 -0800 | [diff] [blame] | 858 | // Take the default set of instruction features from the build. |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 859 | InstructionSetFeatures instruction_set_features = |
Ian Rogers | 8afeb85 | 2014-04-02 14:55:49 -0700 | [diff] [blame] | 860 | ParseFeatureList(Runtime::GetDefaultInstructionSetFeatures()); |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 861 | |
Andreas Gampe | 91268c1 | 2014-04-03 17:50:24 -0700 | [diff] [blame] | 862 | InstructionSet instruction_set = kRuntimeISA; |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 863 | |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 864 | // Profile file to use |
| 865 | std::string profile_file; |
Calin Juravle | c1b643c | 2014-05-30 23:44:11 +0100 | [diff] [blame] | 866 | double top_k_profile_threshold = CompilerOptions::kDefaultTopKProfileThreshold; |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 867 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 868 | bool is_host = false; |
Ian Rogers | e732ef1 | 2013-10-09 15:22:24 -0700 | [diff] [blame] | 869 | bool dump_stats = false; |
Ian Rogers | 4639860 | 2013-08-20 07:50:36 -0700 | [diff] [blame] | 870 | bool dump_timing = false; |
Nicolas Geoffray | ea3fa0b | 2014-02-10 11:59:41 +0000 | [diff] [blame] | 871 | bool dump_passes = false; |
Razvan A Lupusoru | bd25d4b | 2014-07-02 18:16:51 -0700 | [diff] [blame] | 872 | bool print_pass_options = false; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 873 | bool include_patch_information = CompilerOptions::kDefaultIncludePatchInformation; |
Alex Light | 78382fa | 2014-06-06 15:45:32 -0700 | [diff] [blame] | 874 | bool include_debug_symbols = kIsDebugBuild; |
Ian Rogers | 4639860 | 2013-08-20 07:50:36 -0700 | [diff] [blame] | 875 | bool dump_slow_timing = kIsDebugBuild; |
Andreas Gampe | bf40ddb | 2014-07-17 10:18:46 -0700 | [diff] [blame] | 876 | bool watch_dog_enabled = true; |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 877 | bool generate_gdb_information = kIsDebugBuild; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 878 | |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 879 | // Checks are all explicit until we know the architecture. |
| 880 | bool implicit_null_checks = false; |
| 881 | bool implicit_so_checks = false; |
| 882 | bool implicit_suspend_checks = false; |
Andreas Gampe | 5655e84 | 2014-06-17 16:36:07 -0700 | [diff] [blame] | 883 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 884 | for (int i = 0; i < argc; i++) { |
| 885 | const StringPiece option(argv[i]); |
Ian Rogers | b9beb2e | 2014-05-09 16:57:40 -0700 | [diff] [blame] | 886 | const bool log_options = false; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 887 | if (log_options) { |
| 888 | LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i]; |
| 889 | } |
| 890 | if (option.starts_with("--dex-file=")) { |
| 891 | dex_filenames.push_back(option.substr(strlen("--dex-file=")).data()); |
| 892 | } else if (option.starts_with("--dex-location=")) { |
| 893 | dex_locations.push_back(option.substr(strlen("--dex-location=")).data()); |
| 894 | } else if (option.starts_with("--zip-fd=")) { |
| 895 | const char* zip_fd_str = option.substr(strlen("--zip-fd=")).data(); |
| 896 | if (!ParseInt(zip_fd_str, &zip_fd)) { |
Brian Carlstrom | e0948e1 | 2013-08-29 09:36:15 -0700 | [diff] [blame] | 897 | Usage("Failed to parse --zip-fd argument '%s' as an integer", zip_fd_str); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 898 | } |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 899 | if (zip_fd < 0) { |
| 900 | Usage("--zip-fd passed a negative value %d", zip_fd); |
| 901 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 902 | } else if (option.starts_with("--zip-location=")) { |
| 903 | zip_location = option.substr(strlen("--zip-location=")).data(); |
| 904 | } else if (option.starts_with("--oat-file=")) { |
| 905 | oat_filename = option.substr(strlen("--oat-file=")).data(); |
| 906 | } else if (option.starts_with("--oat-symbols=")) { |
| 907 | oat_symbols = option.substr(strlen("--oat-symbols=")).data(); |
| 908 | } else if (option.starts_with("--oat-fd=")) { |
| 909 | const char* oat_fd_str = option.substr(strlen("--oat-fd=")).data(); |
| 910 | if (!ParseInt(oat_fd_str, &oat_fd)) { |
Brian Carlstrom | e0948e1 | 2013-08-29 09:36:15 -0700 | [diff] [blame] | 911 | Usage("Failed to parse --oat-fd argument '%s' as an integer", oat_fd_str); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 912 | } |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 913 | if (oat_fd < 0) { |
| 914 | Usage("--oat-fd passed a negative value %d", oat_fd); |
| 915 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 916 | } else if (option == "--watch-dog") { |
| 917 | watch_dog_enabled = true; |
| 918 | } else if (option == "--no-watch-dog") { |
| 919 | watch_dog_enabled = false; |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 920 | } else if (option == "--gen-gdb-info") { |
| 921 | generate_gdb_information = true; |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 922 | // Debug symbols are needed for gdb information. |
| 923 | include_debug_symbols = true; |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 924 | } else if (option == "--no-gen-gdb-info") { |
| 925 | generate_gdb_information = false; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 926 | } else if (option.starts_with("-j")) { |
| 927 | const char* thread_count_str = option.substr(strlen("-j")).data(); |
| 928 | if (!ParseInt(thread_count_str, &thread_count)) { |
Brian Carlstrom | e0948e1 | 2013-08-29 09:36:15 -0700 | [diff] [blame] | 929 | Usage("Failed to parse -j argument '%s' as an integer", thread_count_str); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 930 | } |
| 931 | } else if (option.starts_with("--oat-location=")) { |
| 932 | oat_location = option.substr(strlen("--oat-location=")).data(); |
| 933 | } else if (option.starts_with("--bitcode=")) { |
| 934 | bitcode_filename = option.substr(strlen("--bitcode=")).data(); |
| 935 | } else if (option.starts_with("--image=")) { |
| 936 | image_filename = option.substr(strlen("--image=")).data(); |
| 937 | } else if (option.starts_with("--image-classes=")) { |
| 938 | image_classes_filename = option.substr(strlen("--image-classes=")).data(); |
| 939 | } else if (option.starts_with("--image-classes-zip=")) { |
| 940 | image_classes_zip_filename = option.substr(strlen("--image-classes-zip=")).data(); |
| 941 | } else if (option.starts_with("--base=")) { |
| 942 | const char* image_base_str = option.substr(strlen("--base=")).data(); |
| 943 | char* end; |
| 944 | image_base = strtoul(image_base_str, &end, 16); |
| 945 | if (end == image_base_str || *end != '\0') { |
| 946 | Usage("Failed to parse hexadecimal value for option %s", option.data()); |
| 947 | } |
| 948 | } else if (option.starts_with("--boot-image=")) { |
| 949 | boot_image_filename = option.substr(strlen("--boot-image=")).data(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 950 | } else if (option.starts_with("--android-root=")) { |
| 951 | android_root = option.substr(strlen("--android-root=")).data(); |
| 952 | } else if (option.starts_with("--instruction-set=")) { |
| 953 | StringPiece instruction_set_str = option.substr(strlen("--instruction-set=")).data(); |
| 954 | if (instruction_set_str == "arm") { |
| 955 | instruction_set = kThumb2; |
Stuart Monteith | b95a534 | 2014-03-12 13:32:32 +0000 | [diff] [blame] | 956 | } else if (instruction_set_str == "arm64") { |
| 957 | instruction_set = kArm64; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 958 | } else if (instruction_set_str == "mips") { |
| 959 | instruction_set = kMips; |
| 960 | } else if (instruction_set_str == "x86") { |
| 961 | instruction_set = kX86; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 962 | } else if (instruction_set_str == "x86_64") { |
| 963 | instruction_set = kX86_64; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 964 | } |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 965 | } else if (option.starts_with("--instruction-set-features=")) { |
| 966 | StringPiece str = option.substr(strlen("--instruction-set-features=")).data(); |
| 967 | instruction_set_features = ParseFeatureList(str.as_string()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 968 | } else if (option.starts_with("--compiler-backend=")) { |
| 969 | StringPiece backend_str = option.substr(strlen("--compiler-backend=")).data(); |
| 970 | if (backend_str == "Quick") { |
Nicolas Geoffray | b34f69a | 2014-03-07 15:28:39 +0000 | [diff] [blame] | 971 | compiler_kind = Compiler::kQuick; |
| 972 | } else if (backend_str == "Optimizing") { |
| 973 | compiler_kind = Compiler::kOptimizing; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 974 | } else if (backend_str == "Portable") { |
Nicolas Geoffray | b34f69a | 2014-03-07 15:28:39 +0000 | [diff] [blame] | 975 | compiler_kind = Compiler::kPortable; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 976 | } |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 977 | } else if (option.starts_with("--compiler-filter=")) { |
| 978 | compiler_filter_string = option.substr(strlen("--compiler-filter=")).data(); |
Igor Murashkin | d6dee67 | 2014-10-16 18:36:16 -0700 | [diff] [blame^] | 979 | } else if (option == "--compile-pic") { |
| 980 | compile_pic = true; |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 981 | } else if (option.starts_with("--huge-method-max=")) { |
| 982 | const char* threshold = option.substr(strlen("--huge-method-max=")).data(); |
| 983 | if (!ParseInt(threshold, &huge_method_threshold)) { |
| 984 | Usage("Failed to parse --huge-method-max '%s' as an integer", threshold); |
| 985 | } |
| 986 | if (huge_method_threshold < 0) { |
| 987 | Usage("--huge-method-max passed a negative value %s", huge_method_threshold); |
| 988 | } |
| 989 | } else if (option.starts_with("--large-method-max=")) { |
| 990 | const char* threshold = option.substr(strlen("--large-method-max=")).data(); |
| 991 | if (!ParseInt(threshold, &large_method_threshold)) { |
| 992 | Usage("Failed to parse --large-method-max '%s' as an integer", threshold); |
| 993 | } |
| 994 | if (large_method_threshold < 0) { |
| 995 | Usage("--large-method-max passed a negative value %s", large_method_threshold); |
| 996 | } |
| 997 | } else if (option.starts_with("--small-method-max=")) { |
| 998 | const char* threshold = option.substr(strlen("--small-method-max=")).data(); |
| 999 | if (!ParseInt(threshold, &small_method_threshold)) { |
| 1000 | Usage("Failed to parse --small-method-max '%s' as an integer", threshold); |
| 1001 | } |
| 1002 | if (small_method_threshold < 0) { |
| 1003 | Usage("--small-method-max passed a negative value %s", small_method_threshold); |
| 1004 | } |
| 1005 | } else if (option.starts_with("--tiny-method-max=")) { |
| 1006 | const char* threshold = option.substr(strlen("--tiny-method-max=")).data(); |
| 1007 | if (!ParseInt(threshold, &tiny_method_threshold)) { |
| 1008 | Usage("Failed to parse --tiny-method-max '%s' as an integer", threshold); |
| 1009 | } |
| 1010 | if (tiny_method_threshold < 0) { |
| 1011 | Usage("--tiny-method-max passed a negative value %s", tiny_method_threshold); |
| 1012 | } |
| 1013 | } else if (option.starts_with("--num-dex-methods=")) { |
| 1014 | const char* threshold = option.substr(strlen("--num-dex-methods=")).data(); |
| 1015 | if (!ParseInt(threshold, &num_dex_methods_threshold)) { |
| 1016 | Usage("Failed to parse --num-dex-methods '%s' as an integer", threshold); |
| 1017 | } |
| 1018 | if (num_dex_methods_threshold < 0) { |
| 1019 | Usage("--num-dex-methods passed a negative value %s", num_dex_methods_threshold); |
| 1020 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1021 | } else if (option == "--host") { |
| 1022 | is_host = true; |
| 1023 | } else if (option == "--runtime-arg") { |
| 1024 | if (++i >= argc) { |
| 1025 | Usage("Missing required argument for --runtime-arg"); |
| 1026 | } |
| 1027 | if (log_options) { |
| 1028 | LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i]; |
| 1029 | } |
| 1030 | runtime_args.push_back(argv[i]); |
Ian Rogers | 4639860 | 2013-08-20 07:50:36 -0700 | [diff] [blame] | 1031 | } else if (option == "--dump-timing") { |
| 1032 | dump_timing = true; |
Nicolas Geoffray | ea3fa0b | 2014-02-10 11:59:41 +0000 | [diff] [blame] | 1033 | } else if (option == "--dump-passes") { |
| 1034 | dump_passes = true; |
Ian Rogers | e732ef1 | 2013-10-09 15:22:24 -0700 | [diff] [blame] | 1035 | } else if (option == "--dump-stats") { |
| 1036 | dump_stats = true; |
Alex Light | 78382fa | 2014-06-06 15:45:32 -0700 | [diff] [blame] | 1037 | } else if (option == "--include-debug-symbols" || option == "--no-strip-symbols") { |
| 1038 | include_debug_symbols = true; |
| 1039 | } else if (option == "--no-include-debug-symbols" || option == "--strip-symbols") { |
| 1040 | include_debug_symbols = false; |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1041 | generate_gdb_information = false; // Depends on debug symbols, see above. |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 1042 | } else if (option.starts_with("--profile-file=")) { |
| 1043 | profile_file = option.substr(strlen("--profile-file=")).data(); |
| 1044 | VLOG(compiler) << "dex2oat: profile file is " << profile_file; |
| 1045 | } else if (option == "--no-profile-file") { |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 1046 | // No profile |
Calin Juravle | c1b643c | 2014-05-30 23:44:11 +0100 | [diff] [blame] | 1047 | } else if (option.starts_with("--top-k-profile-threshold=")) { |
Calin Juravle | 44c5ee7 | 2014-07-02 14:00:33 +0100 | [diff] [blame] | 1048 | ParseDouble(option.data(), '=', 0.0, 100.0, &top_k_profile_threshold); |
Chao-ying Fu | cd8ce66 | 2014-03-11 14:57:19 -0700 | [diff] [blame] | 1049 | } else if (option == "--print-pass-names") { |
Jean Christophe Beyler | 2469e60 | 2014-05-06 20:36:55 -0700 | [diff] [blame] | 1050 | PassDriverMEOpts::PrintPassNames(); |
Chao-ying Fu | cd8ce66 | 2014-03-11 14:57:19 -0700 | [diff] [blame] | 1051 | } else if (option.starts_with("--disable-passes=")) { |
| 1052 | std::string disable_passes = option.substr(strlen("--disable-passes=")).data(); |
Jean Christophe Beyler | 2469e60 | 2014-05-06 20:36:55 -0700 | [diff] [blame] | 1053 | PassDriverMEOpts::CreateDefaultPassList(disable_passes); |
Jean Christophe Beyler | 8bcecce | 2014-04-29 13:42:08 -0700 | [diff] [blame] | 1054 | } else if (option.starts_with("--print-passes=")) { |
| 1055 | std::string print_passes = option.substr(strlen("--print-passes=")).data(); |
Jean Christophe Beyler | 2469e60 | 2014-05-06 20:36:55 -0700 | [diff] [blame] | 1056 | PassDriverMEOpts::SetPrintPassList(print_passes); |
Jean Christophe Beyler | 8bcecce | 2014-04-29 13:42:08 -0700 | [diff] [blame] | 1057 | } else if (option == "--print-all-passes") { |
Jean Christophe Beyler | 2469e60 | 2014-05-06 20:36:55 -0700 | [diff] [blame] | 1058 | PassDriverMEOpts::SetPrintAllPasses(); |
Jean Christophe Beyler | 8bcecce | 2014-04-29 13:42:08 -0700 | [diff] [blame] | 1059 | } else if (option.starts_with("--dump-cfg-passes=")) { |
| 1060 | std::string dump_passes = option.substr(strlen("--dump-cfg-passes=")).data(); |
Jean Christophe Beyler | 2469e60 | 2014-05-06 20:36:55 -0700 | [diff] [blame] | 1061 | PassDriverMEOpts::SetDumpPassList(dump_passes); |
Razvan A Lupusoru | bd25d4b | 2014-07-02 18:16:51 -0700 | [diff] [blame] | 1062 | } else if (option == "--print-pass-options") { |
| 1063 | print_pass_options = true; |
| 1064 | } else if (option.starts_with("--pass-options=")) { |
| 1065 | std::string options = option.substr(strlen("--pass-options=")).data(); |
| 1066 | PassDriverMEOpts::SetOverriddenPassOptions(options); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1067 | } else if (option == "--include-patch-information") { |
| 1068 | include_patch_information = true; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1069 | } else if (option == "--no-include-patch-information") { |
| 1070 | include_patch_information = false; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1071 | } else { |
Brian Carlstrom | e0948e1 | 2013-08-29 09:36:15 -0700 | [diff] [blame] | 1072 | Usage("Unknown argument %s", option.data()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1073 | } |
| 1074 | } |
| 1075 | |
| 1076 | if (oat_filename.empty() && oat_fd == -1) { |
| 1077 | Usage("Output must be supplied with either --oat-file or --oat-fd"); |
| 1078 | } |
| 1079 | |
| 1080 | if (!oat_filename.empty() && oat_fd != -1) { |
| 1081 | Usage("--oat-file should not be used with --oat-fd"); |
| 1082 | } |
| 1083 | |
| 1084 | if (!oat_symbols.empty() && oat_fd != -1) { |
| 1085 | Usage("--oat-symbols should not be used with --oat-fd"); |
| 1086 | } |
| 1087 | |
| 1088 | if (!oat_symbols.empty() && is_host) { |
| 1089 | Usage("--oat-symbols should not be used with --host"); |
| 1090 | } |
| 1091 | |
| 1092 | if (oat_fd != -1 && !image_filename.empty()) { |
| 1093 | Usage("--oat-fd should not be used with --image"); |
| 1094 | } |
| 1095 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1096 | if (android_root.empty()) { |
| 1097 | const char* android_root_env_var = getenv("ANDROID_ROOT"); |
Kenny Root | 5131638 | 2014-05-13 14:59:37 -0700 | [diff] [blame] | 1098 | if (android_root_env_var == nullptr) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1099 | Usage("--android-root unspecified and ANDROID_ROOT not set"); |
| 1100 | } |
| 1101 | android_root += android_root_env_var; |
| 1102 | } |
| 1103 | |
| 1104 | bool image = (!image_filename.empty()); |
| 1105 | if (!image && boot_image_filename.empty()) { |
Ian Rogers | 8872358 | 2014-06-13 11:38:54 -0700 | [diff] [blame] | 1106 | boot_image_filename += android_root; |
Brian Carlstrom | 3ac05bb | 2014-05-13 19:31:38 -0700 | [diff] [blame] | 1107 | boot_image_filename += "/framework/boot.art"; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1108 | } |
| 1109 | std::string boot_image_option; |
| 1110 | if (!boot_image_filename.empty()) { |
| 1111 | boot_image_option += "-Ximage:"; |
| 1112 | boot_image_option += boot_image_filename; |
| 1113 | } |
| 1114 | |
Kenny Root | 5131638 | 2014-05-13 14:59:37 -0700 | [diff] [blame] | 1115 | if (image_classes_filename != nullptr && !image) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1116 | Usage("--image-classes should only be used with --image"); |
| 1117 | } |
| 1118 | |
Kenny Root | 5131638 | 2014-05-13 14:59:37 -0700 | [diff] [blame] | 1119 | if (image_classes_filename != nullptr && !boot_image_option.empty()) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1120 | Usage("--image-classes should not be used with --boot-image"); |
| 1121 | } |
| 1122 | |
Kenny Root | 5131638 | 2014-05-13 14:59:37 -0700 | [diff] [blame] | 1123 | if (image_classes_zip_filename != nullptr && image_classes_filename == nullptr) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1124 | Usage("--image-classes-zip should be used with --image-classes"); |
| 1125 | } |
| 1126 | |
| 1127 | if (dex_filenames.empty() && zip_fd == -1) { |
| 1128 | Usage("Input must be supplied with either --dex-file or --zip-fd"); |
| 1129 | } |
| 1130 | |
| 1131 | if (!dex_filenames.empty() && zip_fd != -1) { |
| 1132 | Usage("--dex-file should not be used with --zip-fd"); |
| 1133 | } |
| 1134 | |
| 1135 | if (!dex_filenames.empty() && !zip_location.empty()) { |
| 1136 | Usage("--dex-file should not be used with --zip-location"); |
| 1137 | } |
| 1138 | |
| 1139 | if (dex_locations.empty()) { |
| 1140 | for (size_t i = 0; i < dex_filenames.size(); i++) { |
| 1141 | dex_locations.push_back(dex_filenames[i]); |
| 1142 | } |
| 1143 | } else if (dex_locations.size() != dex_filenames.size()) { |
| 1144 | Usage("--dex-location arguments do not match --dex-file arguments"); |
| 1145 | } |
| 1146 | |
| 1147 | if (zip_fd != -1 && zip_location.empty()) { |
| 1148 | Usage("--zip-location should be supplied with --zip-fd"); |
| 1149 | } |
| 1150 | |
| 1151 | if (boot_image_option.empty()) { |
| 1152 | if (image_base == 0) { |
Brian Carlstrom | e0948e1 | 2013-08-29 09:36:15 -0700 | [diff] [blame] | 1153 | Usage("Non-zero --base not specified"); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1154 | } |
| 1155 | } |
| 1156 | |
| 1157 | std::string oat_stripped(oat_filename); |
| 1158 | std::string oat_unstripped; |
| 1159 | if (!oat_symbols.empty()) { |
| 1160 | oat_unstripped += oat_symbols; |
| 1161 | } else { |
| 1162 | oat_unstripped += oat_filename; |
| 1163 | } |
| 1164 | |
Kenny Root | 5131638 | 2014-05-13 14:59:37 -0700 | [diff] [blame] | 1165 | if (compiler_filter_string == nullptr) { |
Douglas Leung | 2db3e26 | 2014-06-25 16:02:55 -0700 | [diff] [blame] | 1166 | if (instruction_set == kMips64) { |
| 1167 | // TODO: fix compiler for Mips64. |
Ian Rogers | befbd57 | 2014-03-06 01:13:39 -0800 | [diff] [blame] | 1168 | compiler_filter_string = "interpret-only"; |
| 1169 | } else if (image) { |
Brian Carlstrom | 5e754d8 | 2014-03-05 10:59:04 -0800 | [diff] [blame] | 1170 | compiler_filter_string = "speed"; |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 1171 | } else { |
| 1172 | #if ART_SMALL_MODE |
| 1173 | compiler_filter_string = "interpret-only"; |
| 1174 | #else |
| 1175 | compiler_filter_string = "speed"; |
| 1176 | #endif |
| 1177 | } |
| 1178 | } |
| 1179 | CHECK(compiler_filter_string != nullptr); |
| 1180 | CompilerOptions::CompilerFilter compiler_filter = CompilerOptions::kDefaultCompilerFilter; |
Jeff Hao | 4a200f5 | 2014-04-01 14:58:49 -0700 | [diff] [blame] | 1181 | if (strcmp(compiler_filter_string, "verify-none") == 0) { |
| 1182 | compiler_filter = CompilerOptions::kVerifyNone; |
| 1183 | } else if (strcmp(compiler_filter_string, "interpret-only") == 0) { |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 1184 | compiler_filter = CompilerOptions::kInterpretOnly; |
| 1185 | } else if (strcmp(compiler_filter_string, "space") == 0) { |
| 1186 | compiler_filter = CompilerOptions::kSpace; |
| 1187 | } else if (strcmp(compiler_filter_string, "balanced") == 0) { |
| 1188 | compiler_filter = CompilerOptions::kBalanced; |
| 1189 | } else if (strcmp(compiler_filter_string, "speed") == 0) { |
| 1190 | compiler_filter = CompilerOptions::kSpeed; |
| 1191 | } else if (strcmp(compiler_filter_string, "everything") == 0) { |
| 1192 | compiler_filter = CompilerOptions::kEverything; |
Nicolas Geoffray | 88157ef | 2014-09-12 10:29:53 +0100 | [diff] [blame] | 1193 | } else if (strcmp(compiler_filter_string, "time") == 0) { |
| 1194 | compiler_filter = CompilerOptions::kTime; |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 1195 | } else { |
| 1196 | Usage("Unknown --compiler-filter value %s", compiler_filter_string); |
| 1197 | } |
| 1198 | |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 1199 | // Set the compilation target's implicit checks options. |
| 1200 | switch (instruction_set) { |
| 1201 | case kArm: |
| 1202 | case kThumb2: |
Stuart Monteith | d5c78f4 | 2014-06-11 16:44:46 +0100 | [diff] [blame] | 1203 | case kArm64: |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 1204 | case kX86: |
Dave Allison | dfd3b47 | 2014-07-16 16:04:32 -0700 | [diff] [blame] | 1205 | case kX86_64: |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 1206 | implicit_null_checks = true; |
| 1207 | implicit_so_checks = true; |
| 1208 | break; |
| 1209 | |
| 1210 | default: |
| 1211 | // Defaults are correct. |
| 1212 | break; |
| 1213 | } |
| 1214 | |
Razvan A Lupusoru | bd25d4b | 2014-07-02 18:16:51 -0700 | [diff] [blame] | 1215 | if (print_pass_options) { |
| 1216 | PassDriverMEOpts::PrintPassOptions(); |
| 1217 | } |
| 1218 | |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 1219 | std::unique_ptr<CompilerOptions> compiler_options(new CompilerOptions(compiler_filter, |
| 1220 | huge_method_threshold, |
| 1221 | large_method_threshold, |
| 1222 | small_method_threshold, |
| 1223 | tiny_method_threshold, |
| 1224 | num_dex_methods_threshold, |
| 1225 | generate_gdb_information, |
| 1226 | include_patch_information, |
| 1227 | top_k_profile_threshold, |
| 1228 | include_debug_symbols, |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 1229 | implicit_null_checks, |
| 1230 | implicit_so_checks, |
Igor Murashkin | d6dee67 | 2014-10-16 18:36:16 -0700 | [diff] [blame^] | 1231 | implicit_suspend_checks, |
| 1232 | compile_pic |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 1233 | #ifdef ART_SEA_IR_MODE |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 1234 | , compiler_options.sea_ir_ = |
| 1235 | true; |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 1236 | #endif |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 1237 | )); // NOLINT(whitespace/parens) |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 1238 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1239 | // Done with usage checks, enable watchdog if requested |
| 1240 | WatchDog watch_dog(watch_dog_enabled); |
| 1241 | |
| 1242 | // Check early that the result of compilation can be written |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 1243 | std::unique_ptr<File> oat_file; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1244 | bool create_file = !oat_unstripped.empty(); // as opposed to using open file descriptor |
| 1245 | if (create_file) { |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 1246 | oat_file.reset(OS::CreateEmptyFile(oat_unstripped.c_str())); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1247 | if (oat_location.empty()) { |
| 1248 | oat_location = oat_filename; |
| 1249 | } |
| 1250 | } else { |
| 1251 | oat_file.reset(new File(oat_fd, oat_location)); |
| 1252 | oat_file->DisableAutoClose(); |
Brian Carlstrom | 4f694ba | 2014-09-23 21:13:28 -0700 | [diff] [blame] | 1253 | oat_file->SetLength(0); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1254 | } |
Kenny Root | 5131638 | 2014-05-13 14:59:37 -0700 | [diff] [blame] | 1255 | if (oat_file.get() == nullptr) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1256 | PLOG(ERROR) << "Failed to create oat file: " << oat_location; |
| 1257 | return EXIT_FAILURE; |
| 1258 | } |
| 1259 | if (create_file && fchmod(oat_file->Fd(), 0644) != 0) { |
| 1260 | PLOG(ERROR) << "Failed to make oat file world readable: " << oat_location; |
| 1261 | return EXIT_FAILURE; |
| 1262 | } |
| 1263 | |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 1264 | timings.StartTiming("dex2oat Setup"); |
Brian Carlstrom | 09881a8 | 2014-04-18 17:44:01 -0700 | [diff] [blame] | 1265 | LOG(INFO) << CommandLine(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1266 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 1267 | RuntimeOptions runtime_options; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1268 | std::vector<const DexFile*> boot_class_path; |
| 1269 | if (boot_image_option.empty()) { |
Brian Carlstrom | 3cf59d5 | 2013-11-10 21:04:10 -0800 | [diff] [blame] | 1270 | size_t failure_count = OpenDexFiles(dex_filenames, dex_locations, boot_class_path); |
| 1271 | if (failure_count > 0) { |
| 1272 | LOG(ERROR) << "Failed to open some dex files: " << failure_count; |
| 1273 | return EXIT_FAILURE; |
| 1274 | } |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 1275 | runtime_options.push_back(std::make_pair("bootclasspath", &boot_class_path)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1276 | } else { |
Kenny Root | 5131638 | 2014-05-13 14:59:37 -0700 | [diff] [blame] | 1277 | runtime_options.push_back(std::make_pair(boot_image_option.c_str(), nullptr)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1278 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1279 | for (size_t i = 0; i < runtime_args.size(); i++) { |
Kenny Root | 5131638 | 2014-05-13 14:59:37 -0700 | [diff] [blame] | 1280 | runtime_options.push_back(std::make_pair(runtime_args[i], nullptr)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1281 | } |
| 1282 | |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 1283 | std::unique_ptr<VerificationResults> verification_results(new VerificationResults( |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 1284 | compiler_options.get())); |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 1285 | DexFileToMethodInlinerMap method_inliner_map; |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 1286 | QuickCompilerCallbacks callbacks(verification_results.get(), &method_inliner_map); |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 1287 | runtime_options.push_back(std::make_pair("compilercallbacks", &callbacks)); |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 1288 | runtime_options.push_back( |
| 1289 | std::make_pair("imageinstructionset", |
| 1290 | reinterpret_cast<const void*>(GetInstructionSetString(instruction_set)))); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1291 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1292 | Dex2Oat* p_dex2oat; |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 1293 | if (!Dex2Oat::Create(&p_dex2oat, |
| 1294 | runtime_options, |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 1295 | *compiler_options, |
Nicolas Geoffray | b34f69a | 2014-03-07 15:28:39 +0000 | [diff] [blame] | 1296 | compiler_kind, |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 1297 | instruction_set, |
| 1298 | instruction_set_features, |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 1299 | verification_results.get(), |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 1300 | &method_inliner_map, |
| 1301 | thread_count)) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1302 | LOG(ERROR) << "Failed to create dex2oat"; |
| 1303 | return EXIT_FAILURE; |
| 1304 | } |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 1305 | std::unique_ptr<Dex2Oat> dex2oat(p_dex2oat); |
Andreas Gampe | 5655e84 | 2014-06-17 16:36:07 -0700 | [diff] [blame] | 1306 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1307 | // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start, |
Ian Rogers | 3f3d22c | 2013-08-27 18:11:09 -0700 | [diff] [blame] | 1308 | // give it away now so that we don't starve GC. |
| 1309 | Thread* self = Thread::Current(); |
| 1310 | self->TransitionFromRunnableToSuspended(kNative); |
Ian Rogers | 0f40ac3 | 2013-08-13 22:10:30 -0700 | [diff] [blame] | 1311 | // If we're doing the image, override the compiler filter to force full compilation. Must be |
buzbee | fe9ca40 | 2013-08-21 09:48:11 -0700 | [diff] [blame] | 1312 | // done ahead of WellKnownClasses::Init that causes verification. Note: doesn't force |
| 1313 | // compilation of class initializers. |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1314 | // Whilst we're in native take the opportunity to initialize well known classes. |
Ian Rogers | 3f3d22c | 2013-08-27 18:11:09 -0700 | [diff] [blame] | 1315 | WellKnownClasses::Init(self->GetJniEnv()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1316 | |
| 1317 | // If --image-classes was specified, calculate the full list of classes to include in the image |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1318 | std::unique_ptr<std::set<std::string>> image_classes(nullptr); |
Kenny Root | d518534 | 2014-05-13 14:47:05 -0700 | [diff] [blame] | 1319 | if (image_classes_filename != nullptr) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1320 | std::string error_msg; |
Kenny Root | d518534 | 2014-05-13 14:47:05 -0700 | [diff] [blame] | 1321 | if (image_classes_zip_filename != nullptr) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1322 | image_classes.reset(dex2oat->ReadImageClassesFromZip(image_classes_zip_filename, |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1323 | image_classes_filename, |
| 1324 | &error_msg)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1325 | } else { |
| 1326 | image_classes.reset(dex2oat->ReadImageClassesFromFile(image_classes_filename)); |
| 1327 | } |
Kenny Root | d518534 | 2014-05-13 14:47:05 -0700 | [diff] [blame] | 1328 | if (image_classes.get() == nullptr) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1329 | LOG(ERROR) << "Failed to create list of image classes from '" << image_classes_filename << |
| 1330 | "': " << error_msg; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1331 | return EXIT_FAILURE; |
| 1332 | } |
Kenny Root | d518534 | 2014-05-13 14:47:05 -0700 | [diff] [blame] | 1333 | } else if (image) { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1334 | image_classes.reset(new std::set<std::string>); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1335 | } |
| 1336 | |
| 1337 | std::vector<const DexFile*> dex_files; |
| 1338 | if (boot_image_option.empty()) { |
| 1339 | dex_files = Runtime::Current()->GetClassLinker()->GetBootClassPath(); |
| 1340 | } else { |
| 1341 | if (dex_filenames.empty()) { |
Ian Rogers | 740a11d | 2014-01-14 10:11:25 -0800 | [diff] [blame] | 1342 | ATRACE_BEGIN("Opening zip archive from file descriptor"); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1343 | std::string error_msg; |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 1344 | std::unique_ptr<ZipArchive> zip_archive(ZipArchive::OpenFromFd(zip_fd, zip_location.c_str(), |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1345 | &error_msg)); |
Kenny Root | 5131638 | 2014-05-13 14:59:37 -0700 | [diff] [blame] | 1346 | if (zip_archive.get() == nullptr) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1347 | LOG(ERROR) << "Failed to open zip from file descriptor for '" << zip_location << "': " |
| 1348 | << error_msg; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1349 | return EXIT_FAILURE; |
| 1350 | } |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 1351 | if (!DexFile::OpenFromZip(*zip_archive.get(), zip_location, &error_msg, &dex_files)) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1352 | LOG(ERROR) << "Failed to open dex from file descriptor for zip file '" << zip_location |
| 1353 | << "': " << error_msg; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1354 | return EXIT_FAILURE; |
| 1355 | } |
Ian Rogers | 740a11d | 2014-01-14 10:11:25 -0800 | [diff] [blame] | 1356 | ATRACE_END(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1357 | } else { |
Brian Carlstrom | 3cf59d5 | 2013-11-10 21:04:10 -0800 | [diff] [blame] | 1358 | size_t failure_count = OpenDexFiles(dex_filenames, dex_locations, dex_files); |
| 1359 | if (failure_count > 0) { |
| 1360 | LOG(ERROR) << "Failed to open some dex files: " << failure_count; |
| 1361 | return EXIT_FAILURE; |
| 1362 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1363 | } |
Brian Carlstrom | d76e083 | 2013-08-29 15:17:42 -0700 | [diff] [blame] | 1364 | |
Brian Carlstrom | f79fccb | 2014-02-20 08:55:10 -0800 | [diff] [blame] | 1365 | const bool kSaveDexInput = false; |
| 1366 | if (kSaveDexInput) { |
| 1367 | for (size_t i = 0; i < dex_files.size(); ++i) { |
| 1368 | const DexFile* dex_file = dex_files[i]; |
Ian Rogers | 5180cc1 | 2014-02-21 13:34:16 -0800 | [diff] [blame] | 1369 | std::string tmp_file_name(StringPrintf("/data/local/tmp/dex2oat.%d.%zd.dex", getpid(), i)); |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 1370 | std::unique_ptr<File> tmp_file(OS::CreateEmptyFile(tmp_file_name.c_str())); |
Brian Carlstrom | f79fccb | 2014-02-20 08:55:10 -0800 | [diff] [blame] | 1371 | if (tmp_file.get() == nullptr) { |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 1372 | PLOG(ERROR) << "Failed to open file " << tmp_file_name |
| 1373 | << ". Try: adb shell chmod 777 /data/local/tmp"; |
Brian Carlstrom | f79fccb | 2014-02-20 08:55:10 -0800 | [diff] [blame] | 1374 | continue; |
| 1375 | } |
| 1376 | tmp_file->WriteFully(dex_file->Begin(), dex_file->Size()); |
| 1377 | LOG(INFO) << "Wrote input to " << tmp_file_name; |
| 1378 | } |
| 1379 | } |
Brian Carlstrom | 2ec6520 | 2014-03-03 15:16:37 -0800 | [diff] [blame] | 1380 | } |
| 1381 | // Ensure opened dex files are writable for dex-to-dex transformations. |
| 1382 | for (const auto& dex_file : dex_files) { |
| 1383 | if (!dex_file->EnableWrite()) { |
| 1384 | PLOG(ERROR) << "Failed to make .dex file writeable '" << dex_file->GetLocation() << "'\n"; |
Brian Carlstrom | d76e083 | 2013-08-29 15:17:42 -0700 | [diff] [blame] | 1385 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1386 | } |
| 1387 | |
buzbee | a024a06 | 2013-07-31 10:47:37 -0700 | [diff] [blame] | 1388 | /* |
Jeff Hao | 4a200f5 | 2014-04-01 14:58:49 -0700 | [diff] [blame] | 1389 | * If we're not in interpret-only or verify-none mode, go ahead and compile small applications. |
| 1390 | * Don't bother to check if we're doing the image. |
buzbee | a024a06 | 2013-07-31 10:47:37 -0700 | [diff] [blame] | 1391 | */ |
Nicolas Geoffray | 88157ef | 2014-09-12 10:29:53 +0100 | [diff] [blame] | 1392 | if (!image && compiler_options->IsCompilationEnabled() && compiler_kind == Compiler::kQuick) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1393 | size_t num_methods = 0; |
| 1394 | for (size_t i = 0; i != dex_files.size(); ++i) { |
| 1395 | const DexFile* dex_file = dex_files[i]; |
Kenny Root | 5131638 | 2014-05-13 14:59:37 -0700 | [diff] [blame] | 1396 | CHECK(dex_file != nullptr); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1397 | num_methods += dex_file->NumMethodIds(); |
| 1398 | } |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 1399 | if (num_methods <= compiler_options->GetNumDexMethodsThreshold()) { |
| 1400 | compiler_options->SetCompilerFilter(CompilerOptions::kSpeed); |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 1401 | VLOG(compiler) << "Below method threshold, compiling anyways"; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1402 | } |
| 1403 | } |
| 1404 | |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 1405 | // Fill some values into the key-value store for the oat header. |
| 1406 | std::unique_ptr<SafeMap<std::string, std::string> > key_value_store( |
| 1407 | new SafeMap<std::string, std::string>()); |
| 1408 | |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 1409 | // Insert some compiler things. |
| 1410 | std::ostringstream oss; |
| 1411 | for (int i = 0; i < argc; ++i) { |
| 1412 | if (i > 0) { |
| 1413 | oss << ' '; |
| 1414 | } |
| 1415 | oss << argv[i]; |
| 1416 | } |
| 1417 | key_value_store->Put(OatHeader::kDex2OatCmdLineKey, oss.str()); |
| 1418 | oss.str(""); // Reset. |
| 1419 | oss << kRuntimeISA; |
| 1420 | key_value_store->Put(OatHeader::kDex2OatHostKey, oss.str()); |
| 1421 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1422 | dex2oat->Compile(boot_image_option, |
| 1423 | dex_files, |
| 1424 | bitcode_filename, |
| 1425 | image, |
| 1426 | image_classes, |
| 1427 | dump_stats, |
| 1428 | dump_passes, |
| 1429 | &timings, |
| 1430 | &compiler_phases_timings, |
| 1431 | profile_file); |
| 1432 | |
| 1433 | if (image) { |
| 1434 | dex2oat->PrepareImageWriter(image_base); |
| 1435 | } |
| 1436 | |
| 1437 | if (!dex2oat->CreateOatFile(dex_files, |
| 1438 | android_root, |
| 1439 | is_host, |
| 1440 | oat_file.get(), |
| 1441 | oat_location, |
| 1442 | &timings, |
| 1443 | key_value_store.get())) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1444 | LOG(ERROR) << "Failed to create oat file: " << oat_location; |
| 1445 | return EXIT_FAILURE; |
| 1446 | } |
| 1447 | |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 1448 | VLOG(compiler) << "Oat file written successfully (unstripped): " << oat_location; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1449 | |
| 1450 | // Notes on the interleaving of creating the image and oat file to |
| 1451 | // ensure the references between the two are correct. |
| 1452 | // |
| 1453 | // Currently we have a memory layout that looks something like this: |
| 1454 | // |
| 1455 | // +--------------+ |
| 1456 | // | image | |
| 1457 | // +--------------+ |
| 1458 | // | boot oat | |
| 1459 | // +--------------+ |
| 1460 | // | alloc spaces | |
| 1461 | // +--------------+ |
| 1462 | // |
Brian Carlstrom | 4560248 | 2013-07-21 22:07:55 -0700 | [diff] [blame] | 1463 | // There are several constraints on the loading of the image and boot.oat. |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1464 | // |
| 1465 | // 1. The image is expected to be loaded at an absolute address and |
| 1466 | // contains Objects with absolute pointers within the image. |
| 1467 | // |
| 1468 | // 2. There are absolute pointers from Methods in the image to their |
| 1469 | // code in the oat. |
| 1470 | // |
| 1471 | // 3. There are absolute pointers from the code in the oat to Methods |
| 1472 | // in the image. |
| 1473 | // |
| 1474 | // 4. There are absolute pointers from code in the oat to other code |
| 1475 | // in the oat. |
| 1476 | // |
| 1477 | // To get this all correct, we go through several steps. |
| 1478 | // |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1479 | // 1. We prepare offsets for all data in the oat file and calculate |
| 1480 | // the oat data size and code size. During this stage, we also set |
| 1481 | // oat code offsets in methods for use by the image writer. |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1482 | // |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1483 | // 2. We prepare offsets for the objects in the image and calculate |
| 1484 | // the image size. |
| 1485 | // |
| 1486 | // 3. We create the oat file. Originally this was just our own proprietary |
| 1487 | // file but now it is contained within an ELF dynamic object (aka an .so |
| 1488 | // file). Since we know the image size and oat data size and code size we |
| 1489 | // can prepare the ELF headers and we then know the ELF memory segment |
| 1490 | // layout and we can now resolve all references. The compiler provides |
| 1491 | // LinkerPatch information in each CompiledMethod and we resolve these, |
| 1492 | // using the layout information and image object locations provided by |
| 1493 | // image writer, as we're writing the method code. |
| 1494 | // |
| 1495 | // 4. We create the image file. It needs to know where the oat file |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1496 | // will be loaded after itself. Originally when oat file was simply |
| 1497 | // memory mapped so we could predict where its contents were based |
| 1498 | // on the file size. Now that it is an ELF file, we need to inspect |
| 1499 | // the ELF file to understand the in memory segment layout including |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1500 | // where the oat header is located within. |
| 1501 | // TODO: We could just remember this information from step 3. |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1502 | // |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1503 | // 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] | 1504 | // load the .so at the desired location at runtime by offsetting the |
| 1505 | // Elf32_Phdr.p_vaddr values by the desired base address. |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1506 | // TODO: Do this in step 3. We already know the layout there. |
| 1507 | // |
| 1508 | // Steps 1.-3. are done by the CreateOatFile() above, steps 4.-5. |
| 1509 | // are done by the CreateImageFile() below. |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1510 | // |
| 1511 | if (image) { |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 1512 | TimingLogger::ScopedTiming t("dex2oat ImageWriter", &timings); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1513 | bool image_creation_success = dex2oat->CreateImageFile(image_filename, |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1514 | oat_unstripped, |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1515 | oat_location); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1516 | if (!image_creation_success) { |
| 1517 | return EXIT_FAILURE; |
| 1518 | } |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 1519 | VLOG(compiler) << "Image written successfully: " << image_filename; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1520 | } |
| 1521 | |
| 1522 | if (is_host) { |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 1523 | timings.EndTiming(); |
Ian Rogers | 4639860 | 2013-08-20 07:50:36 -0700 | [diff] [blame] | 1524 | if (dump_timing || (dump_slow_timing && timings.GetTotalNs() > MsToNs(1000))) { |
Ian Rogers | 5fe9af7 | 2013-11-14 00:17:20 -0800 | [diff] [blame] | 1525 | LOG(INFO) << Dumpable<TimingLogger>(timings); |
Brian Carlstrom | 4560248 | 2013-07-21 22:07:55 -0700 | [diff] [blame] | 1526 | } |
Nicolas Geoffray | ea3fa0b | 2014-02-10 11:59:41 +0000 | [diff] [blame] | 1527 | if (dump_passes) { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1528 | LOG(INFO) << Dumpable<CumulativeLogger>(compiler_phases_timings); |
Nicolas Geoffray | ea3fa0b | 2014-02-10 11:59:41 +0000 | [diff] [blame] | 1529 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1530 | return EXIT_SUCCESS; |
| 1531 | } |
| 1532 | |
| 1533 | // If we don't want to strip in place, copy from unstripped location to stripped location. |
| 1534 | // We need to strip after image creation because FixupElf needs to use .strtab. |
| 1535 | if (oat_unstripped != oat_stripped) { |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 1536 | TimingLogger::ScopedTiming t("dex2oat OatFile copy", &timings); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1537 | oat_file.reset(); |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 1538 | std::unique_ptr<File> in(OS::OpenFileForReading(oat_unstripped.c_str())); |
| 1539 | std::unique_ptr<File> out(OS::CreateEmptyFile(oat_stripped.c_str())); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1540 | size_t buffer_size = 8192; |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 1541 | std::unique_ptr<uint8_t> buffer(new uint8_t[buffer_size]); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1542 | while (true) { |
| 1543 | int bytes_read = TEMP_FAILURE_RETRY(read(in->Fd(), buffer.get(), buffer_size)); |
| 1544 | if (bytes_read <= 0) { |
| 1545 | break; |
| 1546 | } |
| 1547 | bool write_ok = out->WriteFully(buffer.get(), bytes_read); |
| 1548 | CHECK(write_ok); |
| 1549 | } |
| 1550 | oat_file.reset(out.release()); |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 1551 | VLOG(compiler) << "Oat file copied successfully (stripped): " << oat_stripped; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1552 | } |
| 1553 | |
Brian Carlstrom | 7fcba11 | 2013-07-22 10:28:48 -0700 | [diff] [blame] | 1554 | #if ART_USE_PORTABLE_COMPILER // We currently only generate symbols on Portable |
Alex Light | 78382fa | 2014-06-06 15:45:32 -0700 | [diff] [blame] | 1555 | if (!compiler_options.GetIncludeDebugSymbols()) { |
| 1556 | timings.NewSplit("dex2oat ElfStripper"); |
| 1557 | // Strip unneeded sections for target |
| 1558 | off_t seek_actual = lseek(oat_file->Fd(), 0, SEEK_SET); |
| 1559 | CHECK_EQ(0, seek_actual); |
| 1560 | std::string error_msg; |
| 1561 | CHECK(ElfStripper::Strip(oat_file.get(), &error_msg)) << error_msg; |
Anwar Ghuloum | 6f28d91 | 2013-07-24 15:02:53 -0700 | [diff] [blame] | 1562 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1563 | |
Alex Light | 78382fa | 2014-06-06 15:45:32 -0700 | [diff] [blame] | 1564 | // We wrote the oat file successfully, and want to keep it. |
| 1565 | VLOG(compiler) << "Oat file written successfully (stripped): " << oat_location; |
| 1566 | } else { |
| 1567 | VLOG(compiler) << "Oat file written successfully without stripping: " << oat_location; |
| 1568 | } |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 1569 | #endif // ART_USE_PORTABLE_COMPILER |
Brian Carlstrom | 4560248 | 2013-07-21 22:07:55 -0700 | [diff] [blame] | 1570 | |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 1571 | timings.EndTiming(); |
Anwar Ghuloum | 6f28d91 | 2013-07-24 15:02:53 -0700 | [diff] [blame] | 1572 | |
Brian Carlstrom | c6dfdac | 2013-08-26 18:57:31 -0700 | [diff] [blame] | 1573 | if (dump_timing || (dump_slow_timing && timings.GetTotalNs() > MsToNs(1000))) { |
Ian Rogers | 5fe9af7 | 2013-11-14 00:17:20 -0800 | [diff] [blame] | 1574 | LOG(INFO) << Dumpable<TimingLogger>(timings); |
Brian Carlstrom | 4560248 | 2013-07-21 22:07:55 -0700 | [diff] [blame] | 1575 | } |
Nicolas Geoffray | ea3fa0b | 2014-02-10 11:59:41 +0000 | [diff] [blame] | 1576 | if (dump_passes) { |
| 1577 | LOG(INFO) << Dumpable<CumulativeLogger>(compiler_phases_timings); |
| 1578 | } |
Ian Rogers | 2672a9f | 2013-09-05 17:24:22 -0700 | [diff] [blame] | 1579 | |
| 1580 | // Everything was successfully written, do an explicit exit here to avoid running Runtime |
| 1581 | // destructors that take time (bug 10645725) unless we're a debug build or running on valgrind. |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 1582 | if (!kIsDebugBuild && (RUNNING_ON_VALGRIND == 0)) { |
Brian Carlstrom | 65c23bb | 2014-02-01 22:12:39 -0800 | [diff] [blame] | 1583 | dex2oat->LogCompletionTime(); |
Ian Rogers | 2672a9f | 2013-09-05 17:24:22 -0700 | [diff] [blame] | 1584 | exit(EXIT_SUCCESS); |
| 1585 | } |
| 1586 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1587 | return EXIT_SUCCESS; |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 1588 | } // NOLINT(readability/fn_size) |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 1589 | } // namespace art |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1590 | |
| 1591 | int main(int argc, char** argv) { |
| 1592 | return art::dex2oat(argc, argv); |
| 1593 | } |