blob: 05c390590bf20bc5986fb44f116c55be11d83bcd [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/arena_allocator.h"
21#include "base/histogram-inl.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080022#include "base/macros.h"
23#include "base/mutex.h"
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070024#include "base/timing_logger.h"
Mathieu Chartieref41db72016-10-25 15:08:01 -070025#include "jit/profile_saver_options.h"
26#include "obj_ptr.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080027#include "object_callbacks.h"
Calin Juravle31f2c152015-10-23 17:56:15 +010028#include "offline_profiling_info.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080029#include "thread_pool.h"
30
31namespace art {
32
Mathieu Chartiere401d142015-04-22 13:56:20 -070033class ArtMethod;
David Sehr9323e6e2016-09-13 08:58:35 -070034class ClassLinker;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080035struct RuntimeArgumentMap;
Vladimir Marko3a21e382016-09-02 12:38:38 +010036union JValue;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080037
David Sehr709b0702016-10-13 09:12:37 -070038namespace mirror {
39class Object;
40class Class;
41} // namespace mirror
42
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080043namespace jit {
44
45class JitCodeCache;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080046class JitOptions;
47
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +010048static constexpr int16_t kJitCheckForOSR = -1;
49static constexpr int16_t kJitHotnessDisabled = -2;
50
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080051class Jit {
52 public:
53 static constexpr bool kStressMode = kIsDebugBuild;
Nicolas Geoffray83f080a2016-03-08 16:50:21 +000054 static constexpr size_t kDefaultCompileThreshold = kStressMode ? 2 : 10000;
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +010055 static constexpr size_t kDefaultPriorityThreadWeightRatio = 1000;
Nicolas Geoffraybd553eb2016-04-28 13:56:04 +010056 static constexpr size_t kDefaultInvokeTransitionWeightRatio = 500;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080057
58 virtual ~Jit();
59 static Jit* Create(JitOptions* options, std::string* error_msg);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +000060 bool CompileMethod(ArtMethod* method, Thread* self, bool osr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070061 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080062 void CreateThreadPool();
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +010063
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080064 const JitCodeCache* GetCodeCache() const {
65 return code_cache_.get();
66 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +010067
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080068 JitCodeCache* GetCodeCache() {
69 return code_cache_.get();
70 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +010071
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080072 void DeleteThreadPool();
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070073 // Dump interesting info: #methods compiled, code vs data size, compile / verify cumulative
74 // loggers.
Nicolas Geoffraya4f81542016-03-08 16:57:48 +000075 void DumpInfo(std::ostream& os) REQUIRES(!lock_);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070076 // Add a timing logger to cumulative_timings_.
77 void AddTimingLogger(const TimingLogger& logger);
Nicolas Geoffraya4f81542016-03-08 16:57:48 +000078
79 void AddMemoryUsage(ArtMethod* method, size_t bytes)
80 REQUIRES(!lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070081 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffraya4f81542016-03-08 16:57:48 +000082
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +010083 size_t OSRMethodThreshold() const {
84 return osr_method_threshold_;
Mathieu Chartiera50f9cf2015-09-25 11:34:45 -070085 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080086
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +010087 size_t HotMethodThreshold() const {
88 return hot_method_threshold_;
89 }
90
91 size_t WarmMethodThreshold() const {
92 return warm_method_threshold_;
93 }
94
95 uint16_t PriorityThreadWeight() const {
96 return priority_thread_weight_;
97 }
98
Calin Juravleffc87072016-04-20 14:22:09 +010099 // Returns false if we only need to save profile information and not compile methods.
100 bool UseJitCompilation() const {
101 return use_jit_compilation_;
102 }
103
Calin Juravle138dbff2016-06-28 19:36:58 +0100104 bool GetSaveProfilingInfo() const {
105 return profile_saver_options_.IsEnabled();
Calin Juravleffc87072016-04-20 14:22:09 +0100106 }
107
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100108 // Wait until there is no more pending compilation tasks.
109 void WaitForCompilationToFinish(Thread* self);
110
111 // Profiling methods.
112 void MethodEntered(Thread* thread, ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700113 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100114
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100115 void AddSamples(Thread* self, ArtMethod* method, uint16_t samples, bool with_backedges)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700116 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100117
Mathieu Chartieref41db72016-10-25 15:08:01 -0700118 void InvokeVirtualOrInterface(ObjPtr<mirror::Object> this_object,
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100119 ArtMethod* caller,
120 uint32_t dex_pc,
121 ArtMethod* callee)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700122 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100123
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100124 void NotifyInterpreterToCompiledCodeTransition(Thread* self, ArtMethod* caller)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700125 REQUIRES_SHARED(Locks::mutator_lock_) {
Calin Juravle155ff3d2016-04-27 14:14:58 +0100126 AddSamples(self, caller, invoke_transition_weight_, false);
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100127 }
128
129 void NotifyCompiledCodeToInterpreterTransition(Thread* self, ArtMethod* callee)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700130 REQUIRES_SHARED(Locks::mutator_lock_) {
Calin Juravle155ff3d2016-04-27 14:14:58 +0100131 AddSamples(self, callee, invoke_transition_weight_, false);
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100132 }
133
Calin Juravlec90bc922016-02-24 10:13:09 +0000134 // Starts the profile saver if the config options allow profile recording.
135 // The profile will be stored in the specified `filename` and will contain
136 // information collected from the given `code_paths` (a set of dex locations).
137 // The `foreign_dex_profile_path` is the path where the saver will put the
138 // profile markers for loaded dex files which are not owned by the application.
139 // The `app_dir` is the application directory and is used to decide which
140 // dex files belong to the application.
141 void StartProfileSaver(const std::string& filename,
142 const std::vector<std::string>& code_paths,
143 const std::string& foreign_dex_profile_path,
144 const std::string& app_dir);
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000145 void StopProfileSaver();
Calin Juravle31f2c152015-10-23 17:56:15 +0100146
Calin Juravleb8e69992016-03-09 15:37:48 +0000147 void DumpForSigQuit(std::ostream& os) REQUIRES(!lock_);
Nicolas Geoffrayaee21562015-12-15 16:39:44 +0000148
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000149 static void NewTypeLoadedIfUsingJit(mirror::Class* type)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700150 REQUIRES_SHARED(Locks::mutator_lock_);
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000151
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000152 // If debug info generation is turned on then write the type information for types already loaded
153 // into the specified class linker to the jit debug interface,
154 void DumpTypeInfoForLoadedTypes(ClassLinker* linker);
155
Nicolas Geoffray35122442016-03-02 12:05:30 +0000156 // Return whether we should try to JIT compiled code as soon as an ArtMethod is invoked.
Siva Chandra05d24152016-01-05 17:43:17 -0800157 bool JitAtFirstUse();
158
Nicolas Geoffray35122442016-03-02 12:05:30 +0000159 // Return whether we can invoke JIT code for `method`.
160 bool CanInvokeCompiledCode(ArtMethod* method);
161
Calin Juravleb2771b42016-04-07 17:09:25 +0100162 // Return whether the runtime should use a priority thread weight when sampling.
163 static bool ShouldUsePriorityThreadWeight();
164
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000165 // If an OSR compiled version is available for `method`,
166 // and `dex_pc + dex_pc_offset` is an entry point of that compiled
167 // version, this method will jump to the compiled code, let it run,
168 // and return true afterwards. Return false otherwise.
169 static bool MaybeDoOnStackReplacement(Thread* thread,
170 ArtMethod* method,
171 uint32_t dex_pc,
172 int32_t dex_pc_offset,
173 JValue* result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700174 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000175
Mathieu Chartierc1bc4152016-03-24 17:22:52 -0700176 static bool LoadCompilerLibrary(std::string* error_msg);
Mathieu Chartier72918ea2016-03-24 11:07:06 -0700177
Andreas Gampef149b3f2016-11-16 14:58:24 -0800178 ThreadPool* GetThreadPool() const {
179 return thread_pool_.get();
180 }
181
Nicolas Geoffray021c5f22016-12-16 11:22:05 +0000182 // Stop the JIT by waiting for all current compilations and enqueued compilations to finish.
183 void Stop();
184
185 // Start JIT threads.
186 void Start();
187
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800188 private:
189 Jit();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800190
Mathieu Chartierc1bc4152016-03-24 17:22:52 -0700191 static bool LoadCompiler(std::string* error_msg);
192
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800193 // JIT compiler
Mathieu Chartier72918ea2016-03-24 11:07:06 -0700194 static void* jit_library_handle_;
195 static void* jit_compiler_handle_;
196 static void* (*jit_load_)(bool*);
197 static void (*jit_unload_)(void*);
198 static bool (*jit_compile_method_)(void*, ArtMethod*, Thread*, bool);
199 static void (*jit_types_loaded_)(void*, mirror::Class**, size_t count);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800200
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700201 // Performance monitoring.
202 bool dump_info_on_shutdown_;
203 CumulativeLogger cumulative_timings_;
Nicolas Geoffraya4f81542016-03-08 16:57:48 +0000204 Histogram<uint64_t> memory_use_ GUARDED_BY(lock_);
205 Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700206
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800207 std::unique_ptr<jit::JitCodeCache> code_cache_;
Mathieu Chartier3130cdf2015-05-03 15:20:23 -0700208
Calin Juravleffc87072016-04-20 14:22:09 +0100209 bool use_jit_compilation_;
Calin Juravle138dbff2016-06-28 19:36:58 +0100210 ProfileSaverOptions profile_saver_options_;
Mathieu Chartier72918ea2016-03-24 11:07:06 -0700211 static bool generate_debug_info_;
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100212 uint16_t hot_method_threshold_;
213 uint16_t warm_method_threshold_;
214 uint16_t osr_method_threshold_;
215 uint16_t priority_thread_weight_;
Calin Juravle155ff3d2016-04-27 14:14:58 +0100216 uint16_t invoke_transition_weight_;
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100217 std::unique_ptr<ThreadPool> thread_pool_;
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000218
Mathieu Chartier3130cdf2015-05-03 15:20:23 -0700219 DISALLOW_COPY_AND_ASSIGN(Jit);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800220};
221
222class JitOptions {
223 public:
224 static JitOptions* CreateFromRuntimeArguments(const RuntimeArgumentMap& options);
225 size_t GetCompileThreshold() const {
226 return compile_threshold_;
227 }
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100228 size_t GetWarmupThreshold() const {
229 return warmup_threshold_;
230 }
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000231 size_t GetOsrThreshold() const {
232 return osr_threshold_;
233 }
Calin Juravleb2771b42016-04-07 17:09:25 +0100234 uint16_t GetPriorityThreadWeight() const {
235 return priority_thread_weight_;
236 }
Calin Juravle155ff3d2016-04-27 14:14:58 +0100237 size_t GetInvokeTransitionWeight() const {
238 return invoke_transition_weight_;
239 }
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000240 size_t GetCodeCacheInitialCapacity() const {
241 return code_cache_initial_capacity_;
242 }
243 size_t GetCodeCacheMaxCapacity() const {
244 return code_cache_max_capacity_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800245 }
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700246 bool DumpJitInfoOnShutdown() const {
247 return dump_info_on_shutdown_;
248 }
Calin Juravle138dbff2016-06-28 19:36:58 +0100249 const ProfileSaverOptions& GetProfileSaverOptions() const {
250 return profile_saver_options_;
251 }
Calin Juravle31f2c152015-10-23 17:56:15 +0100252 bool GetSaveProfilingInfo() const {
Calin Juravle138dbff2016-06-28 19:36:58 +0100253 return profile_saver_options_.IsEnabled();
Calin Juravle31f2c152015-10-23 17:56:15 +0100254 }
Calin Juravleffc87072016-04-20 14:22:09 +0100255 bool UseJitCompilation() const {
256 return use_jit_compilation_;
Mathieu Chartier455f67c2015-03-17 13:48:29 -0700257 }
Calin Juravleffc87072016-04-20 14:22:09 +0100258 void SetUseJitCompilation(bool b) {
259 use_jit_compilation_ = b;
Mathieu Chartier455f67c2015-03-17 13:48:29 -0700260 }
Calin Juravle138dbff2016-06-28 19:36:58 +0100261 void SetSaveProfilingInfo(bool save_profiling_info) {
262 profile_saver_options_.SetEnabled(save_profiling_info);
Calin Juravle31f2c152015-10-23 17:56:15 +0100263 }
Siva Chandra05d24152016-01-05 17:43:17 -0800264 void SetJitAtFirstUse() {
Calin Juravleffc87072016-04-20 14:22:09 +0100265 use_jit_compilation_ = true;
Siva Chandra05d24152016-01-05 17:43:17 -0800266 compile_threshold_ = 0;
267 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800268
269 private:
Calin Juravleffc87072016-04-20 14:22:09 +0100270 bool use_jit_compilation_;
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000271 size_t code_cache_initial_capacity_;
272 size_t code_cache_max_capacity_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800273 size_t compile_threshold_;
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100274 size_t warmup_threshold_;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000275 size_t osr_threshold_;
Calin Juravleb2771b42016-04-07 17:09:25 +0100276 uint16_t priority_thread_weight_;
Calin Juravle155ff3d2016-04-27 14:14:58 +0100277 size_t invoke_transition_weight_;
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700278 bool dump_info_on_shutdown_;
Calin Juravle138dbff2016-06-28 19:36:58 +0100279 ProfileSaverOptions profile_saver_options_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800280
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000281 JitOptions()
Calin Juravleffc87072016-04-20 14:22:09 +0100282 : use_jit_compilation_(false),
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000283 code_cache_initial_capacity_(0),
284 code_cache_max_capacity_(0),
285 compile_threshold_(0),
Calin Juravle138dbff2016-06-28 19:36:58 +0100286 dump_info_on_shutdown_(false) {}
Mathieu Chartier3130cdf2015-05-03 15:20:23 -0700287
288 DISALLOW_COPY_AND_ASSIGN(JitOptions);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800289};
290
Andreas Gampef149b3f2016-11-16 14:58:24 -0800291// Helper class to stop the JIT for a given scope. This will wait for the JIT to quiesce.
292class ScopedJitSuspend {
293 public:
294 ScopedJitSuspend();
295 ~ScopedJitSuspend();
296
297 private:
298 bool was_on_;
299};
300
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800301} // namespace jit
302} // namespace art
303
304#endif // ART_RUNTIME_JIT_JIT_H_