blob: d3178b0b3782dc1a673ab9f9b394e67bb7564628 [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
David Sehr709b0702016-10-13 09:12:37 -070037namespace mirror {
38class Object;
39class Class;
40} // namespace mirror
41
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080042namespace jit {
43
44class JitCodeCache;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080045class JitOptions;
46
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +010047static constexpr int16_t kJitCheckForOSR = -1;
48static constexpr int16_t kJitHotnessDisabled = -2;
49
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080050class Jit {
51 public:
52 static constexpr bool kStressMode = kIsDebugBuild;
Nicolas Geoffray83f080a2016-03-08 16:50:21 +000053 static constexpr size_t kDefaultCompileThreshold = kStressMode ? 2 : 10000;
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +010054 static constexpr size_t kDefaultPriorityThreadWeightRatio = 1000;
Nicolas Geoffraybd553eb2016-04-28 13:56:04 +010055 static constexpr size_t kDefaultInvokeTransitionWeightRatio = 500;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080056
57 virtual ~Jit();
58 static Jit* Create(JitOptions* options, std::string* error_msg);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +000059 bool CompileMethod(ArtMethod* method, Thread* self, bool osr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070060 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080061 void CreateThreadPool();
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +010062
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080063 const JitCodeCache* GetCodeCache() const {
64 return code_cache_.get();
65 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +010066
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080067 JitCodeCache* GetCodeCache() {
68 return code_cache_.get();
69 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +010070
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080071 void DeleteThreadPool();
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070072 // Dump interesting info: #methods compiled, code vs data size, compile / verify cumulative
73 // loggers.
Nicolas Geoffraya4f81542016-03-08 16:57:48 +000074 void DumpInfo(std::ostream& os) REQUIRES(!lock_);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070075 // Add a timing logger to cumulative_timings_.
76 void AddTimingLogger(const TimingLogger& logger);
Nicolas Geoffraya4f81542016-03-08 16:57:48 +000077
78 void AddMemoryUsage(ArtMethod* method, size_t bytes)
79 REQUIRES(!lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070080 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffraya4f81542016-03-08 16:57:48 +000081
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +010082 size_t OSRMethodThreshold() const {
83 return osr_method_threshold_;
Mathieu Chartiera50f9cf2015-09-25 11:34:45 -070084 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080085
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +010086 size_t HotMethodThreshold() const {
87 return hot_method_threshold_;
88 }
89
90 size_t WarmMethodThreshold() const {
91 return warm_method_threshold_;
92 }
93
94 uint16_t PriorityThreadWeight() const {
95 return priority_thread_weight_;
96 }
97
Calin Juravleffc87072016-04-20 14:22:09 +010098 // Returns false if we only need to save profile information and not compile methods.
99 bool UseJitCompilation() const {
100 return use_jit_compilation_;
101 }
102
Calin Juravle138dbff2016-06-28 19:36:58 +0100103 bool GetSaveProfilingInfo() const {
104 return profile_saver_options_.IsEnabled();
Calin Juravleffc87072016-04-20 14:22:09 +0100105 }
106
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100107 // Wait until there is no more pending compilation tasks.
108 void WaitForCompilationToFinish(Thread* self);
109
110 // Profiling methods.
111 void MethodEntered(Thread* thread, ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700112 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100113
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100114 void AddSamples(Thread* self, ArtMethod* method, uint16_t samples, bool with_backedges)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700115 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100116
Mathieu Chartier268764d2016-09-13 12:09:38 -0700117 void InvokeVirtualOrInterface(mirror::Object* this_object,
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100118 ArtMethod* caller,
119 uint32_t dex_pc,
120 ArtMethod* callee)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700121 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100122
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100123 void NotifyInterpreterToCompiledCodeTransition(Thread* self, ArtMethod* caller)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700124 REQUIRES_SHARED(Locks::mutator_lock_) {
Calin Juravle155ff3d2016-04-27 14:14:58 +0100125 AddSamples(self, caller, invoke_transition_weight_, false);
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100126 }
127
128 void NotifyCompiledCodeToInterpreterTransition(Thread* self, ArtMethod* callee)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700129 REQUIRES_SHARED(Locks::mutator_lock_) {
Calin Juravle155ff3d2016-04-27 14:14:58 +0100130 AddSamples(self, callee, invoke_transition_weight_, false);
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100131 }
132
Calin Juravlec90bc922016-02-24 10:13:09 +0000133 // Starts the profile saver if the config options allow profile recording.
134 // The profile will be stored in the specified `filename` and will contain
135 // information collected from the given `code_paths` (a set of dex locations).
136 // The `foreign_dex_profile_path` is the path where the saver will put the
137 // profile markers for loaded dex files which are not owned by the application.
138 // The `app_dir` is the application directory and is used to decide which
139 // dex files belong to the application.
140 void StartProfileSaver(const std::string& filename,
141 const std::vector<std::string>& code_paths,
142 const std::string& foreign_dex_profile_path,
143 const std::string& app_dir);
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000144 void StopProfileSaver();
Calin Juravle31f2c152015-10-23 17:56:15 +0100145
Calin Juravleb8e69992016-03-09 15:37:48 +0000146 void DumpForSigQuit(std::ostream& os) REQUIRES(!lock_);
Nicolas Geoffrayaee21562015-12-15 16:39:44 +0000147
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000148 static void NewTypeLoadedIfUsingJit(mirror::Class* type)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700149 REQUIRES_SHARED(Locks::mutator_lock_);
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000150
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000151 // If debug info generation is turned on then write the type information for types already loaded
152 // into the specified class linker to the jit debug interface,
153 void DumpTypeInfoForLoadedTypes(ClassLinker* linker);
154
Nicolas Geoffray35122442016-03-02 12:05:30 +0000155 // Return whether we should try to JIT compiled code as soon as an ArtMethod is invoked.
Siva Chandra05d24152016-01-05 17:43:17 -0800156 bool JitAtFirstUse();
157
Nicolas Geoffray35122442016-03-02 12:05:30 +0000158 // Return whether we can invoke JIT code for `method`.
159 bool CanInvokeCompiledCode(ArtMethod* method);
160
Calin Juravleb2771b42016-04-07 17:09:25 +0100161 // Return whether the runtime should use a priority thread weight when sampling.
162 static bool ShouldUsePriorityThreadWeight();
163
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000164 // If an OSR compiled version is available for `method`,
165 // and `dex_pc + dex_pc_offset` is an entry point of that compiled
166 // version, this method will jump to the compiled code, let it run,
167 // and return true afterwards. Return false otherwise.
168 static bool MaybeDoOnStackReplacement(Thread* thread,
169 ArtMethod* method,
170 uint32_t dex_pc,
171 int32_t dex_pc_offset,
172 JValue* result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700173 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000174
Mathieu Chartierc1bc4152016-03-24 17:22:52 -0700175 static bool LoadCompilerLibrary(std::string* error_msg);
Mathieu Chartier72918ea2016-03-24 11:07:06 -0700176
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800177 private:
178 Jit();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800179
Mathieu Chartierc1bc4152016-03-24 17:22:52 -0700180 static bool LoadCompiler(std::string* error_msg);
181
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800182 // JIT compiler
Mathieu Chartier72918ea2016-03-24 11:07:06 -0700183 static void* jit_library_handle_;
184 static void* jit_compiler_handle_;
185 static void* (*jit_load_)(bool*);
186 static void (*jit_unload_)(void*);
187 static bool (*jit_compile_method_)(void*, ArtMethod*, Thread*, bool);
188 static void (*jit_types_loaded_)(void*, mirror::Class**, size_t count);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800189
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700190 // Performance monitoring.
191 bool dump_info_on_shutdown_;
192 CumulativeLogger cumulative_timings_;
Nicolas Geoffraya4f81542016-03-08 16:57:48 +0000193 Histogram<uint64_t> memory_use_ GUARDED_BY(lock_);
194 Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700195
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800196 std::unique_ptr<jit::JitCodeCache> code_cache_;
Mathieu Chartier3130cdf2015-05-03 15:20:23 -0700197
Calin Juravleffc87072016-04-20 14:22:09 +0100198 bool use_jit_compilation_;
Calin Juravle138dbff2016-06-28 19:36:58 +0100199 ProfileSaverOptions profile_saver_options_;
Mathieu Chartier72918ea2016-03-24 11:07:06 -0700200 static bool generate_debug_info_;
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100201 uint16_t hot_method_threshold_;
202 uint16_t warm_method_threshold_;
203 uint16_t osr_method_threshold_;
204 uint16_t priority_thread_weight_;
Calin Juravle155ff3d2016-04-27 14:14:58 +0100205 uint16_t invoke_transition_weight_;
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100206 std::unique_ptr<ThreadPool> thread_pool_;
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000207
Mathieu Chartier3130cdf2015-05-03 15:20:23 -0700208 DISALLOW_COPY_AND_ASSIGN(Jit);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800209};
210
211class JitOptions {
212 public:
213 static JitOptions* CreateFromRuntimeArguments(const RuntimeArgumentMap& options);
214 size_t GetCompileThreshold() const {
215 return compile_threshold_;
216 }
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100217 size_t GetWarmupThreshold() const {
218 return warmup_threshold_;
219 }
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000220 size_t GetOsrThreshold() const {
221 return osr_threshold_;
222 }
Calin Juravleb2771b42016-04-07 17:09:25 +0100223 uint16_t GetPriorityThreadWeight() const {
224 return priority_thread_weight_;
225 }
Calin Juravle155ff3d2016-04-27 14:14:58 +0100226 size_t GetInvokeTransitionWeight() const {
227 return invoke_transition_weight_;
228 }
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000229 size_t GetCodeCacheInitialCapacity() const {
230 return code_cache_initial_capacity_;
231 }
232 size_t GetCodeCacheMaxCapacity() const {
233 return code_cache_max_capacity_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800234 }
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700235 bool DumpJitInfoOnShutdown() const {
236 return dump_info_on_shutdown_;
237 }
Calin Juravle138dbff2016-06-28 19:36:58 +0100238 const ProfileSaverOptions& GetProfileSaverOptions() const {
239 return profile_saver_options_;
240 }
Calin Juravle31f2c152015-10-23 17:56:15 +0100241 bool GetSaveProfilingInfo() const {
Calin Juravle138dbff2016-06-28 19:36:58 +0100242 return profile_saver_options_.IsEnabled();
Calin Juravle31f2c152015-10-23 17:56:15 +0100243 }
Calin Juravleffc87072016-04-20 14:22:09 +0100244 bool UseJitCompilation() const {
245 return use_jit_compilation_;
Mathieu Chartier455f67c2015-03-17 13:48:29 -0700246 }
Calin Juravleffc87072016-04-20 14:22:09 +0100247 void SetUseJitCompilation(bool b) {
248 use_jit_compilation_ = b;
Mathieu Chartier455f67c2015-03-17 13:48:29 -0700249 }
Calin Juravle138dbff2016-06-28 19:36:58 +0100250 void SetSaveProfilingInfo(bool save_profiling_info) {
251 profile_saver_options_.SetEnabled(save_profiling_info);
Calin Juravle31f2c152015-10-23 17:56:15 +0100252 }
Siva Chandra05d24152016-01-05 17:43:17 -0800253 void SetJitAtFirstUse() {
Calin Juravleffc87072016-04-20 14:22:09 +0100254 use_jit_compilation_ = true;
Siva Chandra05d24152016-01-05 17:43:17 -0800255 compile_threshold_ = 0;
256 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800257
258 private:
Calin Juravleffc87072016-04-20 14:22:09 +0100259 bool use_jit_compilation_;
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000260 size_t code_cache_initial_capacity_;
261 size_t code_cache_max_capacity_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800262 size_t compile_threshold_;
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100263 size_t warmup_threshold_;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000264 size_t osr_threshold_;
Calin Juravleb2771b42016-04-07 17:09:25 +0100265 uint16_t priority_thread_weight_;
Calin Juravle155ff3d2016-04-27 14:14:58 +0100266 size_t invoke_transition_weight_;
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700267 bool dump_info_on_shutdown_;
Calin Juravle138dbff2016-06-28 19:36:58 +0100268 ProfileSaverOptions profile_saver_options_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800269
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000270 JitOptions()
Calin Juravleffc87072016-04-20 14:22:09 +0100271 : use_jit_compilation_(false),
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000272 code_cache_initial_capacity_(0),
273 code_cache_max_capacity_(0),
274 compile_threshold_(0),
Calin Juravle138dbff2016-06-28 19:36:58 +0100275 dump_info_on_shutdown_(false) {}
Mathieu Chartier3130cdf2015-05-03 15:20:23 -0700276
277 DISALLOW_COPY_AND_ASSIGN(JitOptions);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800278};
279
280} // namespace jit
281} // namespace art
282
283#endif // ART_RUNTIME_JIT_JIT_H_