blob: 042da92b3b2c9bf50c84b615c512b0f75a50ee2f [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
20#include <unordered_map>
21
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080022#include "atomic.h"
23#include "base/macros.h"
24#include "base/mutex.h"
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070025#include "base/timing_logger.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080026#include "gc_root.h"
27#include "jni.h"
28#include "object_callbacks.h"
Calin Juravle31f2c152015-10-23 17:56:15 +010029#include "offline_profiling_info.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080030#include "thread_pool.h"
31
32namespace art {
33
Mathieu Chartiere401d142015-04-22 13:56:20 -070034class ArtMethod;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080035class CompilerCallbacks;
36struct RuntimeArgumentMap;
37
38namespace jit {
39
40class JitCodeCache;
41class JitInstrumentationCache;
42class JitOptions;
43
44class Jit {
45 public:
46 static constexpr bool kStressMode = kIsDebugBuild;
Nicolas Geoffray4e915fb2015-10-28 17:39:47 +000047 static constexpr size_t kDefaultCompileThreshold = kStressMode ? 2 : 500;
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010048 static constexpr size_t kDefaultWarmupThreshold = kDefaultCompileThreshold / 2;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080049
50 virtual ~Jit();
51 static Jit* Create(JitOptions* options, std::string* error_msg);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +000052 bool CompileMethod(ArtMethod* method, Thread* self, bool osr)
Mathieu Chartier90443472015-07-16 20:32:27 -070053 SHARED_REQUIRES(Locks::mutator_lock_);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +000054 void CreateInstrumentationCache(size_t compile_threshold,
55 size_t warmup_threshold,
56 size_t osr_threshold);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080057 void CreateThreadPool();
58 CompilerCallbacks* GetCompilerCallbacks() {
59 return compiler_callbacks_;
60 }
61 const JitCodeCache* GetCodeCache() const {
62 return code_cache_.get();
63 }
64 JitCodeCache* GetCodeCache() {
65 return code_cache_.get();
66 }
67 void DeleteThreadPool();
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070068 // Dump interesting info: #methods compiled, code vs data size, compile / verify cumulative
69 // loggers.
70 void DumpInfo(std::ostream& os);
71 // Add a timing logger to cumulative_timings_.
72 void AddTimingLogger(const TimingLogger& logger);
Mathieu Chartiera50f9cf2015-09-25 11:34:45 -070073 JitInstrumentationCache* GetInstrumentationCache() const {
74 return instrumentation_cache_.get();
75 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080076
Calin Juravle4d77b6a2015-12-01 18:38:09 +000077 void StartProfileSaver(const std::string& filename, const std::vector<std::string>& code_paths);
78 void StopProfileSaver();
Calin Juravle31f2c152015-10-23 17:56:15 +010079
Nicolas Geoffrayaee21562015-12-15 16:39:44 +000080 void DumpForSigQuit(std::ostream& os) {
81 DumpInfo(os);
82 }
83
Tamas Berghammer160e6df2016-01-05 14:29:02 +000084 static void NewTypeLoadedIfUsingJit(mirror::Class* type)
85 SHARED_REQUIRES(Locks::mutator_lock_);
86
Tamas Berghammerfffbee42016-01-15 13:09:34 +000087 // If debug info generation is turned on then write the type information for types already loaded
88 // into the specified class linker to the jit debug interface,
89 void DumpTypeInfoForLoadedTypes(ClassLinker* linker);
90
Siva Chandra05d24152016-01-05 17:43:17 -080091 bool JitAtFirstUse();
92
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +000093 // If an OSR compiled version is available for `method`,
94 // and `dex_pc + dex_pc_offset` is an entry point of that compiled
95 // version, this method will jump to the compiled code, let it run,
96 // and return true afterwards. Return false otherwise.
97 static bool MaybeDoOnStackReplacement(Thread* thread,
98 ArtMethod* method,
99 uint32_t dex_pc,
100 int32_t dex_pc_offset,
101 JValue* result)
102 SHARED_REQUIRES(Locks::mutator_lock_);
103
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800104 private:
105 Jit();
106 bool LoadCompiler(std::string* error_msg);
107
108 // JIT compiler
109 void* jit_library_handle_;
110 void* jit_compiler_handle_;
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000111 void* (*jit_load_)(CompilerCallbacks**, bool*);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800112 void (*jit_unload_)(void*);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000113 bool (*jit_compile_method_)(void*, ArtMethod*, Thread*, bool);
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000114 void (*jit_types_loaded_)(void*, mirror::Class**, size_t count);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800115
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700116 // Performance monitoring.
117 bool dump_info_on_shutdown_;
118 CumulativeLogger cumulative_timings_;
119
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800120 std::unique_ptr<jit::JitInstrumentationCache> instrumentation_cache_;
121 std::unique_ptr<jit::JitCodeCache> code_cache_;
122 CompilerCallbacks* compiler_callbacks_; // Owned by the jit compiler.
Mathieu Chartier3130cdf2015-05-03 15:20:23 -0700123
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000124 bool save_profiling_info_;
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000125 bool generate_debug_info_;
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000126
Mathieu Chartier3130cdf2015-05-03 15:20:23 -0700127 DISALLOW_COPY_AND_ASSIGN(Jit);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800128};
129
130class JitOptions {
131 public:
132 static JitOptions* CreateFromRuntimeArguments(const RuntimeArgumentMap& options);
133 size_t GetCompileThreshold() const {
134 return compile_threshold_;
135 }
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100136 size_t GetWarmupThreshold() const {
137 return warmup_threshold_;
138 }
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000139 size_t GetOsrThreshold() const {
140 return osr_threshold_;
141 }
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000142 size_t GetCodeCacheInitialCapacity() const {
143 return code_cache_initial_capacity_;
144 }
145 size_t GetCodeCacheMaxCapacity() const {
146 return code_cache_max_capacity_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800147 }
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700148 bool DumpJitInfoOnShutdown() const {
149 return dump_info_on_shutdown_;
150 }
Calin Juravle31f2c152015-10-23 17:56:15 +0100151 bool GetSaveProfilingInfo() const {
152 return save_profiling_info_;
153 }
Mathieu Chartier455f67c2015-03-17 13:48:29 -0700154 bool UseJIT() const {
155 return use_jit_;
156 }
157 void SetUseJIT(bool b) {
158 use_jit_ = b;
159 }
Calin Juravle31f2c152015-10-23 17:56:15 +0100160 void SetSaveProfilingInfo(bool b) {
161 save_profiling_info_ = b;
162 }
Siva Chandra05d24152016-01-05 17:43:17 -0800163 void SetJitAtFirstUse() {
164 use_jit_ = true;
165 compile_threshold_ = 0;
166 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800167
168 private:
Mathieu Chartier455f67c2015-03-17 13:48:29 -0700169 bool use_jit_;
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000170 size_t code_cache_initial_capacity_;
171 size_t code_cache_max_capacity_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800172 size_t compile_threshold_;
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100173 size_t warmup_threshold_;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000174 size_t osr_threshold_;
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700175 bool dump_info_on_shutdown_;
Calin Juravle31f2c152015-10-23 17:56:15 +0100176 bool save_profiling_info_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800177
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000178 JitOptions()
179 : use_jit_(false),
180 code_cache_initial_capacity_(0),
181 code_cache_max_capacity_(0),
182 compile_threshold_(0),
Calin Juravle31f2c152015-10-23 17:56:15 +0100183 dump_info_on_shutdown_(false),
184 save_profiling_info_(false) { }
Mathieu Chartier3130cdf2015-05-03 15:20:23 -0700185
186 DISALLOW_COPY_AND_ASSIGN(JitOptions);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800187};
188
189} // namespace jit
190} // namespace art
191
192#endif // ART_RUNTIME_JIT_JIT_H_