blob: 15cdacba8437b5814b61f9eba31baad739636268 [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;
Nicolas Geoffraydc2fbb62019-04-11 22:55:50 +010032class OatDexFile;
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;
Nicolas Geoffray47b95802018-05-16 15:42:17 +010048// At what priority to schedule jit threads. 9 is the lowest foreground priority on device.
49// See android/os/Process.java.
50static constexpr int kJitPoolThreadPthreadDefaultPriority = 9;
David Srbeckye3fc2d12018-11-30 13:41:14 +000051static constexpr uint32_t kJitSamplesBatchSize = 32; // Must be power of 2.
Nicolas Geoffray47b95802018-05-16 15:42:17 +010052
53class JitOptions {
54 public:
55 static JitOptions* CreateFromRuntimeArguments(const RuntimeArgumentMap& options);
56
57 uint16_t GetCompileThreshold() const {
58 return compile_threshold_;
59 }
60
61 uint16_t GetWarmupThreshold() const {
62 return warmup_threshold_;
63 }
64
65 uint16_t GetOsrThreshold() const {
66 return osr_threshold_;
67 }
68
69 uint16_t GetPriorityThreadWeight() const {
70 return priority_thread_weight_;
71 }
72
73 uint16_t GetInvokeTransitionWeight() const {
74 return invoke_transition_weight_;
75 }
76
77 size_t GetCodeCacheInitialCapacity() const {
78 return code_cache_initial_capacity_;
79 }
80
81 size_t GetCodeCacheMaxCapacity() const {
82 return code_cache_max_capacity_;
83 }
84
85 bool DumpJitInfoOnShutdown() const {
86 return dump_info_on_shutdown_;
87 }
88
89 const ProfileSaverOptions& GetProfileSaverOptions() const {
90 return profile_saver_options_;
91 }
92
93 bool GetSaveProfilingInfo() const {
94 return profile_saver_options_.IsEnabled();
95 }
96
97 int GetThreadPoolPthreadPriority() const {
98 return thread_pool_pthread_priority_;
99 }
100
101 bool UseJitCompilation() const {
102 return use_jit_compilation_;
103 }
104
105 void SetUseJitCompilation(bool b) {
106 use_jit_compilation_ = b;
107 }
108
109 void SetSaveProfilingInfo(bool save_profiling_info) {
110 profile_saver_options_.SetEnabled(save_profiling_info);
111 }
112
113 void SetWaitForJitNotificationsToSaveProfile(bool value) {
114 profile_saver_options_.SetWaitForJitNotificationsToSave(value);
115 }
116
117 void SetProfileAOTCode(bool value) {
118 profile_saver_options_.SetProfileAOTCode(value);
119 }
120
121 void SetJitAtFirstUse() {
122 use_jit_compilation_ = true;
123 compile_threshold_ = 0;
124 }
125
126 private:
David Srbeckye3fc2d12018-11-30 13:41:14 +0000127 // We add the sample in batches of size kJitSamplesBatchSize.
128 // This method rounds the threshold so that it is multiple of the batch size.
129 static uint32_t RoundUpThreshold(uint32_t threshold);
130
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100131 bool use_jit_compilation_;
132 size_t code_cache_initial_capacity_;
133 size_t code_cache_max_capacity_;
David Srbeckye3fc2d12018-11-30 13:41:14 +0000134 uint32_t compile_threshold_;
135 uint32_t warmup_threshold_;
136 uint32_t osr_threshold_;
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100137 uint16_t priority_thread_weight_;
138 uint16_t invoke_transition_weight_;
139 bool dump_info_on_shutdown_;
140 int thread_pool_pthread_priority_;
141 ProfileSaverOptions profile_saver_options_;
142
143 JitOptions()
144 : use_jit_compilation_(false),
145 code_cache_initial_capacity_(0),
146 code_cache_max_capacity_(0),
147 compile_threshold_(0),
148 warmup_threshold_(0),
149 osr_threshold_(0),
150 priority_thread_weight_(0),
151 invoke_transition_weight_(0),
152 dump_info_on_shutdown_(false),
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +0000153 thread_pool_pthread_priority_(kJitPoolThreadPthreadDefaultPriority) {}
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100154
155 DISALLOW_COPY_AND_ASSIGN(JitOptions);
156};
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100157
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800158class Jit {
159 public:
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100160 static constexpr size_t kDefaultPriorityThreadWeightRatio = 1000;
Nicolas Geoffraybd553eb2016-04-28 13:56:04 +0100161 static constexpr size_t kDefaultInvokeTransitionWeightRatio = 500;
buzbee42a09cb02017-02-01 09:08:31 -0800162 // How frequently should the interpreter check to see if OSR compilation is ready.
David Srbeckye3fc2d12018-11-30 13:41:14 +0000163 static constexpr int16_t kJitRecheckOSRThreshold = 101; // Prime number to avoid patterns.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800164
165 virtual ~Jit();
Orion Hodsonad28f5e2018-10-17 09:08:17 +0100166
167 // Create JIT itself.
168 static Jit* Create(JitCodeCache* code_cache, JitOptions* options);
169
Nicolas Geoffray075456e2018-12-14 08:54:21 +0000170 bool CompileMethod(ArtMethod* method, Thread* self, bool baseline, bool osr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700171 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100172
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800173 const JitCodeCache* GetCodeCache() const {
Orion Hodsonad28f5e2018-10-17 09:08:17 +0100174 return code_cache_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800175 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100176
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800177 JitCodeCache* GetCodeCache() {
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
Orion Hodsonad28f5e2018-10-17 09:08:17 +0100181 void CreateThreadPool();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800182 void DeleteThreadPool();
Mathieu Chartier93c21ba2018-12-10 13:08:30 -0800183 void WaitForWorkersToBeCreated();
Orion Hodsonad28f5e2018-10-17 09:08:17 +0100184
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700185 // Dump interesting info: #methods compiled, code vs data size, compile / verify cumulative
186 // loggers.
Nicolas Geoffraya4f81542016-03-08 16:57:48 +0000187 void DumpInfo(std::ostream& os) REQUIRES(!lock_);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700188 // Add a timing logger to cumulative_timings_.
189 void AddTimingLogger(const TimingLogger& logger);
Nicolas Geoffraya4f81542016-03-08 16:57:48 +0000190
191 void AddMemoryUsage(ArtMethod* method, size_t bytes)
192 REQUIRES(!lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700193 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffraya4f81542016-03-08 16:57:48 +0000194
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100195 uint16_t OSRMethodThreshold() const {
196 return options_->GetOsrThreshold();
Mathieu Chartiera50f9cf2015-09-25 11:34:45 -0700197 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800198
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100199 uint16_t HotMethodThreshold() const {
200 return options_->GetCompileThreshold();
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100201 }
202
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100203 uint16_t WarmMethodThreshold() const {
204 return options_->GetWarmupThreshold();
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100205 }
206
207 uint16_t PriorityThreadWeight() const {
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100208 return options_->GetPriorityThreadWeight();
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100209 }
210
Calin Juravleffc87072016-04-20 14:22:09 +0100211 // Returns false if we only need to save profile information and not compile methods.
212 bool UseJitCompilation() const {
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100213 return options_->UseJitCompilation();
Calin Juravleffc87072016-04-20 14:22:09 +0100214 }
215
Calin Juravle138dbff2016-06-28 19:36:58 +0100216 bool GetSaveProfilingInfo() const {
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100217 return options_->GetSaveProfilingInfo();
Calin Juravleffc87072016-04-20 14:22:09 +0100218 }
219
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100220 // Wait until there is no more pending compilation tasks.
221 void WaitForCompilationToFinish(Thread* self);
222
223 // Profiling methods.
224 void MethodEntered(Thread* thread, ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700225 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100226
David Srbeckye3fc2d12018-11-30 13:41:14 +0000227 ALWAYS_INLINE void AddSamples(Thread* self,
228 ArtMethod* method,
229 uint16_t samples,
230 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
Nicolas Geoffrayce9ed362018-11-29 03:19:28 +0000298 // Transition to a child state.
Nicolas Geoffray0d54cfb2019-05-03 09:13:52 +0100299 void PostForkChildAction(bool is_system_server, bool is_zygote);
Nicolas Geoffrayce9ed362018-11-29 03:19:28 +0000300
301 // Prepare for forking.
302 void PreZygoteFork();
303
304 // Adjust state after forking.
305 void PostZygoteFork();
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +0000306
Nicolas Geoffrayde1b2a22019-02-27 09:10:57 +0000307 // In case the boot classpath is not fully AOTed, add methods from the boot profile to the
308 // compilation queue.
309 void AddNonAotBootMethodsToQueue(Thread* self);
310
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800311 private:
Orion Hodsonad28f5e2018-10-17 09:08:17 +0100312 Jit(JitCodeCache* code_cache, JitOptions* options);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800313
David Srbeckye3fc2d12018-11-30 13:41:14 +0000314 // Compile the method if the number of samples passes a threshold.
315 // Returns false if we can not compile now - don't increment the counter and retry later.
316 bool MaybeCompileMethod(Thread* self,
317 ArtMethod* method,
318 uint32_t old_count,
319 uint32_t new_count,
320 bool with_backedges)
321 REQUIRES_SHARED(Locks::mutator_lock_);
322
Orion Hodsonad28f5e2018-10-17 09:08:17 +0100323 static bool BindCompilerMethods(std::string* error_msg);
Mathieu Chartierc1bc4152016-03-24 17:22:52 -0700324
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800325 // JIT compiler
Mathieu Chartier72918ea2016-03-24 11:07:06 -0700326 static void* jit_library_handle_;
327 static void* jit_compiler_handle_;
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +0000328 static void* (*jit_load_)(void);
Mathieu Chartier72918ea2016-03-24 11:07:06 -0700329 static void (*jit_unload_)(void*);
Nicolas Geoffray075456e2018-12-14 08:54:21 +0000330 static bool (*jit_compile_method_)(void*, ArtMethod*, Thread*, bool, bool);
Mathieu Chartier72918ea2016-03-24 11:07:06 -0700331 static void (*jit_types_loaded_)(void*, mirror::Class**, size_t count);
Nicolas Geoffrayc9de61c2018-11-27 17:34:31 +0000332 static void (*jit_update_options_)(void*);
333 static bool (*jit_generate_debug_info_)(void*);
334 template <typename T> static bool LoadSymbol(T*, const char* symbol, std::string* error_msg);
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100335
Orion Hodsonad28f5e2018-10-17 09:08:17 +0100336 // JIT resources owned by runtime.
337 jit::JitCodeCache* const code_cache_;
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100338 const JitOptions* const options_;
339
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100340 std::unique_ptr<ThreadPool> thread_pool_;
Nicolas Geoffraydc2fbb62019-04-11 22:55:50 +0100341 std::vector<std::unique_ptr<OatDexFile>> type_lookup_tables_;
Nicolas Geoffray47b95802018-05-16 15:42:17 +0100342
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700343 // Performance monitoring.
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700344 CumulativeLogger cumulative_timings_;
Nicolas Geoffraya4f81542016-03-08 16:57:48 +0000345 Histogram<uint64_t> memory_use_ GUARDED_BY(lock_);
346 Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700347
Mathieu Chartier3130cdf2015-05-03 15:20:23 -0700348 DISALLOW_COPY_AND_ASSIGN(Jit);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800349};
350
Andreas Gampef149b3f2016-11-16 14:58:24 -0800351// Helper class to stop the JIT for a given scope. This will wait for the JIT to quiesce.
352class ScopedJitSuspend {
353 public:
354 ScopedJitSuspend();
355 ~ScopedJitSuspend();
356
357 private:
358 bool was_on_;
359};
360
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800361} // namespace jit
362} // namespace art
363
364#endif // ART_RUNTIME_JIT_JIT_H_