blob: 52c3843a63596dc773c37c07c427dfe9c2fbe822 [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 Chartiere5f13e52015-02-24 09:37:21 -080025#include "object_callbacks.h"
Calin Juravle31f2c152015-10-23 17:56:15 +010026#include "offline_profiling_info.h"
Calin Juravle138dbff2016-06-28 19:36:58 +010027#include "jit/profile_saver_options.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080028#include "thread_pool.h"
29
30namespace art {
31
Mathieu Chartiere401d142015-04-22 13:56:20 -070032class ArtMethod;
David Sehr9323e6e2016-09-13 08:58:35 -070033class ClassLinker;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080034struct RuntimeArgumentMap;
Vladimir Marko3a21e382016-09-02 12:38:38 +010035union JValue;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080036
37namespace jit {
38
39class JitCodeCache;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080040class JitOptions;
41
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +010042static constexpr int16_t kJitCheckForOSR = -1;
43static constexpr int16_t kJitHotnessDisabled = -2;
44
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080045class Jit {
46 public:
47 static constexpr bool kStressMode = kIsDebugBuild;
Nicolas Geoffray83f080a2016-03-08 16:50:21 +000048 static constexpr size_t kDefaultCompileThreshold = kStressMode ? 2 : 10000;
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +010049 static constexpr size_t kDefaultPriorityThreadWeightRatio = 1000;
Nicolas Geoffraybd553eb2016-04-28 13:56:04 +010050 static constexpr size_t kDefaultInvokeTransitionWeightRatio = 500;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080051
52 virtual ~Jit();
53 static Jit* Create(JitOptions* options, std::string* error_msg);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +000054 bool CompileMethod(ArtMethod* method, Thread* self, bool osr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070055 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080056 void CreateThreadPool();
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +010057
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080058 const JitCodeCache* GetCodeCache() const {
59 return code_cache_.get();
60 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +010061
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080062 JitCodeCache* GetCodeCache() {
63 return code_cache_.get();
64 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +010065
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080066 void DeleteThreadPool();
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070067 // Dump interesting info: #methods compiled, code vs data size, compile / verify cumulative
68 // loggers.
Nicolas Geoffraya4f81542016-03-08 16:57:48 +000069 void DumpInfo(std::ostream& os) REQUIRES(!lock_);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070070 // Add a timing logger to cumulative_timings_.
71 void AddTimingLogger(const TimingLogger& logger);
Nicolas Geoffraya4f81542016-03-08 16:57:48 +000072
73 void AddMemoryUsage(ArtMethod* method, size_t bytes)
74 REQUIRES(!lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070075 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffraya4f81542016-03-08 16:57:48 +000076
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +010077 size_t OSRMethodThreshold() const {
78 return osr_method_threshold_;
Mathieu Chartiera50f9cf2015-09-25 11:34:45 -070079 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080080
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +010081 size_t HotMethodThreshold() const {
82 return hot_method_threshold_;
83 }
84
85 size_t WarmMethodThreshold() const {
86 return warm_method_threshold_;
87 }
88
89 uint16_t PriorityThreadWeight() const {
90 return priority_thread_weight_;
91 }
92
Calin Juravleffc87072016-04-20 14:22:09 +010093 // Returns false if we only need to save profile information and not compile methods.
94 bool UseJitCompilation() const {
95 return use_jit_compilation_;
96 }
97
Calin Juravle138dbff2016-06-28 19:36:58 +010098 bool GetSaveProfilingInfo() const {
99 return profile_saver_options_.IsEnabled();
Calin Juravleffc87072016-04-20 14:22:09 +0100100 }
101
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100102 // Wait until there is no more pending compilation tasks.
103 void WaitForCompilationToFinish(Thread* self);
104
105 // Profiling methods.
106 void MethodEntered(Thread* thread, ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700107 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100108
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100109 void AddSamples(Thread* self, ArtMethod* method, uint16_t samples, bool with_backedges)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700110 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100111
112 void InvokeVirtualOrInterface(Thread* thread,
113 mirror::Object* this_object,
114 ArtMethod* caller,
115 uint32_t dex_pc,
116 ArtMethod* callee)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700117 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100118
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100119 void NotifyInterpreterToCompiledCodeTransition(Thread* self, ArtMethod* caller)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700120 REQUIRES_SHARED(Locks::mutator_lock_) {
Calin Juravle155ff3d2016-04-27 14:14:58 +0100121 AddSamples(self, caller, invoke_transition_weight_, false);
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100122 }
123
124 void NotifyCompiledCodeToInterpreterTransition(Thread* self, ArtMethod* callee)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700125 REQUIRES_SHARED(Locks::mutator_lock_) {
Calin Juravle155ff3d2016-04-27 14:14:58 +0100126 AddSamples(self, callee, invoke_transition_weight_, false);
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100127 }
128
Calin Juravlec90bc922016-02-24 10:13:09 +0000129 // Starts the profile saver if the config options allow profile recording.
130 // The profile will be stored in the specified `filename` and will contain
131 // information collected from the given `code_paths` (a set of dex locations).
132 // The `foreign_dex_profile_path` is the path where the saver will put the
133 // profile markers for loaded dex files which are not owned by the application.
134 // The `app_dir` is the application directory and is used to decide which
135 // dex files belong to the application.
136 void StartProfileSaver(const std::string& filename,
137 const std::vector<std::string>& code_paths,
138 const std::string& foreign_dex_profile_path,
139 const std::string& app_dir);
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000140 void StopProfileSaver();
Calin Juravle31f2c152015-10-23 17:56:15 +0100141
Calin Juravleb8e69992016-03-09 15:37:48 +0000142 void DumpForSigQuit(std::ostream& os) REQUIRES(!lock_);
Nicolas Geoffrayaee21562015-12-15 16:39:44 +0000143
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000144 static void NewTypeLoadedIfUsingJit(mirror::Class* type)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700145 REQUIRES_SHARED(Locks::mutator_lock_);
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000146
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000147 // If debug info generation is turned on then write the type information for types already loaded
148 // into the specified class linker to the jit debug interface,
149 void DumpTypeInfoForLoadedTypes(ClassLinker* linker);
150
Nicolas Geoffray35122442016-03-02 12:05:30 +0000151 // Return whether we should try to JIT compiled code as soon as an ArtMethod is invoked.
Siva Chandra05d24152016-01-05 17:43:17 -0800152 bool JitAtFirstUse();
153
Nicolas Geoffray35122442016-03-02 12:05:30 +0000154 // Return whether we can invoke JIT code for `method`.
155 bool CanInvokeCompiledCode(ArtMethod* method);
156
Calin Juravleb2771b42016-04-07 17:09:25 +0100157 // Return whether the runtime should use a priority thread weight when sampling.
158 static bool ShouldUsePriorityThreadWeight();
159
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000160 // If an OSR compiled version is available for `method`,
161 // and `dex_pc + dex_pc_offset` is an entry point of that compiled
162 // version, this method will jump to the compiled code, let it run,
163 // and return true afterwards. Return false otherwise.
164 static bool MaybeDoOnStackReplacement(Thread* thread,
165 ArtMethod* method,
166 uint32_t dex_pc,
167 int32_t dex_pc_offset,
168 JValue* result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700169 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000170
Mathieu Chartierc1bc4152016-03-24 17:22:52 -0700171 static bool LoadCompilerLibrary(std::string* error_msg);
Mathieu Chartier72918ea2016-03-24 11:07:06 -0700172
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800173 private:
174 Jit();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800175
Mathieu Chartierc1bc4152016-03-24 17:22:52 -0700176 static bool LoadCompiler(std::string* error_msg);
177
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800178 // JIT compiler
Mathieu Chartier72918ea2016-03-24 11:07:06 -0700179 static void* jit_library_handle_;
180 static void* jit_compiler_handle_;
181 static void* (*jit_load_)(bool*);
182 static void (*jit_unload_)(void*);
183 static bool (*jit_compile_method_)(void*, ArtMethod*, Thread*, bool);
184 static void (*jit_types_loaded_)(void*, mirror::Class**, size_t count);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800185
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700186 // Performance monitoring.
187 bool dump_info_on_shutdown_;
188 CumulativeLogger cumulative_timings_;
Nicolas Geoffraya4f81542016-03-08 16:57:48 +0000189 Histogram<uint64_t> memory_use_ GUARDED_BY(lock_);
190 Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700191
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800192 std::unique_ptr<jit::JitCodeCache> code_cache_;
Mathieu Chartier3130cdf2015-05-03 15:20:23 -0700193
Calin Juravleffc87072016-04-20 14:22:09 +0100194 bool use_jit_compilation_;
Calin Juravle138dbff2016-06-28 19:36:58 +0100195 ProfileSaverOptions profile_saver_options_;
Mathieu Chartier72918ea2016-03-24 11:07:06 -0700196 static bool generate_debug_info_;
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100197 uint16_t hot_method_threshold_;
198 uint16_t warm_method_threshold_;
199 uint16_t osr_method_threshold_;
200 uint16_t priority_thread_weight_;
Calin Juravle155ff3d2016-04-27 14:14:58 +0100201 uint16_t invoke_transition_weight_;
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100202 std::unique_ptr<ThreadPool> thread_pool_;
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000203
Mathieu Chartier3130cdf2015-05-03 15:20:23 -0700204 DISALLOW_COPY_AND_ASSIGN(Jit);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800205};
206
207class JitOptions {
208 public:
209 static JitOptions* CreateFromRuntimeArguments(const RuntimeArgumentMap& options);
210 size_t GetCompileThreshold() const {
211 return compile_threshold_;
212 }
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100213 size_t GetWarmupThreshold() const {
214 return warmup_threshold_;
215 }
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000216 size_t GetOsrThreshold() const {
217 return osr_threshold_;
218 }
Calin Juravleb2771b42016-04-07 17:09:25 +0100219 uint16_t GetPriorityThreadWeight() const {
220 return priority_thread_weight_;
221 }
Calin Juravle155ff3d2016-04-27 14:14:58 +0100222 size_t GetInvokeTransitionWeight() const {
223 return invoke_transition_weight_;
224 }
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000225 size_t GetCodeCacheInitialCapacity() const {
226 return code_cache_initial_capacity_;
227 }
228 size_t GetCodeCacheMaxCapacity() const {
229 return code_cache_max_capacity_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800230 }
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700231 bool DumpJitInfoOnShutdown() const {
232 return dump_info_on_shutdown_;
233 }
Calin Juravle138dbff2016-06-28 19:36:58 +0100234 const ProfileSaverOptions& GetProfileSaverOptions() const {
235 return profile_saver_options_;
236 }
Calin Juravle31f2c152015-10-23 17:56:15 +0100237 bool GetSaveProfilingInfo() const {
Calin Juravle138dbff2016-06-28 19:36:58 +0100238 return profile_saver_options_.IsEnabled();
Calin Juravle31f2c152015-10-23 17:56:15 +0100239 }
Calin Juravleffc87072016-04-20 14:22:09 +0100240 bool UseJitCompilation() const {
241 return use_jit_compilation_;
Mathieu Chartier455f67c2015-03-17 13:48:29 -0700242 }
Calin Juravleffc87072016-04-20 14:22:09 +0100243 void SetUseJitCompilation(bool b) {
244 use_jit_compilation_ = b;
Mathieu Chartier455f67c2015-03-17 13:48:29 -0700245 }
Calin Juravle138dbff2016-06-28 19:36:58 +0100246 void SetSaveProfilingInfo(bool save_profiling_info) {
247 profile_saver_options_.SetEnabled(save_profiling_info);
Calin Juravle31f2c152015-10-23 17:56:15 +0100248 }
Siva Chandra05d24152016-01-05 17:43:17 -0800249 void SetJitAtFirstUse() {
Calin Juravleffc87072016-04-20 14:22:09 +0100250 use_jit_compilation_ = true;
Siva Chandra05d24152016-01-05 17:43:17 -0800251 compile_threshold_ = 0;
252 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800253
254 private:
Calin Juravleffc87072016-04-20 14:22:09 +0100255 bool use_jit_compilation_;
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000256 size_t code_cache_initial_capacity_;
257 size_t code_cache_max_capacity_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800258 size_t compile_threshold_;
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100259 size_t warmup_threshold_;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000260 size_t osr_threshold_;
Calin Juravleb2771b42016-04-07 17:09:25 +0100261 uint16_t priority_thread_weight_;
Calin Juravle155ff3d2016-04-27 14:14:58 +0100262 size_t invoke_transition_weight_;
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700263 bool dump_info_on_shutdown_;
Calin Juravle138dbff2016-06-28 19:36:58 +0100264 ProfileSaverOptions profile_saver_options_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800265
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000266 JitOptions()
Calin Juravleffc87072016-04-20 14:22:09 +0100267 : use_jit_compilation_(false),
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000268 code_cache_initial_capacity_(0),
269 code_cache_max_capacity_(0),
270 compile_threshold_(0),
Calin Juravle138dbff2016-06-28 19:36:58 +0100271 dump_info_on_shutdown_(false) {}
Mathieu Chartier3130cdf2015-05-03 15:20:23 -0700272
273 DISALLOW_COPY_AND_ASSIGN(JitOptions);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800274};
275
276} // namespace jit
277} // namespace art
278
279#endif // ART_RUNTIME_JIT_JIT_H_