blob: 10f609481307370df233563081a93784ecc67b01 [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 Srbeckye3fc2d12018-11-30 13:41:14 +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:
David Srbeckye3fc2d12018-11-30 13:41:14 +0000126 // We add the sample in batches of size kJitSamplesBatchSize.
127 // This method rounds the threshold so that it is multiple of the batch size.
128 static uint32_t RoundUpThreshold(uint32_t threshold);
129
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100130 bool use_jit_compilation_;
131 size_t code_cache_initial_capacity_;
132 size_t code_cache_max_capacity_;
David Srbeckye3fc2d12018-11-30 13:41:14 +0000133 uint32_t compile_threshold_;
134 uint32_t warmup_threshold_;
135 uint32_t osr_threshold_;
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100136 uint16_t priority_thread_weight_;
137 uint16_t invoke_transition_weight_;
138 bool dump_info_on_shutdown_;
139 int thread_pool_pthread_priority_;
140 ProfileSaverOptions profile_saver_options_;
141
142 JitOptions()
143 : use_jit_compilation_(false),
144 code_cache_initial_capacity_(0),
145 code_cache_max_capacity_(0),
146 compile_threshold_(0),
147 warmup_threshold_(0),
148 osr_threshold_(0),
149 priority_thread_weight_(0),
150 invoke_transition_weight_(0),
151 dump_info_on_shutdown_(false),
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +0000152 thread_pool_pthread_priority_(kJitPoolThreadPthreadDefaultPriority) {}
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100153
154 DISALLOW_COPY_AND_ASSIGN(JitOptions);
155};
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100156
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800157class Jit {
158 public:
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100159 static constexpr size_t kDefaultPriorityThreadWeightRatio = 1000;
Nicolas Geoffraybd553eb2016-04-28 13:56:04 +0100160 static constexpr size_t kDefaultInvokeTransitionWeightRatio = 500;
buzbee42a09cb02017-02-01 09:08:31 -0800161 // How frequently should the interpreter check to see if OSR compilation is ready.
David Srbeckye3fc2d12018-11-30 13:41:14 +0000162 static constexpr int16_t kJitRecheckOSRThreshold = 101; // Prime number to avoid patterns.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800163
164 virtual ~Jit();
Orion Hodsonad28f5e2018-10-17 09:08:17 +0100165
166 // Create JIT itself.
167 static Jit* Create(JitCodeCache* code_cache, JitOptions* options);
168
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000169 bool CompileMethod(ArtMethod* method, Thread* self, bool osr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700170 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100171
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800172 const JitCodeCache* GetCodeCache() const {
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
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800176 JitCodeCache* GetCodeCache() {
Orion Hodsonad28f5e2018-10-17 09:08:17 +0100177 return code_cache_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800178 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100179
Orion Hodsonad28f5e2018-10-17 09:08:17 +0100180 void CreateThreadPool();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800181 void DeleteThreadPool();
Orion Hodsonad28f5e2018-10-17 09:08:17 +0100182
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700183 // Dump interesting info: #methods compiled, code vs data size, compile / verify cumulative
184 // loggers.
Nicolas Geoffraya4f81542016-03-08 16:57:48 +0000185 void DumpInfo(std::ostream& os) REQUIRES(!lock_);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700186 // Add a timing logger to cumulative_timings_.
187 void AddTimingLogger(const TimingLogger& logger);
Nicolas Geoffraya4f81542016-03-08 16:57:48 +0000188
189 void AddMemoryUsage(ArtMethod* method, size_t bytes)
190 REQUIRES(!lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700191 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffraya4f81542016-03-08 16:57:48 +0000192
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100193 uint16_t OSRMethodThreshold() const {
194 return options_->GetOsrThreshold();
Mathieu Chartiera50f9cf2015-09-25 11:34:45 -0700195 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800196
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100197 uint16_t HotMethodThreshold() const {
198 return options_->GetCompileThreshold();
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100199 }
200
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100201 uint16_t WarmMethodThreshold() const {
202 return options_->GetWarmupThreshold();
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100203 }
204
205 uint16_t PriorityThreadWeight() const {
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100206 return options_->GetPriorityThreadWeight();
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100207 }
208
Calin Juravleffc87072016-04-20 14:22:09 +0100209 // Returns false if we only need to save profile information and not compile methods.
210 bool UseJitCompilation() const {
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100211 return options_->UseJitCompilation();
Calin Juravleffc87072016-04-20 14:22:09 +0100212 }
213
Calin Juravle138dbff2016-06-28 19:36:58 +0100214 bool GetSaveProfilingInfo() const {
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100215 return options_->GetSaveProfilingInfo();
Calin Juravleffc87072016-04-20 14:22:09 +0100216 }
217
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100218 // Wait until there is no more pending compilation tasks.
219 void WaitForCompilationToFinish(Thread* self);
220
221 // Profiling methods.
222 void MethodEntered(Thread* thread, ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700223 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100224
David Srbeckye3fc2d12018-11-30 13:41:14 +0000225 ALWAYS_INLINE void AddSamples(Thread* self,
226 ArtMethod* method,
227 uint16_t samples,
228 bool with_backedges)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700229 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100230
Mathieu Chartieref41db72016-10-25 15:08:01 -0700231 void InvokeVirtualOrInterface(ObjPtr<mirror::Object> this_object,
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100232 ArtMethod* caller,
233 uint32_t dex_pc,
234 ArtMethod* callee)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700235 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100236
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100237 void NotifyInterpreterToCompiledCodeTransition(Thread* self, ArtMethod* caller)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700238 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100239 AddSamples(self, caller, options_->GetInvokeTransitionWeight(), false);
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100240 }
241
242 void NotifyCompiledCodeToInterpreterTransition(Thread* self, ArtMethod* callee)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700243 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100244 AddSamples(self, callee, options_->GetInvokeTransitionWeight(), false);
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100245 }
246
Calin Juravlec90bc922016-02-24 10:13:09 +0000247 // Starts the profile saver if the config options allow profile recording.
248 // The profile will be stored in the specified `filename` and will contain
249 // information collected from the given `code_paths` (a set of dex locations).
Calin Juravlec90bc922016-02-24 10:13:09 +0000250 void StartProfileSaver(const std::string& filename,
Calin Juravle77651c42017-03-03 18:04:02 -0800251 const std::vector<std::string>& code_paths);
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000252 void StopProfileSaver();
Calin Juravle31f2c152015-10-23 17:56:15 +0100253
Calin Juravleb8e69992016-03-09 15:37:48 +0000254 void DumpForSigQuit(std::ostream& os) REQUIRES(!lock_);
Nicolas Geoffrayaee21562015-12-15 16:39:44 +0000255
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000256 static void NewTypeLoadedIfUsingJit(mirror::Class* type)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700257 REQUIRES_SHARED(Locks::mutator_lock_);
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000258
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000259 // If debug info generation is turned on then write the type information for types already loaded
260 // into the specified class linker to the jit debug interface,
261 void DumpTypeInfoForLoadedTypes(ClassLinker* linker);
262
Nicolas Geoffray35122442016-03-02 12:05:30 +0000263 // Return whether we should try to JIT compiled code as soon as an ArtMethod is invoked.
Siva Chandra05d24152016-01-05 17:43:17 -0800264 bool JitAtFirstUse();
265
Nicolas Geoffray35122442016-03-02 12:05:30 +0000266 // Return whether we can invoke JIT code for `method`.
267 bool CanInvokeCompiledCode(ArtMethod* method);
268
Calin Juravleb2771b42016-04-07 17:09:25 +0100269 // Return whether the runtime should use a priority thread weight when sampling.
Vladimir Markoa710d912017-09-12 14:56:07 +0100270 static bool ShouldUsePriorityThreadWeight(Thread* self);
Calin Juravleb2771b42016-04-07 17:09:25 +0100271
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000272 // If an OSR compiled version is available for `method`,
273 // and `dex_pc + dex_pc_offset` is an entry point of that compiled
274 // version, this method will jump to the compiled code, let it run,
275 // and return true afterwards. Return false otherwise.
276 static bool MaybeDoOnStackReplacement(Thread* thread,
277 ArtMethod* method,
278 uint32_t dex_pc,
279 int32_t dex_pc_offset,
280 JValue* result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700281 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000282
Nicolas Geoffraya7edd0d2018-11-07 03:18:16 +0000283 // Load the compiler library.
284 static bool LoadCompilerLibrary(std::string* error_msg);
Mathieu Chartier72918ea2016-03-24 11:07:06 -0700285
Andreas Gampef149b3f2016-11-16 14:58:24 -0800286 ThreadPool* GetThreadPool() const {
287 return thread_pool_.get();
288 }
289
Nicolas Geoffray021c5f22016-12-16 11:22:05 +0000290 // Stop the JIT by waiting for all current compilations and enqueued compilations to finish.
291 void Stop();
292
293 // Start JIT threads.
294 void Start();
295
Nicolas Geoffrayce9ed362018-11-29 03:19:28 +0000296 // Transition to a child state.
297 void PostForkChildAction(bool is_zygote);
298
299 // Prepare for forking.
300 void PreZygoteFork();
301
302 // Adjust state after forking.
303 void PostZygoteFork();
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +0000304
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800305 private:
Orion Hodsonad28f5e2018-10-17 09:08:17 +0100306 Jit(JitCodeCache* code_cache, JitOptions* options);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800307
David Srbeckye3fc2d12018-11-30 13:41:14 +0000308 // Compile the method if the number of samples passes a threshold.
309 // Returns false if we can not compile now - don't increment the counter and retry later.
310 bool MaybeCompileMethod(Thread* self,
311 ArtMethod* method,
312 uint32_t old_count,
313 uint32_t new_count,
314 bool with_backedges)
315 REQUIRES_SHARED(Locks::mutator_lock_);
316
Orion Hodsonad28f5e2018-10-17 09:08:17 +0100317 static bool BindCompilerMethods(std::string* error_msg);
Mathieu Chartierc1bc4152016-03-24 17:22:52 -0700318
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800319 // JIT compiler
Mathieu Chartier72918ea2016-03-24 11:07:06 -0700320 static void* jit_library_handle_;
321 static void* jit_compiler_handle_;
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +0000322 static void* (*jit_load_)(void);
Mathieu Chartier72918ea2016-03-24 11:07:06 -0700323 static void (*jit_unload_)(void*);
324 static bool (*jit_compile_method_)(void*, ArtMethod*, Thread*, bool);
325 static void (*jit_types_loaded_)(void*, mirror::Class**, size_t count);
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +0000326 static void (*jit_update_options_)(void*);
327 static bool (*jit_generate_debug_info_)(void*);
328 template <typename T> static bool LoadSymbol(T*, const char* symbol, std::string* error_msg);
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100329
Orion Hodsonad28f5e2018-10-17 09:08:17 +0100330 // JIT resources owned by runtime.
331 jit::JitCodeCache* const code_cache_;
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100332 const JitOptions* const options_;
333
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100334 std::unique_ptr<ThreadPool> thread_pool_;
335
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700336 // Performance monitoring.
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700337 CumulativeLogger cumulative_timings_;
Nicolas Geoffraya4f81542016-03-08 16:57:48 +0000338 Histogram<uint64_t> memory_use_ GUARDED_BY(lock_);
339 Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700340
Mathieu Chartier3130cdf2015-05-03 15:20:23 -0700341 DISALLOW_COPY_AND_ASSIGN(Jit);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800342};
343
Andreas Gampef149b3f2016-11-16 14:58:24 -0800344// Helper class to stop the JIT for a given scope. This will wait for the JIT to quiesce.
345class ScopedJitSuspend {
346 public:
347 ScopedJitSuspend();
348 ~ScopedJitSuspend();
349
350 private:
351 bool was_on_;
352};
353
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800354} // namespace jit
355} // namespace art
356
357#endif // ART_RUNTIME_JIT_JIT_H_