jit.cc revision 77651c4bbd56d502bcf05347e641061bbebca099
1e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier/* 2e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier * Copyright 2014 The Android Open Source Project 3e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier * 4e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier * Licensed under the Apache License, Version 2.0 (the "License"); 5e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier * you may not use this file except in compliance with the License. 6e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier * You may obtain a copy of the License at 7e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier * 8e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier * http://www.apache.org/licenses/LICENSE-2.0 9e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier * 10e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier * Unless required by applicable law or agreed to in writing, software 11e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier * distributed under the License is distributed on an "AS IS" BASIS, 12e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier * See the License for the specific language governing permissions and 14e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier * limitations under the License. 15e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier */ 16e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier 17e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier#include "jit.h" 18e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier 19e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier#include <dlfcn.h> 20e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier 21e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier#include "art_method-inl.h" 22542451cc546779f5c67840e105c51205a1b0a8fdAndreas Gampe#include "base/enums.h" 232a5c4681ba19411c1cb22e9a7ab446dab910af1cAndreas Gampe#include "debugger.h" 24e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier#include "entrypoints/runtime_asm_entrypoints.h" 25e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier#include "interpreter/interpreter.h" 26e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier#include "jit_code_cache.h" 2731f2c155975c5794d481df03eb0947cb48d2c6b5Calin Juravle#include "oat_file_manager.h" 28b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray#include "oat_quick_method_header.h" 2933083d626dbf2c8b06badfd73f50e98114483059Calin Juravle#include "profile_compilation_info.h" 304d77b6a511659f26fdc711e23825ffa6e7feed7aCalin Juravle#include "profile_saver.h" 31e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier#include "runtime.h" 32e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier#include "runtime_options.h" 33b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray#include "stack_map.h" 34274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray#include "thread_list.h" 35e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier#include "utils.h" 36e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier 37e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartiernamespace art { 38e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartiernamespace jit { 39e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier 40d186dd8ecb1f25d3786d6b27adcd6b0b9ca04ea0Nicolas Geoffraystatic constexpr bool kEnableOnStackReplacement = true; 41274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray// At what priority to schedule jit threads. 9 is the lowest foreground priority on device. 42274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffraystatic constexpr int kJitPoolThreadPthreadPriority = 9; 43e86621386d18a3a7178af6cfc2c05d1b34c3b995Nicolas Geoffray 4472918ea854d182ef25b2352bfe1c46c1e916a141Mathieu Chartier// JIT compiler 4572918ea854d182ef25b2352bfe1c46c1e916a141Mathieu Chartiervoid* Jit::jit_library_handle_= nullptr; 4672918ea854d182ef25b2352bfe1c46c1e916a141Mathieu Chartiervoid* Jit::jit_compiler_handle_ = nullptr; 4772918ea854d182ef25b2352bfe1c46c1e916a141Mathieu Chartiervoid* (*Jit::jit_load_)(bool*) = nullptr; 4872918ea854d182ef25b2352bfe1c46c1e916a141Mathieu Chartiervoid (*Jit::jit_unload_)(void*) = nullptr; 4972918ea854d182ef25b2352bfe1c46c1e916a141Mathieu Chartierbool (*Jit::jit_compile_method_)(void*, ArtMethod*, Thread*, bool) = nullptr; 5072918ea854d182ef25b2352bfe1c46c1e916a141Mathieu Chartiervoid (*Jit::jit_types_loaded_)(void*, mirror::Class**, size_t count) = nullptr; 5172918ea854d182ef25b2352bfe1c46c1e916a141Mathieu Chartierbool Jit::generate_debug_info_ = false; 5272918ea854d182ef25b2352bfe1c46c1e916a141Mathieu Chartier 53e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu ChartierJitOptions* JitOptions::CreateFromRuntimeArguments(const RuntimeArgumentMap& options) { 54e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier auto* jit_options = new JitOptions; 55ffc87076dda9878cb2cc098149bae441d38b9268Calin Juravle jit_options->use_jit_compilation_ = options.GetOrDefault(RuntimeArgumentMap::UseJitCompilation); 5683f080ac824d0964941c3fbaa957cac874f827b0Nicolas Geoffray 570a3be1620a3560253cfa789cb9819013293c5654Nicolas Geoffray jit_options->code_cache_initial_capacity_ = 580a3be1620a3560253cfa789cb9819013293c5654Nicolas Geoffray options.GetOrDefault(RuntimeArgumentMap::JITCodeCacheInitialCapacity); 590a3be1620a3560253cfa789cb9819013293c5654Nicolas Geoffray jit_options->code_cache_max_capacity_ = 600a3be1620a3560253cfa789cb9819013293c5654Nicolas Geoffray options.GetOrDefault(RuntimeArgumentMap::JITCodeCacheMaxCapacity); 61a4885cbaafd35fe9c60eb6cd95e41e2c86f54f66Mathieu Chartier jit_options->dump_info_on_shutdown_ = 62a4885cbaafd35fe9c60eb6cd95e41e2c86f54f66Mathieu Chartier options.Exists(RuntimeArgumentMap::DumpJITInfoOnShutdown); 63138dbff9246c89ac9fbe0b086b54fdab3f4451fbCalin Juravle jit_options->profile_saver_options_ = 64138dbff9246c89ac9fbe0b086b54fdab3f4451fbCalin Juravle options.GetOrDefault(RuntimeArgumentMap::ProfileSaverOpts); 6583f080ac824d0964941c3fbaa957cac874f827b0Nicolas Geoffray 6683f080ac824d0964941c3fbaa957cac874f827b0Nicolas Geoffray jit_options->compile_threshold_ = options.GetOrDefault(RuntimeArgumentMap::JITCompileThreshold); 6783f080ac824d0964941c3fbaa957cac874f827b0Nicolas Geoffray if (jit_options->compile_threshold_ > std::numeric_limits<uint16_t>::max()) { 6883f080ac824d0964941c3fbaa957cac874f827b0Nicolas Geoffray LOG(FATAL) << "Method compilation threshold is above its internal limit."; 6983f080ac824d0964941c3fbaa957cac874f827b0Nicolas Geoffray } 7083f080ac824d0964941c3fbaa957cac874f827b0Nicolas Geoffray 7183f080ac824d0964941c3fbaa957cac874f827b0Nicolas Geoffray if (options.Exists(RuntimeArgumentMap::JITWarmupThreshold)) { 7283f080ac824d0964941c3fbaa957cac874f827b0Nicolas Geoffray jit_options->warmup_threshold_ = *options.Get(RuntimeArgumentMap::JITWarmupThreshold); 7383f080ac824d0964941c3fbaa957cac874f827b0Nicolas Geoffray if (jit_options->warmup_threshold_ > std::numeric_limits<uint16_t>::max()) { 7483f080ac824d0964941c3fbaa957cac874f827b0Nicolas Geoffray LOG(FATAL) << "Method warmup threshold is above its internal limit."; 7583f080ac824d0964941c3fbaa957cac874f827b0Nicolas Geoffray } 7683f080ac824d0964941c3fbaa957cac874f827b0Nicolas Geoffray } else { 7783f080ac824d0964941c3fbaa957cac874f827b0Nicolas Geoffray jit_options->warmup_threshold_ = jit_options->compile_threshold_ / 2; 7883f080ac824d0964941c3fbaa957cac874f827b0Nicolas Geoffray } 7983f080ac824d0964941c3fbaa957cac874f827b0Nicolas Geoffray 8083f080ac824d0964941c3fbaa957cac874f827b0Nicolas Geoffray if (options.Exists(RuntimeArgumentMap::JITOsrThreshold)) { 8183f080ac824d0964941c3fbaa957cac874f827b0Nicolas Geoffray jit_options->osr_threshold_ = *options.Get(RuntimeArgumentMap::JITOsrThreshold); 8283f080ac824d0964941c3fbaa957cac874f827b0Nicolas Geoffray if (jit_options->osr_threshold_ > std::numeric_limits<uint16_t>::max()) { 8383f080ac824d0964941c3fbaa957cac874f827b0Nicolas Geoffray LOG(FATAL) << "Method on stack replacement threshold is above its internal limit."; 8483f080ac824d0964941c3fbaa957cac874f827b0Nicolas Geoffray } 8583f080ac824d0964941c3fbaa957cac874f827b0Nicolas Geoffray } else { 8683f080ac824d0964941c3fbaa957cac874f827b0Nicolas Geoffray jit_options->osr_threshold_ = jit_options->compile_threshold_ * 2; 8783f080ac824d0964941c3fbaa957cac874f827b0Nicolas Geoffray if (jit_options->osr_threshold_ > std::numeric_limits<uint16_t>::max()) { 8883f080ac824d0964941c3fbaa957cac874f827b0Nicolas Geoffray jit_options->osr_threshold_ = std::numeric_limits<uint16_t>::max(); 8983f080ac824d0964941c3fbaa957cac874f827b0Nicolas Geoffray } 9083f080ac824d0964941c3fbaa957cac874f827b0Nicolas Geoffray } 9183f080ac824d0964941c3fbaa957cac874f827b0Nicolas Geoffray 92b2771b41a956b50266d4d83fbb067f99faf7b7dcCalin Juravle if (options.Exists(RuntimeArgumentMap::JITPriorityThreadWeight)) { 93b2771b41a956b50266d4d83fbb067f99faf7b7dcCalin Juravle jit_options->priority_thread_weight_ = 94b2771b41a956b50266d4d83fbb067f99faf7b7dcCalin Juravle *options.Get(RuntimeArgumentMap::JITPriorityThreadWeight); 95b2771b41a956b50266d4d83fbb067f99faf7b7dcCalin Juravle if (jit_options->priority_thread_weight_ > jit_options->warmup_threshold_) { 96b2771b41a956b50266d4d83fbb067f99faf7b7dcCalin Juravle LOG(FATAL) << "Priority thread weight is above the warmup threshold."; 97b2771b41a956b50266d4d83fbb067f99faf7b7dcCalin Juravle } else if (jit_options->priority_thread_weight_ == 0) { 98b2771b41a956b50266d4d83fbb067f99faf7b7dcCalin Juravle LOG(FATAL) << "Priority thread weight cannot be 0."; 99b2771b41a956b50266d4d83fbb067f99faf7b7dcCalin Juravle } 100b2771b41a956b50266d4d83fbb067f99faf7b7dcCalin Juravle } else { 10171cd50fb67fa48667b0ab59aa436a582c04ba43dNicolas Geoffray jit_options->priority_thread_weight_ = std::max( 10271cd50fb67fa48667b0ab59aa436a582c04ba43dNicolas Geoffray jit_options->warmup_threshold_ / Jit::kDefaultPriorityThreadWeightRatio, 10371cd50fb67fa48667b0ab59aa436a582c04ba43dNicolas Geoffray static_cast<size_t>(1)); 104b2771b41a956b50266d4d83fbb067f99faf7b7dcCalin Juravle } 105b2771b41a956b50266d4d83fbb067f99faf7b7dcCalin Juravle 106155ff3d1c59c0f88993559b0b9bd639f7e32f048Calin Juravle if (options.Exists(RuntimeArgumentMap::JITInvokeTransitionWeight)) { 1077c9f3ba7b4f7b9543dbd539ffdc7be818284d922Nicolas Geoffray jit_options->invoke_transition_weight_ = 1087c9f3ba7b4f7b9543dbd539ffdc7be818284d922Nicolas Geoffray *options.Get(RuntimeArgumentMap::JITInvokeTransitionWeight); 109155ff3d1c59c0f88993559b0b9bd639f7e32f048Calin Juravle if (jit_options->invoke_transition_weight_ > jit_options->warmup_threshold_) { 110155ff3d1c59c0f88993559b0b9bd639f7e32f048Calin Juravle LOG(FATAL) << "Invoke transition weight is above the warmup threshold."; 111155ff3d1c59c0f88993559b0b9bd639f7e32f048Calin Juravle } else if (jit_options->invoke_transition_weight_ == 0) { 1127c9f3ba7b4f7b9543dbd539ffdc7be818284d922Nicolas Geoffray LOG(FATAL) << "Invoke transition weight cannot be 0."; 113155ff3d1c59c0f88993559b0b9bd639f7e32f048Calin Juravle } 114155ff3d1c59c0f88993559b0b9bd639f7e32f048Calin Juravle } else { 115155ff3d1c59c0f88993559b0b9bd639f7e32f048Calin Juravle jit_options->invoke_transition_weight_ = std::max( 116155ff3d1c59c0f88993559b0b9bd639f7e32f048Calin Juravle jit_options->warmup_threshold_ / Jit::kDefaultInvokeTransitionWeightRatio, 1176beced4c017826f7c449f12fac7fa42403657f2bMathieu Chartier static_cast<size_t>(1)); 118155ff3d1c59c0f88993559b0b9bd639f7e32f048Calin Juravle } 119155ff3d1c59c0f88993559b0b9bd639f7e32f048Calin Juravle 120e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier return jit_options; 121e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier} 122e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier 123b2771b41a956b50266d4d83fbb067f99faf7b7dcCalin Juravlebool Jit::ShouldUsePriorityThreadWeight() { 12497cbc9206e9adc473a90650ebdb5d620f517ff04Calin Juravle return Runtime::Current()->InJankPerceptibleProcessState() 12597cbc9206e9adc473a90650ebdb5d620f517ff04Calin Juravle && Thread::Current()->IsJitSensitiveThread(); 126b2771b41a956b50266d4d83fbb067f99faf7b7dcCalin Juravle} 127b2771b41a956b50266d4d83fbb067f99faf7b7dcCalin Juravle 128a4885cbaafd35fe9c60eb6cd95e41e2c86f54f66Mathieu Chartiervoid Jit::DumpInfo(std::ostream& os) { 129bcd94c8ea9bde4e075c25fbdfb3a2ef6858eed7bNicolas Geoffray code_cache_->Dump(os); 130a4885cbaafd35fe9c60eb6cd95e41e2c86f54f66Mathieu Chartier cumulative_timings_.Dump(os); 131a4f81546373f4cb5fa6dfc135307ee0a1d930872Nicolas Geoffray MutexLock mu(Thread::Current(), lock_); 132a4f81546373f4cb5fa6dfc135307ee0a1d930872Nicolas Geoffray memory_use_.PrintMemoryUse(os); 133a4885cbaafd35fe9c60eb6cd95e41e2c86f54f66Mathieu Chartier} 134a4885cbaafd35fe9c60eb6cd95e41e2c86f54f66Mathieu Chartier 135b8e69994d10534e0f4f96878725bc53e531f2c6fCalin Juravlevoid Jit::DumpForSigQuit(std::ostream& os) { 136b8e69994d10534e0f4f96878725bc53e531f2c6fCalin Juravle DumpInfo(os); 137b8e69994d10534e0f4f96878725bc53e531f2c6fCalin Juravle ProfileSaver::DumpInstanceInfo(os); 138b8e69994d10534e0f4f96878725bc53e531f2c6fCalin Juravle} 139b8e69994d10534e0f4f96878725bc53e531f2c6fCalin Juravle 140a4885cbaafd35fe9c60eb6cd95e41e2c86f54f66Mathieu Chartiervoid Jit::AddTimingLogger(const TimingLogger& logger) { 141a4885cbaafd35fe9c60eb6cd95e41e2c86f54f66Mathieu Chartier cumulative_timings_.AddLogger(logger); 142a4885cbaafd35fe9c60eb6cd95e41e2c86f54f66Mathieu Chartier} 143a4885cbaafd35fe9c60eb6cd95e41e2c86f54f66Mathieu Chartier 14472918ea854d182ef25b2352bfe1c46c1e916a141Mathieu ChartierJit::Jit() : dump_info_on_shutdown_(false), 145a25dce9b452ba17ef7cef768926c884177a3025eNicolas Geoffray cumulative_timings_("JIT timings"), 146a4f81546373f4cb5fa6dfc135307ee0a1d930872Nicolas Geoffray memory_use_("Memory used for compilation", 16), 147a4f81546373f4cb5fa6dfc135307ee0a1d930872Nicolas Geoffray lock_("JIT memory use lock"), 1484471e4f7c5874bdaf93762b6047d4a4bebc465dfAndreas Gampe use_jit_compilation_(true), 1494471e4f7c5874bdaf93762b6047d4a4bebc465dfAndreas Gampe hot_method_threshold_(0), 1504471e4f7c5874bdaf93762b6047d4a4bebc465dfAndreas Gampe warm_method_threshold_(0), 1514471e4f7c5874bdaf93762b6047d4a4bebc465dfAndreas Gampe osr_method_threshold_(0), 1524471e4f7c5874bdaf93762b6047d4a4bebc465dfAndreas Gampe priority_thread_weight_(0), 1534471e4f7c5874bdaf93762b6047d4a4bebc465dfAndreas Gampe invoke_transition_weight_(0) {} 154e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier 155e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu ChartierJit* Jit::Create(JitOptions* options, std::string* error_msg) { 156138dbff9246c89ac9fbe0b086b54fdab3f4451fbCalin Juravle DCHECK(options->UseJitCompilation() || options->GetProfileSaverOptions().IsEnabled()); 157e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier std::unique_ptr<Jit> jit(new Jit); 158a4885cbaafd35fe9c60eb6cd95e41e2c86f54f66Mathieu Chartier jit->dump_info_on_shutdown_ = options->DumpJitInfoOnShutdown(); 15972918ea854d182ef25b2352bfe1c46c1e916a141Mathieu Chartier if (jit_compiler_handle_ == nullptr && !LoadCompiler(error_msg)) { 160e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier return nullptr; 161e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier } 1620a3be1620a3560253cfa789cb9819013293c5654Nicolas Geoffray jit->code_cache_.reset(JitCodeCache::Create( 163a25dce9b452ba17ef7cef768926c884177a3025eNicolas Geoffray options->GetCodeCacheInitialCapacity(), 164a25dce9b452ba17ef7cef768926c884177a3025eNicolas Geoffray options->GetCodeCacheMaxCapacity(), 165a25dce9b452ba17ef7cef768926c884177a3025eNicolas Geoffray jit->generate_debug_info_, 166a25dce9b452ba17ef7cef768926c884177a3025eNicolas Geoffray error_msg)); 167e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier if (jit->GetCodeCache() == nullptr) { 168e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier return nullptr; 169e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier } 170ffc87076dda9878cb2cc098149bae441d38b9268Calin Juravle jit->use_jit_compilation_ = options->UseJitCompilation(); 171138dbff9246c89ac9fbe0b086b54fdab3f4451fbCalin Juravle jit->profile_saver_options_ = options->GetProfileSaverOptions(); 172bcd94c8ea9bde4e075c25fbdfb3a2ef6858eed7bNicolas Geoffray VLOG(jit) << "JIT created with initial_capacity=" 1730a3be1620a3560253cfa789cb9819013293c5654Nicolas Geoffray << PrettySize(options->GetCodeCacheInitialCapacity()) 1740a3be1620a3560253cfa789cb9819013293c5654Nicolas Geoffray << ", max_capacity=" << PrettySize(options->GetCodeCacheMaxCapacity()) 1754d77b6a511659f26fdc711e23825ffa6e7feed7aCalin Juravle << ", compile_threshold=" << options->GetCompileThreshold() 176138dbff9246c89ac9fbe0b086b54fdab3f4451fbCalin Juravle << ", profile_saver_options=" << options->GetProfileSaverOptions(); 177274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray 178274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray 179274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray jit->hot_method_threshold_ = options->GetCompileThreshold(); 180274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray jit->warm_method_threshold_ = options->GetWarmupThreshold(); 181274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray jit->osr_method_threshold_ = options->GetOsrThreshold(); 182ba6aae0b49932a0ae3050d1fc22c6571e51f8180Nicolas Geoffray jit->priority_thread_weight_ = options->GetPriorityThreadWeight(); 183155ff3d1c59c0f88993559b0b9bd639f7e32f048Calin Juravle jit->invoke_transition_weight_ = options->GetInvokeTransitionWeight(); 184274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray 185274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray jit->CreateThreadPool(); 186274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray 187274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray // Notify native debugger about the classes already loaded before the creation of the jit. 188274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray jit->DumpTypeInfoForLoadedTypes(Runtime::Current()->GetClassLinker()); 189e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier return jit.release(); 190e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier} 191e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier 192c1bc4150415686e6240343c7345c49d80e351df3Mathieu Chartierbool Jit::LoadCompilerLibrary(std::string* error_msg) { 193e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier jit_library_handle_ = dlopen( 194e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier kIsDebugBuild ? "libartd-compiler.so" : "libart-compiler.so", RTLD_NOW); 195e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier if (jit_library_handle_ == nullptr) { 196e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier std::ostringstream oss; 197e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier oss << "JIT could not load libart-compiler.so: " << dlerror(); 198e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier *error_msg = oss.str(); 199e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier return false; 200e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier } 2015b82d339955d1a0dc23eeb8d2d5659459ff987baNicolas Geoffray jit_load_ = reinterpret_cast<void* (*)(bool*)>(dlsym(jit_library_handle_, "jit_load")); 202e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier if (jit_load_ == nullptr) { 203e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier dlclose(jit_library_handle_); 204e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier *error_msg = "JIT couldn't find jit_load entry point"; 205e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier return false; 206e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier } 207e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier jit_unload_ = reinterpret_cast<void (*)(void*)>( 208e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier dlsym(jit_library_handle_, "jit_unload")); 209e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier if (jit_unload_ == nullptr) { 210e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier dlclose(jit_library_handle_); 211e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier *error_msg = "JIT couldn't find jit_unload entry point"; 212e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier return false; 213e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier } 214b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray jit_compile_method_ = reinterpret_cast<bool (*)(void*, ArtMethod*, Thread*, bool)>( 215e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier dlsym(jit_library_handle_, "jit_compile_method")); 216e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier if (jit_compile_method_ == nullptr) { 217e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier dlclose(jit_library_handle_); 218e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier *error_msg = "JIT couldn't find jit_compile_method entry point"; 219e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier return false; 220e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier } 221fffbee4d158259633ec7b7f712eaf75be86bd4e5Tamas Berghammer jit_types_loaded_ = reinterpret_cast<void (*)(void*, mirror::Class**, size_t)>( 222fffbee4d158259633ec7b7f712eaf75be86bd4e5Tamas Berghammer dlsym(jit_library_handle_, "jit_types_loaded")); 223fffbee4d158259633ec7b7f712eaf75be86bd4e5Tamas Berghammer if (jit_types_loaded_ == nullptr) { 224160e6df5debaf77223eebddb8a4e3f7c5e729ad0Tamas Berghammer dlclose(jit_library_handle_); 225fffbee4d158259633ec7b7f712eaf75be86bd4e5Tamas Berghammer *error_msg = "JIT couldn't find jit_types_loaded entry point"; 226160e6df5debaf77223eebddb8a4e3f7c5e729ad0Tamas Berghammer return false; 227160e6df5debaf77223eebddb8a4e3f7c5e729ad0Tamas Berghammer } 228c1bc4150415686e6240343c7345c49d80e351df3Mathieu Chartier return true; 229c1bc4150415686e6240343c7345c49d80e351df3Mathieu Chartier} 230c1bc4150415686e6240343c7345c49d80e351df3Mathieu Chartier 231c1bc4150415686e6240343c7345c49d80e351df3Mathieu Chartierbool Jit::LoadCompiler(std::string* error_msg) { 232c1bc4150415686e6240343c7345c49d80e351df3Mathieu Chartier if (jit_library_handle_ == nullptr && !LoadCompilerLibrary(error_msg)) { 233c1bc4150415686e6240343c7345c49d80e351df3Mathieu Chartier return false; 234c1bc4150415686e6240343c7345c49d80e351df3Mathieu Chartier } 235a25dce9b452ba17ef7cef768926c884177a3025eNicolas Geoffray bool will_generate_debug_symbols = false; 236e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier VLOG(jit) << "Calling JitLoad interpreter_only=" 237e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier << Runtime::Current()->GetInstrumentation()->InterpretOnly(); 2385b82d339955d1a0dc23eeb8d2d5659459ff987baNicolas Geoffray jit_compiler_handle_ = (jit_load_)(&will_generate_debug_symbols); 239e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier if (jit_compiler_handle_ == nullptr) { 240e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier dlclose(jit_library_handle_); 241e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier *error_msg = "JIT couldn't load compiler"; 242e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier return false; 243e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier } 244a25dce9b452ba17ef7cef768926c884177a3025eNicolas Geoffray generate_debug_info_ = will_generate_debug_symbols; 245e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier return true; 246e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier} 247e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier 248b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffraybool Jit::CompileMethod(ArtMethod* method, Thread* self, bool osr) { 249ffc87076dda9878cb2cc098149bae441d38b9268Calin Juravle DCHECK(Runtime::Current()->UseJitCompilation()); 250e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier DCHECK(!method->IsRuntimeMethod()); 251d9994f069dfeaa32ba929ca78816b5b83e2a4134Nicolas Geoffray 25273be1e8f8609708f6624bb297c9628de44fd8b6fNicolas Geoffray // Don't compile the method if it has breakpoints. 253d8565456d29f4ad05f11cf84d2d2dac488508e06Mathieu Chartier if (Dbg::IsDebuggerActive() && Dbg::MethodHasAnyBreakpoints(method)) { 254709b070044354d9f47641f273edacaeeb0240ab7David Sehr VLOG(jit) << "JIT not compiling " << method->PrettyMethod() << " due to breakpoint"; 255d8565456d29f4ad05f11cf84d2d2dac488508e06Mathieu Chartier return false; 256d8565456d29f4ad05f11cf84d2d2dac488508e06Mathieu Chartier } 25773be1e8f8609708f6624bb297c9628de44fd8b6fNicolas Geoffray 25873be1e8f8609708f6624bb297c9628de44fd8b6fNicolas Geoffray // Don't compile the method if we are supposed to be deoptimized. 25973be1e8f8609708f6624bb297c9628de44fd8b6fNicolas Geoffray instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); 26073be1e8f8609708f6624bb297c9628de44fd8b6fNicolas Geoffray if (instrumentation->AreAllMethodsDeoptimized() || instrumentation->IsDeoptimized(method)) { 261709b070044354d9f47641f273edacaeeb0240ab7David Sehr VLOG(jit) << "JIT not compiling " << method->PrettyMethod() << " due to deoptimization"; 26273be1e8f8609708f6624bb297c9628de44fd8b6fNicolas Geoffray return false; 26373be1e8f8609708f6624bb297c9628de44fd8b6fNicolas Geoffray } 26473be1e8f8609708f6624bb297c9628de44fd8b6fNicolas Geoffray 265d9994f069dfeaa32ba929ca78816b5b83e2a4134Nicolas Geoffray // If we get a request to compile a proxy method, we pass the actual Java method 266d9994f069dfeaa32ba929ca78816b5b83e2a4134Nicolas Geoffray // of that proxy method, as the compiler does not expect a proxy method. 267542451cc546779f5c67840e105c51205a1b0a8fdAndreas Gampe ArtMethod* method_to_compile = method->GetInterfaceMethodIfProxy(kRuntimePointerSize); 268d9994f069dfeaa32ba929ca78816b5b83e2a4134Nicolas Geoffray if (!code_cache_->NotifyCompilationOf(method_to_compile, self, osr)) { 26973be1e8f8609708f6624bb297c9628de44fd8b6fNicolas Geoffray return false; 27073be1e8f8609708f6624bb297c9628de44fd8b6fNicolas Geoffray } 27171cd50fb67fa48667b0ab59aa436a582c04ba43dNicolas Geoffray 27271cd50fb67fa48667b0ab59aa436a582c04ba43dNicolas Geoffray VLOG(jit) << "Compiling method " 273709b070044354d9f47641f273edacaeeb0240ab7David Sehr << ArtMethod::PrettyMethod(method_to_compile) 27471cd50fb67fa48667b0ab59aa436a582c04ba43dNicolas Geoffray << " osr=" << std::boolalpha << osr; 275d9994f069dfeaa32ba929ca78816b5b83e2a4134Nicolas Geoffray bool success = jit_compile_method_(jit_compiler_handle_, method_to_compile, self, osr); 276454b3b6774fe07765273b917ccc97da2eba776acbuzbee code_cache_->DoneCompiling(method_to_compile, self, osr); 27771cd50fb67fa48667b0ab59aa436a582c04ba43dNicolas Geoffray if (!success) { 27871cd50fb67fa48667b0ab59aa436a582c04ba43dNicolas Geoffray VLOG(jit) << "Failed to compile method " 279709b070044354d9f47641f273edacaeeb0240ab7David Sehr << ArtMethod::PrettyMethod(method_to_compile) 28071cd50fb67fa48667b0ab59aa436a582c04ba43dNicolas Geoffray << " osr=" << std::boolalpha << osr; 28171cd50fb67fa48667b0ab59aa436a582c04ba43dNicolas Geoffray } 282320ba91911c840528893e07b0af43d317cd4c9aaAndreas Gampe if (kIsDebugBuild) { 283320ba91911c840528893e07b0af43d317cd4c9aaAndreas Gampe if (self->IsExceptionPending()) { 284320ba91911c840528893e07b0af43d317cd4c9aaAndreas Gampe mirror::Throwable* exception = self->GetException(); 285320ba91911c840528893e07b0af43d317cd4c9aaAndreas Gampe LOG(FATAL) << "No pending exception expected after compiling " 286320ba91911c840528893e07b0af43d317cd4c9aaAndreas Gampe << ArtMethod::PrettyMethod(method) 287320ba91911c840528893e07b0af43d317cd4c9aaAndreas Gampe << ": " 288320ba91911c840528893e07b0af43d317cd4c9aaAndreas Gampe << exception->Dump(); 289320ba91911c840528893e07b0af43d317cd4c9aaAndreas Gampe } 290320ba91911c840528893e07b0af43d317cd4c9aaAndreas Gampe } 29173be1e8f8609708f6624bb297c9628de44fd8b6fNicolas Geoffray return success; 292e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier} 293e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier 294e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartiervoid Jit::CreateThreadPool() { 295274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray // There is a DCHECK in the 'AddSamples' method to ensure the tread pool 296274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray // is not null when we instrument. 2974471e4f7c5874bdaf93762b6047d4a4bebc465dfAndreas Gampe 2984471e4f7c5874bdaf93762b6047d4a4bebc465dfAndreas Gampe // We need peers as we may report the JIT thread, e.g., in the debugger. 2994471e4f7c5874bdaf93762b6047d4a4bebc465dfAndreas Gampe constexpr bool kJitPoolNeedsPeers = true; 3004471e4f7c5874bdaf93762b6047d4a4bebc465dfAndreas Gampe thread_pool_.reset(new ThreadPool("Jit thread pool", 1, kJitPoolNeedsPeers)); 3014471e4f7c5874bdaf93762b6047d4a4bebc465dfAndreas Gampe 302274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray thread_pool_->SetPthreadPriority(kJitPoolThreadPthreadPriority); 303021c5f285550bf1cfa435db6a8bc7e77843e2b7dNicolas Geoffray Start(); 304e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier} 305e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier 306e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartiervoid Jit::DeleteThreadPool() { 307274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray Thread* self = Thread::Current(); 308274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray DCHECK(Runtime::Current()->IsShuttingDown(self)); 309274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray if (thread_pool_ != nullptr) { 310274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray ThreadPool* cache = nullptr; 311274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray { 312274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray ScopedSuspendAll ssa(__FUNCTION__); 313274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray // Clear thread_pool_ field while the threads are suspended. 314274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray // A mutator in the 'AddSamples' method will check against it. 315274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray cache = thread_pool_.release(); 316274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray } 317274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray cache->StopWorkers(self); 318274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray cache->RemoveAllTasks(self); 319274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray // We could just suspend all threads, but we know those threads 320274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray // will finish in a short period, so it's not worth adding a suspend logic 321274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray // here. Besides, this is only done for shutdown. 322274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray cache->Wait(self, false, false); 323274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray delete cache; 324e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier } 325e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier} 326e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier 3274d77b6a511659f26fdc711e23825ffa6e7feed7aCalin Juravlevoid Jit::StartProfileSaver(const std::string& filename, 32877651c4bbd56d502bcf05347e641061bbebca099Calin Juravle const std::vector<std::string>& code_paths) { 329138dbff9246c89ac9fbe0b086b54fdab3f4451fbCalin Juravle if (profile_saver_options_.IsEnabled()) { 330138dbff9246c89ac9fbe0b086b54fdab3f4451fbCalin Juravle ProfileSaver::Start(profile_saver_options_, 331138dbff9246c89ac9fbe0b086b54fdab3f4451fbCalin Juravle filename, 332138dbff9246c89ac9fbe0b086b54fdab3f4451fbCalin Juravle code_cache_.get(), 33377651c4bbd56d502bcf05347e641061bbebca099Calin Juravle code_paths); 3344d77b6a511659f26fdc711e23825ffa6e7feed7aCalin Juravle } 3354d77b6a511659f26fdc711e23825ffa6e7feed7aCalin Juravle} 3364d77b6a511659f26fdc711e23825ffa6e7feed7aCalin Juravle 3374d77b6a511659f26fdc711e23825ffa6e7feed7aCalin Juravlevoid Jit::StopProfileSaver() { 338138dbff9246c89ac9fbe0b086b54fdab3f4451fbCalin Juravle if (profile_saver_options_.IsEnabled() && ProfileSaver::IsStarted()) { 339b8e69994d10534e0f4f96878725bc53e531f2c6fCalin Juravle ProfileSaver::Stop(dump_info_on_shutdown_); 34031f2c155975c5794d481df03eb0947cb48d2c6b5Calin Juravle } 34131f2c155975c5794d481df03eb0947cb48d2c6b5Calin Juravle} 34231f2c155975c5794d481df03eb0947cb48d2c6b5Calin Juravle 34305d241565f36df825cf56a4f1b61bfb7e4dcb056Siva Chandrabool Jit::JitAtFirstUse() { 344274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray return HotMethodThreshold() == 0; 34505d241565f36df825cf56a4f1b61bfb7e4dcb056Siva Chandra} 34605d241565f36df825cf56a4f1b61bfb7e4dcb056Siva Chandra 34735122443e5f8606cc5a660ac32745a06aefb341bNicolas Geoffraybool Jit::CanInvokeCompiledCode(ArtMethod* method) { 34835122443e5f8606cc5a660ac32745a06aefb341bNicolas Geoffray return code_cache_->ContainsPc(method->GetEntryPointFromQuickCompiledCode()); 34935122443e5f8606cc5a660ac32745a06aefb341bNicolas Geoffray} 35035122443e5f8606cc5a660ac32745a06aefb341bNicolas Geoffray 351e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu ChartierJit::~Jit() { 352138dbff9246c89ac9fbe0b086b54fdab3f4451fbCalin Juravle DCHECK(!profile_saver_options_.IsEnabled() || !ProfileSaver::IsStarted()); 353a4885cbaafd35fe9c60eb6cd95e41e2c86f54f66Mathieu Chartier if (dump_info_on_shutdown_) { 3543fec9ac0d5af1358d216eb2fdc2000ec0205f3f0Andreas Gampe DumpInfo(LOG_STREAM(INFO)); 355a4885cbaafd35fe9c60eb6cd95e41e2c86f54f66Mathieu Chartier } 356e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier DeleteThreadPool(); 357e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier if (jit_compiler_handle_ != nullptr) { 358e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier jit_unload_(jit_compiler_handle_); 35972918ea854d182ef25b2352bfe1c46c1e916a141Mathieu Chartier jit_compiler_handle_ = nullptr; 360e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier } 361e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier if (jit_library_handle_ != nullptr) { 362e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier dlclose(jit_library_handle_); 36372918ea854d182ef25b2352bfe1c46c1e916a141Mathieu Chartier jit_library_handle_ = nullptr; 364e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier } 365e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier} 366e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier 367160e6df5debaf77223eebddb8a4e3f7c5e729ad0Tamas Berghammervoid Jit::NewTypeLoadedIfUsingJit(mirror::Class* type) { 368ffc87076dda9878cb2cc098149bae441d38b9268Calin Juravle if (!Runtime::Current()->UseJitCompilation()) { 369ffc87076dda9878cb2cc098149bae441d38b9268Calin Juravle // No need to notify if we only use the JIT to save profiles. 370ffc87076dda9878cb2cc098149bae441d38b9268Calin Juravle return; 371ffc87076dda9878cb2cc098149bae441d38b9268Calin Juravle } 372160e6df5debaf77223eebddb8a4e3f7c5e729ad0Tamas Berghammer jit::Jit* jit = Runtime::Current()->GetJit(); 373ffc87076dda9878cb2cc098149bae441d38b9268Calin Juravle if (jit->generate_debug_info_) { 374fffbee4d158259633ec7b7f712eaf75be86bd4e5Tamas Berghammer DCHECK(jit->jit_types_loaded_ != nullptr); 375fffbee4d158259633ec7b7f712eaf75be86bd4e5Tamas Berghammer jit->jit_types_loaded_(jit->jit_compiler_handle_, &type, 1); 376fffbee4d158259633ec7b7f712eaf75be86bd4e5Tamas Berghammer } 377fffbee4d158259633ec7b7f712eaf75be86bd4e5Tamas Berghammer} 378fffbee4d158259633ec7b7f712eaf75be86bd4e5Tamas Berghammer 379fffbee4d158259633ec7b7f712eaf75be86bd4e5Tamas Berghammervoid Jit::DumpTypeInfoForLoadedTypes(ClassLinker* linker) { 380fffbee4d158259633ec7b7f712eaf75be86bd4e5Tamas Berghammer struct CollectClasses : public ClassVisitor { 38128357fab628bd9b91749988b554977398caf9963Mathieu Chartier bool operator()(ObjPtr<mirror::Class> klass) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { 38228357fab628bd9b91749988b554977398caf9963Mathieu Chartier classes_.push_back(klass.Ptr()); 383fffbee4d158259633ec7b7f712eaf75be86bd4e5Tamas Berghammer return true; 384fffbee4d158259633ec7b7f712eaf75be86bd4e5Tamas Berghammer } 3859b1c9b761dea9bc48a2994e3d4de46fc10343a25Mathieu Chartier std::vector<mirror::Class*> classes_; 386fffbee4d158259633ec7b7f712eaf75be86bd4e5Tamas Berghammer }; 387fffbee4d158259633ec7b7f712eaf75be86bd4e5Tamas Berghammer 388fffbee4d158259633ec7b7f712eaf75be86bd4e5Tamas Berghammer if (generate_debug_info_) { 389fffbee4d158259633ec7b7f712eaf75be86bd4e5Tamas Berghammer ScopedObjectAccess so(Thread::Current()); 390fffbee4d158259633ec7b7f712eaf75be86bd4e5Tamas Berghammer 391fffbee4d158259633ec7b7f712eaf75be86bd4e5Tamas Berghammer CollectClasses visitor; 392fffbee4d158259633ec7b7f712eaf75be86bd4e5Tamas Berghammer linker->VisitClasses(&visitor); 393fffbee4d158259633ec7b7f712eaf75be86bd4e5Tamas Berghammer jit_types_loaded_(jit_compiler_handle_, visitor.classes_.data(), visitor.classes_.size()); 394160e6df5debaf77223eebddb8a4e3f7c5e729ad0Tamas Berghammer } 395160e6df5debaf77223eebddb8a4e3f7c5e729ad0Tamas Berghammer} 396160e6df5debaf77223eebddb8a4e3f7c5e729ad0Tamas Berghammer 397b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffrayextern "C" void art_quick_osr_stub(void** stack, 398b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray uint32_t stack_size_in_bytes, 399b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray const uint8_t* native_pc, 400b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray JValue* result, 401b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray const char* shorty, 402b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray Thread* self); 403b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray 404b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffraybool Jit::MaybeDoOnStackReplacement(Thread* thread, 405b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray ArtMethod* method, 406b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray uint32_t dex_pc, 407b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray int32_t dex_pc_offset, 408b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray JValue* result) { 409e86621386d18a3a7178af6cfc2c05d1b34c3b995Nicolas Geoffray if (!kEnableOnStackReplacement) { 410e86621386d18a3a7178af6cfc2c05d1b34c3b995Nicolas Geoffray return false; 411e86621386d18a3a7178af6cfc2c05d1b34c3b995Nicolas Geoffray } 412e86621386d18a3a7178af6cfc2c05d1b34c3b995Nicolas Geoffray 413b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray Jit* jit = Runtime::Current()->GetJit(); 414b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray if (jit == nullptr) { 415b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray return false; 416b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray } 417b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray 418b88d59ef4fe611fe47e50a6a19785e03bbd5f93bNicolas Geoffray if (UNLIKELY(__builtin_frame_address(0) < thread->GetStackEnd())) { 419b88d59ef4fe611fe47e50a6a19785e03bbd5f93bNicolas Geoffray // Don't attempt to do an OSR if we are close to the stack limit. Since 420b88d59ef4fe611fe47e50a6a19785e03bbd5f93bNicolas Geoffray // the interpreter frames are still on stack, OSR has the potential 421b88d59ef4fe611fe47e50a6a19785e03bbd5f93bNicolas Geoffray // to stack overflow even for a simple loop. 422b88d59ef4fe611fe47e50a6a19785e03bbd5f93bNicolas Geoffray // b/27094810. 423b88d59ef4fe611fe47e50a6a19785e03bbd5f93bNicolas Geoffray return false; 424b88d59ef4fe611fe47e50a6a19785e03bbd5f93bNicolas Geoffray } 425b88d59ef4fe611fe47e50a6a19785e03bbd5f93bNicolas Geoffray 426d9bc433a89c41a255d1b669d075f802597839bdcNicolas Geoffray // Get the actual Java method if this method is from a proxy class. The compiler 427d9bc433a89c41a255d1b669d075f802597839bdcNicolas Geoffray // and the JIT code cache do not expect methods from proxy classes. 428542451cc546779f5c67840e105c51205a1b0a8fdAndreas Gampe method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize); 429d9bc433a89c41a255d1b669d075f802597839bdcNicolas Geoffray 430b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray // Cheap check if the method has been compiled already. That's an indicator that we should 431b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray // osr into it. 432b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray if (!jit->GetCodeCache()->ContainsPc(method->GetEntryPointFromQuickCompiledCode())) { 433b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray return false; 434b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray } 435b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray 436c0b2796ec2056a6ea15c67df1251db250ba7e9a2Nicolas Geoffray // Fetch some data before looking up for an OSR method. We don't want thread 437c0b2796ec2056a6ea15c67df1251db250ba7e9a2Nicolas Geoffray // suspension once we hold an OSR method, as the JIT code cache could delete the OSR 438c0b2796ec2056a6ea15c67df1251db250ba7e9a2Nicolas Geoffray // method while we are being suspended. 439b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray const size_t number_of_vregs = method->GetCodeItem()->registers_size_; 440d186dd8ecb1f25d3786d6b27adcd6b0b9ca04ea0Nicolas Geoffray const char* shorty = method->GetShorty(); 441709b070044354d9f47641f273edacaeeb0240ab7David Sehr std::string method_name(VLOG_IS_ON(jit) ? method->PrettyMethod() : ""); 442d186dd8ecb1f25d3786d6b27adcd6b0b9ca04ea0Nicolas Geoffray void** memory = nullptr; 443d186dd8ecb1f25d3786d6b27adcd6b0b9ca04ea0Nicolas Geoffray size_t frame_size = 0; 444d186dd8ecb1f25d3786d6b27adcd6b0b9ca04ea0Nicolas Geoffray ShadowFrame* shadow_frame = nullptr; 445d186dd8ecb1f25d3786d6b27adcd6b0b9ca04ea0Nicolas Geoffray const uint8_t* native_pc = nullptr; 446b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray 447d186dd8ecb1f25d3786d6b27adcd6b0b9ca04ea0Nicolas Geoffray { 448268764da8022cafa5661c5b514eaa343c5257e57Mathieu Chartier ScopedAssertNoThreadSuspension sts("Holding OSR method"); 449d186dd8ecb1f25d3786d6b27adcd6b0b9ca04ea0Nicolas Geoffray const OatQuickMethodHeader* osr_method = jit->GetCodeCache()->LookupOsrMethodHeader(method); 450d186dd8ecb1f25d3786d6b27adcd6b0b9ca04ea0Nicolas Geoffray if (osr_method == nullptr) { 451d186dd8ecb1f25d3786d6b27adcd6b0b9ca04ea0Nicolas Geoffray // No osr method yet, just return to the interpreter. 452d186dd8ecb1f25d3786d6b27adcd6b0b9ca04ea0Nicolas Geoffray return false; 453d186dd8ecb1f25d3786d6b27adcd6b0b9ca04ea0Nicolas Geoffray } 454b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray 455d186dd8ecb1f25d3786d6b27adcd6b0b9ca04ea0Nicolas Geoffray CodeInfo code_info = osr_method->GetOptimizedCodeInfo(); 45609ed09866da6d8c7448ef297c148bfa577a247c2David Srbecky CodeInfoEncoding encoding = code_info.ExtractEncoding(); 457b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray 458d186dd8ecb1f25d3786d6b27adcd6b0b9ca04ea0Nicolas Geoffray // Find stack map starting at the target dex_pc. 459d186dd8ecb1f25d3786d6b27adcd6b0b9ca04ea0Nicolas Geoffray StackMap stack_map = code_info.GetOsrStackMapForDexPc(dex_pc + dex_pc_offset, encoding); 460d186dd8ecb1f25d3786d6b27adcd6b0b9ca04ea0Nicolas Geoffray if (!stack_map.IsValid()) { 461d186dd8ecb1f25d3786d6b27adcd6b0b9ca04ea0Nicolas Geoffray // There is no OSR stack map for this dex pc offset. Just return to the interpreter in the 462d186dd8ecb1f25d3786d6b27adcd6b0b9ca04ea0Nicolas Geoffray // hope that the next branch has one. 463d186dd8ecb1f25d3786d6b27adcd6b0b9ca04ea0Nicolas Geoffray return false; 464d186dd8ecb1f25d3786d6b27adcd6b0b9ca04ea0Nicolas Geoffray } 465b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray 46629bdaeef925f35eed68f3e52d04416daa0a62317Aart Bik // Before allowing the jump, make sure the debugger is not active to avoid jumping from 46729bdaeef925f35eed68f3e52d04416daa0a62317Aart Bik // interpreter to OSR while e.g. single stepping. Note that we could selectively disable 46829bdaeef925f35eed68f3e52d04416daa0a62317Aart Bik // OSR when single stepping, but that's currently hard to know at this point. 46929bdaeef925f35eed68f3e52d04416daa0a62317Aart Bik if (Dbg::IsDebuggerActive()) { 47029bdaeef925f35eed68f3e52d04416daa0a62317Aart Bik return false; 47129bdaeef925f35eed68f3e52d04416daa0a62317Aart Bik } 47229bdaeef925f35eed68f3e52d04416daa0a62317Aart Bik 473d186dd8ecb1f25d3786d6b27adcd6b0b9ca04ea0Nicolas Geoffray // We found a stack map, now fill the frame with dex register values from the interpreter's 474d186dd8ecb1f25d3786d6b27adcd6b0b9ca04ea0Nicolas Geoffray // shadow frame. 475d186dd8ecb1f25d3786d6b27adcd6b0b9ca04ea0Nicolas Geoffray DexRegisterMap vreg_map = 476d186dd8ecb1f25d3786d6b27adcd6b0b9ca04ea0Nicolas Geoffray code_info.GetDexRegisterMapOf(stack_map, encoding, number_of_vregs); 477d186dd8ecb1f25d3786d6b27adcd6b0b9ca04ea0Nicolas Geoffray 478d186dd8ecb1f25d3786d6b27adcd6b0b9ca04ea0Nicolas Geoffray frame_size = osr_method->GetFrameSizeInBytes(); 479d186dd8ecb1f25d3786d6b27adcd6b0b9ca04ea0Nicolas Geoffray 480d186dd8ecb1f25d3786d6b27adcd6b0b9ca04ea0Nicolas Geoffray // Allocate memory to put shadow frame values. The osr stub will copy that memory to 481d186dd8ecb1f25d3786d6b27adcd6b0b9ca04ea0Nicolas Geoffray // stack. 482d186dd8ecb1f25d3786d6b27adcd6b0b9ca04ea0Nicolas Geoffray // Note that we could pass the shadow frame to the stub, and let it copy the values there, 483d186dd8ecb1f25d3786d6b27adcd6b0b9ca04ea0Nicolas Geoffray // but that is engineering complexity not worth the effort for something like OSR. 484d186dd8ecb1f25d3786d6b27adcd6b0b9ca04ea0Nicolas Geoffray memory = reinterpret_cast<void**>(malloc(frame_size)); 485d186dd8ecb1f25d3786d6b27adcd6b0b9ca04ea0Nicolas Geoffray CHECK(memory != nullptr); 486d186dd8ecb1f25d3786d6b27adcd6b0b9ca04ea0Nicolas Geoffray memset(memory, 0, frame_size); 487d186dd8ecb1f25d3786d6b27adcd6b0b9ca04ea0Nicolas Geoffray 488d186dd8ecb1f25d3786d6b27adcd6b0b9ca04ea0Nicolas Geoffray // Art ABI: ArtMethod is at the bottom of the stack. 489d186dd8ecb1f25d3786d6b27adcd6b0b9ca04ea0Nicolas Geoffray memory[0] = method; 490d186dd8ecb1f25d3786d6b27adcd6b0b9ca04ea0Nicolas Geoffray 491d186dd8ecb1f25d3786d6b27adcd6b0b9ca04ea0Nicolas Geoffray shadow_frame = thread->PopShadowFrame(); 492d186dd8ecb1f25d3786d6b27adcd6b0b9ca04ea0Nicolas Geoffray if (!vreg_map.IsValid()) { 493d186dd8ecb1f25d3786d6b27adcd6b0b9ca04ea0Nicolas Geoffray // If we don't have a dex register map, then there are no live dex registers at 494d186dd8ecb1f25d3786d6b27adcd6b0b9ca04ea0Nicolas Geoffray // this dex pc. 495d186dd8ecb1f25d3786d6b27adcd6b0b9ca04ea0Nicolas Geoffray } else { 496d186dd8ecb1f25d3786d6b27adcd6b0b9ca04ea0Nicolas Geoffray for (uint16_t vreg = 0; vreg < number_of_vregs; ++vreg) { 497d186dd8ecb1f25d3786d6b27adcd6b0b9ca04ea0Nicolas Geoffray DexRegisterLocation::Kind location = 498d186dd8ecb1f25d3786d6b27adcd6b0b9ca04ea0Nicolas Geoffray vreg_map.GetLocationKind(vreg, number_of_vregs, code_info, encoding); 499d186dd8ecb1f25d3786d6b27adcd6b0b9ca04ea0Nicolas Geoffray if (location == DexRegisterLocation::Kind::kNone) { 500c0b2796ec2056a6ea15c67df1251db250ba7e9a2Nicolas Geoffray // Dex register is dead or uninitialized. 501d186dd8ecb1f25d3786d6b27adcd6b0b9ca04ea0Nicolas Geoffray continue; 502d186dd8ecb1f25d3786d6b27adcd6b0b9ca04ea0Nicolas Geoffray } 503d186dd8ecb1f25d3786d6b27adcd6b0b9ca04ea0Nicolas Geoffray 504d186dd8ecb1f25d3786d6b27adcd6b0b9ca04ea0Nicolas Geoffray if (location == DexRegisterLocation::Kind::kConstant) { 505d186dd8ecb1f25d3786d6b27adcd6b0b9ca04ea0Nicolas Geoffray // We skip constants because the compiled code knows how to handle them. 506d186dd8ecb1f25d3786d6b27adcd6b0b9ca04ea0Nicolas Geoffray continue; 507d186dd8ecb1f25d3786d6b27adcd6b0b9ca04ea0Nicolas Geoffray } 508d186dd8ecb1f25d3786d6b27adcd6b0b9ca04ea0Nicolas Geoffray 5097dc11782ff0a5dffcd8108f256f8975f0b3e8076David Srbecky DCHECK_EQ(location, DexRegisterLocation::Kind::kInStack); 510d186dd8ecb1f25d3786d6b27adcd6b0b9ca04ea0Nicolas Geoffray 511d186dd8ecb1f25d3786d6b27adcd6b0b9ca04ea0Nicolas Geoffray int32_t vreg_value = shadow_frame->GetVReg(vreg); 512d186dd8ecb1f25d3786d6b27adcd6b0b9ca04ea0Nicolas Geoffray int32_t slot_offset = vreg_map.GetStackOffsetInBytes(vreg, 513d186dd8ecb1f25d3786d6b27adcd6b0b9ca04ea0Nicolas Geoffray number_of_vregs, 514d186dd8ecb1f25d3786d6b27adcd6b0b9ca04ea0Nicolas Geoffray code_info, 515d186dd8ecb1f25d3786d6b27adcd6b0b9ca04ea0Nicolas Geoffray encoding); 516d186dd8ecb1f25d3786d6b27adcd6b0b9ca04ea0Nicolas Geoffray DCHECK_LT(slot_offset, static_cast<int32_t>(frame_size)); 517d186dd8ecb1f25d3786d6b27adcd6b0b9ca04ea0Nicolas Geoffray DCHECK_GT(slot_offset, 0); 518d186dd8ecb1f25d3786d6b27adcd6b0b9ca04ea0Nicolas Geoffray (reinterpret_cast<int32_t*>(memory))[slot_offset / sizeof(int32_t)] = vreg_value; 519d186dd8ecb1f25d3786d6b27adcd6b0b9ca04ea0Nicolas Geoffray } 520b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray } 521d186dd8ecb1f25d3786d6b27adcd6b0b9ca04ea0Nicolas Geoffray 522575d3e60c68b5cf481b615dde4a16283507b19edMathieu Chartier native_pc = stack_map.GetNativePcOffset(encoding.stack_map.encoding, kRuntimeISA) + 52309ed09866da6d8c7448ef297c148bfa577a247c2David Srbecky osr_method->GetEntryPoint(); 524d186dd8ecb1f25d3786d6b27adcd6b0b9ca04ea0Nicolas Geoffray VLOG(jit) << "Jumping to " 525d186dd8ecb1f25d3786d6b27adcd6b0b9ca04ea0Nicolas Geoffray << method_name 526d186dd8ecb1f25d3786d6b27adcd6b0b9ca04ea0Nicolas Geoffray << "@" 527d186dd8ecb1f25d3786d6b27adcd6b0b9ca04ea0Nicolas Geoffray << std::hex << reinterpret_cast<uintptr_t>(native_pc); 528b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray } 529b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray 530b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray { 531b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray ManagedStack fragment; 532b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray thread->PushManagedStackFragment(&fragment); 533b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray (*art_quick_osr_stub)(memory, 534b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray frame_size, 535b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray native_pc, 536b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray result, 537d186dd8ecb1f25d3786d6b27adcd6b0b9ca04ea0Nicolas Geoffray shorty, 538b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray thread); 539d186dd8ecb1f25d3786d6b27adcd6b0b9ca04ea0Nicolas Geoffray 540b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray if (UNLIKELY(thread->GetException() == Thread::GetDeoptimizationException())) { 541b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray thread->DeoptimizeWithDeoptimizationException(result); 542b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray } 543b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray thread->PopManagedStackFragment(fragment); 544b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray } 545b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray free(memory); 546b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray thread->PushShadowFrame(shadow_frame); 547d186dd8ecb1f25d3786d6b27adcd6b0b9ca04ea0Nicolas Geoffray VLOG(jit) << "Done running OSR code for " << method_name; 548b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray return true; 549b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray} 550b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray 551a4f81546373f4cb5fa6dfc135307ee0a1d930872Nicolas Geoffrayvoid Jit::AddMemoryUsage(ArtMethod* method, size_t bytes) { 552a4f81546373f4cb5fa6dfc135307ee0a1d930872Nicolas Geoffray if (bytes > 4 * MB) { 553a4f81546373f4cb5fa6dfc135307ee0a1d930872Nicolas Geoffray LOG(INFO) << "Compiler allocated " 554a4f81546373f4cb5fa6dfc135307ee0a1d930872Nicolas Geoffray << PrettySize(bytes) 555a4f81546373f4cb5fa6dfc135307ee0a1d930872Nicolas Geoffray << " to compile " 556709b070044354d9f47641f273edacaeeb0240ab7David Sehr << ArtMethod::PrettyMethod(method); 557a4f81546373f4cb5fa6dfc135307ee0a1d930872Nicolas Geoffray } 558a4f81546373f4cb5fa6dfc135307ee0a1d930872Nicolas Geoffray MutexLock mu(Thread::Current(), lock_); 559a4f81546373f4cb5fa6dfc135307ee0a1d930872Nicolas Geoffray memory_use_.AddValue(bytes); 560a4f81546373f4cb5fa6dfc135307ee0a1d930872Nicolas Geoffray} 561a4f81546373f4cb5fa6dfc135307ee0a1d930872Nicolas Geoffray 562274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffrayclass JitCompileTask FINAL : public Task { 563274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray public: 564274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray enum TaskKind { 565274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray kAllocateProfile, 566274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray kCompile, 567274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray kCompileOsr 568274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray }; 569274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray 570274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray JitCompileTask(ArtMethod* method, TaskKind kind) : method_(method), kind_(kind) { 571274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray ScopedObjectAccess soa(Thread::Current()); 572274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray // Add a global ref to the class to prevent class unloading until compilation is done. 573274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray klass_ = soa.Vm()->AddGlobalRef(soa.Self(), method_->GetDeclaringClass()); 574274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray CHECK(klass_ != nullptr); 575274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray } 576274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray 577274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray ~JitCompileTask() { 578274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray ScopedObjectAccess soa(Thread::Current()); 579274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray soa.Vm()->DeleteGlobalRef(soa.Self(), klass_); 580274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray } 581274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray 582274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray void Run(Thread* self) OVERRIDE { 583274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray ScopedObjectAccess soa(self); 584274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray if (kind_ == kCompile) { 58571cd50fb67fa48667b0ab59aa436a582c04ba43dNicolas Geoffray Runtime::Current()->GetJit()->CompileMethod(method_, self, /* osr */ false); 586274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray } else if (kind_ == kCompileOsr) { 58771cd50fb67fa48667b0ab59aa436a582c04ba43dNicolas Geoffray Runtime::Current()->GetJit()->CompileMethod(method_, self, /* osr */ true); 588274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray } else { 589274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray DCHECK(kind_ == kAllocateProfile); 590274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray if (ProfilingInfo::Create(self, method_, /* retry_allocation */ true)) { 591709b070044354d9f47641f273edacaeeb0240ab7David Sehr VLOG(jit) << "Start profiling " << ArtMethod::PrettyMethod(method_); 592274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray } 593274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray } 594a26389234ac66a4a5042c7bf7195c6531663d24dCalin Juravle ProfileSaver::NotifyJitActivity(); 595274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray } 596274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray 597274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray void Finalize() OVERRIDE { 598274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray delete this; 599274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray } 600274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray 601274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray private: 602274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray ArtMethod* const method_; 603274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray const TaskKind kind_; 604274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray jobject klass_; 605274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray 606274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray DISALLOW_IMPLICIT_CONSTRUCTORS(JitCompileTask); 607274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray}; 608274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray 60971cd50fb67fa48667b0ab59aa436a582c04ba43dNicolas Geoffrayvoid Jit::AddSamples(Thread* self, ArtMethod* method, uint16_t count, bool with_backedges) { 610274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray if (thread_pool_ == nullptr) { 611274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray // Should only see this when shutting down. 612274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray DCHECK(Runtime::Current()->IsShuttingDown(self)); 613274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray return; 614274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray } 615274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray 616250a378d5a2152662e0fa820f2b38f794ddd3596Nicolas Geoffray if (method->IsClassInitializer() || method->IsNative() || !method->IsCompilable()) { 617274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray // We do not want to compile such methods. 618274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray return; 619274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray } 620274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray DCHECK(thread_pool_ != nullptr); 621274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray DCHECK_GT(warm_method_threshold_, 0); 622274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray DCHECK_GT(hot_method_threshold_, warm_method_threshold_); 623274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray DCHECK_GT(osr_method_threshold_, hot_method_threshold_); 624274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray DCHECK_GE(priority_thread_weight_, 1); 625274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray DCHECK_LE(priority_thread_weight_, hot_method_threshold_); 626274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray 627274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray int32_t starting_count = method->GetCounter(); 628274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray if (Jit::ShouldUsePriorityThreadWeight()) { 629274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray count *= priority_thread_weight_; 630274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray } 631274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray int32_t new_count = starting_count + count; // int32 here to avoid wrap-around; 632274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray if (starting_count < warm_method_threshold_) { 63371cd50fb67fa48667b0ab59aa436a582c04ba43dNicolas Geoffray if ((new_count >= warm_method_threshold_) && 634542451cc546779f5c67840e105c51205a1b0a8fdAndreas Gampe (method->GetProfilingInfo(kRuntimePointerSize) == nullptr)) { 635274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray bool success = ProfilingInfo::Create(self, method, /* retry_allocation */ false); 636274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray if (success) { 637709b070044354d9f47641f273edacaeeb0240ab7David Sehr VLOG(jit) << "Start profiling " << method->PrettyMethod(); 638274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray } 639274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray 640274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray if (thread_pool_ == nullptr) { 641274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray // Calling ProfilingInfo::Create might put us in a suspended state, which could 642274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray // lead to the thread pool being deleted when we are shutting down. 643274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray DCHECK(Runtime::Current()->IsShuttingDown(self)); 644274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray return; 645274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray } 646274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray 647274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray if (!success) { 648274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray // We failed allocating. Instead of doing the collection on the Java thread, we push 649274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray // an allocation to a compiler thread, that will do the collection. 650274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray thread_pool_->AddTask(self, new JitCompileTask(method, JitCompileTask::kAllocateProfile)); 651274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray } 652274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray } 653274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray // Avoid jumping more than one state at a time. 654274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray new_count = std::min(new_count, hot_method_threshold_ - 1); 655ffc87076dda9878cb2cc098149bae441d38b9268Calin Juravle } else if (use_jit_compilation_) { 656ffc87076dda9878cb2cc098149bae441d38b9268Calin Juravle if (starting_count < hot_method_threshold_) { 657ffc87076dda9878cb2cc098149bae441d38b9268Calin Juravle if ((new_count >= hot_method_threshold_) && 658ffc87076dda9878cb2cc098149bae441d38b9268Calin Juravle !code_cache_->ContainsPc(method->GetEntryPointFromQuickCompiledCode())) { 659ffc87076dda9878cb2cc098149bae441d38b9268Calin Juravle DCHECK(thread_pool_ != nullptr); 660ffc87076dda9878cb2cc098149bae441d38b9268Calin Juravle thread_pool_->AddTask(self, new JitCompileTask(method, JitCompileTask::kCompile)); 661ffc87076dda9878cb2cc098149bae441d38b9268Calin Juravle } 662ffc87076dda9878cb2cc098149bae441d38b9268Calin Juravle // Avoid jumping more than one state at a time. 663ffc87076dda9878cb2cc098149bae441d38b9268Calin Juravle new_count = std::min(new_count, osr_method_threshold_ - 1); 664ffc87076dda9878cb2cc098149bae441d38b9268Calin Juravle } else if (starting_count < osr_method_threshold_) { 665ffc87076dda9878cb2cc098149bae441d38b9268Calin Juravle if (!with_backedges) { 666ffc87076dda9878cb2cc098149bae441d38b9268Calin Juravle // If the samples don't contain any back edge, we don't increment the hotness. 667ffc87076dda9878cb2cc098149bae441d38b9268Calin Juravle return; 668ffc87076dda9878cb2cc098149bae441d38b9268Calin Juravle } 669ffc87076dda9878cb2cc098149bae441d38b9268Calin Juravle if ((new_count >= osr_method_threshold_) && !code_cache_->IsOsrCompiled(method)) { 670ffc87076dda9878cb2cc098149bae441d38b9268Calin Juravle DCHECK(thread_pool_ != nullptr); 671ffc87076dda9878cb2cc098149bae441d38b9268Calin Juravle thread_pool_->AddTask(self, new JitCompileTask(method, JitCompileTask::kCompileOsr)); 672ffc87076dda9878cb2cc098149bae441d38b9268Calin Juravle } 673274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray } 674274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray } 675274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray // Update hotness counter 676274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray method->SetCounter(new_count); 677274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray} 678274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray 679274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffrayvoid Jit::MethodEntered(Thread* thread, ArtMethod* method) { 680ffc87076dda9878cb2cc098149bae441d38b9268Calin Juravle Runtime* runtime = Runtime::Current(); 681ffc87076dda9878cb2cc098149bae441d38b9268Calin Juravle if (UNLIKELY(runtime->UseJitCompilation() && runtime->GetJit()->JitAtFirstUse())) { 682274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray // The compiler requires a ProfilingInfo object. 683274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray ProfilingInfo::Create(thread, method, /* retry_allocation */ true); 684274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray JitCompileTask compile_task(method, JitCompileTask::kCompile); 685274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray compile_task.Run(thread); 686274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray return; 687274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray } 688274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray 689542451cc546779f5c67840e105c51205a1b0a8fdAndreas Gampe ProfilingInfo* profiling_info = method->GetProfilingInfo(kRuntimePointerSize); 690274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray // Update the entrypoint if the ProfilingInfo has one. The interpreter will call it 691274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray // instead of interpreting the method. 692480d5108fa62f28cbc2e7072610f974953ff73a8Nicolas Geoffray if ((profiling_info != nullptr) && (profiling_info->GetSavedEntryPoint() != nullptr)) { 693480d5108fa62f28cbc2e7072610f974953ff73a8Nicolas Geoffray Runtime::Current()->GetInstrumentation()->UpdateMethodsCode( 694480d5108fa62f28cbc2e7072610f974953ff73a8Nicolas Geoffray method, profiling_info->GetSavedEntryPoint()); 695274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray } else { 69671cd50fb67fa48667b0ab59aa436a582c04ba43dNicolas Geoffray AddSamples(thread, method, 1, /* with_backedges */false); 697274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray } 698274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray} 699274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray 700ef41db7a3f322a1feb305fdb457410c4cea94d00Mathieu Chartiervoid Jit::InvokeVirtualOrInterface(ObjPtr<mirror::Object> this_object, 701274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray ArtMethod* caller, 702274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray uint32_t dex_pc, 703274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray ArtMethod* callee ATTRIBUTE_UNUSED) { 704268764da8022cafa5661c5b514eaa343c5257e57Mathieu Chartier ScopedAssertNoThreadSuspension ants(__FUNCTION__); 705274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray DCHECK(this_object != nullptr); 706542451cc546779f5c67840e105c51205a1b0a8fdAndreas Gampe ProfilingInfo* info = caller->GetProfilingInfo(kRuntimePointerSize); 707274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray if (info != nullptr) { 708274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray info->AddInvokeInfo(dex_pc, this_object->GetClass()); 709274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray } 710274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray} 711274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray 712274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffrayvoid Jit::WaitForCompilationToFinish(Thread* self) { 713274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray if (thread_pool_ != nullptr) { 714274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray thread_pool_->Wait(self, false, false); 715274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray } 716274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray} 717274fe4adcb0610a9920be7814d9beb9cac6417ceNicolas Geoffray 718021c5f285550bf1cfa435db6a8bc7e77843e2b7dNicolas Geoffrayvoid Jit::Stop() { 719021c5f285550bf1cfa435db6a8bc7e77843e2b7dNicolas Geoffray Thread* self = Thread::Current(); 720021c5f285550bf1cfa435db6a8bc7e77843e2b7dNicolas Geoffray // TODO(ngeoffray): change API to not require calling WaitForCompilationToFinish twice. 721021c5f285550bf1cfa435db6a8bc7e77843e2b7dNicolas Geoffray WaitForCompilationToFinish(self); 722021c5f285550bf1cfa435db6a8bc7e77843e2b7dNicolas Geoffray GetThreadPool()->StopWorkers(self); 723021c5f285550bf1cfa435db6a8bc7e77843e2b7dNicolas Geoffray WaitForCompilationToFinish(self); 724021c5f285550bf1cfa435db6a8bc7e77843e2b7dNicolas Geoffray} 725021c5f285550bf1cfa435db6a8bc7e77843e2b7dNicolas Geoffray 726021c5f285550bf1cfa435db6a8bc7e77843e2b7dNicolas Geoffrayvoid Jit::Start() { 727021c5f285550bf1cfa435db6a8bc7e77843e2b7dNicolas Geoffray GetThreadPool()->StartWorkers(Thread::Current()); 728021c5f285550bf1cfa435db6a8bc7e77843e2b7dNicolas Geoffray} 729021c5f285550bf1cfa435db6a8bc7e77843e2b7dNicolas Geoffray 730f149b3fc6fd315d34244bce709898fdbbddc246fAndreas GampeScopedJitSuspend::ScopedJitSuspend() { 731f149b3fc6fd315d34244bce709898fdbbddc246fAndreas Gampe jit::Jit* jit = Runtime::Current()->GetJit(); 732f149b3fc6fd315d34244bce709898fdbbddc246fAndreas Gampe was_on_ = (jit != nullptr) && (jit->GetThreadPool() != nullptr); 733f149b3fc6fd315d34244bce709898fdbbddc246fAndreas Gampe if (was_on_) { 734021c5f285550bf1cfa435db6a8bc7e77843e2b7dNicolas Geoffray jit->Stop(); 735f149b3fc6fd315d34244bce709898fdbbddc246fAndreas Gampe } 736f149b3fc6fd315d34244bce709898fdbbddc246fAndreas Gampe} 737f149b3fc6fd315d34244bce709898fdbbddc246fAndreas Gampe 738f149b3fc6fd315d34244bce709898fdbbddc246fAndreas GampeScopedJitSuspend::~ScopedJitSuspend() { 739f149b3fc6fd315d34244bce709898fdbbddc246fAndreas Gampe if (was_on_) { 740f149b3fc6fd315d34244bce709898fdbbddc246fAndreas Gampe DCHECK(Runtime::Current()->GetJit() != nullptr); 741f149b3fc6fd315d34244bce709898fdbbddc246fAndreas Gampe DCHECK(Runtime::Current()->GetJit()->GetThreadPool() != nullptr); 742021c5f285550bf1cfa435db6a8bc7e77843e2b7dNicolas Geoffray Runtime::Current()->GetJit()->Start(); 743f149b3fc6fd315d34244bce709898fdbbddc246fAndreas Gampe } 744f149b3fc6fd315d34244bce709898fdbbddc246fAndreas Gampe} 745f149b3fc6fd315d34244bce709898fdbbddc246fAndreas Gampe 746e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier} // namespace jit 747e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier} // namespace art 748