blob: 3f54192d9f7924c690e12112527fa543e84e616d [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 -080035struct RuntimeArgumentMap;
36
37namespace jit {
38
39class JitCodeCache;
40class JitInstrumentationCache;
41class JitOptions;
42
43class Jit {
44 public:
45 static constexpr bool kStressMode = kIsDebugBuild;
Nicolas Geoffray4e915fb2015-10-28 17:39:47 +000046 static constexpr size_t kDefaultCompileThreshold = kStressMode ? 2 : 500;
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010047 static constexpr size_t kDefaultWarmupThreshold = kDefaultCompileThreshold / 2;
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_);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +000053 void CreateInstrumentationCache(size_t compile_threshold,
54 size_t warmup_threshold,
55 size_t osr_threshold);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080056 void CreateThreadPool();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080057 const JitCodeCache* GetCodeCache() const {
58 return code_cache_.get();
59 }
60 JitCodeCache* GetCodeCache() {
61 return code_cache_.get();
62 }
63 void DeleteThreadPool();
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070064 // Dump interesting info: #methods compiled, code vs data size, compile / verify cumulative
65 // loggers.
66 void DumpInfo(std::ostream& os);
67 // Add a timing logger to cumulative_timings_.
68 void AddTimingLogger(const TimingLogger& logger);
Mathieu Chartiera50f9cf2015-09-25 11:34:45 -070069 JitInstrumentationCache* GetInstrumentationCache() const {
70 return instrumentation_cache_.get();
71 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080072
Calin Juravle4d77b6a2015-12-01 18:38:09 +000073 void StartProfileSaver(const std::string& filename, const std::vector<std::string>& code_paths);
74 void StopProfileSaver();
Calin Juravle31f2c152015-10-23 17:56:15 +010075
Nicolas Geoffrayaee21562015-12-15 16:39:44 +000076 void DumpForSigQuit(std::ostream& os) {
77 DumpInfo(os);
78 }
79
Tamas Berghammer160e6df2016-01-05 14:29:02 +000080 static void NewTypeLoadedIfUsingJit(mirror::Class* type)
81 SHARED_REQUIRES(Locks::mutator_lock_);
82
Tamas Berghammerfffbee42016-01-15 13:09:34 +000083 // If debug info generation is turned on then write the type information for types already loaded
84 // into the specified class linker to the jit debug interface,
85 void DumpTypeInfoForLoadedTypes(ClassLinker* linker);
86
Nicolas Geoffray35122442016-03-02 12:05:30 +000087 // Return whether we should try to JIT compiled code as soon as an ArtMethod is invoked.
Siva Chandra05d24152016-01-05 17:43:17 -080088 bool JitAtFirstUse();
89
Nicolas Geoffray35122442016-03-02 12:05:30 +000090 // Return whether we can invoke JIT code for `method`.
91 bool CanInvokeCompiledCode(ArtMethod* method);
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 Geoffray5b82d332016-02-18 14:22:32 +0000111 void* (*jit_load_)(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_;
Mathieu Chartier3130cdf2015-05-03 15:20:23 -0700122
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000123 bool save_profiling_info_;
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000124 bool generate_debug_info_;
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000125
Mathieu Chartier3130cdf2015-05-03 15:20:23 -0700126 DISALLOW_COPY_AND_ASSIGN(Jit);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800127};
128
129class JitOptions {
130 public:
131 static JitOptions* CreateFromRuntimeArguments(const RuntimeArgumentMap& options);
132 size_t GetCompileThreshold() const {
133 return compile_threshold_;
134 }
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100135 size_t GetWarmupThreshold() const {
136 return warmup_threshold_;
137 }
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000138 size_t GetOsrThreshold() const {
139 return osr_threshold_;
140 }
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000141 size_t GetCodeCacheInitialCapacity() const {
142 return code_cache_initial_capacity_;
143 }
144 size_t GetCodeCacheMaxCapacity() const {
145 return code_cache_max_capacity_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800146 }
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700147 bool DumpJitInfoOnShutdown() const {
148 return dump_info_on_shutdown_;
149 }
Calin Juravle31f2c152015-10-23 17:56:15 +0100150 bool GetSaveProfilingInfo() const {
151 return save_profiling_info_;
152 }
Mathieu Chartier455f67c2015-03-17 13:48:29 -0700153 bool UseJIT() const {
154 return use_jit_;
155 }
156 void SetUseJIT(bool b) {
157 use_jit_ = b;
158 }
Calin Juravle31f2c152015-10-23 17:56:15 +0100159 void SetSaveProfilingInfo(bool b) {
160 save_profiling_info_ = b;
161 }
Siva Chandra05d24152016-01-05 17:43:17 -0800162 void SetJitAtFirstUse() {
163 use_jit_ = true;
164 compile_threshold_ = 0;
165 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800166
167 private:
Mathieu Chartier455f67c2015-03-17 13:48:29 -0700168 bool use_jit_;
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000169 size_t code_cache_initial_capacity_;
170 size_t code_cache_max_capacity_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800171 size_t compile_threshold_;
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100172 size_t warmup_threshold_;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000173 size_t osr_threshold_;
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700174 bool dump_info_on_shutdown_;
Calin Juravle31f2c152015-10-23 17:56:15 +0100175 bool save_profiling_info_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800176
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000177 JitOptions()
178 : use_jit_(false),
179 code_cache_initial_capacity_(0),
180 code_cache_max_capacity_(0),
181 compile_threshold_(0),
Calin Juravle31f2c152015-10-23 17:56:15 +0100182 dump_info_on_shutdown_(false),
183 save_profiling_info_(false) { }
Mathieu Chartier3130cdf2015-05-03 15:20:23 -0700184
185 DISALLOW_COPY_AND_ASSIGN(JitOptions);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800186};
187
188} // namespace jit
189} // namespace art
190
191#endif // ART_RUNTIME_JIT_JIT_H_