blob: 791c3386cf7ddf707025f6a56d4a40264365ade9 [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"
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070023#include "base/timing_logger.h"
Mathieu Chartieref41db72016-10-25 15:08:01 -070024#include "jit/profile_saver_options.h"
25#include "obj_ptr.h"
Calin Juravle33083d62017-01-18 15:29:12 -080026#include "profile_compilation_info.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080027#include "thread_pool.h"
28
29namespace art {
30
Mathieu Chartiere401d142015-04-22 13:56:20 -070031class ArtMethod;
David Sehr9323e6e2016-09-13 08:58:35 -070032class ClassLinker;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080033struct RuntimeArgumentMap;
Vladimir Marko3a21e382016-09-02 12:38:38 +010034union JValue;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080035
David Sehr709b0702016-10-13 09:12:37 -070036namespace mirror {
37class Object;
38class Class;
39} // namespace mirror
40
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080041namespace jit {
42
43class JitCodeCache;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080044class JitOptions;
45
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +010046static constexpr int16_t kJitCheckForOSR = -1;
47static constexpr int16_t kJitHotnessDisabled = -2;
48
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080049class Jit {
50 public:
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +010051 static constexpr size_t kDefaultPriorityThreadWeightRatio = 1000;
Nicolas Geoffraybd553eb2016-04-28 13:56:04 +010052 static constexpr size_t kDefaultInvokeTransitionWeightRatio = 500;
buzbee42a09cb02017-02-01 09:08:31 -080053 // How frequently should the interpreter check to see if OSR compilation is ready.
54 static constexpr int16_t kJitRecheckOSRThreshold = 100;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080055
56 virtual ~Jit();
57 static Jit* Create(JitOptions* options, std::string* error_msg);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +000058 bool CompileMethod(ArtMethod* method, Thread* self, bool osr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070059 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080060 void CreateThreadPool();
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +010061
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080062 const JitCodeCache* GetCodeCache() const {
63 return code_cache_.get();
64 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +010065
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080066 JitCodeCache* GetCodeCache() {
67 return code_cache_.get();
68 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +010069
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080070 void DeleteThreadPool();
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070071 // Dump interesting info: #methods compiled, code vs data size, compile / verify cumulative
72 // loggers.
Nicolas Geoffraya4f81542016-03-08 16:57:48 +000073 void DumpInfo(std::ostream& os) REQUIRES(!lock_);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070074 // Add a timing logger to cumulative_timings_.
75 void AddTimingLogger(const TimingLogger& logger);
Nicolas Geoffraya4f81542016-03-08 16:57:48 +000076
77 void AddMemoryUsage(ArtMethod* method, size_t bytes)
78 REQUIRES(!lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070079 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffraya4f81542016-03-08 16:57:48 +000080
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +010081 size_t OSRMethodThreshold() const {
82 return osr_method_threshold_;
Mathieu Chartiera50f9cf2015-09-25 11:34:45 -070083 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080084
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +010085 size_t HotMethodThreshold() const {
86 return hot_method_threshold_;
87 }
88
89 size_t WarmMethodThreshold() const {
90 return warm_method_threshold_;
91 }
92
93 uint16_t PriorityThreadWeight() const {
94 return priority_thread_weight_;
95 }
96
Calin Juravleffc87072016-04-20 14:22:09 +010097 // Returns false if we only need to save profile information and not compile methods.
98 bool UseJitCompilation() const {
99 return use_jit_compilation_;
100 }
101
Calin Juravle138dbff2016-06-28 19:36:58 +0100102 bool GetSaveProfilingInfo() const {
103 return profile_saver_options_.IsEnabled();
Calin Juravleffc87072016-04-20 14:22:09 +0100104 }
105
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100106 // Wait until there is no more pending compilation tasks.
107 void WaitForCompilationToFinish(Thread* self);
108
109 // Profiling methods.
110 void MethodEntered(Thread* thread, ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700111 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100112
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100113 void AddSamples(Thread* self, ArtMethod* method, uint16_t samples, bool with_backedges)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700114 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100115
Mathieu Chartieref41db72016-10-25 15:08:01 -0700116 void InvokeVirtualOrInterface(ObjPtr<mirror::Object> this_object,
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100117 ArtMethod* caller,
118 uint32_t dex_pc,
119 ArtMethod* callee)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700120 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100121
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100122 void NotifyInterpreterToCompiledCodeTransition(Thread* self, ArtMethod* caller)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700123 REQUIRES_SHARED(Locks::mutator_lock_) {
Calin Juravle155ff3d2016-04-27 14:14:58 +0100124 AddSamples(self, caller, invoke_transition_weight_, false);
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100125 }
126
127 void NotifyCompiledCodeToInterpreterTransition(Thread* self, ArtMethod* callee)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700128 REQUIRES_SHARED(Locks::mutator_lock_) {
Calin Juravle155ff3d2016-04-27 14:14:58 +0100129 AddSamples(self, callee, invoke_transition_weight_, false);
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100130 }
131
Calin Juravlec90bc922016-02-24 10:13:09 +0000132 // Starts the profile saver if the config options allow profile recording.
133 // The profile will be stored in the specified `filename` and will contain
134 // information collected from the given `code_paths` (a set of dex locations).
Calin Juravlec90bc922016-02-24 10:13:09 +0000135 void StartProfileSaver(const std::string& filename,
Calin Juravle77651c42017-03-03 18:04:02 -0800136 const std::vector<std::string>& code_paths);
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000137 void StopProfileSaver();
Calin Juravle31f2c152015-10-23 17:56:15 +0100138
Calin Juravleb8e69992016-03-09 15:37:48 +0000139 void DumpForSigQuit(std::ostream& os) REQUIRES(!lock_);
Nicolas Geoffrayaee21562015-12-15 16:39:44 +0000140
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000141 static void NewTypeLoadedIfUsingJit(mirror::Class* type)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700142 REQUIRES_SHARED(Locks::mutator_lock_);
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000143
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000144 // If debug info generation is turned on then write the type information for types already loaded
145 // into the specified class linker to the jit debug interface,
146 void DumpTypeInfoForLoadedTypes(ClassLinker* linker);
147
Nicolas Geoffray35122442016-03-02 12:05:30 +0000148 // Return whether we should try to JIT compiled code as soon as an ArtMethod is invoked.
Siva Chandra05d24152016-01-05 17:43:17 -0800149 bool JitAtFirstUse();
150
Nicolas Geoffray35122442016-03-02 12:05:30 +0000151 // Return whether we can invoke JIT code for `method`.
152 bool CanInvokeCompiledCode(ArtMethod* method);
153
Calin Juravleb2771b42016-04-07 17:09:25 +0100154 // Return whether the runtime should use a priority thread weight when sampling.
Vladimir Markoa710d912017-09-12 14:56:07 +0100155 static bool ShouldUsePriorityThreadWeight(Thread* self);
Calin Juravleb2771b42016-04-07 17:09:25 +0100156
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000157 // If an OSR compiled version is available for `method`,
158 // and `dex_pc + dex_pc_offset` is an entry point of that compiled
159 // version, this method will jump to the compiled code, let it run,
160 // and return true afterwards. Return false otherwise.
161 static bool MaybeDoOnStackReplacement(Thread* thread,
162 ArtMethod* method,
163 uint32_t dex_pc,
164 int32_t dex_pc_offset,
165 JValue* result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700166 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000167
Mathieu Chartierc1bc4152016-03-24 17:22:52 -0700168 static bool LoadCompilerLibrary(std::string* error_msg);
Mathieu Chartier72918ea2016-03-24 11:07:06 -0700169
Andreas Gampef149b3f2016-11-16 14:58:24 -0800170 ThreadPool* GetThreadPool() const {
171 return thread_pool_.get();
172 }
173
Nicolas Geoffray021c5f22016-12-16 11:22:05 +0000174 // Stop the JIT by waiting for all current compilations and enqueued compilations to finish.
175 void Stop();
176
177 // Start JIT threads.
178 void Start();
179
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800180 private:
181 Jit();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800182
Mathieu Chartierc1bc4152016-03-24 17:22:52 -0700183 static bool LoadCompiler(std::string* error_msg);
184
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800185 // JIT compiler
Mathieu Chartier72918ea2016-03-24 11:07:06 -0700186 static void* jit_library_handle_;
187 static void* jit_compiler_handle_;
188 static void* (*jit_load_)(bool*);
189 static void (*jit_unload_)(void*);
190 static bool (*jit_compile_method_)(void*, ArtMethod*, Thread*, bool);
191 static void (*jit_types_loaded_)(void*, mirror::Class**, size_t count);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800192
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700193 // Performance monitoring.
194 bool dump_info_on_shutdown_;
195 CumulativeLogger cumulative_timings_;
Nicolas Geoffraya4f81542016-03-08 16:57:48 +0000196 Histogram<uint64_t> memory_use_ GUARDED_BY(lock_);
197 Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700198
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800199 std::unique_ptr<jit::JitCodeCache> code_cache_;
Mathieu Chartier3130cdf2015-05-03 15:20:23 -0700200
Calin Juravleffc87072016-04-20 14:22:09 +0100201 bool use_jit_compilation_;
Calin Juravle138dbff2016-06-28 19:36:58 +0100202 ProfileSaverOptions profile_saver_options_;
Mathieu Chartier72918ea2016-03-24 11:07:06 -0700203 static bool generate_debug_info_;
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100204 uint16_t hot_method_threshold_;
205 uint16_t warm_method_threshold_;
206 uint16_t osr_method_threshold_;
207 uint16_t priority_thread_weight_;
Calin Juravle155ff3d2016-04-27 14:14:58 +0100208 uint16_t invoke_transition_weight_;
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100209 std::unique_ptr<ThreadPool> thread_pool_;
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000210
Mathieu Chartier3130cdf2015-05-03 15:20:23 -0700211 DISALLOW_COPY_AND_ASSIGN(Jit);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800212};
213
214class JitOptions {
215 public:
216 static JitOptions* CreateFromRuntimeArguments(const RuntimeArgumentMap& options);
217 size_t GetCompileThreshold() const {
218 return compile_threshold_;
219 }
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100220 size_t GetWarmupThreshold() const {
221 return warmup_threshold_;
222 }
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000223 size_t GetOsrThreshold() const {
224 return osr_threshold_;
225 }
Calin Juravleb2771b42016-04-07 17:09:25 +0100226 uint16_t GetPriorityThreadWeight() const {
227 return priority_thread_weight_;
228 }
Calin Juravle155ff3d2016-04-27 14:14:58 +0100229 size_t GetInvokeTransitionWeight() const {
230 return invoke_transition_weight_;
231 }
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000232 size_t GetCodeCacheInitialCapacity() const {
233 return code_cache_initial_capacity_;
234 }
235 size_t GetCodeCacheMaxCapacity() const {
236 return code_cache_max_capacity_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800237 }
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700238 bool DumpJitInfoOnShutdown() const {
239 return dump_info_on_shutdown_;
240 }
Calin Juravle138dbff2016-06-28 19:36:58 +0100241 const ProfileSaverOptions& GetProfileSaverOptions() const {
242 return profile_saver_options_;
243 }
Calin Juravle31f2c152015-10-23 17:56:15 +0100244 bool GetSaveProfilingInfo() const {
Calin Juravle138dbff2016-06-28 19:36:58 +0100245 return profile_saver_options_.IsEnabled();
Calin Juravle31f2c152015-10-23 17:56:15 +0100246 }
Calin Juravleffc87072016-04-20 14:22:09 +0100247 bool UseJitCompilation() const {
248 return use_jit_compilation_;
Mathieu Chartier455f67c2015-03-17 13:48:29 -0700249 }
Calin Juravleffc87072016-04-20 14:22:09 +0100250 void SetUseJitCompilation(bool b) {
251 use_jit_compilation_ = b;
Mathieu Chartier455f67c2015-03-17 13:48:29 -0700252 }
Calin Juravle138dbff2016-06-28 19:36:58 +0100253 void SetSaveProfilingInfo(bool save_profiling_info) {
254 profile_saver_options_.SetEnabled(save_profiling_info);
Calin Juravle31f2c152015-10-23 17:56:15 +0100255 }
Siva Chandra05d24152016-01-05 17:43:17 -0800256 void SetJitAtFirstUse() {
Calin Juravleffc87072016-04-20 14:22:09 +0100257 use_jit_compilation_ = true;
Siva Chandra05d24152016-01-05 17:43:17 -0800258 compile_threshold_ = 0;
259 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800260
261 private:
Calin Juravleffc87072016-04-20 14:22:09 +0100262 bool use_jit_compilation_;
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000263 size_t code_cache_initial_capacity_;
264 size_t code_cache_max_capacity_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800265 size_t compile_threshold_;
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100266 size_t warmup_threshold_;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000267 size_t osr_threshold_;
Calin Juravleb2771b42016-04-07 17:09:25 +0100268 uint16_t priority_thread_weight_;
Calin Juravle155ff3d2016-04-27 14:14:58 +0100269 size_t invoke_transition_weight_;
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700270 bool dump_info_on_shutdown_;
Calin Juravle138dbff2016-06-28 19:36:58 +0100271 ProfileSaverOptions profile_saver_options_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800272
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000273 JitOptions()
Calin Juravleffc87072016-04-20 14:22:09 +0100274 : use_jit_compilation_(false),
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000275 code_cache_initial_capacity_(0),
276 code_cache_max_capacity_(0),
277 compile_threshold_(0),
Andreas Gamped9911ee2017-03-27 13:27:24 -0700278 warmup_threshold_(0),
279 osr_threshold_(0),
280 priority_thread_weight_(0),
281 invoke_transition_weight_(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_