blob: f3a6240e80e8714015ec84ba8f40f78f3b35dc7a [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"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080027#include "thread_pool.h"
28
29namespace art {
30
Mathieu Chartiere401d142015-04-22 13:56:20 -070031class ArtMethod;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080032struct RuntimeArgumentMap;
33
34namespace jit {
35
36class JitCodeCache;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080037class JitOptions;
38
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +010039static constexpr int16_t kJitCheckForOSR = -1;
40static constexpr int16_t kJitHotnessDisabled = -2;
41
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080042class Jit {
43 public:
44 static constexpr bool kStressMode = kIsDebugBuild;
Nicolas Geoffray83f080a2016-03-08 16:50:21 +000045 static constexpr size_t kDefaultCompileThreshold = kStressMode ? 2 : 10000;
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +010046 static constexpr size_t kDefaultPriorityThreadWeightRatio = 1000;
Nicolas Geoffraybd553eb2016-04-28 13:56:04 +010047 static constexpr size_t kDefaultInvokeTransitionWeightRatio = 500;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080048
49 virtual ~Jit();
50 static Jit* Create(JitOptions* options, std::string* error_msg);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +000051 bool CompileMethod(ArtMethod* method, Thread* self, bool osr)
Mathieu Chartier90443472015-07-16 20:32:27 -070052 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080053 void CreateThreadPool();
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +010054
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080055 const JitCodeCache* GetCodeCache() const {
56 return code_cache_.get();
57 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +010058
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080059 JitCodeCache* GetCodeCache() {
60 return code_cache_.get();
61 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +010062
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080063 void DeleteThreadPool();
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070064 // Dump interesting info: #methods compiled, code vs data size, compile / verify cumulative
65 // loggers.
Nicolas Geoffraya4f81542016-03-08 16:57:48 +000066 void DumpInfo(std::ostream& os) REQUIRES(!lock_);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070067 // Add a timing logger to cumulative_timings_.
68 void AddTimingLogger(const TimingLogger& logger);
Nicolas Geoffraya4f81542016-03-08 16:57:48 +000069
70 void AddMemoryUsage(ArtMethod* method, size_t bytes)
71 REQUIRES(!lock_)
72 SHARED_REQUIRES(Locks::mutator_lock_);
73
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +010074 size_t OSRMethodThreshold() const {
75 return osr_method_threshold_;
Mathieu Chartiera50f9cf2015-09-25 11:34:45 -070076 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080077
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +010078 size_t HotMethodThreshold() const {
79 return hot_method_threshold_;
80 }
81
82 size_t WarmMethodThreshold() const {
83 return warm_method_threshold_;
84 }
85
86 uint16_t PriorityThreadWeight() const {
87 return priority_thread_weight_;
88 }
89
Calin Juravleffc87072016-04-20 14:22:09 +010090 // Returns false if we only need to save profile information and not compile methods.
91 bool UseJitCompilation() const {
92 return use_jit_compilation_;
93 }
94
95 bool SaveProfilingInfo() const {
96 return save_profiling_info_;
97 }
98
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +010099 // Wait until there is no more pending compilation tasks.
100 void WaitForCompilationToFinish(Thread* self);
101
102 // Profiling methods.
103 void MethodEntered(Thread* thread, ArtMethod* method)
104 SHARED_REQUIRES(Locks::mutator_lock_);
105
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100106 void AddSamples(Thread* self, ArtMethod* method, uint16_t samples, bool with_backedges)
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100107 SHARED_REQUIRES(Locks::mutator_lock_);
108
109 void InvokeVirtualOrInterface(Thread* thread,
110 mirror::Object* this_object,
111 ArtMethod* caller,
112 uint32_t dex_pc,
113 ArtMethod* callee)
114 SHARED_REQUIRES(Locks::mutator_lock_);
115
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100116 void NotifyInterpreterToCompiledCodeTransition(Thread* self, ArtMethod* caller)
117 SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle155ff3d2016-04-27 14:14:58 +0100118 AddSamples(self, caller, invoke_transition_weight_, false);
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100119 }
120
121 void NotifyCompiledCodeToInterpreterTransition(Thread* self, ArtMethod* callee)
122 SHARED_REQUIRES(Locks::mutator_lock_) {
Calin Juravle155ff3d2016-04-27 14:14:58 +0100123 AddSamples(self, callee, invoke_transition_weight_, false);
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100124 }
125
Calin Juravlec90bc922016-02-24 10:13:09 +0000126 // Starts the profile saver if the config options allow profile recording.
127 // The profile will be stored in the specified `filename` and will contain
128 // information collected from the given `code_paths` (a set of dex locations).
129 // The `foreign_dex_profile_path` is the path where the saver will put the
130 // profile markers for loaded dex files which are not owned by the application.
131 // The `app_dir` is the application directory and is used to decide which
132 // dex files belong to the application.
133 void StartProfileSaver(const std::string& filename,
134 const std::vector<std::string>& code_paths,
135 const std::string& foreign_dex_profile_path,
136 const std::string& app_dir);
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000137 void StopProfileSaver();
Calin Juravle31f2c152015-10-23 17:56:15 +0100138
Calin Juravleb8e69992016-03-09 15:37:48 +0000139 void DumpForSigQuit(std::ostream& os) REQUIRES(!lock_);
Nicolas Geoffrayaee21562015-12-15 16:39:44 +0000140
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000141 static void NewTypeLoadedIfUsingJit(mirror::Class* type)
142 SHARED_REQUIRES(Locks::mutator_lock_);
143
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000144 // If debug info generation is turned on then write the type information for types already loaded
145 // into the specified class linker to the jit debug interface,
146 void DumpTypeInfoForLoadedTypes(ClassLinker* linker);
147
Nicolas Geoffray35122442016-03-02 12:05:30 +0000148 // Return whether we should try to JIT compiled code as soon as an ArtMethod is invoked.
Siva Chandra05d24152016-01-05 17:43:17 -0800149 bool JitAtFirstUse();
150
Nicolas Geoffray35122442016-03-02 12:05:30 +0000151 // Return whether we can invoke JIT code for `method`.
152 bool CanInvokeCompiledCode(ArtMethod* method);
153
Calin Juravleb2771b42016-04-07 17:09:25 +0100154 // Return whether the runtime should use a priority thread weight when sampling.
155 static bool ShouldUsePriorityThreadWeight();
156
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000157 // If an OSR compiled version is available for `method`,
158 // and `dex_pc + dex_pc_offset` is an entry point of that compiled
159 // version, this method will jump to the compiled code, let it run,
160 // and return true afterwards. Return false otherwise.
161 static bool MaybeDoOnStackReplacement(Thread* thread,
162 ArtMethod* method,
163 uint32_t dex_pc,
164 int32_t dex_pc_offset,
165 JValue* result)
166 SHARED_REQUIRES(Locks::mutator_lock_);
167
Mathieu Chartierc1bc4152016-03-24 17:22:52 -0700168 static bool LoadCompilerLibrary(std::string* error_msg);
Mathieu Chartier72918ea2016-03-24 11:07:06 -0700169
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800170 private:
171 Jit();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800172
Mathieu Chartierc1bc4152016-03-24 17:22:52 -0700173 static bool LoadCompiler(std::string* error_msg);
174
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800175 // JIT compiler
Mathieu Chartier72918ea2016-03-24 11:07:06 -0700176 static void* jit_library_handle_;
177 static void* jit_compiler_handle_;
178 static void* (*jit_load_)(bool*);
179 static void (*jit_unload_)(void*);
180 static bool (*jit_compile_method_)(void*, ArtMethod*, Thread*, bool);
181 static void (*jit_types_loaded_)(void*, mirror::Class**, size_t count);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800182
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700183 // Performance monitoring.
184 bool dump_info_on_shutdown_;
185 CumulativeLogger cumulative_timings_;
Nicolas Geoffraya4f81542016-03-08 16:57:48 +0000186 Histogram<uint64_t> memory_use_ GUARDED_BY(lock_);
187 Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700188
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800189 std::unique_ptr<jit::JitCodeCache> code_cache_;
Mathieu Chartier3130cdf2015-05-03 15:20:23 -0700190
Calin Juravleffc87072016-04-20 14:22:09 +0100191 bool use_jit_compilation_;
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000192 bool save_profiling_info_;
Mathieu Chartier72918ea2016-03-24 11:07:06 -0700193 static bool generate_debug_info_;
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100194 uint16_t hot_method_threshold_;
195 uint16_t warm_method_threshold_;
196 uint16_t osr_method_threshold_;
197 uint16_t priority_thread_weight_;
Calin Juravle155ff3d2016-04-27 14:14:58 +0100198 uint16_t invoke_transition_weight_;
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100199 std::unique_ptr<ThreadPool> thread_pool_;
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000200
Mathieu Chartier3130cdf2015-05-03 15:20:23 -0700201 DISALLOW_COPY_AND_ASSIGN(Jit);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800202};
203
204class JitOptions {
205 public:
206 static JitOptions* CreateFromRuntimeArguments(const RuntimeArgumentMap& options);
207 size_t GetCompileThreshold() const {
208 return compile_threshold_;
209 }
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100210 size_t GetWarmupThreshold() const {
211 return warmup_threshold_;
212 }
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000213 size_t GetOsrThreshold() const {
214 return osr_threshold_;
215 }
Calin Juravleb2771b42016-04-07 17:09:25 +0100216 uint16_t GetPriorityThreadWeight() const {
217 return priority_thread_weight_;
218 }
Calin Juravle155ff3d2016-04-27 14:14:58 +0100219 size_t GetInvokeTransitionWeight() const {
220 return invoke_transition_weight_;
221 }
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000222 size_t GetCodeCacheInitialCapacity() const {
223 return code_cache_initial_capacity_;
224 }
225 size_t GetCodeCacheMaxCapacity() const {
226 return code_cache_max_capacity_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800227 }
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700228 bool DumpJitInfoOnShutdown() const {
229 return dump_info_on_shutdown_;
230 }
Calin Juravle31f2c152015-10-23 17:56:15 +0100231 bool GetSaveProfilingInfo() const {
232 return save_profiling_info_;
233 }
Calin Juravleffc87072016-04-20 14:22:09 +0100234 bool UseJitCompilation() const {
235 return use_jit_compilation_;
Mathieu Chartier455f67c2015-03-17 13:48:29 -0700236 }
Calin Juravleffc87072016-04-20 14:22:09 +0100237 void SetUseJitCompilation(bool b) {
238 use_jit_compilation_ = b;
Mathieu Chartier455f67c2015-03-17 13:48:29 -0700239 }
Calin Juravle31f2c152015-10-23 17:56:15 +0100240 void SetSaveProfilingInfo(bool b) {
241 save_profiling_info_ = b;
242 }
Siva Chandra05d24152016-01-05 17:43:17 -0800243 void SetJitAtFirstUse() {
Calin Juravleffc87072016-04-20 14:22:09 +0100244 use_jit_compilation_ = true;
Siva Chandra05d24152016-01-05 17:43:17 -0800245 compile_threshold_ = 0;
246 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800247
248 private:
Calin Juravleffc87072016-04-20 14:22:09 +0100249 bool use_jit_compilation_;
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000250 size_t code_cache_initial_capacity_;
251 size_t code_cache_max_capacity_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800252 size_t compile_threshold_;
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100253 size_t warmup_threshold_;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000254 size_t osr_threshold_;
Calin Juravleb2771b42016-04-07 17:09:25 +0100255 uint16_t priority_thread_weight_;
Calin Juravle155ff3d2016-04-27 14:14:58 +0100256 size_t invoke_transition_weight_;
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700257 bool dump_info_on_shutdown_;
Calin Juravle31f2c152015-10-23 17:56:15 +0100258 bool save_profiling_info_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800259
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000260 JitOptions()
Calin Juravleffc87072016-04-20 14:22:09 +0100261 : use_jit_compilation_(false),
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000262 code_cache_initial_capacity_(0),
263 code_cache_max_capacity_(0),
264 compile_threshold_(0),
Calin Juravle31f2c152015-10-23 17:56:15 +0100265 dump_info_on_shutdown_(false),
266 save_profiling_info_(false) { }
Mathieu Chartier3130cdf2015-05-03 15:20:23 -0700267
268 DISALLOW_COPY_AND_ASSIGN(JitOptions);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800269};
270
271} // namespace jit
272} // namespace art
273
274#endif // ART_RUNTIME_JIT_JIT_H_