• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2014 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 "jit.h"
18 
19 #include <dlfcn.h>
20 
21 #include "art_method-inl.h"
22 #include "base/enums.h"
23 #include "base/logging.h"
24 #include "base/memory_tool.h"
25 #include "debugger.h"
26 #include "entrypoints/runtime_asm_entrypoints.h"
27 #include "interpreter/interpreter.h"
28 #include "java_vm_ext.h"
29 #include "jit_code_cache.h"
30 #include "oat_file_manager.h"
31 #include "oat_quick_method_header.h"
32 #include "profile_compilation_info.h"
33 #include "profile_saver.h"
34 #include "runtime.h"
35 #include "runtime_options.h"
36 #include "stack.h"
37 #include "stack_map.h"
38 #include "thread-inl.h"
39 #include "thread_list.h"
40 #include "utils.h"
41 
42 namespace art {
43 namespace jit {
44 
45 static constexpr bool kEnableOnStackReplacement = true;
46 // At what priority to schedule jit threads. 9 is the lowest foreground priority on device.
47 static constexpr int kJitPoolThreadPthreadPriority = 9;
48 
49 // Different compilation threshold constants. These can be overridden on the command line.
50 static constexpr size_t kJitDefaultCompileThreshold           = 10000;  // Non-debug default.
51 static constexpr size_t kJitStressDefaultCompileThreshold     = 100;    // Fast-debug build.
52 static constexpr size_t kJitSlowStressDefaultCompileThreshold = 2;      // Slow-debug build.
53 
54 // JIT compiler
55 void* Jit::jit_library_handle_= nullptr;
56 void* Jit::jit_compiler_handle_ = nullptr;
57 void* (*Jit::jit_load_)(bool*) = nullptr;
58 void (*Jit::jit_unload_)(void*) = nullptr;
59 bool (*Jit::jit_compile_method_)(void*, ArtMethod*, Thread*, bool) = nullptr;
60 void (*Jit::jit_types_loaded_)(void*, mirror::Class**, size_t count) = nullptr;
61 bool Jit::generate_debug_info_ = false;
62 
63 struct StressModeHelper {
64   DECLARE_RUNTIME_DEBUG_FLAG(kSlowMode);
65 };
66 DEFINE_RUNTIME_DEBUG_FLAG(StressModeHelper, kSlowMode);
67 
CreateFromRuntimeArguments(const RuntimeArgumentMap & options)68 JitOptions* JitOptions::CreateFromRuntimeArguments(const RuntimeArgumentMap& options) {
69   auto* jit_options = new JitOptions;
70   jit_options->use_jit_compilation_ = options.GetOrDefault(RuntimeArgumentMap::UseJitCompilation);
71 
72   jit_options->code_cache_initial_capacity_ =
73       options.GetOrDefault(RuntimeArgumentMap::JITCodeCacheInitialCapacity);
74   jit_options->code_cache_max_capacity_ =
75       options.GetOrDefault(RuntimeArgumentMap::JITCodeCacheMaxCapacity);
76   jit_options->dump_info_on_shutdown_ =
77       options.Exists(RuntimeArgumentMap::DumpJITInfoOnShutdown);
78   jit_options->profile_saver_options_ =
79       options.GetOrDefault(RuntimeArgumentMap::ProfileSaverOpts);
80 
81   if (options.Exists(RuntimeArgumentMap::JITCompileThreshold)) {
82     jit_options->compile_threshold_ = *options.Get(RuntimeArgumentMap::JITCompileThreshold);
83   } else {
84     jit_options->compile_threshold_ =
85         kIsDebugBuild
86             ? (StressModeHelper::kSlowMode
87                    ? kJitSlowStressDefaultCompileThreshold
88                    : kJitStressDefaultCompileThreshold)
89             : kJitDefaultCompileThreshold;
90   }
91   if (jit_options->compile_threshold_ > std::numeric_limits<uint16_t>::max()) {
92     LOG(FATAL) << "Method compilation threshold is above its internal limit.";
93   }
94 
95   if (options.Exists(RuntimeArgumentMap::JITWarmupThreshold)) {
96     jit_options->warmup_threshold_ = *options.Get(RuntimeArgumentMap::JITWarmupThreshold);
97     if (jit_options->warmup_threshold_ > std::numeric_limits<uint16_t>::max()) {
98       LOG(FATAL) << "Method warmup threshold is above its internal limit.";
99     }
100   } else {
101     jit_options->warmup_threshold_ = jit_options->compile_threshold_ / 2;
102   }
103 
104   if (options.Exists(RuntimeArgumentMap::JITOsrThreshold)) {
105     jit_options->osr_threshold_ = *options.Get(RuntimeArgumentMap::JITOsrThreshold);
106     if (jit_options->osr_threshold_ > std::numeric_limits<uint16_t>::max()) {
107       LOG(FATAL) << "Method on stack replacement threshold is above its internal limit.";
108     }
109   } else {
110     jit_options->osr_threshold_ = jit_options->compile_threshold_ * 2;
111     if (jit_options->osr_threshold_ > std::numeric_limits<uint16_t>::max()) {
112       jit_options->osr_threshold_ = std::numeric_limits<uint16_t>::max();
113     }
114   }
115 
116   if (options.Exists(RuntimeArgumentMap::JITPriorityThreadWeight)) {
117     jit_options->priority_thread_weight_ =
118         *options.Get(RuntimeArgumentMap::JITPriorityThreadWeight);
119     if (jit_options->priority_thread_weight_ > jit_options->warmup_threshold_) {
120       LOG(FATAL) << "Priority thread weight is above the warmup threshold.";
121     } else if (jit_options->priority_thread_weight_ == 0) {
122       LOG(FATAL) << "Priority thread weight cannot be 0.";
123     }
124   } else {
125     jit_options->priority_thread_weight_ = std::max(
126         jit_options->warmup_threshold_ / Jit::kDefaultPriorityThreadWeightRatio,
127         static_cast<size_t>(1));
128   }
129 
130   if (options.Exists(RuntimeArgumentMap::JITInvokeTransitionWeight)) {
131     jit_options->invoke_transition_weight_ =
132         *options.Get(RuntimeArgumentMap::JITInvokeTransitionWeight);
133     if (jit_options->invoke_transition_weight_ > jit_options->warmup_threshold_) {
134       LOG(FATAL) << "Invoke transition weight is above the warmup threshold.";
135     } else if (jit_options->invoke_transition_weight_  == 0) {
136       LOG(FATAL) << "Invoke transition weight cannot be 0.";
137     }
138   } else {
139     jit_options->invoke_transition_weight_ = std::max(
140         jit_options->warmup_threshold_ / Jit::kDefaultInvokeTransitionWeightRatio,
141         static_cast<size_t>(1));
142   }
143 
144   return jit_options;
145 }
146 
ShouldUsePriorityThreadWeight()147 bool Jit::ShouldUsePriorityThreadWeight() {
148   return Runtime::Current()->InJankPerceptibleProcessState()
149       && Thread::Current()->IsJitSensitiveThread();
150 }
151 
DumpInfo(std::ostream & os)152 void Jit::DumpInfo(std::ostream& os) {
153   code_cache_->Dump(os);
154   cumulative_timings_.Dump(os);
155   MutexLock mu(Thread::Current(), lock_);
156   memory_use_.PrintMemoryUse(os);
157 }
158 
DumpForSigQuit(std::ostream & os)159 void Jit::DumpForSigQuit(std::ostream& os) {
160   DumpInfo(os);
161   ProfileSaver::DumpInstanceInfo(os);
162 }
163 
AddTimingLogger(const TimingLogger & logger)164 void Jit::AddTimingLogger(const TimingLogger& logger) {
165   cumulative_timings_.AddLogger(logger);
166 }
167 
Jit()168 Jit::Jit() : dump_info_on_shutdown_(false),
169              cumulative_timings_("JIT timings"),
170              memory_use_("Memory used for compilation", 16),
171              lock_("JIT memory use lock"),
172              use_jit_compilation_(true),
173              hot_method_threshold_(0),
174              warm_method_threshold_(0),
175              osr_method_threshold_(0),
176              priority_thread_weight_(0),
177              invoke_transition_weight_(0) {}
178 
Create(JitOptions * options,std::string * error_msg)179 Jit* Jit::Create(JitOptions* options, std::string* error_msg) {
180   DCHECK(options->UseJitCompilation() || options->GetProfileSaverOptions().IsEnabled());
181   std::unique_ptr<Jit> jit(new Jit);
182   jit->dump_info_on_shutdown_ = options->DumpJitInfoOnShutdown();
183   if (jit_compiler_handle_ == nullptr && !LoadCompiler(error_msg)) {
184     return nullptr;
185   }
186   jit->code_cache_.reset(JitCodeCache::Create(
187       options->GetCodeCacheInitialCapacity(),
188       options->GetCodeCacheMaxCapacity(),
189       jit->generate_debug_info_,
190       error_msg));
191   if (jit->GetCodeCache() == nullptr) {
192     return nullptr;
193   }
194   jit->use_jit_compilation_ = options->UseJitCompilation();
195   jit->profile_saver_options_ = options->GetProfileSaverOptions();
196   VLOG(jit) << "JIT created with initial_capacity="
197       << PrettySize(options->GetCodeCacheInitialCapacity())
198       << ", max_capacity=" << PrettySize(options->GetCodeCacheMaxCapacity())
199       << ", compile_threshold=" << options->GetCompileThreshold()
200       << ", profile_saver_options=" << options->GetProfileSaverOptions();
201 
202 
203   jit->hot_method_threshold_ = options->GetCompileThreshold();
204   jit->warm_method_threshold_ = options->GetWarmupThreshold();
205   jit->osr_method_threshold_ = options->GetOsrThreshold();
206   jit->priority_thread_weight_ = options->GetPriorityThreadWeight();
207   jit->invoke_transition_weight_ = options->GetInvokeTransitionWeight();
208 
209   jit->CreateThreadPool();
210 
211   // Notify native debugger about the classes already loaded before the creation of the jit.
212   jit->DumpTypeInfoForLoadedTypes(Runtime::Current()->GetClassLinker());
213   return jit.release();
214 }
215 
LoadCompilerLibrary(std::string * error_msg)216 bool Jit::LoadCompilerLibrary(std::string* error_msg) {
217   jit_library_handle_ = dlopen(
218       kIsDebugBuild ? "libartd-compiler.so" : "libart-compiler.so", RTLD_NOW);
219   if (jit_library_handle_ == nullptr) {
220     std::ostringstream oss;
221     oss << "JIT could not load libart-compiler.so: " << dlerror();
222     *error_msg = oss.str();
223     return false;
224   }
225   jit_load_ = reinterpret_cast<void* (*)(bool*)>(dlsym(jit_library_handle_, "jit_load"));
226   if (jit_load_ == nullptr) {
227     dlclose(jit_library_handle_);
228     *error_msg = "JIT couldn't find jit_load entry point";
229     return false;
230   }
231   jit_unload_ = reinterpret_cast<void (*)(void*)>(
232       dlsym(jit_library_handle_, "jit_unload"));
233   if (jit_unload_ == nullptr) {
234     dlclose(jit_library_handle_);
235     *error_msg = "JIT couldn't find jit_unload entry point";
236     return false;
237   }
238   jit_compile_method_ = reinterpret_cast<bool (*)(void*, ArtMethod*, Thread*, bool)>(
239       dlsym(jit_library_handle_, "jit_compile_method"));
240   if (jit_compile_method_ == nullptr) {
241     dlclose(jit_library_handle_);
242     *error_msg = "JIT couldn't find jit_compile_method entry point";
243     return false;
244   }
245   jit_types_loaded_ = reinterpret_cast<void (*)(void*, mirror::Class**, size_t)>(
246       dlsym(jit_library_handle_, "jit_types_loaded"));
247   if (jit_types_loaded_ == nullptr) {
248     dlclose(jit_library_handle_);
249     *error_msg = "JIT couldn't find jit_types_loaded entry point";
250     return false;
251   }
252   return true;
253 }
254 
LoadCompiler(std::string * error_msg)255 bool Jit::LoadCompiler(std::string* error_msg) {
256   if (jit_library_handle_ == nullptr && !LoadCompilerLibrary(error_msg)) {
257     return false;
258   }
259   bool will_generate_debug_symbols = false;
260   VLOG(jit) << "Calling JitLoad interpreter_only="
261       << Runtime::Current()->GetInstrumentation()->InterpretOnly();
262   jit_compiler_handle_ = (jit_load_)(&will_generate_debug_symbols);
263   if (jit_compiler_handle_ == nullptr) {
264     dlclose(jit_library_handle_);
265     *error_msg = "JIT couldn't load compiler";
266     return false;
267   }
268   generate_debug_info_ = will_generate_debug_symbols;
269   return true;
270 }
271 
CompileMethod(ArtMethod * method,Thread * self,bool osr)272 bool Jit::CompileMethod(ArtMethod* method, Thread* self, bool osr) {
273   DCHECK(Runtime::Current()->UseJitCompilation());
274   DCHECK(!method->IsRuntimeMethod());
275 
276   // Don't compile the method if it has breakpoints.
277   if (Dbg::IsDebuggerActive() && Dbg::MethodHasAnyBreakpoints(method)) {
278     VLOG(jit) << "JIT not compiling " << method->PrettyMethod() << " due to breakpoint";
279     return false;
280   }
281 
282   // Don't compile the method if we are supposed to be deoptimized.
283   instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
284   if (instrumentation->AreAllMethodsDeoptimized() || instrumentation->IsDeoptimized(method)) {
285     VLOG(jit) << "JIT not compiling " << method->PrettyMethod() << " due to deoptimization";
286     return false;
287   }
288 
289   // If we get a request to compile a proxy method, we pass the actual Java method
290   // of that proxy method, as the compiler does not expect a proxy method.
291   ArtMethod* method_to_compile = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
292   if (!code_cache_->NotifyCompilationOf(method_to_compile, self, osr)) {
293     return false;
294   }
295 
296   VLOG(jit) << "Compiling method "
297             << ArtMethod::PrettyMethod(method_to_compile)
298             << " osr=" << std::boolalpha << osr;
299   bool success = jit_compile_method_(jit_compiler_handle_, method_to_compile, self, osr);
300   code_cache_->DoneCompiling(method_to_compile, self, osr);
301   if (!success) {
302     VLOG(jit) << "Failed to compile method "
303               << ArtMethod::PrettyMethod(method_to_compile)
304               << " osr=" << std::boolalpha << osr;
305   }
306   if (kIsDebugBuild) {
307     if (self->IsExceptionPending()) {
308       mirror::Throwable* exception = self->GetException();
309       LOG(FATAL) << "No pending exception expected after compiling "
310                  << ArtMethod::PrettyMethod(method)
311                  << ": "
312                  << exception->Dump();
313     }
314   }
315   return success;
316 }
317 
CreateThreadPool()318 void Jit::CreateThreadPool() {
319   // There is a DCHECK in the 'AddSamples' method to ensure the tread pool
320   // is not null when we instrument.
321 
322   // We need peers as we may report the JIT thread, e.g., in the debugger.
323   constexpr bool kJitPoolNeedsPeers = true;
324   thread_pool_.reset(new ThreadPool("Jit thread pool", 1, kJitPoolNeedsPeers));
325 
326   thread_pool_->SetPthreadPriority(kJitPoolThreadPthreadPriority);
327   Start();
328 }
329 
DeleteThreadPool()330 void Jit::DeleteThreadPool() {
331   Thread* self = Thread::Current();
332   DCHECK(Runtime::Current()->IsShuttingDown(self));
333   if (thread_pool_ != nullptr) {
334     std::unique_ptr<ThreadPool> pool;
335     {
336       ScopedSuspendAll ssa(__FUNCTION__);
337       // Clear thread_pool_ field while the threads are suspended.
338       // A mutator in the 'AddSamples' method will check against it.
339       pool = std::move(thread_pool_);
340     }
341 
342     // When running sanitized, let all tasks finish to not leak. Otherwise just clear the queue.
343     if (!RUNNING_ON_MEMORY_TOOL) {
344       pool->StopWorkers(self);
345       pool->RemoveAllTasks(self);
346     }
347     // We could just suspend all threads, but we know those threads
348     // will finish in a short period, so it's not worth adding a suspend logic
349     // here. Besides, this is only done for shutdown.
350     pool->Wait(self, false, false);
351   }
352 }
353 
StartProfileSaver(const std::string & filename,const std::vector<std::string> & code_paths)354 void Jit::StartProfileSaver(const std::string& filename,
355                             const std::vector<std::string>& code_paths) {
356   if (profile_saver_options_.IsEnabled()) {
357     ProfileSaver::Start(profile_saver_options_,
358                         filename,
359                         code_cache_.get(),
360                         code_paths);
361   }
362 }
363 
StopProfileSaver()364 void Jit::StopProfileSaver() {
365   if (profile_saver_options_.IsEnabled() && ProfileSaver::IsStarted()) {
366     ProfileSaver::Stop(dump_info_on_shutdown_);
367   }
368 }
369 
JitAtFirstUse()370 bool Jit::JitAtFirstUse() {
371   return HotMethodThreshold() == 0;
372 }
373 
CanInvokeCompiledCode(ArtMethod * method)374 bool Jit::CanInvokeCompiledCode(ArtMethod* method) {
375   return code_cache_->ContainsPc(method->GetEntryPointFromQuickCompiledCode());
376 }
377 
~Jit()378 Jit::~Jit() {
379   DCHECK(!profile_saver_options_.IsEnabled() || !ProfileSaver::IsStarted());
380   if (dump_info_on_shutdown_) {
381     DumpInfo(LOG_STREAM(INFO));
382     Runtime::Current()->DumpDeoptimizations(LOG_STREAM(INFO));
383   }
384   DeleteThreadPool();
385   if (jit_compiler_handle_ != nullptr) {
386     jit_unload_(jit_compiler_handle_);
387     jit_compiler_handle_ = nullptr;
388   }
389   if (jit_library_handle_ != nullptr) {
390     dlclose(jit_library_handle_);
391     jit_library_handle_ = nullptr;
392   }
393 }
394 
NewTypeLoadedIfUsingJit(mirror::Class * type)395 void Jit::NewTypeLoadedIfUsingJit(mirror::Class* type) {
396   if (!Runtime::Current()->UseJitCompilation()) {
397     // No need to notify if we only use the JIT to save profiles.
398     return;
399   }
400   jit::Jit* jit = Runtime::Current()->GetJit();
401   if (jit->generate_debug_info_) {
402     DCHECK(jit->jit_types_loaded_ != nullptr);
403     jit->jit_types_loaded_(jit->jit_compiler_handle_, &type, 1);
404   }
405 }
406 
DumpTypeInfoForLoadedTypes(ClassLinker * linker)407 void Jit::DumpTypeInfoForLoadedTypes(ClassLinker* linker) {
408   struct CollectClasses : public ClassVisitor {
409     bool operator()(ObjPtr<mirror::Class> klass) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
410       classes_.push_back(klass.Ptr());
411       return true;
412     }
413     std::vector<mirror::Class*> classes_;
414   };
415 
416   if (generate_debug_info_) {
417     ScopedObjectAccess so(Thread::Current());
418 
419     CollectClasses visitor;
420     linker->VisitClasses(&visitor);
421     jit_types_loaded_(jit_compiler_handle_, visitor.classes_.data(), visitor.classes_.size());
422   }
423 }
424 
425 extern "C" void art_quick_osr_stub(void** stack,
426                                    uint32_t stack_size_in_bytes,
427                                    const uint8_t* native_pc,
428                                    JValue* result,
429                                    const char* shorty,
430                                    Thread* self);
431 
MaybeDoOnStackReplacement(Thread * thread,ArtMethod * method,uint32_t dex_pc,int32_t dex_pc_offset,JValue * result)432 bool Jit::MaybeDoOnStackReplacement(Thread* thread,
433                                     ArtMethod* method,
434                                     uint32_t dex_pc,
435                                     int32_t dex_pc_offset,
436                                     JValue* result) {
437   if (!kEnableOnStackReplacement) {
438     return false;
439   }
440 
441   Jit* jit = Runtime::Current()->GetJit();
442   if (jit == nullptr) {
443     return false;
444   }
445 
446   if (UNLIKELY(__builtin_frame_address(0) < thread->GetStackEnd())) {
447     // Don't attempt to do an OSR if we are close to the stack limit. Since
448     // the interpreter frames are still on stack, OSR has the potential
449     // to stack overflow even for a simple loop.
450     // b/27094810.
451     return false;
452   }
453 
454   // Get the actual Java method if this method is from a proxy class. The compiler
455   // and the JIT code cache do not expect methods from proxy classes.
456   method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
457 
458   // Cheap check if the method has been compiled already. That's an indicator that we should
459   // osr into it.
460   if (!jit->GetCodeCache()->ContainsPc(method->GetEntryPointFromQuickCompiledCode())) {
461     return false;
462   }
463 
464   // Fetch some data before looking up for an OSR method. We don't want thread
465   // suspension once we hold an OSR method, as the JIT code cache could delete the OSR
466   // method while we are being suspended.
467   const size_t number_of_vregs = method->GetCodeItem()->registers_size_;
468   const char* shorty = method->GetShorty();
469   std::string method_name(VLOG_IS_ON(jit) ? method->PrettyMethod() : "");
470   void** memory = nullptr;
471   size_t frame_size = 0;
472   ShadowFrame* shadow_frame = nullptr;
473   const uint8_t* native_pc = nullptr;
474 
475   {
476     ScopedAssertNoThreadSuspension sts("Holding OSR method");
477     const OatQuickMethodHeader* osr_method = jit->GetCodeCache()->LookupOsrMethodHeader(method);
478     if (osr_method == nullptr) {
479       // No osr method yet, just return to the interpreter.
480       return false;
481     }
482 
483     CodeInfo code_info = osr_method->GetOptimizedCodeInfo();
484     CodeInfoEncoding encoding = code_info.ExtractEncoding();
485 
486     // Find stack map starting at the target dex_pc.
487     StackMap stack_map = code_info.GetOsrStackMapForDexPc(dex_pc + dex_pc_offset, encoding);
488     if (!stack_map.IsValid()) {
489       // There is no OSR stack map for this dex pc offset. Just return to the interpreter in the
490       // hope that the next branch has one.
491       return false;
492     }
493 
494     // Before allowing the jump, make sure the debugger is not active to avoid jumping from
495     // interpreter to OSR while e.g. single stepping. Note that we could selectively disable
496     // OSR when single stepping, but that's currently hard to know at this point.
497     if (Dbg::IsDebuggerActive()) {
498       return false;
499     }
500 
501     // We found a stack map, now fill the frame with dex register values from the interpreter's
502     // shadow frame.
503     DexRegisterMap vreg_map =
504         code_info.GetDexRegisterMapOf(stack_map, encoding, number_of_vregs);
505 
506     frame_size = osr_method->GetFrameSizeInBytes();
507 
508     // Allocate memory to put shadow frame values. The osr stub will copy that memory to
509     // stack.
510     // Note that we could pass the shadow frame to the stub, and let it copy the values there,
511     // but that is engineering complexity not worth the effort for something like OSR.
512     memory = reinterpret_cast<void**>(malloc(frame_size));
513     CHECK(memory != nullptr);
514     memset(memory, 0, frame_size);
515 
516     // Art ABI: ArtMethod is at the bottom of the stack.
517     memory[0] = method;
518 
519     shadow_frame = thread->PopShadowFrame();
520     if (!vreg_map.IsValid()) {
521       // If we don't have a dex register map, then there are no live dex registers at
522       // this dex pc.
523     } else {
524       for (uint16_t vreg = 0; vreg < number_of_vregs; ++vreg) {
525         DexRegisterLocation::Kind location =
526             vreg_map.GetLocationKind(vreg, number_of_vregs, code_info, encoding);
527         if (location == DexRegisterLocation::Kind::kNone) {
528           // Dex register is dead or uninitialized.
529           continue;
530         }
531 
532         if (location == DexRegisterLocation::Kind::kConstant) {
533           // We skip constants because the compiled code knows how to handle them.
534           continue;
535         }
536 
537         DCHECK_EQ(location, DexRegisterLocation::Kind::kInStack);
538 
539         int32_t vreg_value = shadow_frame->GetVReg(vreg);
540         int32_t slot_offset = vreg_map.GetStackOffsetInBytes(vreg,
541                                                              number_of_vregs,
542                                                              code_info,
543                                                              encoding);
544         DCHECK_LT(slot_offset, static_cast<int32_t>(frame_size));
545         DCHECK_GT(slot_offset, 0);
546         (reinterpret_cast<int32_t*>(memory))[slot_offset / sizeof(int32_t)] = vreg_value;
547       }
548     }
549 
550     native_pc = stack_map.GetNativePcOffset(encoding.stack_map.encoding, kRuntimeISA) +
551         osr_method->GetEntryPoint();
552     VLOG(jit) << "Jumping to "
553               << method_name
554               << "@"
555               << std::hex << reinterpret_cast<uintptr_t>(native_pc);
556   }
557 
558   {
559     ManagedStack fragment;
560     thread->PushManagedStackFragment(&fragment);
561     (*art_quick_osr_stub)(memory,
562                           frame_size,
563                           native_pc,
564                           result,
565                           shorty,
566                           thread);
567 
568     if (UNLIKELY(thread->GetException() == Thread::GetDeoptimizationException())) {
569       thread->DeoptimizeWithDeoptimizationException(result);
570     }
571     thread->PopManagedStackFragment(fragment);
572   }
573   free(memory);
574   thread->PushShadowFrame(shadow_frame);
575   VLOG(jit) << "Done running OSR code for " << method_name;
576   return true;
577 }
578 
AddMemoryUsage(ArtMethod * method,size_t bytes)579 void Jit::AddMemoryUsage(ArtMethod* method, size_t bytes) {
580   if (bytes > 4 * MB) {
581     LOG(INFO) << "Compiler allocated "
582               << PrettySize(bytes)
583               << " to compile "
584               << ArtMethod::PrettyMethod(method);
585   }
586   MutexLock mu(Thread::Current(), lock_);
587   memory_use_.AddValue(bytes);
588 }
589 
590 class JitCompileTask FINAL : public Task {
591  public:
592   enum TaskKind {
593     kAllocateProfile,
594     kCompile,
595     kCompileOsr
596   };
597 
JitCompileTask(ArtMethod * method,TaskKind kind)598   JitCompileTask(ArtMethod* method, TaskKind kind) : method_(method), kind_(kind) {
599     ScopedObjectAccess soa(Thread::Current());
600     // Add a global ref to the class to prevent class unloading until compilation is done.
601     klass_ = soa.Vm()->AddGlobalRef(soa.Self(), method_->GetDeclaringClass());
602     CHECK(klass_ != nullptr);
603   }
604 
~JitCompileTask()605   ~JitCompileTask() {
606     ScopedObjectAccess soa(Thread::Current());
607     soa.Vm()->DeleteGlobalRef(soa.Self(), klass_);
608   }
609 
Run(Thread * self)610   void Run(Thread* self) OVERRIDE {
611     ScopedObjectAccess soa(self);
612     if (kind_ == kCompile) {
613       Runtime::Current()->GetJit()->CompileMethod(method_, self, /* osr */ false);
614     } else if (kind_ == kCompileOsr) {
615       Runtime::Current()->GetJit()->CompileMethod(method_, self, /* osr */ true);
616     } else {
617       DCHECK(kind_ == kAllocateProfile);
618       if (ProfilingInfo::Create(self, method_, /* retry_allocation */ true)) {
619         VLOG(jit) << "Start profiling " << ArtMethod::PrettyMethod(method_);
620       }
621     }
622     ProfileSaver::NotifyJitActivity();
623   }
624 
Finalize()625   void Finalize() OVERRIDE {
626     delete this;
627   }
628 
629  private:
630   ArtMethod* const method_;
631   const TaskKind kind_;
632   jobject klass_;
633 
634   DISALLOW_IMPLICIT_CONSTRUCTORS(JitCompileTask);
635 };
636 
AddSamples(Thread * self,ArtMethod * method,uint16_t count,bool with_backedges)637 void Jit::AddSamples(Thread* self, ArtMethod* method, uint16_t count, bool with_backedges) {
638   if (thread_pool_ == nullptr) {
639     // Should only see this when shutting down.
640     DCHECK(Runtime::Current()->IsShuttingDown(self));
641     return;
642   }
643 
644   if (method->IsClassInitializer() || method->IsNative() || !method->IsCompilable()) {
645     // We do not want to compile such methods.
646     return;
647   }
648   DCHECK(thread_pool_ != nullptr);
649   DCHECK_GT(warm_method_threshold_, 0);
650   DCHECK_GT(hot_method_threshold_, warm_method_threshold_);
651   DCHECK_GT(osr_method_threshold_, hot_method_threshold_);
652   DCHECK_GE(priority_thread_weight_, 1);
653   DCHECK_LE(priority_thread_weight_, hot_method_threshold_);
654 
655   int32_t starting_count = method->GetCounter();
656   if (Jit::ShouldUsePriorityThreadWeight()) {
657     count *= priority_thread_weight_;
658   }
659   int32_t new_count = starting_count + count;   // int32 here to avoid wrap-around;
660   if (starting_count < warm_method_threshold_) {
661     if ((new_count >= warm_method_threshold_) &&
662         (method->GetProfilingInfo(kRuntimePointerSize) == nullptr)) {
663       bool success = ProfilingInfo::Create(self, method, /* retry_allocation */ false);
664       if (success) {
665         VLOG(jit) << "Start profiling " << method->PrettyMethod();
666       }
667 
668       if (thread_pool_ == nullptr) {
669         // Calling ProfilingInfo::Create might put us in a suspended state, which could
670         // lead to the thread pool being deleted when we are shutting down.
671         DCHECK(Runtime::Current()->IsShuttingDown(self));
672         return;
673       }
674 
675       if (!success) {
676         // We failed allocating. Instead of doing the collection on the Java thread, we push
677         // an allocation to a compiler thread, that will do the collection.
678         thread_pool_->AddTask(self, new JitCompileTask(method, JitCompileTask::kAllocateProfile));
679       }
680     }
681     // Avoid jumping more than one state at a time.
682     new_count = std::min(new_count, hot_method_threshold_ - 1);
683   } else if (use_jit_compilation_) {
684     if (starting_count < hot_method_threshold_) {
685       if ((new_count >= hot_method_threshold_) &&
686           !code_cache_->ContainsPc(method->GetEntryPointFromQuickCompiledCode())) {
687         DCHECK(thread_pool_ != nullptr);
688         thread_pool_->AddTask(self, new JitCompileTask(method, JitCompileTask::kCompile));
689       }
690       // Avoid jumping more than one state at a time.
691       new_count = std::min(new_count, osr_method_threshold_ - 1);
692     } else if (starting_count < osr_method_threshold_) {
693       if (!with_backedges) {
694         // If the samples don't contain any back edge, we don't increment the hotness.
695         return;
696       }
697       if ((new_count >= osr_method_threshold_) &&  !code_cache_->IsOsrCompiled(method)) {
698         DCHECK(thread_pool_ != nullptr);
699         thread_pool_->AddTask(self, new JitCompileTask(method, JitCompileTask::kCompileOsr));
700       }
701     }
702   }
703   // Update hotness counter
704   method->SetCounter(new_count);
705 }
706 
MethodEntered(Thread * thread,ArtMethod * method)707 void Jit::MethodEntered(Thread* thread, ArtMethod* method) {
708   Runtime* runtime = Runtime::Current();
709   if (UNLIKELY(runtime->UseJitCompilation() && runtime->GetJit()->JitAtFirstUse())) {
710     // The compiler requires a ProfilingInfo object.
711     ProfilingInfo::Create(thread, method, /* retry_allocation */ true);
712     JitCompileTask compile_task(method, JitCompileTask::kCompile);
713     compile_task.Run(thread);
714     return;
715   }
716 
717   ProfilingInfo* profiling_info = method->GetProfilingInfo(kRuntimePointerSize);
718   // Update the entrypoint if the ProfilingInfo has one. The interpreter will call it
719   // instead of interpreting the method.
720   if ((profiling_info != nullptr) && (profiling_info->GetSavedEntryPoint() != nullptr)) {
721     Runtime::Current()->GetInstrumentation()->UpdateMethodsCode(
722         method, profiling_info->GetSavedEntryPoint());
723   } else {
724     AddSamples(thread, method, 1, /* with_backedges */false);
725   }
726 }
727 
InvokeVirtualOrInterface(ObjPtr<mirror::Object> this_object,ArtMethod * caller,uint32_t dex_pc,ArtMethod * callee ATTRIBUTE_UNUSED)728 void Jit::InvokeVirtualOrInterface(ObjPtr<mirror::Object> this_object,
729                                    ArtMethod* caller,
730                                    uint32_t dex_pc,
731                                    ArtMethod* callee ATTRIBUTE_UNUSED) {
732   ScopedAssertNoThreadSuspension ants(__FUNCTION__);
733   DCHECK(this_object != nullptr);
734   ProfilingInfo* info = caller->GetProfilingInfo(kRuntimePointerSize);
735   if (info != nullptr) {
736     info->AddInvokeInfo(dex_pc, this_object->GetClass());
737   }
738 }
739 
WaitForCompilationToFinish(Thread * self)740 void Jit::WaitForCompilationToFinish(Thread* self) {
741   if (thread_pool_ != nullptr) {
742     thread_pool_->Wait(self, false, false);
743   }
744 }
745 
Stop()746 void Jit::Stop() {
747   Thread* self = Thread::Current();
748   // TODO(ngeoffray): change API to not require calling WaitForCompilationToFinish twice.
749   WaitForCompilationToFinish(self);
750   GetThreadPool()->StopWorkers(self);
751   WaitForCompilationToFinish(self);
752 }
753 
Start()754 void Jit::Start() {
755   GetThreadPool()->StartWorkers(Thread::Current());
756 }
757 
ScopedJitSuspend()758 ScopedJitSuspend::ScopedJitSuspend() {
759   jit::Jit* jit = Runtime::Current()->GetJit();
760   was_on_ = (jit != nullptr) && (jit->GetThreadPool() != nullptr);
761   if (was_on_) {
762     jit->Stop();
763   }
764 }
765 
~ScopedJitSuspend()766 ScopedJitSuspend::~ScopedJitSuspend() {
767   if (was_on_) {
768     DCHECK(Runtime::Current()->GetJit() != nullptr);
769     DCHECK(Runtime::Current()->GetJit()->GetThreadPool() != nullptr);
770     Runtime::Current()->GetJit()->Start();
771   }
772 }
773 
774 }  // namespace jit
775 }  // namespace art
776