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