blob: 13a9c7b7289307ea55036197c3cfb6c0afed2904 [file] [log] [blame]
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001/*
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
Nicolas Geoffraya4f81542016-03-08 16:57:48 +000020#include "base/histogram-inl.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080021#include "base/macros.h"
22#include "base/mutex.h"
David Srbecky21821472019-07-29 15:10:31 +010023#include "base/runtime_debug.h"
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070024#include "base/timing_logger.h"
Nicolas Geoffray9ac09ee2019-05-08 23:38:27 +010025#include "handle.h"
David Srbecky8fc2f952019-07-31 18:40:09 +010026#include "jit/debugger_interface.h"
Mathieu Chartieref41db72016-10-25 15:08:01 -070027#include "jit/profile_saver_options.h"
28#include "obj_ptr.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080029#include "thread_pool.h"
30
31namespace art {
32
Mathieu Chartiere401d142015-04-22 13:56:20 -070033class ArtMethod;
David Sehr9323e6e2016-09-13 08:58:35 -070034class ClassLinker;
Nicolas Geoffray9ac09ee2019-05-08 23:38:27 +010035class DexFile;
Nicolas Geoffraydc2fbb62019-04-11 22:55:50 +010036class OatDexFile;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080037struct RuntimeArgumentMap;
Vladimir Marko3a21e382016-09-02 12:38:38 +010038union JValue;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080039
David Sehr709b0702016-10-13 09:12:37 -070040namespace mirror {
41class Object;
42class Class;
Nicolas Geoffray9ac09ee2019-05-08 23:38:27 +010043class ClassLoader;
Nicolas Geoffray0f8950a2019-07-24 13:22:11 +010044class DexCache;
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +000045class String;
David Sehr709b0702016-10-13 09:12:37 -070046} // namespace mirror
47
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080048namespace jit {
49
50class JitCodeCache;
Nicolas Geoffray7f7539b2019-06-06 16:20:54 +010051class JitMemoryRegion;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080052class JitOptions;
53
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +010054static constexpr int16_t kJitCheckForOSR = -1;
55static constexpr int16_t kJitHotnessDisabled = -2;
Nicolas Geoffray47b95802018-05-16 15:42:17 +010056// At what priority to schedule jit threads. 9 is the lowest foreground priority on device.
57// See android/os/Process.java.
58static constexpr int kJitPoolThreadPthreadDefaultPriority = 9;
David Srbecky81448a22019-08-01 14:29:53 +010059// We check whether to jit-compile the method every Nth invoke.
60// The tests often use threshold of 1000 (and thus 500 to start profiling).
61static constexpr uint32_t kJitSamplesBatchSize = 512; // Must be power of 2.
Nicolas Geoffray47b95802018-05-16 15:42:17 +010062
63class JitOptions {
64 public:
65 static JitOptions* CreateFromRuntimeArguments(const RuntimeArgumentMap& options);
66
67 uint16_t GetCompileThreshold() const {
68 return compile_threshold_;
69 }
70
71 uint16_t GetWarmupThreshold() const {
72 return warmup_threshold_;
73 }
74
75 uint16_t GetOsrThreshold() const {
76 return osr_threshold_;
77 }
78
79 uint16_t GetPriorityThreadWeight() const {
80 return priority_thread_weight_;
81 }
82
83 uint16_t GetInvokeTransitionWeight() const {
84 return invoke_transition_weight_;
85 }
86
87 size_t GetCodeCacheInitialCapacity() const {
88 return code_cache_initial_capacity_;
89 }
90
91 size_t GetCodeCacheMaxCapacity() const {
92 return code_cache_max_capacity_;
93 }
94
95 bool DumpJitInfoOnShutdown() const {
96 return dump_info_on_shutdown_;
97 }
98
99 const ProfileSaverOptions& GetProfileSaverOptions() const {
100 return profile_saver_options_;
101 }
102
103 bool GetSaveProfilingInfo() const {
104 return profile_saver_options_.IsEnabled();
105 }
106
107 int GetThreadPoolPthreadPriority() const {
108 return thread_pool_pthread_priority_;
109 }
110
111 bool UseJitCompilation() const {
112 return use_jit_compilation_;
113 }
114
115 void SetUseJitCompilation(bool b) {
116 use_jit_compilation_ = b;
117 }
118
119 void SetSaveProfilingInfo(bool save_profiling_info) {
120 profile_saver_options_.SetEnabled(save_profiling_info);
121 }
122
123 void SetWaitForJitNotificationsToSaveProfile(bool value) {
124 profile_saver_options_.SetWaitForJitNotificationsToSave(value);
125 }
126
Nicolas Geoffray775d38d2019-08-30 08:28:01 +0000127 void SetProfileAOTCode(bool value) {
128 profile_saver_options_.SetProfileAOTCode(value);
129 }
130
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100131 void SetJitAtFirstUse() {
132 use_jit_compilation_ = true;
133 compile_threshold_ = 0;
134 }
135
136 private:
David Srbeckye3fc2d12018-11-30 13:41:14 +0000137 // We add the sample in batches of size kJitSamplesBatchSize.
138 // This method rounds the threshold so that it is multiple of the batch size.
139 static uint32_t RoundUpThreshold(uint32_t threshold);
140
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100141 bool use_jit_compilation_;
142 size_t code_cache_initial_capacity_;
143 size_t code_cache_max_capacity_;
David Srbeckye3fc2d12018-11-30 13:41:14 +0000144 uint32_t compile_threshold_;
145 uint32_t warmup_threshold_;
146 uint32_t osr_threshold_;
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100147 uint16_t priority_thread_weight_;
148 uint16_t invoke_transition_weight_;
149 bool dump_info_on_shutdown_;
150 int thread_pool_pthread_priority_;
151 ProfileSaverOptions profile_saver_options_;
152
153 JitOptions()
154 : use_jit_compilation_(false),
155 code_cache_initial_capacity_(0),
156 code_cache_max_capacity_(0),
157 compile_threshold_(0),
158 warmup_threshold_(0),
159 osr_threshold_(0),
160 priority_thread_weight_(0),
161 invoke_transition_weight_(0),
162 dump_info_on_shutdown_(false),
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +0000163 thread_pool_pthread_priority_(kJitPoolThreadPthreadDefaultPriority) {}
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100164
165 DISALLOW_COPY_AND_ASSIGN(JitOptions);
166};
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100167
David Srbecky46b53532019-08-06 13:39:05 +0100168// Implemented and provided by the compiler library.
169class JitCompilerInterface {
170 public:
171 virtual ~JitCompilerInterface() {}
172 virtual bool CompileMethod(
173 Thread* self, JitMemoryRegion* region, ArtMethod* method, bool baseline, bool osr)
David Srbeckyb41869a2019-08-07 12:12:52 +0100174 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
David Srbecky46b53532019-08-06 13:39:05 +0100175 virtual void TypesLoaded(mirror::Class**, size_t count)
David Srbeckyb41869a2019-08-07 12:12:52 +0100176 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
177 virtual bool GenerateDebugInfo() = 0;
178 virtual void ParseCompilerOptions() = 0;
David Srbecky8fc2f952019-07-31 18:40:09 +0100179
David Srbeckye09b87e2019-08-19 21:31:31 +0100180 virtual std::vector<uint8_t> PackElfFileForJIT(ArrayRef<const JITCodeEntry*> elf_files,
David Srbecky8fc2f952019-07-31 18:40:09 +0100181 ArrayRef<const void*> removed_symbols,
182 bool compress,
183 /*out*/ size_t* num_symbols) = 0;
David Srbecky46b53532019-08-06 13:39:05 +0100184};
185
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800186class Jit {
187 public:
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100188 static constexpr size_t kDefaultPriorityThreadWeightRatio = 1000;
Nicolas Geoffraybd553eb2016-04-28 13:56:04 +0100189 static constexpr size_t kDefaultInvokeTransitionWeightRatio = 500;
buzbee42a09cb02017-02-01 09:08:31 -0800190 // How frequently should the interpreter check to see if OSR compilation is ready.
David Srbeckye3fc2d12018-11-30 13:41:14 +0000191 static constexpr int16_t kJitRecheckOSRThreshold = 101; // Prime number to avoid patterns.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800192
David Srbecky21821472019-07-29 15:10:31 +0100193 DECLARE_RUNTIME_DEBUG_FLAG(kSlowMode);
194
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800195 virtual ~Jit();
Orion Hodsonad28f5e2018-10-17 09:08:17 +0100196
197 // Create JIT itself.
198 static Jit* Create(JitCodeCache* code_cache, JitOptions* options);
199
Nicolas Geoffrayd2f13ba2019-06-04 16:48:58 +0100200 bool CompileMethod(ArtMethod* method, Thread* self, bool baseline, bool osr, bool prejit)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700201 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100202
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800203 const JitCodeCache* GetCodeCache() const {
Orion Hodsonad28f5e2018-10-17 09:08:17 +0100204 return code_cache_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800205 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100206
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800207 JitCodeCache* GetCodeCache() {
Orion Hodsonad28f5e2018-10-17 09:08:17 +0100208 return code_cache_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800209 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100210
David Srbecky8fc2f952019-07-31 18:40:09 +0100211 JitCompilerInterface* GetJitCompiler() const {
212 return jit_compiler_;
213 }
214
Orion Hodsonad28f5e2018-10-17 09:08:17 +0100215 void CreateThreadPool();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800216 void DeleteThreadPool();
Mathieu Chartier93c21ba2018-12-10 13:08:30 -0800217 void WaitForWorkersToBeCreated();
Orion Hodsonad28f5e2018-10-17 09:08:17 +0100218
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700219 // Dump interesting info: #methods compiled, code vs data size, compile / verify cumulative
220 // loggers.
Nicolas Geoffraya4f81542016-03-08 16:57:48 +0000221 void DumpInfo(std::ostream& os) REQUIRES(!lock_);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700222 // Add a timing logger to cumulative_timings_.
223 void AddTimingLogger(const TimingLogger& logger);
Nicolas Geoffraya4f81542016-03-08 16:57:48 +0000224
225 void AddMemoryUsage(ArtMethod* method, size_t bytes)
226 REQUIRES(!lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700227 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffraya4f81542016-03-08 16:57:48 +0000228
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100229 uint16_t OSRMethodThreshold() const {
230 return options_->GetOsrThreshold();
Mathieu Chartiera50f9cf2015-09-25 11:34:45 -0700231 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800232
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100233 uint16_t HotMethodThreshold() const {
234 return options_->GetCompileThreshold();
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100235 }
236
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100237 uint16_t WarmMethodThreshold() const {
238 return options_->GetWarmupThreshold();
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100239 }
240
241 uint16_t PriorityThreadWeight() const {
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100242 return options_->GetPriorityThreadWeight();
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100243 }
244
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +0000245 // Return whether we should do JIT compilation. Note this will returns false
246 // if we only need to save profile information and not compile methods.
Nicolas Geoffray2fef66b2019-06-26 22:00:02 +0000247 bool UseJitCompilation() const {
248 return options_->UseJitCompilation();
249 }
Calin Juravleffc87072016-04-20 14:22:09 +0100250
Calin Juravle138dbff2016-06-28 19:36:58 +0100251 bool GetSaveProfilingInfo() const {
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100252 return options_->GetSaveProfilingInfo();
Calin Juravleffc87072016-04-20 14:22:09 +0100253 }
254
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100255 // Wait until there is no more pending compilation tasks.
256 void WaitForCompilationToFinish(Thread* self);
257
258 // Profiling methods.
259 void MethodEntered(Thread* thread, ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700260 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100261
David Srbeckye3fc2d12018-11-30 13:41:14 +0000262 ALWAYS_INLINE void AddSamples(Thread* self,
263 ArtMethod* method,
264 uint16_t samples,
265 bool with_backedges)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700266 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100267
Mathieu Chartieref41db72016-10-25 15:08:01 -0700268 void InvokeVirtualOrInterface(ObjPtr<mirror::Object> this_object,
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100269 ArtMethod* caller,
270 uint32_t dex_pc,
271 ArtMethod* callee)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700272 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100273
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100274 void NotifyInterpreterToCompiledCodeTransition(Thread* self, ArtMethod* caller)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700275 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100276 AddSamples(self, caller, options_->GetInvokeTransitionWeight(), false);
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100277 }
278
279 void NotifyCompiledCodeToInterpreterTransition(Thread* self, ArtMethod* callee)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700280 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100281 AddSamples(self, callee, options_->GetInvokeTransitionWeight(), false);
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100282 }
283
Calin Juravlec90bc922016-02-24 10:13:09 +0000284 // Starts the profile saver if the config options allow profile recording.
285 // The profile will be stored in the specified `filename` and will contain
286 // information collected from the given `code_paths` (a set of dex locations).
Calin Juravlec90bc922016-02-24 10:13:09 +0000287 void StartProfileSaver(const std::string& filename,
Calin Juravle77651c42017-03-03 18:04:02 -0800288 const std::vector<std::string>& code_paths);
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000289 void StopProfileSaver();
Calin Juravle31f2c152015-10-23 17:56:15 +0100290
Calin Juravleb8e69992016-03-09 15:37:48 +0000291 void DumpForSigQuit(std::ostream& os) REQUIRES(!lock_);
Nicolas Geoffrayaee21562015-12-15 16:39:44 +0000292
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000293 static void NewTypeLoadedIfUsingJit(mirror::Class* type)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700294 REQUIRES_SHARED(Locks::mutator_lock_);
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000295
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000296 // If debug info generation is turned on then write the type information for types already loaded
297 // into the specified class linker to the jit debug interface,
298 void DumpTypeInfoForLoadedTypes(ClassLinker* linker);
299
Nicolas Geoffray35122442016-03-02 12:05:30 +0000300 // Return whether we should try to JIT compiled code as soon as an ArtMethod is invoked.
Siva Chandra05d24152016-01-05 17:43:17 -0800301 bool JitAtFirstUse();
302
Nicolas Geoffray35122442016-03-02 12:05:30 +0000303 // Return whether we can invoke JIT code for `method`.
304 bool CanInvokeCompiledCode(ArtMethod* method);
305
Calin Juravleb2771b42016-04-07 17:09:25 +0100306 // Return whether the runtime should use a priority thread weight when sampling.
Vladimir Markoa710d912017-09-12 14:56:07 +0100307 static bool ShouldUsePriorityThreadWeight(Thread* self);
Calin Juravleb2771b42016-04-07 17:09:25 +0100308
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000309 // If an OSR compiled version is available for `method`,
310 // and `dex_pc + dex_pc_offset` is an entry point of that compiled
311 // version, this method will jump to the compiled code, let it run,
312 // and return true afterwards. Return false otherwise.
313 static bool MaybeDoOnStackReplacement(Thread* thread,
314 ArtMethod* method,
315 uint32_t dex_pc,
316 int32_t dex_pc_offset,
317 JValue* result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700318 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000319
Nicolas Geoffraya7edd0d2018-11-07 03:18:16 +0000320 // Load the compiler library.
321 static bool LoadCompilerLibrary(std::string* error_msg);
Mathieu Chartier72918ea2016-03-24 11:07:06 -0700322
Andreas Gampef149b3f2016-11-16 14:58:24 -0800323 ThreadPool* GetThreadPool() const {
324 return thread_pool_.get();
325 }
326
Nicolas Geoffray021c5f22016-12-16 11:22:05 +0000327 // Stop the JIT by waiting for all current compilations and enqueued compilations to finish.
328 void Stop();
329
330 // Start JIT threads.
331 void Start();
332
Nicolas Geoffrayce9ed362018-11-29 03:19:28 +0000333 // Transition to a child state.
Nicolas Geoffray0d54cfb2019-05-03 09:13:52 +0100334 void PostForkChildAction(bool is_system_server, bool is_zygote);
Nicolas Geoffrayce9ed362018-11-29 03:19:28 +0000335
336 // Prepare for forking.
337 void PreZygoteFork();
338
339 // Adjust state after forking.
340 void PostZygoteFork();
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +0000341
David Srbeckybfcea3d2019-08-05 15:44:00 +0100342 // Called when system finishes booting.
343 void BootCompleted();
344
Nicolas Geoffray0f8950a2019-07-24 13:22:11 +0100345 // Compile methods from the given profile (.prof extension). If `add_to_queue`
346 // is true, methods in the profile are added to the JIT queue. Otherwise they are compiled
Nicolas Geoffray9ac09ee2019-05-08 23:38:27 +0100347 // directly.
Nicolas Geoffraye32d24c2019-07-05 10:28:59 +0100348 // Return the number of methods added to the queue.
349 uint32_t CompileMethodsFromProfile(Thread* self,
350 const std::vector<const DexFile*>& dex_files,
351 const std::string& profile_path,
352 Handle<mirror::ClassLoader> class_loader,
353 bool add_to_queue);
Nicolas Geoffray9ac09ee2019-05-08 23:38:27 +0100354
Nicolas Geoffray0f8950a2019-07-24 13:22:11 +0100355 // Compile methods from the given boot profile (.bprof extension). If `add_to_queue`
356 // is true, methods in the profile are added to the JIT queue. Otherwise they are compiled
357 // directly.
358 // Return the number of methods added to the queue.
359 uint32_t CompileMethodsFromBootProfile(Thread* self,
360 const std::vector<const DexFile*>& dex_files,
361 const std::string& profile_path,
362 Handle<mirror::ClassLoader> class_loader,
363 bool add_to_queue);
364
Nicolas Geoffray9ac09ee2019-05-08 23:38:27 +0100365 // Register the dex files to the JIT. This is to perform any compilation/optimization
366 // at the point of loading the dex files.
367 void RegisterDexFiles(const std::vector<std::unique_ptr<const DexFile>>& dex_files,
Nicolas Geoffray741a0702019-06-10 11:18:11 +0100368 jobject class_loader);
Nicolas Geoffrayde1b2a22019-02-27 09:10:57 +0000369
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +0000370 // Called by the compiler to know whether it can directly encode the
371 // method/class/string.
Nicolas Geoffray05b41c42019-06-28 12:46:33 +0100372 bool CanEncodeMethod(ArtMethod* method, bool is_for_shared_region) const
373 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +0000374 bool CanEncodeClass(ObjPtr<mirror::Class> cls, bool is_for_shared_region) const
375 REQUIRES_SHARED(Locks::mutator_lock_);
376 bool CanEncodeString(ObjPtr<mirror::String> string, bool is_for_shared_region) const
377 REQUIRES_SHARED(Locks::mutator_lock_);
378 bool CanAssumeInitialized(ObjPtr<mirror::Class> cls, bool is_for_shared_region) const
379 REQUIRES_SHARED(Locks::mutator_lock_);
380
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800381 private:
Orion Hodsonad28f5e2018-10-17 09:08:17 +0100382 Jit(JitCodeCache* code_cache, JitOptions* options);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800383
Nicolas Geoffray0f8950a2019-07-24 13:22:11 +0100384 // Compile an individual method listed in a profile. If `add_to_queue` is
385 // true and the method was resolved, return true. Otherwise return false.
386 bool CompileMethodFromProfile(Thread* self,
387 ClassLinker* linker,
388 uint32_t method_idx,
389 Handle<mirror::DexCache> dex_cache,
390 Handle<mirror::ClassLoader> class_loader,
David Srbeckybfcea3d2019-08-05 15:44:00 +0100391 bool add_to_queue,
392 bool compile_after_boot)
Nicolas Geoffray0f8950a2019-07-24 13:22:11 +0100393 REQUIRES_SHARED(Locks::mutator_lock_);
394
David Srbeckye3fc2d12018-11-30 13:41:14 +0000395 // Compile the method if the number of samples passes a threshold.
396 // Returns false if we can not compile now - don't increment the counter and retry later.
397 bool MaybeCompileMethod(Thread* self,
398 ArtMethod* method,
399 uint32_t old_count,
400 uint32_t new_count,
401 bool with_backedges)
402 REQUIRES_SHARED(Locks::mutator_lock_);
403
Orion Hodsonad28f5e2018-10-17 09:08:17 +0100404 static bool BindCompilerMethods(std::string* error_msg);
Mathieu Chartierc1bc4152016-03-24 17:22:52 -0700405
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800406 // JIT compiler
Mathieu Chartier72918ea2016-03-24 11:07:06 -0700407 static void* jit_library_handle_;
David Srbecky46b53532019-08-06 13:39:05 +0100408 static JitCompilerInterface* jit_compiler_;
409 static JitCompilerInterface* (*jit_load_)(void);
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +0000410 template <typename T> static bool LoadSymbol(T*, const char* symbol, std::string* error_msg);
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100411
Orion Hodsonad28f5e2018-10-17 09:08:17 +0100412 // JIT resources owned by runtime.
413 jit::JitCodeCache* const code_cache_;
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100414 const JitOptions* const options_;
415
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100416 std::unique_ptr<ThreadPool> thread_pool_;
Nicolas Geoffraydc2fbb62019-04-11 22:55:50 +0100417 std::vector<std::unique_ptr<OatDexFile>> type_lookup_tables_;
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100418
David Srbeckybfcea3d2019-08-05 15:44:00 +0100419 Mutex boot_completed_lock_;
420 bool boot_completed_ GUARDED_BY(boot_completed_lock_) = false;
421 std::deque<Task*> tasks_after_boot_ GUARDED_BY(boot_completed_lock_);
422
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700423 // Performance monitoring.
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700424 CumulativeLogger cumulative_timings_;
Nicolas Geoffraya4f81542016-03-08 16:57:48 +0000425 Histogram<uint64_t> memory_use_ GUARDED_BY(lock_);
426 Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700427
Mathieu Chartier3130cdf2015-05-03 15:20:23 -0700428 DISALLOW_COPY_AND_ASSIGN(Jit);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800429};
430
Andreas Gampef149b3f2016-11-16 14:58:24 -0800431// Helper class to stop the JIT for a given scope. This will wait for the JIT to quiesce.
432class ScopedJitSuspend {
433 public:
434 ScopedJitSuspend();
435 ~ScopedJitSuspend();
436
437 private:
438 bool was_on_;
439};
440
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800441} // namespace jit
442} // namespace art
443
444#endif // ART_RUNTIME_JIT_JIT_H_