blob: 2027719bd9524e063d627ca5224c0e9ffe0578ba [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;
David Srbeckydb94f2b2018-11-09 12:58:28 +000050static constexpr uint32_t kJitSamplesBatchSize = 32; // Must be power of 2.
Nicolas Geoffray47b95802018-05-16 15:42:17 +010051
52class JitOptions {
53 public:
54 static JitOptions* CreateFromRuntimeArguments(const RuntimeArgumentMap& options);
55
56 uint16_t GetCompileThreshold() const {
57 return compile_threshold_;
58 }
59
60 uint16_t GetWarmupThreshold() const {
61 return warmup_threshold_;
62 }
63
64 uint16_t GetOsrThreshold() const {
65 return osr_threshold_;
66 }
67
68 uint16_t GetPriorityThreadWeight() const {
69 return priority_thread_weight_;
70 }
71
72 uint16_t GetInvokeTransitionWeight() const {
73 return invoke_transition_weight_;
74 }
75
76 size_t GetCodeCacheInitialCapacity() const {
77 return code_cache_initial_capacity_;
78 }
79
80 size_t GetCodeCacheMaxCapacity() const {
81 return code_cache_max_capacity_;
82 }
83
84 bool DumpJitInfoOnShutdown() const {
85 return dump_info_on_shutdown_;
86 }
87
88 const ProfileSaverOptions& GetProfileSaverOptions() const {
89 return profile_saver_options_;
90 }
91
92 bool GetSaveProfilingInfo() const {
93 return profile_saver_options_.IsEnabled();
94 }
95
96 int GetThreadPoolPthreadPriority() const {
97 return thread_pool_pthread_priority_;
98 }
99
100 bool UseJitCompilation() const {
101 return use_jit_compilation_;
102 }
103
104 void SetUseJitCompilation(bool b) {
105 use_jit_compilation_ = b;
106 }
107
108 void SetSaveProfilingInfo(bool save_profiling_info) {
109 profile_saver_options_.SetEnabled(save_profiling_info);
110 }
111
112 void SetWaitForJitNotificationsToSaveProfile(bool value) {
113 profile_saver_options_.SetWaitForJitNotificationsToSave(value);
114 }
115
116 void SetProfileAOTCode(bool value) {
117 profile_saver_options_.SetProfileAOTCode(value);
118 }
119
120 void SetJitAtFirstUse() {
121 use_jit_compilation_ = true;
122 compile_threshold_ = 0;
123 }
124
125 private:
126 bool use_jit_compilation_;
127 size_t code_cache_initial_capacity_;
128 size_t code_cache_max_capacity_;
129 uint16_t compile_threshold_;
130 uint16_t warmup_threshold_;
131 uint16_t osr_threshold_;
132 uint16_t priority_thread_weight_;
133 uint16_t invoke_transition_weight_;
134 bool dump_info_on_shutdown_;
135 int thread_pool_pthread_priority_;
136 ProfileSaverOptions profile_saver_options_;
137
138 JitOptions()
139 : use_jit_compilation_(false),
140 code_cache_initial_capacity_(0),
141 code_cache_max_capacity_(0),
142 compile_threshold_(0),
143 warmup_threshold_(0),
144 osr_threshold_(0),
145 priority_thread_weight_(0),
146 invoke_transition_weight_(0),
147 dump_info_on_shutdown_(false),
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +0000148 thread_pool_pthread_priority_(kJitPoolThreadPthreadDefaultPriority) {}
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100149
150 DISALLOW_COPY_AND_ASSIGN(JitOptions);
151};
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100152
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800153class Jit {
154 public:
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100155 static constexpr size_t kDefaultPriorityThreadWeightRatio = 1000;
Nicolas Geoffraybd553eb2016-04-28 13:56:04 +0100156 static constexpr size_t kDefaultInvokeTransitionWeightRatio = 500;
buzbee42a09cb02017-02-01 09:08:31 -0800157 // How frequently should the interpreter check to see if OSR compilation is ready.
158 static constexpr int16_t kJitRecheckOSRThreshold = 100;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800159
160 virtual ~Jit();
Orion Hodsonad28f5e2018-10-17 09:08:17 +0100161
162 // Create JIT itself.
163 static Jit* Create(JitCodeCache* code_cache, JitOptions* options);
164
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000165 bool CompileMethod(ArtMethod* method, Thread* self, bool osr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700166 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100167
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800168 const JitCodeCache* GetCodeCache() const {
Orion Hodsonad28f5e2018-10-17 09:08:17 +0100169 return code_cache_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800170 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100171
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800172 JitCodeCache* GetCodeCache() {
Orion Hodsonad28f5e2018-10-17 09:08:17 +0100173 return code_cache_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800174 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100175
Orion Hodsonad28f5e2018-10-17 09:08:17 +0100176 void CreateThreadPool();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800177 void DeleteThreadPool();
Orion Hodsonad28f5e2018-10-17 09:08:17 +0100178
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700179 // Dump interesting info: #methods compiled, code vs data size, compile / verify cumulative
180 // loggers.
Nicolas Geoffraya4f81542016-03-08 16:57:48 +0000181 void DumpInfo(std::ostream& os) REQUIRES(!lock_);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700182 // Add a timing logger to cumulative_timings_.
183 void AddTimingLogger(const TimingLogger& logger);
Nicolas Geoffraya4f81542016-03-08 16:57:48 +0000184
185 void AddMemoryUsage(ArtMethod* method, size_t bytes)
186 REQUIRES(!lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700187 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffraya4f81542016-03-08 16:57:48 +0000188
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100189 uint16_t OSRMethodThreshold() const {
190 return options_->GetOsrThreshold();
Mathieu Chartiera50f9cf2015-09-25 11:34:45 -0700191 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800192
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100193 uint16_t HotMethodThreshold() const {
194 return options_->GetCompileThreshold();
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100195 }
196
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100197 uint16_t WarmMethodThreshold() const {
198 return options_->GetWarmupThreshold();
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100199 }
200
201 uint16_t PriorityThreadWeight() const {
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100202 return options_->GetPriorityThreadWeight();
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100203 }
204
Calin Juravleffc87072016-04-20 14:22:09 +0100205 // Returns false if we only need to save profile information and not compile methods.
206 bool UseJitCompilation() const {
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100207 return options_->UseJitCompilation();
Calin Juravleffc87072016-04-20 14:22:09 +0100208 }
209
Calin Juravle138dbff2016-06-28 19:36:58 +0100210 bool GetSaveProfilingInfo() const {
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100211 return options_->GetSaveProfilingInfo();
Calin Juravleffc87072016-04-20 14:22:09 +0100212 }
213
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100214 // Wait until there is no more pending compilation tasks.
215 void WaitForCompilationToFinish(Thread* self);
216
217 // Profiling methods.
218 void MethodEntered(Thread* thread, ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700219 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100220
David Srbeckydb94f2b2018-11-09 12:58:28 +0000221 ALWAYS_INLINE void AddSamples(Thread* self,
222 ArtMethod* method,
223 uint16_t samples,
224 bool with_backedges)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700225 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100226
Mathieu Chartieref41db72016-10-25 15:08:01 -0700227 void InvokeVirtualOrInterface(ObjPtr<mirror::Object> this_object,
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100228 ArtMethod* caller,
229 uint32_t dex_pc,
230 ArtMethod* callee)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700231 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100232
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100233 void NotifyInterpreterToCompiledCodeTransition(Thread* self, ArtMethod* caller)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700234 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100235 AddSamples(self, caller, options_->GetInvokeTransitionWeight(), false);
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100236 }
237
238 void NotifyCompiledCodeToInterpreterTransition(Thread* self, ArtMethod* callee)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700239 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100240 AddSamples(self, callee, options_->GetInvokeTransitionWeight(), false);
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100241 }
242
Calin Juravlec90bc922016-02-24 10:13:09 +0000243 // Starts the profile saver if the config options allow profile recording.
244 // The profile will be stored in the specified `filename` and will contain
245 // information collected from the given `code_paths` (a set of dex locations).
Calin Juravlec90bc922016-02-24 10:13:09 +0000246 void StartProfileSaver(const std::string& filename,
Calin Juravle77651c42017-03-03 18:04:02 -0800247 const std::vector<std::string>& code_paths);
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000248 void StopProfileSaver();
Calin Juravle31f2c152015-10-23 17:56:15 +0100249
Calin Juravleb8e69992016-03-09 15:37:48 +0000250 void DumpForSigQuit(std::ostream& os) REQUIRES(!lock_);
Nicolas Geoffrayaee21562015-12-15 16:39:44 +0000251
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000252 static void NewTypeLoadedIfUsingJit(mirror::Class* type)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700253 REQUIRES_SHARED(Locks::mutator_lock_);
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000254
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000255 // If debug info generation is turned on then write the type information for types already loaded
256 // into the specified class linker to the jit debug interface,
257 void DumpTypeInfoForLoadedTypes(ClassLinker* linker);
258
Nicolas Geoffray35122442016-03-02 12:05:30 +0000259 // Return whether we should try to JIT compiled code as soon as an ArtMethod is invoked.
Siva Chandra05d24152016-01-05 17:43:17 -0800260 bool JitAtFirstUse();
261
Nicolas Geoffray35122442016-03-02 12:05:30 +0000262 // Return whether we can invoke JIT code for `method`.
263 bool CanInvokeCompiledCode(ArtMethod* method);
264
Calin Juravleb2771b42016-04-07 17:09:25 +0100265 // Return whether the runtime should use a priority thread weight when sampling.
Vladimir Markoa710d912017-09-12 14:56:07 +0100266 static bool ShouldUsePriorityThreadWeight(Thread* self);
Calin Juravleb2771b42016-04-07 17:09:25 +0100267
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000268 // If an OSR compiled version is available for `method`,
269 // and `dex_pc + dex_pc_offset` is an entry point of that compiled
270 // version, this method will jump to the compiled code, let it run,
271 // and return true afterwards. Return false otherwise.
272 static bool MaybeDoOnStackReplacement(Thread* thread,
273 ArtMethod* method,
274 uint32_t dex_pc,
275 int32_t dex_pc_offset,
276 JValue* result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700277 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000278
Nicolas Geoffraya7edd0d2018-11-07 03:18:16 +0000279 // Load the compiler library.
280 static bool LoadCompilerLibrary(std::string* error_msg);
Mathieu Chartier72918ea2016-03-24 11:07:06 -0700281
Andreas Gampef149b3f2016-11-16 14:58:24 -0800282 ThreadPool* GetThreadPool() const {
283 return thread_pool_.get();
284 }
285
Nicolas Geoffray021c5f22016-12-16 11:22:05 +0000286 // Stop the JIT by waiting for all current compilations and enqueued compilations to finish.
287 void Stop();
288
289 // Start JIT threads.
290 void Start();
291
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +0000292 // Transition to a zygote child state.
293 void PostForkChildAction();
294
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800295 private:
Orion Hodsonad28f5e2018-10-17 09:08:17 +0100296 Jit(JitCodeCache* code_cache, JitOptions* options);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800297
David Srbeckydb94f2b2018-11-09 12:58:28 +0000298 // Compile the method if the number of samples passes a threshold.
299 // Returns false if we can not compile now - don't increment the counter and retry later.
300 bool MaybeCompileMethod(Thread* self,
301 ArtMethod* method,
302 uint32_t old_count,
303 uint32_t new_count,
304 bool with_backedges)
305 REQUIRES_SHARED(Locks::mutator_lock_);
306
Orion Hodsonad28f5e2018-10-17 09:08:17 +0100307 static bool BindCompilerMethods(std::string* error_msg);
Mathieu Chartierc1bc4152016-03-24 17:22:52 -0700308
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800309 // JIT compiler
Mathieu Chartier72918ea2016-03-24 11:07:06 -0700310 static void* jit_library_handle_;
311 static void* jit_compiler_handle_;
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +0000312 static void* (*jit_load_)(void);
Mathieu Chartier72918ea2016-03-24 11:07:06 -0700313 static void (*jit_unload_)(void*);
314 static bool (*jit_compile_method_)(void*, ArtMethod*, Thread*, bool);
315 static void (*jit_types_loaded_)(void*, mirror::Class**, size_t count);
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +0000316 static void (*jit_update_options_)(void*);
317 static bool (*jit_generate_debug_info_)(void*);
318 template <typename T> static bool LoadSymbol(T*, const char* symbol, std::string* error_msg);
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100319
Orion Hodsonad28f5e2018-10-17 09:08:17 +0100320 // JIT resources owned by runtime.
321 jit::JitCodeCache* const code_cache_;
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100322 const JitOptions* const options_;
323
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100324 std::unique_ptr<ThreadPool> thread_pool_;
325
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700326 // Performance monitoring.
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700327 CumulativeLogger cumulative_timings_;
Nicolas Geoffraya4f81542016-03-08 16:57:48 +0000328 Histogram<uint64_t> memory_use_ GUARDED_BY(lock_);
329 Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700330
Mathieu Chartier3130cdf2015-05-03 15:20:23 -0700331 DISALLOW_COPY_AND_ASSIGN(Jit);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800332};
333
Andreas Gampef149b3f2016-11-16 14:58:24 -0800334// Helper class to stop the JIT for a given scope. This will wait for the JIT to quiesce.
335class ScopedJitSuspend {
336 public:
337 ScopedJitSuspend();
338 ~ScopedJitSuspend();
339
340 private:
341 bool was_on_;
342};
343
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800344} // namespace jit
345} // namespace art
346
347#endif // ART_RUNTIME_JIT_JIT_H_