blob: 96f9608a94c61dff0aa93b07bad7d8872750412c [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;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080046
47 virtual ~Jit();
48 static Jit* Create(JitOptions* options, std::string* error_msg);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +000049 bool CompileMethod(ArtMethod* method, Thread* self, bool osr)
Mathieu Chartier90443472015-07-16 20:32:27 -070050 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080051 void CreateThreadPool();
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +010052
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080053 const JitCodeCache* GetCodeCache() const {
54 return code_cache_.get();
55 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +010056
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080057 JitCodeCache* GetCodeCache() {
58 return code_cache_.get();
59 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +010060
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080061 void DeleteThreadPool();
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070062 // Dump interesting info: #methods compiled, code vs data size, compile / verify cumulative
63 // loggers.
Nicolas Geoffraya4f81542016-03-08 16:57:48 +000064 void DumpInfo(std::ostream& os) REQUIRES(!lock_);
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070065 // Add a timing logger to cumulative_timings_.
66 void AddTimingLogger(const TimingLogger& logger);
Nicolas Geoffraya4f81542016-03-08 16:57:48 +000067
68 void AddMemoryUsage(ArtMethod* method, size_t bytes)
69 REQUIRES(!lock_)
70 SHARED_REQUIRES(Locks::mutator_lock_);
71
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +010072 size_t OSRMethodThreshold() const {
73 return osr_method_threshold_;
Mathieu Chartiera50f9cf2015-09-25 11:34:45 -070074 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080075
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +010076 size_t HotMethodThreshold() const {
77 return hot_method_threshold_;
78 }
79
80 size_t WarmMethodThreshold() const {
81 return warm_method_threshold_;
82 }
83
84 uint16_t PriorityThreadWeight() const {
85 return priority_thread_weight_;
86 }
87
88 // Wait until there is no more pending compilation tasks.
89 void WaitForCompilationToFinish(Thread* self);
90
91 // Profiling methods.
92 void MethodEntered(Thread* thread, ArtMethod* method)
93 SHARED_REQUIRES(Locks::mutator_lock_);
94
95 void AddSamples(Thread* self, ArtMethod* method, uint16_t samples)
96 SHARED_REQUIRES(Locks::mutator_lock_);
97
98 void InvokeVirtualOrInterface(Thread* thread,
99 mirror::Object* this_object,
100 ArtMethod* caller,
101 uint32_t dex_pc,
102 ArtMethod* callee)
103 SHARED_REQUIRES(Locks::mutator_lock_);
104
Calin Juravlec90bc922016-02-24 10:13:09 +0000105 // Starts the profile saver if the config options allow profile recording.
106 // The profile will be stored in the specified `filename` and will contain
107 // information collected from the given `code_paths` (a set of dex locations).
108 // The `foreign_dex_profile_path` is the path where the saver will put the
109 // profile markers for loaded dex files which are not owned by the application.
110 // The `app_dir` is the application directory and is used to decide which
111 // dex files belong to the application.
112 void StartProfileSaver(const std::string& filename,
113 const std::vector<std::string>& code_paths,
114 const std::string& foreign_dex_profile_path,
115 const std::string& app_dir);
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000116 void StopProfileSaver();
Calin Juravle31f2c152015-10-23 17:56:15 +0100117
Nicolas Geoffraya4f81542016-03-08 16:57:48 +0000118 void DumpForSigQuit(std::ostream& os) REQUIRES(!lock_) {
Nicolas Geoffrayaee21562015-12-15 16:39:44 +0000119 DumpInfo(os);
120 }
121
Tamas Berghammer160e6df2016-01-05 14:29:02 +0000122 static void NewTypeLoadedIfUsingJit(mirror::Class* type)
123 SHARED_REQUIRES(Locks::mutator_lock_);
124
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000125 // If debug info generation is turned on then write the type information for types already loaded
126 // into the specified class linker to the jit debug interface,
127 void DumpTypeInfoForLoadedTypes(ClassLinker* linker);
128
Nicolas Geoffray35122442016-03-02 12:05:30 +0000129 // Return whether we should try to JIT compiled code as soon as an ArtMethod is invoked.
Siva Chandra05d24152016-01-05 17:43:17 -0800130 bool JitAtFirstUse();
131
Nicolas Geoffray35122442016-03-02 12:05:30 +0000132 // Return whether we can invoke JIT code for `method`.
133 bool CanInvokeCompiledCode(ArtMethod* method);
134
Calin Juravleb2771b42016-04-07 17:09:25 +0100135 // Return whether the runtime should use a priority thread weight when sampling.
136 static bool ShouldUsePriorityThreadWeight();
137
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000138 // If an OSR compiled version is available for `method`,
139 // and `dex_pc + dex_pc_offset` is an entry point of that compiled
140 // version, this method will jump to the compiled code, let it run,
141 // and return true afterwards. Return false otherwise.
142 static bool MaybeDoOnStackReplacement(Thread* thread,
143 ArtMethod* method,
144 uint32_t dex_pc,
145 int32_t dex_pc_offset,
146 JValue* result)
147 SHARED_REQUIRES(Locks::mutator_lock_);
148
Mathieu Chartierc1bc4152016-03-24 17:22:52 -0700149 static bool LoadCompilerLibrary(std::string* error_msg);
Mathieu Chartier72918ea2016-03-24 11:07:06 -0700150
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800151 private:
152 Jit();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800153
Mathieu Chartierc1bc4152016-03-24 17:22:52 -0700154 static bool LoadCompiler(std::string* error_msg);
155
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800156 // JIT compiler
Mathieu Chartier72918ea2016-03-24 11:07:06 -0700157 static void* jit_library_handle_;
158 static void* jit_compiler_handle_;
159 static void* (*jit_load_)(bool*);
160 static void (*jit_unload_)(void*);
161 static bool (*jit_compile_method_)(void*, ArtMethod*, Thread*, bool);
162 static void (*jit_types_loaded_)(void*, mirror::Class**, size_t count);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800163
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700164 // Performance monitoring.
165 bool dump_info_on_shutdown_;
166 CumulativeLogger cumulative_timings_;
Nicolas Geoffraya4f81542016-03-08 16:57:48 +0000167 Histogram<uint64_t> memory_use_ GUARDED_BY(lock_);
168 Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700169
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800170 std::unique_ptr<jit::JitCodeCache> code_cache_;
Mathieu Chartier3130cdf2015-05-03 15:20:23 -0700171
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000172 bool save_profiling_info_;
Mathieu Chartier72918ea2016-03-24 11:07:06 -0700173 static bool generate_debug_info_;
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100174 uint16_t hot_method_threshold_;
175 uint16_t warm_method_threshold_;
176 uint16_t osr_method_threshold_;
177 uint16_t priority_thread_weight_;
178 std::unique_ptr<ThreadPool> thread_pool_;
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000179
Mathieu Chartier3130cdf2015-05-03 15:20:23 -0700180 DISALLOW_COPY_AND_ASSIGN(Jit);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800181};
182
183class JitOptions {
184 public:
185 static JitOptions* CreateFromRuntimeArguments(const RuntimeArgumentMap& options);
186 size_t GetCompileThreshold() const {
187 return compile_threshold_;
188 }
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100189 size_t GetWarmupThreshold() const {
190 return warmup_threshold_;
191 }
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000192 size_t GetOsrThreshold() const {
193 return osr_threshold_;
194 }
Calin Juravleb2771b42016-04-07 17:09:25 +0100195 uint16_t GetPriorityThreadWeight() const {
196 return priority_thread_weight_;
197 }
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000198 size_t GetCodeCacheInitialCapacity() const {
199 return code_cache_initial_capacity_;
200 }
201 size_t GetCodeCacheMaxCapacity() const {
202 return code_cache_max_capacity_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800203 }
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700204 bool DumpJitInfoOnShutdown() const {
205 return dump_info_on_shutdown_;
206 }
Calin Juravle31f2c152015-10-23 17:56:15 +0100207 bool GetSaveProfilingInfo() const {
208 return save_profiling_info_;
209 }
Mathieu Chartier455f67c2015-03-17 13:48:29 -0700210 bool UseJIT() const {
211 return use_jit_;
212 }
213 void SetUseJIT(bool b) {
214 use_jit_ = b;
215 }
Calin Juravle31f2c152015-10-23 17:56:15 +0100216 void SetSaveProfilingInfo(bool b) {
217 save_profiling_info_ = b;
218 }
Siva Chandra05d24152016-01-05 17:43:17 -0800219 void SetJitAtFirstUse() {
220 use_jit_ = true;
221 compile_threshold_ = 0;
222 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800223
224 private:
Mathieu Chartier455f67c2015-03-17 13:48:29 -0700225 bool use_jit_;
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000226 size_t code_cache_initial_capacity_;
227 size_t code_cache_max_capacity_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800228 size_t compile_threshold_;
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100229 size_t warmup_threshold_;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000230 size_t osr_threshold_;
Calin Juravleb2771b42016-04-07 17:09:25 +0100231 uint16_t priority_thread_weight_;
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700232 bool dump_info_on_shutdown_;
Calin Juravle31f2c152015-10-23 17:56:15 +0100233 bool save_profiling_info_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800234
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000235 JitOptions()
236 : use_jit_(false),
237 code_cache_initial_capacity_(0),
238 code_cache_max_capacity_(0),
239 compile_threshold_(0),
Calin Juravle31f2c152015-10-23 17:56:15 +0100240 dump_info_on_shutdown_(false),
241 save_profiling_info_(false) { }
Mathieu Chartier3130cdf2015-05-03 15:20:23 -0700242
243 DISALLOW_COPY_AND_ASSIGN(JitOptions);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800244};
245
246} // namespace jit
247} // namespace art
248
249#endif // ART_RUNTIME_JIT_JIT_H_