blob: 5da1ea119662b2e01b1af433b3c6d1bc870aba36 [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/arena_allocator.h"
21#include "base/histogram-inl.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080022#include "base/macros.h"
23#include "base/mutex.h"
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070024#include "base/timing_logger.h"
Mathieu Chartieref41db72016-10-25 15:08:01 -070025#include "jit/profile_saver_options.h"
26#include "obj_ptr.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080027#include "object_callbacks.h"
Calin Juravle33083d62017-01-18 15:29:12 -080028#include "profile_compilation_info.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;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080035struct RuntimeArgumentMap;
Vladimir Marko3a21e382016-09-02 12:38:38 +010036union JValue;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080037
David Sehr709b0702016-10-13 09:12:37 -070038namespace mirror {
39class Object;
40class Class;
41} // namespace mirror
42
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080043namespace jit {
44
45class JitCodeCache;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080046class JitOptions;
47
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +010048static constexpr int16_t kJitCheckForOSR = -1;
49static constexpr int16_t kJitHotnessDisabled = -2;
50
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080051class Jit {
52 public:
53 static constexpr bool kStressMode = kIsDebugBuild;
Nicolas Geoffray83f080a2016-03-08 16:50:21 +000054 static constexpr size_t kDefaultCompileThreshold = kStressMode ? 2 : 10000;
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +010055 static constexpr size_t kDefaultPriorityThreadWeightRatio = 1000;
Nicolas Geoffraybd553eb2016-04-28 13:56:04 +010056 static constexpr size_t kDefaultInvokeTransitionWeightRatio = 500;
buzbee42a09cb02017-02-01 09:08:31 -080057 // How frequently should the interpreter check to see if OSR compilation is ready.
58 static constexpr int16_t kJitRecheckOSRThreshold = 100;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080059
60 virtual ~Jit();
61 static Jit* Create(JitOptions* options, std::string* error_msg);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +000062 bool CompileMethod(ArtMethod* method, Thread* self, bool osr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070063 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080064 void CreateThreadPool();
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +010065
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080066 const JitCodeCache* GetCodeCache() const {
67 return code_cache_.get();
68 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +010069
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080070 JitCodeCache* GetCodeCache() {
71 return code_cache_.get();
72 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +010073
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080074 void DeleteThreadPool();
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070075 // Dump interesting info: #methods compiled, code vs data size, compile / verify cumulative
76 // loggers.
Nicolas Geoffraya4f81542016-03-08 16:57:48 +000077 void DumpInfo(std::ostream& os) REQUIRES(!lock_);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070078 // Add a timing logger to cumulative_timings_.
79 void AddTimingLogger(const TimingLogger& logger);
Nicolas Geoffraya4f81542016-03-08 16:57:48 +000080
81 void AddMemoryUsage(ArtMethod* method, size_t bytes)
82 REQUIRES(!lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070083 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffraya4f81542016-03-08 16:57:48 +000084
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +010085 size_t OSRMethodThreshold() const {
86 return osr_method_threshold_;
Mathieu Chartiera50f9cf2015-09-25 11:34:45 -070087 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080088
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +010089 size_t HotMethodThreshold() const {
90 return hot_method_threshold_;
91 }
92
93 size_t WarmMethodThreshold() const {
94 return warm_method_threshold_;
95 }
96
97 uint16_t PriorityThreadWeight() const {
98 return priority_thread_weight_;
99 }
100
Calin Juravleffc87072016-04-20 14:22:09 +0100101 // Returns false if we only need to save profile information and not compile methods.
102 bool UseJitCompilation() const {
103 return use_jit_compilation_;
104 }
105
Calin Juravle138dbff2016-06-28 19:36:58 +0100106 bool GetSaveProfilingInfo() const {
107 return profile_saver_options_.IsEnabled();
Calin Juravleffc87072016-04-20 14:22:09 +0100108 }
109
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100110 // Wait until there is no more pending compilation tasks.
111 void WaitForCompilationToFinish(Thread* self);
112
113 // Profiling methods.
114 void MethodEntered(Thread* thread, ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700115 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100116
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100117 void AddSamples(Thread* self, ArtMethod* method, uint16_t samples, bool with_backedges)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700118 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100119
Mathieu Chartieref41db72016-10-25 15:08:01 -0700120 void InvokeVirtualOrInterface(ObjPtr<mirror::Object> this_object,
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100121 ArtMethod* caller,
122 uint32_t dex_pc,
123 ArtMethod* callee)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700124 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100125
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100126 void NotifyInterpreterToCompiledCodeTransition(Thread* self, ArtMethod* caller)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700127 REQUIRES_SHARED(Locks::mutator_lock_) {
Calin Juravle155ff3d2016-04-27 14:14:58 +0100128 AddSamples(self, caller, invoke_transition_weight_, false);
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100129 }
130
131 void NotifyCompiledCodeToInterpreterTransition(Thread* self, ArtMethod* callee)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700132 REQUIRES_SHARED(Locks::mutator_lock_) {
Calin Juravle155ff3d2016-04-27 14:14:58 +0100133 AddSamples(self, callee, invoke_transition_weight_, false);
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100134 }
135
Calin Juravlec90bc922016-02-24 10:13:09 +0000136 // Starts the profile saver if the config options allow profile recording.
137 // The profile will be stored in the specified `filename` and will contain
138 // information collected from the given `code_paths` (a set of dex locations).
Calin Juravlec90bc922016-02-24 10:13:09 +0000139 void StartProfileSaver(const std::string& filename,
Calin Juravle77651c42017-03-03 18:04:02 -0800140 const std::vector<std::string>& code_paths);
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000141 void StopProfileSaver();
Calin Juravle31f2c152015-10-23 17:56:15 +0100142
Calin Juravleb8e69992016-03-09 15:37:48 +0000143 void DumpForSigQuit(std::ostream& os) REQUIRES(!lock_);
Nicolas Geoffrayaee21562015-12-15 16:39:44 +0000144
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000145 static void NewTypeLoadedIfUsingJit(mirror::Class* type)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700146 REQUIRES_SHARED(Locks::mutator_lock_);
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000147
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000148 // If debug info generation is turned on then write the type information for types already loaded
149 // into the specified class linker to the jit debug interface,
150 void DumpTypeInfoForLoadedTypes(ClassLinker* linker);
151
Nicolas Geoffray35122442016-03-02 12:05:30 +0000152 // Return whether we should try to JIT compiled code as soon as an ArtMethod is invoked.
Siva Chandra05d24152016-01-05 17:43:17 -0800153 bool JitAtFirstUse();
154
Nicolas Geoffray35122442016-03-02 12:05:30 +0000155 // Return whether we can invoke JIT code for `method`.
156 bool CanInvokeCompiledCode(ArtMethod* method);
157
Calin Juravleb2771b42016-04-07 17:09:25 +0100158 // Return whether the runtime should use a priority thread weight when sampling.
159 static bool ShouldUsePriorityThreadWeight();
160
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000161 // If an OSR compiled version is available for `method`,
162 // and `dex_pc + dex_pc_offset` is an entry point of that compiled
163 // version, this method will jump to the compiled code, let it run,
164 // and return true afterwards. Return false otherwise.
165 static bool MaybeDoOnStackReplacement(Thread* thread,
166 ArtMethod* method,
167 uint32_t dex_pc,
168 int32_t dex_pc_offset,
169 JValue* result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700170 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000171
Mathieu Chartierc1bc4152016-03-24 17:22:52 -0700172 static bool LoadCompilerLibrary(std::string* error_msg);
Mathieu Chartier72918ea2016-03-24 11:07:06 -0700173
Andreas Gampef149b3f2016-11-16 14:58:24 -0800174 ThreadPool* GetThreadPool() const {
175 return thread_pool_.get();
176 }
177
Nicolas Geoffray021c5f22016-12-16 11:22:05 +0000178 // Stop the JIT by waiting for all current compilations and enqueued compilations to finish.
179 void Stop();
180
181 // Start JIT threads.
182 void Start();
183
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800184 private:
185 Jit();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800186
Mathieu Chartierc1bc4152016-03-24 17:22:52 -0700187 static bool LoadCompiler(std::string* error_msg);
188
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800189 // JIT compiler
Mathieu Chartier72918ea2016-03-24 11:07:06 -0700190 static void* jit_library_handle_;
191 static void* jit_compiler_handle_;
192 static void* (*jit_load_)(bool*);
193 static void (*jit_unload_)(void*);
194 static bool (*jit_compile_method_)(void*, ArtMethod*, Thread*, bool);
195 static void (*jit_types_loaded_)(void*, mirror::Class**, size_t count);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800196
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700197 // Performance monitoring.
198 bool dump_info_on_shutdown_;
199 CumulativeLogger cumulative_timings_;
Nicolas Geoffraya4f81542016-03-08 16:57:48 +0000200 Histogram<uint64_t> memory_use_ GUARDED_BY(lock_);
201 Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700202
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800203 std::unique_ptr<jit::JitCodeCache> code_cache_;
Mathieu Chartier3130cdf2015-05-03 15:20:23 -0700204
Calin Juravleffc87072016-04-20 14:22:09 +0100205 bool use_jit_compilation_;
Calin Juravle138dbff2016-06-28 19:36:58 +0100206 ProfileSaverOptions profile_saver_options_;
Mathieu Chartier72918ea2016-03-24 11:07:06 -0700207 static bool generate_debug_info_;
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100208 uint16_t hot_method_threshold_;
209 uint16_t warm_method_threshold_;
210 uint16_t osr_method_threshold_;
211 uint16_t priority_thread_weight_;
Calin Juravle155ff3d2016-04-27 14:14:58 +0100212 uint16_t invoke_transition_weight_;
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100213 std::unique_ptr<ThreadPool> thread_pool_;
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000214
Mathieu Chartier3130cdf2015-05-03 15:20:23 -0700215 DISALLOW_COPY_AND_ASSIGN(Jit);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800216};
217
218class JitOptions {
219 public:
220 static JitOptions* CreateFromRuntimeArguments(const RuntimeArgumentMap& options);
221 size_t GetCompileThreshold() const {
222 return compile_threshold_;
223 }
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100224 size_t GetWarmupThreshold() const {
225 return warmup_threshold_;
226 }
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000227 size_t GetOsrThreshold() const {
228 return osr_threshold_;
229 }
Calin Juravleb2771b42016-04-07 17:09:25 +0100230 uint16_t GetPriorityThreadWeight() const {
231 return priority_thread_weight_;
232 }
Calin Juravle155ff3d2016-04-27 14:14:58 +0100233 size_t GetInvokeTransitionWeight() const {
234 return invoke_transition_weight_;
235 }
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000236 size_t GetCodeCacheInitialCapacity() const {
237 return code_cache_initial_capacity_;
238 }
239 size_t GetCodeCacheMaxCapacity() const {
240 return code_cache_max_capacity_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800241 }
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700242 bool DumpJitInfoOnShutdown() const {
243 return dump_info_on_shutdown_;
244 }
Calin Juravle138dbff2016-06-28 19:36:58 +0100245 const ProfileSaverOptions& GetProfileSaverOptions() const {
246 return profile_saver_options_;
247 }
Calin Juravle31f2c152015-10-23 17:56:15 +0100248 bool GetSaveProfilingInfo() const {
Calin Juravle138dbff2016-06-28 19:36:58 +0100249 return profile_saver_options_.IsEnabled();
Calin Juravle31f2c152015-10-23 17:56:15 +0100250 }
Calin Juravleffc87072016-04-20 14:22:09 +0100251 bool UseJitCompilation() const {
252 return use_jit_compilation_;
Mathieu Chartier455f67c2015-03-17 13:48:29 -0700253 }
Calin Juravleffc87072016-04-20 14:22:09 +0100254 void SetUseJitCompilation(bool b) {
255 use_jit_compilation_ = b;
Mathieu Chartier455f67c2015-03-17 13:48:29 -0700256 }
Calin Juravle138dbff2016-06-28 19:36:58 +0100257 void SetSaveProfilingInfo(bool save_profiling_info) {
258 profile_saver_options_.SetEnabled(save_profiling_info);
Calin Juravle31f2c152015-10-23 17:56:15 +0100259 }
Siva Chandra05d24152016-01-05 17:43:17 -0800260 void SetJitAtFirstUse() {
Calin Juravleffc87072016-04-20 14:22:09 +0100261 use_jit_compilation_ = true;
Siva Chandra05d24152016-01-05 17:43:17 -0800262 compile_threshold_ = 0;
263 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800264
265 private:
Calin Juravleffc87072016-04-20 14:22:09 +0100266 bool use_jit_compilation_;
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000267 size_t code_cache_initial_capacity_;
268 size_t code_cache_max_capacity_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800269 size_t compile_threshold_;
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100270 size_t warmup_threshold_;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000271 size_t osr_threshold_;
Calin Juravleb2771b42016-04-07 17:09:25 +0100272 uint16_t priority_thread_weight_;
Calin Juravle155ff3d2016-04-27 14:14:58 +0100273 size_t invoke_transition_weight_;
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700274 bool dump_info_on_shutdown_;
Calin Juravle138dbff2016-06-28 19:36:58 +0100275 ProfileSaverOptions profile_saver_options_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800276
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000277 JitOptions()
Calin Juravleffc87072016-04-20 14:22:09 +0100278 : use_jit_compilation_(false),
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000279 code_cache_initial_capacity_(0),
280 code_cache_max_capacity_(0),
281 compile_threshold_(0),
Calin Juravle138dbff2016-06-28 19:36:58 +0100282 dump_info_on_shutdown_(false) {}
Mathieu Chartier3130cdf2015-05-03 15:20:23 -0700283
284 DISALLOW_COPY_AND_ASSIGN(JitOptions);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800285};
286
Andreas Gampef149b3f2016-11-16 14:58:24 -0800287// Helper class to stop the JIT for a given scope. This will wait for the JIT to quiesce.
288class ScopedJitSuspend {
289 public:
290 ScopedJitSuspend();
291 ~ScopedJitSuspend();
292
293 private:
294 bool was_on_;
295};
296
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800297} // namespace jit
298} // namespace art
299
300#endif // ART_RUNTIME_JIT_JIT_H_