blob: 46b0762629fbf88ea0653bd076cab3e096339503 [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"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080026#include "thread_pool.h"
27
28namespace art {
29
Mathieu Chartiere401d142015-04-22 13:56:20 -070030class ArtMethod;
David Sehr9323e6e2016-09-13 08:58:35 -070031class ClassLinker;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080032struct RuntimeArgumentMap;
Vladimir Marko3a21e382016-09-02 12:38:38 +010033union JValue;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080034
David Sehr709b0702016-10-13 09:12:37 -070035namespace mirror {
36class Object;
37class Class;
38} // namespace mirror
39
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080040namespace jit {
41
42class JitCodeCache;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080043class JitOptions;
44
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +010045static constexpr int16_t kJitCheckForOSR = -1;
46static constexpr int16_t kJitHotnessDisabled = -2;
Nicolas Geoffray47b95802018-05-16 15:42:17 +010047// At what priority to schedule jit threads. 9 is the lowest foreground priority on device.
48// See android/os/Process.java.
49static constexpr int kJitPoolThreadPthreadDefaultPriority = 9;
50
51class JitOptions {
52 public:
53 static JitOptions* CreateFromRuntimeArguments(const RuntimeArgumentMap& options);
54
55 uint16_t GetCompileThreshold() const {
56 return compile_threshold_;
57 }
58
59 uint16_t GetWarmupThreshold() const {
60 return warmup_threshold_;
61 }
62
63 uint16_t GetOsrThreshold() const {
64 return osr_threshold_;
65 }
66
67 uint16_t GetPriorityThreadWeight() const {
68 return priority_thread_weight_;
69 }
70
71 uint16_t GetInvokeTransitionWeight() const {
72 return invoke_transition_weight_;
73 }
74
75 size_t GetCodeCacheInitialCapacity() const {
76 return code_cache_initial_capacity_;
77 }
78
79 size_t GetCodeCacheMaxCapacity() const {
80 return code_cache_max_capacity_;
81 }
82
83 bool DumpJitInfoOnShutdown() const {
84 return dump_info_on_shutdown_;
85 }
86
87 const ProfileSaverOptions& GetProfileSaverOptions() const {
88 return profile_saver_options_;
89 }
90
91 bool GetSaveProfilingInfo() const {
92 return profile_saver_options_.IsEnabled();
93 }
94
95 int GetThreadPoolPthreadPriority() const {
96 return thread_pool_pthread_priority_;
97 }
98
99 bool UseJitCompilation() const {
100 return use_jit_compilation_;
101 }
102
Orion Hodsonad28f5e2018-10-17 09:08:17 +0100103 bool RWXMemoryAllowed() const {
104 return rwx_memory_allowed_;
105 }
106
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100107 void SetUseJitCompilation(bool b) {
108 use_jit_compilation_ = b;
109 }
110
111 void SetSaveProfilingInfo(bool save_profiling_info) {
112 profile_saver_options_.SetEnabled(save_profiling_info);
113 }
114
115 void SetWaitForJitNotificationsToSaveProfile(bool value) {
116 profile_saver_options_.SetWaitForJitNotificationsToSave(value);
117 }
118
119 void SetProfileAOTCode(bool value) {
120 profile_saver_options_.SetProfileAOTCode(value);
121 }
122
123 void SetJitAtFirstUse() {
124 use_jit_compilation_ = true;
125 compile_threshold_ = 0;
126 }
127
Orion Hodsonad28f5e2018-10-17 09:08:17 +0100128 void SetRWXMemoryAllowed(bool rwx_allowed) {
129 rwx_memory_allowed_ = rwx_allowed;
130 }
131
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100132 private:
133 bool use_jit_compilation_;
134 size_t code_cache_initial_capacity_;
135 size_t code_cache_max_capacity_;
136 uint16_t compile_threshold_;
137 uint16_t warmup_threshold_;
138 uint16_t osr_threshold_;
139 uint16_t priority_thread_weight_;
140 uint16_t invoke_transition_weight_;
141 bool dump_info_on_shutdown_;
142 int thread_pool_pthread_priority_;
Orion Hodsonad28f5e2018-10-17 09:08:17 +0100143 bool rwx_memory_allowed_;
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100144 ProfileSaverOptions profile_saver_options_;
145
146 JitOptions()
147 : use_jit_compilation_(false),
148 code_cache_initial_capacity_(0),
149 code_cache_max_capacity_(0),
150 compile_threshold_(0),
151 warmup_threshold_(0),
152 osr_threshold_(0),
153 priority_thread_weight_(0),
154 invoke_transition_weight_(0),
155 dump_info_on_shutdown_(false),
Orion Hodsonad28f5e2018-10-17 09:08:17 +0100156 thread_pool_pthread_priority_(kJitPoolThreadPthreadDefaultPriority),
157 rwx_memory_allowed_(true) {}
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100158
159 DISALLOW_COPY_AND_ASSIGN(JitOptions);
160};
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100161
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800162class Jit {
163 public:
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100164 static constexpr size_t kDefaultPriorityThreadWeightRatio = 1000;
Nicolas Geoffraybd553eb2016-04-28 13:56:04 +0100165 static constexpr size_t kDefaultInvokeTransitionWeightRatio = 500;
buzbee42a09cb02017-02-01 09:08:31 -0800166 // How frequently should the interpreter check to see if OSR compilation is ready.
167 static constexpr int16_t kJitRecheckOSRThreshold = 100;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800168
169 virtual ~Jit();
Orion Hodsonad28f5e2018-10-17 09:08:17 +0100170
171 // Create JIT itself.
172 static Jit* Create(JitCodeCache* code_cache, JitOptions* options);
173
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000174 bool CompileMethod(ArtMethod* method, Thread* self, bool osr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700175 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100176
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800177 const JitCodeCache* GetCodeCache() const {
Orion Hodsonad28f5e2018-10-17 09:08:17 +0100178 return code_cache_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800179 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100180
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800181 JitCodeCache* GetCodeCache() {
Orion Hodsonad28f5e2018-10-17 09:08:17 +0100182 return code_cache_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800183 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100184
Orion Hodsonad28f5e2018-10-17 09:08:17 +0100185 void CreateThreadPool();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800186 void DeleteThreadPool();
Orion Hodsonad28f5e2018-10-17 09:08:17 +0100187
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700188 // Dump interesting info: #methods compiled, code vs data size, compile / verify cumulative
189 // loggers.
Nicolas Geoffraya4f81542016-03-08 16:57:48 +0000190 void DumpInfo(std::ostream& os) REQUIRES(!lock_);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700191 // Add a timing logger to cumulative_timings_.
192 void AddTimingLogger(const TimingLogger& logger);
Nicolas Geoffraya4f81542016-03-08 16:57:48 +0000193
194 void AddMemoryUsage(ArtMethod* method, size_t bytes)
195 REQUIRES(!lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700196 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffraya4f81542016-03-08 16:57:48 +0000197
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100198 uint16_t OSRMethodThreshold() const {
199 return options_->GetOsrThreshold();
Mathieu Chartiera50f9cf2015-09-25 11:34:45 -0700200 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800201
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100202 uint16_t HotMethodThreshold() const {
203 return options_->GetCompileThreshold();
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100204 }
205
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100206 uint16_t WarmMethodThreshold() const {
207 return options_->GetWarmupThreshold();
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100208 }
209
210 uint16_t PriorityThreadWeight() const {
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100211 return options_->GetPriorityThreadWeight();
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100212 }
213
Calin Juravleffc87072016-04-20 14:22:09 +0100214 // Returns false if we only need to save profile information and not compile methods.
215 bool UseJitCompilation() const {
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100216 return options_->UseJitCompilation();
Calin Juravleffc87072016-04-20 14:22:09 +0100217 }
218
Calin Juravle138dbff2016-06-28 19:36:58 +0100219 bool GetSaveProfilingInfo() const {
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100220 return options_->GetSaveProfilingInfo();
Calin Juravleffc87072016-04-20 14:22:09 +0100221 }
222
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100223 // Wait until there is no more pending compilation tasks.
224 void WaitForCompilationToFinish(Thread* self);
225
226 // Profiling methods.
227 void MethodEntered(Thread* thread, ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700228 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100229
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100230 void AddSamples(Thread* self, ArtMethod* method, uint16_t samples, bool with_backedges)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700231 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100232
Mathieu Chartieref41db72016-10-25 15:08:01 -0700233 void InvokeVirtualOrInterface(ObjPtr<mirror::Object> this_object,
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100234 ArtMethod* caller,
235 uint32_t dex_pc,
236 ArtMethod* callee)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700237 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100238
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100239 void NotifyInterpreterToCompiledCodeTransition(Thread* self, ArtMethod* caller)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700240 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100241 AddSamples(self, caller, options_->GetInvokeTransitionWeight(), false);
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100242 }
243
244 void NotifyCompiledCodeToInterpreterTransition(Thread* self, ArtMethod* callee)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700245 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100246 AddSamples(self, callee, options_->GetInvokeTransitionWeight(), false);
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100247 }
248
Calin Juravlec90bc922016-02-24 10:13:09 +0000249 // Starts the profile saver if the config options allow profile recording.
250 // The profile will be stored in the specified `filename` and will contain
251 // information collected from the given `code_paths` (a set of dex locations).
Calin Juravlec90bc922016-02-24 10:13:09 +0000252 void StartProfileSaver(const std::string& filename,
Calin Juravle77651c42017-03-03 18:04:02 -0800253 const std::vector<std::string>& code_paths);
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000254 void StopProfileSaver();
Calin Juravle31f2c152015-10-23 17:56:15 +0100255
Calin Juravleb8e69992016-03-09 15:37:48 +0000256 void DumpForSigQuit(std::ostream& os) REQUIRES(!lock_);
Nicolas Geoffrayaee21562015-12-15 16:39:44 +0000257
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000258 static void NewTypeLoadedIfUsingJit(mirror::Class* type)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700259 REQUIRES_SHARED(Locks::mutator_lock_);
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000260
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000261 // If debug info generation is turned on then write the type information for types already loaded
262 // into the specified class linker to the jit debug interface,
263 void DumpTypeInfoForLoadedTypes(ClassLinker* linker);
264
Nicolas Geoffray35122442016-03-02 12:05:30 +0000265 // Return whether we should try to JIT compiled code as soon as an ArtMethod is invoked.
Siva Chandra05d24152016-01-05 17:43:17 -0800266 bool JitAtFirstUse();
267
Nicolas Geoffray35122442016-03-02 12:05:30 +0000268 // Return whether we can invoke JIT code for `method`.
269 bool CanInvokeCompiledCode(ArtMethod* method);
270
Calin Juravleb2771b42016-04-07 17:09:25 +0100271 // Return whether the runtime should use a priority thread weight when sampling.
Vladimir Markoa710d912017-09-12 14:56:07 +0100272 static bool ShouldUsePriorityThreadWeight(Thread* self);
Calin Juravleb2771b42016-04-07 17:09:25 +0100273
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000274 // If an OSR compiled version is available for `method`,
275 // and `dex_pc + dex_pc_offset` is an entry point of that compiled
276 // version, this method will jump to the compiled code, let it run,
277 // and return true afterwards. Return false otherwise.
278 static bool MaybeDoOnStackReplacement(Thread* thread,
279 ArtMethod* method,
280 uint32_t dex_pc,
281 int32_t dex_pc_offset,
282 JValue* result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700283 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000284
Nicolas Geoffraya7edd0d2018-11-07 03:18:16 +0000285 // Load the compiler library.
286 static bool LoadCompilerLibrary(std::string* error_msg);
Mathieu Chartier72918ea2016-03-24 11:07:06 -0700287
Andreas Gampef149b3f2016-11-16 14:58:24 -0800288 ThreadPool* GetThreadPool() const {
289 return thread_pool_.get();
290 }
291
Nicolas Geoffray021c5f22016-12-16 11:22:05 +0000292 // Stop the JIT by waiting for all current compilations and enqueued compilations to finish.
293 void Stop();
294
295 // Start JIT threads.
296 void Start();
297
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800298 private:
Orion Hodsonad28f5e2018-10-17 09:08:17 +0100299 Jit(JitCodeCache* code_cache, JitOptions* options);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800300
Orion Hodsonad28f5e2018-10-17 09:08:17 +0100301 static bool BindCompilerMethods(std::string* error_msg);
Mathieu Chartierc1bc4152016-03-24 17:22:52 -0700302
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800303 // JIT compiler
Mathieu Chartier72918ea2016-03-24 11:07:06 -0700304 static void* jit_library_handle_;
305 static void* jit_compiler_handle_;
306 static void* (*jit_load_)(bool*);
307 static void (*jit_unload_)(void*);
308 static bool (*jit_compile_method_)(void*, ArtMethod*, Thread*, bool);
309 static void (*jit_types_loaded_)(void*, mirror::Class**, size_t count);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800310
Nicolas Geoffraya7edd0d2018-11-07 03:18:16 +0000311 // Whether we should generate debug info when compiling.
312 bool generate_debug_info_;
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100313
Orion Hodsonad28f5e2018-10-17 09:08:17 +0100314 // JIT resources owned by runtime.
315 jit::JitCodeCache* const code_cache_;
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100316 const JitOptions* const options_;
317
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100318 std::unique_ptr<ThreadPool> thread_pool_;
319
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700320 // Performance monitoring.
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700321 CumulativeLogger cumulative_timings_;
Nicolas Geoffraya4f81542016-03-08 16:57:48 +0000322 Histogram<uint64_t> memory_use_ GUARDED_BY(lock_);
323 Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700324
Mathieu Chartier3130cdf2015-05-03 15:20:23 -0700325 DISALLOW_COPY_AND_ASSIGN(Jit);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800326};
327
Andreas Gampef149b3f2016-11-16 14:58:24 -0800328// Helper class to stop the JIT for a given scope. This will wait for the JIT to quiesce.
329class ScopedJitSuspend {
330 public:
331 ScopedJitSuspend();
332 ~ScopedJitSuspend();
333
334 private:
335 bool was_on_;
336};
337
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800338} // namespace jit
339} // namespace art
340
341#endif // ART_RUNTIME_JIT_JIT_H_