blob: d5c213416af76ec1bb37a144bfd9819cadf3e5b0 [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 Geoffray83f080a2016-03-08 16:50:21 +000046 static constexpr size_t kDefaultCompileThreshold = kStressMode ? 2 : 10000;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080047
48 virtual ~Jit();
49 static Jit* Create(JitOptions* options, std::string* error_msg);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +000050 bool CompileMethod(ArtMethod* method, Thread* self, bool osr)
Mathieu Chartier90443472015-07-16 20:32:27 -070051 SHARED_REQUIRES(Locks::mutator_lock_);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +000052 void CreateInstrumentationCache(size_t compile_threshold,
53 size_t warmup_threshold,
54 size_t osr_threshold);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080055 void CreateThreadPool();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080056 const JitCodeCache* GetCodeCache() const {
57 return code_cache_.get();
58 }
59 JitCodeCache* GetCodeCache() {
60 return code_cache_.get();
61 }
62 void DeleteThreadPool();
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070063 // Dump interesting info: #methods compiled, code vs data size, compile / verify cumulative
64 // loggers.
65 void DumpInfo(std::ostream& os);
66 // Add a timing logger to cumulative_timings_.
67 void AddTimingLogger(const TimingLogger& logger);
Mathieu Chartiera50f9cf2015-09-25 11:34:45 -070068 JitInstrumentationCache* GetInstrumentationCache() const {
69 return instrumentation_cache_.get();
70 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080071
Calin Juravlec90bc922016-02-24 10:13:09 +000072 // Starts the profile saver if the config options allow profile recording.
73 // The profile will be stored in the specified `filename` and will contain
74 // information collected from the given `code_paths` (a set of dex locations).
75 // The `foreign_dex_profile_path` is the path where the saver will put the
76 // profile markers for loaded dex files which are not owned by the application.
77 // The `app_dir` is the application directory and is used to decide which
78 // dex files belong to the application.
79 void StartProfileSaver(const std::string& filename,
80 const std::vector<std::string>& code_paths,
81 const std::string& foreign_dex_profile_path,
82 const std::string& app_dir);
Calin Juravle4d77b6a2015-12-01 18:38:09 +000083 void StopProfileSaver();
Calin Juravle31f2c152015-10-23 17:56:15 +010084
Nicolas Geoffrayaee21562015-12-15 16:39:44 +000085 void DumpForSigQuit(std::ostream& os) {
86 DumpInfo(os);
87 }
88
Tamas Berghammer160e6df2016-01-05 14:29:02 +000089 static void NewTypeLoadedIfUsingJit(mirror::Class* type)
90 SHARED_REQUIRES(Locks::mutator_lock_);
91
Tamas Berghammerfffbee42016-01-15 13:09:34 +000092 // If debug info generation is turned on then write the type information for types already loaded
93 // into the specified class linker to the jit debug interface,
94 void DumpTypeInfoForLoadedTypes(ClassLinker* linker);
95
Nicolas Geoffray35122442016-03-02 12:05:30 +000096 // Return whether we should try to JIT compiled code as soon as an ArtMethod is invoked.
Siva Chandra05d24152016-01-05 17:43:17 -080097 bool JitAtFirstUse();
98
Nicolas Geoffray35122442016-03-02 12:05:30 +000099 // Return whether we can invoke JIT code for `method`.
100 bool CanInvokeCompiledCode(ArtMethod* method);
101
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000102 // If an OSR compiled version is available for `method`,
103 // and `dex_pc + dex_pc_offset` is an entry point of that compiled
104 // version, this method will jump to the compiled code, let it run,
105 // and return true afterwards. Return false otherwise.
106 static bool MaybeDoOnStackReplacement(Thread* thread,
107 ArtMethod* method,
108 uint32_t dex_pc,
109 int32_t dex_pc_offset,
110 JValue* result)
111 SHARED_REQUIRES(Locks::mutator_lock_);
112
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800113 private:
114 Jit();
115 bool LoadCompiler(std::string* error_msg);
116
117 // JIT compiler
118 void* jit_library_handle_;
119 void* jit_compiler_handle_;
Nicolas Geoffray5b82d332016-02-18 14:22:32 +0000120 void* (*jit_load_)(bool*);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800121 void (*jit_unload_)(void*);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000122 bool (*jit_compile_method_)(void*, ArtMethod*, Thread*, bool);
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000123 void (*jit_types_loaded_)(void*, mirror::Class**, size_t count);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800124
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700125 // Performance monitoring.
126 bool dump_info_on_shutdown_;
127 CumulativeLogger cumulative_timings_;
128
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800129 std::unique_ptr<jit::JitInstrumentationCache> instrumentation_cache_;
130 std::unique_ptr<jit::JitCodeCache> code_cache_;
Mathieu Chartier3130cdf2015-05-03 15:20:23 -0700131
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000132 bool save_profiling_info_;
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000133 bool generate_debug_info_;
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000134
Mathieu Chartier3130cdf2015-05-03 15:20:23 -0700135 DISALLOW_COPY_AND_ASSIGN(Jit);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800136};
137
138class JitOptions {
139 public:
140 static JitOptions* CreateFromRuntimeArguments(const RuntimeArgumentMap& options);
141 size_t GetCompileThreshold() const {
142 return compile_threshold_;
143 }
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100144 size_t GetWarmupThreshold() const {
145 return warmup_threshold_;
146 }
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000147 size_t GetOsrThreshold() const {
148 return osr_threshold_;
149 }
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000150 size_t GetCodeCacheInitialCapacity() const {
151 return code_cache_initial_capacity_;
152 }
153 size_t GetCodeCacheMaxCapacity() const {
154 return code_cache_max_capacity_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800155 }
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700156 bool DumpJitInfoOnShutdown() const {
157 return dump_info_on_shutdown_;
158 }
Calin Juravle31f2c152015-10-23 17:56:15 +0100159 bool GetSaveProfilingInfo() const {
160 return save_profiling_info_;
161 }
Mathieu Chartier455f67c2015-03-17 13:48:29 -0700162 bool UseJIT() const {
163 return use_jit_;
164 }
165 void SetUseJIT(bool b) {
166 use_jit_ = b;
167 }
Calin Juravle31f2c152015-10-23 17:56:15 +0100168 void SetSaveProfilingInfo(bool b) {
169 save_profiling_info_ = b;
170 }
Siva Chandra05d24152016-01-05 17:43:17 -0800171 void SetJitAtFirstUse() {
172 use_jit_ = true;
173 compile_threshold_ = 0;
174 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800175
176 private:
Mathieu Chartier455f67c2015-03-17 13:48:29 -0700177 bool use_jit_;
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000178 size_t code_cache_initial_capacity_;
179 size_t code_cache_max_capacity_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800180 size_t compile_threshold_;
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100181 size_t warmup_threshold_;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000182 size_t osr_threshold_;
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700183 bool dump_info_on_shutdown_;
Calin Juravle31f2c152015-10-23 17:56:15 +0100184 bool save_profiling_info_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800185
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000186 JitOptions()
187 : use_jit_(false),
188 code_cache_initial_capacity_(0),
189 code_cache_max_capacity_(0),
190 compile_threshold_(0),
Calin Juravle31f2c152015-10-23 17:56:15 +0100191 dump_info_on_shutdown_(false),
192 save_profiling_info_(false) { }
Mathieu Chartier3130cdf2015-05-03 15:20:23 -0700193
194 DISALLOW_COPY_AND_ASSIGN(JitOptions);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800195};
196
197} // namespace jit
198} // namespace art
199
200#endif // ART_RUNTIME_JIT_JIT_H_