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 #ifndef ART_RUNTIME_JIT_JIT_H_
18 #define ART_RUNTIME_JIT_JIT_H_
19 
20 #include <android-base/unique_fd.h>
21 
22 #include "base/histogram-inl.h"
23 #include "base/macros.h"
24 #include "base/mutex.h"
25 #include "base/runtime_debug.h"
26 #include "base/timing_logger.h"
27 #include "compilation_kind.h"
28 #include "handle.h"
29 #include "offsets.h"
30 #include "interpreter/mterp/nterp.h"
31 #include "jit/debugger_interface.h"
32 #include "jit/profile_saver_options.h"
33 #include "obj_ptr.h"
34 #include "thread_pool.h"
35 
36 namespace art {
37 
38 class ArtMethod;
39 class ClassLinker;
40 class DexFile;
41 class OatDexFile;
42 struct RuntimeArgumentMap;
43 union JValue;
44 
45 namespace mirror {
46 class Object;
47 class Class;
48 class ClassLoader;
49 class DexCache;
50 class String;
51 }   // namespace mirror
52 
53 namespace jit {
54 
55 class JitCodeCache;
56 class JitMemoryRegion;
57 class JitOptions;
58 
59 static constexpr int16_t kJitCheckForOSR = -1;
60 static constexpr int16_t kJitHotnessDisabled = -2;
61 // At what priority to schedule jit threads. 9 is the lowest foreground priority on device.
62 // See android/os/Process.java.
63 static constexpr int kJitPoolThreadPthreadDefaultPriority = 9;
64 // We check whether to jit-compile the method every Nth invoke.
65 // The tests often use threshold of 1000 (and thus 500 to start profiling).
66 static constexpr uint32_t kJitSamplesBatchSize = 512;  // Must be power of 2.
67 
68 class JitOptions {
69  public:
70   static JitOptions* CreateFromRuntimeArguments(const RuntimeArgumentMap& options);
71 
GetCompileThreshold()72   uint16_t GetCompileThreshold() const {
73     return compile_threshold_;
74   }
75 
GetWarmupThreshold()76   uint16_t GetWarmupThreshold() const {
77     return warmup_threshold_;
78   }
79 
GetOsrThreshold()80   uint16_t GetOsrThreshold() const {
81     return osr_threshold_;
82   }
83 
GetPriorityThreadWeight()84   uint16_t GetPriorityThreadWeight() const {
85     return priority_thread_weight_;
86   }
87 
GetInvokeTransitionWeight()88   uint16_t GetInvokeTransitionWeight() const {
89     return invoke_transition_weight_;
90   }
91 
GetCodeCacheInitialCapacity()92   size_t GetCodeCacheInitialCapacity() const {
93     return code_cache_initial_capacity_;
94   }
95 
GetCodeCacheMaxCapacity()96   size_t GetCodeCacheMaxCapacity() const {
97     return code_cache_max_capacity_;
98   }
99 
DumpJitInfoOnShutdown()100   bool DumpJitInfoOnShutdown() const {
101     return dump_info_on_shutdown_;
102   }
103 
GetProfileSaverOptions()104   const ProfileSaverOptions& GetProfileSaverOptions() const {
105     return profile_saver_options_;
106   }
107 
GetSaveProfilingInfo()108   bool GetSaveProfilingInfo() const {
109     return profile_saver_options_.IsEnabled();
110   }
111 
GetThreadPoolPthreadPriority()112   int GetThreadPoolPthreadPriority() const {
113     return thread_pool_pthread_priority_;
114   }
115 
UseJitCompilation()116   bool UseJitCompilation() const {
117     return use_jit_compilation_;
118   }
119 
UseTieredJitCompilation()120   bool UseTieredJitCompilation() const {
121     return use_tiered_jit_compilation_;
122   }
123 
CanCompileBaseline()124   bool CanCompileBaseline() const {
125     return use_tiered_jit_compilation_ ||
126            use_baseline_compiler_ ||
127            interpreter::IsNterpSupported();
128   }
129 
SetUseJitCompilation(bool b)130   void SetUseJitCompilation(bool b) {
131     use_jit_compilation_ = b;
132   }
133 
SetSaveProfilingInfo(bool save_profiling_info)134   void SetSaveProfilingInfo(bool save_profiling_info) {
135     profile_saver_options_.SetEnabled(save_profiling_info);
136   }
137 
SetWaitForJitNotificationsToSaveProfile(bool value)138   void SetWaitForJitNotificationsToSaveProfile(bool value) {
139     profile_saver_options_.SetWaitForJitNotificationsToSave(value);
140   }
141 
SetJitAtFirstUse()142   void SetJitAtFirstUse() {
143     use_jit_compilation_ = true;
144     compile_threshold_ = 0;
145   }
146 
SetUseBaselineCompiler()147   void SetUseBaselineCompiler() {
148     use_baseline_compiler_ = true;
149   }
150 
UseBaselineCompiler()151   bool UseBaselineCompiler() const {
152     return use_baseline_compiler_;
153   }
154 
155  private:
156   // We add the sample in batches of size kJitSamplesBatchSize.
157   // This method rounds the threshold so that it is multiple of the batch size.
158   static uint32_t RoundUpThreshold(uint32_t threshold);
159 
160   bool use_jit_compilation_;
161   bool use_tiered_jit_compilation_;
162   bool use_baseline_compiler_;
163   size_t code_cache_initial_capacity_;
164   size_t code_cache_max_capacity_;
165   uint32_t compile_threshold_;
166   uint32_t warmup_threshold_;
167   uint32_t osr_threshold_;
168   uint16_t priority_thread_weight_;
169   uint16_t invoke_transition_weight_;
170   bool dump_info_on_shutdown_;
171   int thread_pool_pthread_priority_;
172   ProfileSaverOptions profile_saver_options_;
173 
JitOptions()174   JitOptions()
175       : use_jit_compilation_(false),
176         use_tiered_jit_compilation_(false),
177         use_baseline_compiler_(false),
178         code_cache_initial_capacity_(0),
179         code_cache_max_capacity_(0),
180         compile_threshold_(0),
181         warmup_threshold_(0),
182         osr_threshold_(0),
183         priority_thread_weight_(0),
184         invoke_transition_weight_(0),
185         dump_info_on_shutdown_(false),
186         thread_pool_pthread_priority_(kJitPoolThreadPthreadDefaultPriority) {}
187 
188   DISALLOW_COPY_AND_ASSIGN(JitOptions);
189 };
190 
191 // Implemented and provided by the compiler library.
192 class JitCompilerInterface {
193  public:
~JitCompilerInterface()194   virtual ~JitCompilerInterface() {}
195   virtual bool CompileMethod(
196       Thread* self, JitMemoryRegion* region, ArtMethod* method, CompilationKind compilation_kind)
197       REQUIRES_SHARED(Locks::mutator_lock_) = 0;
198   virtual void TypesLoaded(mirror::Class**, size_t count)
199       REQUIRES_SHARED(Locks::mutator_lock_) = 0;
200   virtual bool GenerateDebugInfo() = 0;
201   virtual void ParseCompilerOptions() = 0;
202 
203   virtual std::vector<uint8_t> PackElfFileForJIT(ArrayRef<const JITCodeEntry*> elf_files,
204                                                  ArrayRef<const void*> removed_symbols,
205                                                  bool compress,
206                                                  /*out*/ size_t* num_symbols) = 0;
207 };
208 
209 // Data structure holding information to perform an OSR.
210 struct OsrData {
211   // The native PC to jump to.
212   const uint8_t* native_pc;
213 
214   // The frame size of the compiled code to jump to.
215   size_t frame_size;
216 
217   // The dynamically allocated memory of size `frame_size` to copy to stack.
218   void* memory[0];
219 
NativePcOffsetOsrData220   static constexpr MemberOffset NativePcOffset() {
221     return MemberOffset(OFFSETOF_MEMBER(OsrData, native_pc));
222   }
223 
FrameSizeOffsetOsrData224   static constexpr MemberOffset FrameSizeOffset() {
225     return MemberOffset(OFFSETOF_MEMBER(OsrData, frame_size));
226   }
227 
MemoryOffsetOsrData228   static constexpr MemberOffset MemoryOffset() {
229     return MemberOffset(OFFSETOF_MEMBER(OsrData, memory));
230   }
231 };
232 
233 class Jit {
234  public:
235   static constexpr size_t kDefaultPriorityThreadWeightRatio = 1000;
236   static constexpr size_t kDefaultInvokeTransitionWeightRatio = 500;
237   // How frequently should the interpreter check to see if OSR compilation is ready.
238   static constexpr int16_t kJitRecheckOSRThreshold = 101;  // Prime number to avoid patterns.
239 
240   DECLARE_RUNTIME_DEBUG_FLAG(kSlowMode);
241 
242   virtual ~Jit();
243 
244   // Create JIT itself.
245   static Jit* Create(JitCodeCache* code_cache, JitOptions* options);
246 
247   bool CompileMethod(ArtMethod* method, Thread* self, CompilationKind compilation_kind, bool prejit)
248       REQUIRES_SHARED(Locks::mutator_lock_);
249 
GetCodeCache()250   const JitCodeCache* GetCodeCache() const {
251     return code_cache_;
252   }
253 
GetCodeCache()254   JitCodeCache* GetCodeCache() {
255     return code_cache_;
256   }
257 
GetJitCompiler()258   JitCompilerInterface* GetJitCompiler() const {
259     return jit_compiler_;
260   }
261 
262   void CreateThreadPool();
263   void DeleteThreadPool();
264   void WaitForWorkersToBeCreated();
265 
266   // Dump interesting info: #methods compiled, code vs data size, compile / verify cumulative
267   // loggers.
268   void DumpInfo(std::ostream& os) REQUIRES(!lock_);
269   // Add a timing logger to cumulative_timings_.
270   void AddTimingLogger(const TimingLogger& logger);
271 
272   void AddMemoryUsage(ArtMethod* method, size_t bytes)
273       REQUIRES(!lock_)
274       REQUIRES_SHARED(Locks::mutator_lock_);
275 
OSRMethodThreshold()276   uint16_t OSRMethodThreshold() const {
277     return options_->GetOsrThreshold();
278   }
279 
HotMethodThreshold()280   uint16_t HotMethodThreshold() const {
281     return options_->GetCompileThreshold();
282   }
283 
WarmMethodThreshold()284   uint16_t WarmMethodThreshold() const {
285     return options_->GetWarmupThreshold();
286   }
287 
PriorityThreadWeight()288   uint16_t PriorityThreadWeight() const {
289     return options_->GetPriorityThreadWeight();
290   }
291 
292   // Return whether we should do JIT compilation. Note this will returns false
293   // if we only need to save profile information and not compile methods.
UseJitCompilation()294   bool UseJitCompilation() const {
295     return options_->UseJitCompilation();
296   }
297 
GetSaveProfilingInfo()298   bool GetSaveProfilingInfo() const {
299     return options_->GetSaveProfilingInfo();
300   }
301 
302   // Wait until there is no more pending compilation tasks.
303   void WaitForCompilationToFinish(Thread* self);
304 
305   // Profiling methods.
306   void MethodEntered(Thread* thread, ArtMethod* method)
307       REQUIRES_SHARED(Locks::mutator_lock_);
308 
309   ALWAYS_INLINE void AddSamples(Thread* self,
310                                 ArtMethod* method,
311                                 uint16_t samples,
312                                 bool with_backedges)
313       REQUIRES_SHARED(Locks::mutator_lock_);
314 
315   void InvokeVirtualOrInterface(ObjPtr<mirror::Object> this_object,
316                                 ArtMethod* caller,
317                                 uint32_t dex_pc,
318                                 ArtMethod* callee)
319       REQUIRES_SHARED(Locks::mutator_lock_);
320 
NotifyInterpreterToCompiledCodeTransition(Thread * self,ArtMethod * caller)321   void NotifyInterpreterToCompiledCodeTransition(Thread* self, ArtMethod* caller)
322       REQUIRES_SHARED(Locks::mutator_lock_) {
323     if (!IgnoreSamplesForMethod(caller)) {
324       AddSamples(self, caller, options_->GetInvokeTransitionWeight(), false);
325     }
326   }
327 
NotifyCompiledCodeToInterpreterTransition(Thread * self,ArtMethod * callee)328   void NotifyCompiledCodeToInterpreterTransition(Thread* self, ArtMethod* callee)
329       REQUIRES_SHARED(Locks::mutator_lock_) {
330     if (!IgnoreSamplesForMethod(callee)) {
331       AddSamples(self, callee, options_->GetInvokeTransitionWeight(), false);
332     }
333   }
334 
335   // Starts the profile saver if the config options allow profile recording.
336   // The profile will be stored in the specified `filename` and will contain
337   // information collected from the given `code_paths` (a set of dex locations).
338   void StartProfileSaver(const std::string& filename,
339                          const std::vector<std::string>& code_paths);
340   void StopProfileSaver();
341 
342   void DumpForSigQuit(std::ostream& os) REQUIRES(!lock_);
343 
344   static void NewTypeLoadedIfUsingJit(mirror::Class* type)
345       REQUIRES_SHARED(Locks::mutator_lock_);
346 
347   // If debug info generation is turned on then write the type information for types already loaded
348   // into the specified class linker to the jit debug interface,
349   void DumpTypeInfoForLoadedTypes(ClassLinker* linker);
350 
351   // Return whether we should try to JIT compiled code as soon as an ArtMethod is invoked.
352   bool JitAtFirstUse();
353 
354   // Return whether we can invoke JIT code for `method`.
355   bool CanInvokeCompiledCode(ArtMethod* method);
356 
357   // Return whether the runtime should use a priority thread weight when sampling.
358   static bool ShouldUsePriorityThreadWeight(Thread* self);
359 
360   // Return the information required to do an OSR jump. Return null if the OSR
361   // cannot be done.
362   OsrData* PrepareForOsr(ArtMethod* method, uint32_t dex_pc, uint32_t* vregs)
363       REQUIRES_SHARED(Locks::mutator_lock_);
364 
365   // If an OSR compiled version is available for `method`,
366   // and `dex_pc + dex_pc_offset` is an entry point of that compiled
367   // version, this method will jump to the compiled code, let it run,
368   // and return true afterwards. Return false otherwise.
369   static bool MaybeDoOnStackReplacement(Thread* thread,
370                                         ArtMethod* method,
371                                         uint32_t dex_pc,
372                                         int32_t dex_pc_offset,
373                                         JValue* result)
374       REQUIRES_SHARED(Locks::mutator_lock_);
375 
376   // Load the compiler library.
377   static bool LoadCompilerLibrary(std::string* error_msg);
378 
GetThreadPool()379   ThreadPool* GetThreadPool() const {
380     return thread_pool_.get();
381   }
382 
383   // Stop the JIT by waiting for all current compilations and enqueued compilations to finish.
384   void Stop();
385 
386   // Start JIT threads.
387   void Start();
388 
389   // Transition to a child state.
390   void PostForkChildAction(bool is_system_server, bool is_zygote);
391 
392   // Prepare for forking.
393   void PreZygoteFork();
394 
395   // Adjust state after forking.
396   void PostZygoteFork();
397 
398   // Called when system finishes booting.
399   void BootCompleted();
400 
401   // Compile methods from the given profile (.prof extension). If `add_to_queue`
402   // is true, methods in the profile are added to the JIT queue. Otherwise they are compiled
403   // directly.
404   // Return the number of methods added to the queue.
405   uint32_t CompileMethodsFromProfile(Thread* self,
406                                      const std::vector<const DexFile*>& dex_files,
407                                      const std::string& profile_path,
408                                      Handle<mirror::ClassLoader> class_loader,
409                                      bool add_to_queue);
410 
411   // Compile methods from the given boot profile (.bprof extension). If `add_to_queue`
412   // is true, methods in the profile are added to the JIT queue. Otherwise they are compiled
413   // directly.
414   // Return the number of methods added to the queue.
415   uint32_t CompileMethodsFromBootProfile(Thread* self,
416                                          const std::vector<const DexFile*>& dex_files,
417                                          const std::string& profile_path,
418                                          Handle<mirror::ClassLoader> class_loader,
419                                          bool add_to_queue);
420 
421   // Register the dex files to the JIT. This is to perform any compilation/optimization
422   // at the point of loading the dex files.
423   void RegisterDexFiles(const std::vector<std::unique_ptr<const DexFile>>& dex_files,
424                         jobject class_loader);
425 
426   // Called by the compiler to know whether it can directly encode the
427   // method/class/string.
428   bool CanEncodeMethod(ArtMethod* method, bool is_for_shared_region) const
429       REQUIRES_SHARED(Locks::mutator_lock_);
430   bool CanEncodeClass(ObjPtr<mirror::Class> cls, bool is_for_shared_region) const
431       REQUIRES_SHARED(Locks::mutator_lock_);
432   bool CanEncodeString(ObjPtr<mirror::String> string, bool is_for_shared_region) const
433       REQUIRES_SHARED(Locks::mutator_lock_);
434   bool CanAssumeInitialized(ObjPtr<mirror::Class> cls, bool is_for_shared_region) const
435       REQUIRES_SHARED(Locks::mutator_lock_);
436 
437   // Map boot image methods after all compilation in zygote has been done.
438   void MapBootImageMethods() REQUIRES(Locks::mutator_lock_);
439 
440   // Notify to other processes that the zygote is done profile compiling boot
441   // class path methods.
442   void NotifyZygoteCompilationDone();
443 
444   void EnqueueOptimizedCompilation(ArtMethod* method, Thread* self);
445 
446   void EnqueueCompilationFromNterp(ArtMethod* method, Thread* self)
447       REQUIRES_SHARED(Locks::mutator_lock_);
448 
449  private:
450   Jit(JitCodeCache* code_cache, JitOptions* options);
451 
452   // Whether we should not add hotness counts for the given method.
453   bool IgnoreSamplesForMethod(ArtMethod* method)
454       REQUIRES_SHARED(Locks::mutator_lock_);
455 
456   // Compile an individual method listed in a profile. If `add_to_queue` is
457   // true and the method was resolved, return true. Otherwise return false.
458   bool CompileMethodFromProfile(Thread* self,
459                                 ClassLinker* linker,
460                                 uint32_t method_idx,
461                                 Handle<mirror::DexCache> dex_cache,
462                                 Handle<mirror::ClassLoader> class_loader,
463                                 bool add_to_queue,
464                                 bool compile_after_boot)
465       REQUIRES_SHARED(Locks::mutator_lock_);
466 
467   // Compile the method if the number of samples passes a threshold.
468   // Returns false if we can not compile now - don't increment the counter and retry later.
469   bool MaybeCompileMethod(Thread* self,
470                           ArtMethod* method,
471                           uint32_t old_count,
472                           uint32_t new_count,
473                           bool with_backedges)
474       REQUIRES_SHARED(Locks::mutator_lock_);
475 
476   static bool BindCompilerMethods(std::string* error_msg);
477 
478   // JIT compiler
479   static void* jit_library_handle_;
480   static JitCompilerInterface* jit_compiler_;
481   static JitCompilerInterface* (*jit_load_)(void);
482   template <typename T> static bool LoadSymbol(T*, const char* symbol, std::string* error_msg);
483 
484   // JIT resources owned by runtime.
485   jit::JitCodeCache* const code_cache_;
486   const JitOptions* const options_;
487 
488   std::unique_ptr<ThreadPool> thread_pool_;
489   std::vector<std::unique_ptr<OatDexFile>> type_lookup_tables_;
490 
491   Mutex boot_completed_lock_;
492   bool boot_completed_ GUARDED_BY(boot_completed_lock_) = false;
493   std::deque<Task*> tasks_after_boot_ GUARDED_BY(boot_completed_lock_);
494 
495   // Performance monitoring.
496   CumulativeLogger cumulative_timings_;
497   Histogram<uint64_t> memory_use_ GUARDED_BY(lock_);
498   Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
499 
500   // In the JIT zygote configuration, after all compilation is done, the zygote
501   // will copy its contents of the boot image to the zygote_mapping_methods_,
502   // which will be picked up by processes that will map the memory
503   // in-place within the boot image mapping.
504   //
505   // zygote_mapping_methods_ is shared memory only usable by the zygote and not
506   // inherited by child processes. We create it eagerly to ensure other
507   // processes cannot seal writable the file.
508   MemMap zygote_mapping_methods_;
509 
510   // The file descriptor created through memfd_create pointing to memory holding
511   // boot image methods. Created by the zygote, and inherited by child
512   // processes. The descriptor will be closed in each process (including the
513   // zygote) once they don't need it.
514   android::base::unique_fd fd_methods_;
515 
516   // The size of the memory pointed by `fd_methods_`. Cached here to avoid
517   // recomputing it.
518   size_t fd_methods_size_;
519 
520   DISALLOW_COPY_AND_ASSIGN(Jit);
521 };
522 
523 // Helper class to stop the JIT for a given scope. This will wait for the JIT to quiesce.
524 class ScopedJitSuspend {
525  public:
526   ScopedJitSuspend();
527   ~ScopedJitSuspend();
528 
529  private:
530   bool was_on_;
531 };
532 
533 }  // namespace jit
534 }  // namespace art
535 
536 #endif  // ART_RUNTIME_JIT_JIT_H_
537