blob: a80f51f3971c11a42c141c67d0cb9673a682e838 [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);
Mathieu Chartiere401d142015-04-22 13:56:20 -070052 bool CompileMethod(ArtMethod* method, Thread* self)
Mathieu Chartier90443472015-07-16 20:32:27 -070053 SHARED_REQUIRES(Locks::mutator_lock_);
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010054 void CreateInstrumentationCache(size_t compile_threshold, size_t warmup_threshold);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080055 void CreateThreadPool();
56 CompilerCallbacks* GetCompilerCallbacks() {
57 return compiler_callbacks_;
58 }
59 const JitCodeCache* GetCodeCache() const {
60 return code_cache_.get();
61 }
62 JitCodeCache* GetCodeCache() {
63 return code_cache_.get();
64 }
65 void DeleteThreadPool();
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070066 // Dump interesting info: #methods compiled, code vs data size, compile / verify cumulative
67 // loggers.
68 void DumpInfo(std::ostream& os);
69 // Add a timing logger to cumulative_timings_.
70 void AddTimingLogger(const TimingLogger& logger);
Mathieu Chartiera50f9cf2015-09-25 11:34:45 -070071 JitInstrumentationCache* GetInstrumentationCache() const {
72 return instrumentation_cache_.get();
73 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080074
Calin Juravle4d77b6a2015-12-01 18:38:09 +000075 void StartProfileSaver(const std::string& filename, const std::vector<std::string>& code_paths);
76 void StopProfileSaver();
Calin Juravle31f2c152015-10-23 17:56:15 +010077
Nicolas Geoffrayaee21562015-12-15 16:39:44 +000078 void DumpForSigQuit(std::ostream& os) {
79 DumpInfo(os);
80 }
81
Tamas Berghammer160e6df2016-01-05 14:29:02 +000082 static void NewTypeLoadedIfUsingJit(mirror::Class* type)
83 SHARED_REQUIRES(Locks::mutator_lock_);
84
Tamas Berghammerfffbee42016-01-15 13:09:34 +000085 // If debug info generation is turned on then write the type information for types already loaded
86 // into the specified class linker to the jit debug interface,
87 void DumpTypeInfoForLoadedTypes(ClassLinker* linker);
88
Siva Chandra05d24152016-01-05 17:43:17 -080089 bool JitAtFirstUse();
90
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080091 private:
92 Jit();
93 bool LoadCompiler(std::string* error_msg);
94
95 // JIT compiler
96 void* jit_library_handle_;
97 void* jit_compiler_handle_;
Nicolas Geoffraya25dce92016-01-12 16:41:10 +000098 void* (*jit_load_)(CompilerCallbacks**, bool*);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080099 void (*jit_unload_)(void*);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700100 bool (*jit_compile_method_)(void*, ArtMethod*, Thread*);
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000101 void (*jit_types_loaded_)(void*, mirror::Class**, size_t count);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800102
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700103 // Performance monitoring.
104 bool dump_info_on_shutdown_;
105 CumulativeLogger cumulative_timings_;
106
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800107 std::unique_ptr<jit::JitInstrumentationCache> instrumentation_cache_;
108 std::unique_ptr<jit::JitCodeCache> code_cache_;
109 CompilerCallbacks* compiler_callbacks_; // Owned by the jit compiler.
Mathieu Chartier3130cdf2015-05-03 15:20:23 -0700110
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000111 bool save_profiling_info_;
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000112 bool generate_debug_info_;
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000113
Mathieu Chartier3130cdf2015-05-03 15:20:23 -0700114 DISALLOW_COPY_AND_ASSIGN(Jit);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800115};
116
117class JitOptions {
118 public:
119 static JitOptions* CreateFromRuntimeArguments(const RuntimeArgumentMap& options);
120 size_t GetCompileThreshold() const {
121 return compile_threshold_;
122 }
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100123 size_t GetWarmupThreshold() const {
124 return warmup_threshold_;
125 }
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000126 size_t GetCodeCacheInitialCapacity() const {
127 return code_cache_initial_capacity_;
128 }
129 size_t GetCodeCacheMaxCapacity() const {
130 return code_cache_max_capacity_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800131 }
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700132 bool DumpJitInfoOnShutdown() const {
133 return dump_info_on_shutdown_;
134 }
Calin Juravle31f2c152015-10-23 17:56:15 +0100135 bool GetSaveProfilingInfo() const {
136 return save_profiling_info_;
137 }
Mathieu Chartier455f67c2015-03-17 13:48:29 -0700138 bool UseJIT() const {
139 return use_jit_;
140 }
141 void SetUseJIT(bool b) {
142 use_jit_ = b;
143 }
Calin Juravle31f2c152015-10-23 17:56:15 +0100144 void SetSaveProfilingInfo(bool b) {
145 save_profiling_info_ = b;
146 }
Siva Chandra05d24152016-01-05 17:43:17 -0800147 void SetJitAtFirstUse() {
148 use_jit_ = true;
149 compile_threshold_ = 0;
150 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800151
152 private:
Mathieu Chartier455f67c2015-03-17 13:48:29 -0700153 bool use_jit_;
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000154 size_t code_cache_initial_capacity_;
155 size_t code_cache_max_capacity_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800156 size_t compile_threshold_;
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100157 size_t warmup_threshold_;
Mathieu Chartiera4885cb2015-03-09 15:38:54 -0700158 bool dump_info_on_shutdown_;
Calin Juravle31f2c152015-10-23 17:56:15 +0100159 bool save_profiling_info_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800160
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000161 JitOptions()
162 : use_jit_(false),
163 code_cache_initial_capacity_(0),
164 code_cache_max_capacity_(0),
165 compile_threshold_(0),
Calin Juravle31f2c152015-10-23 17:56:15 +0100166 dump_info_on_shutdown_(false),
167 save_profiling_info_(false) { }
Mathieu Chartier3130cdf2015-05-03 15:20:23 -0700168
169 DISALLOW_COPY_AND_ASSIGN(JitOptions);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800170};
171
172} // namespace jit
173} // namespace art
174
175#endif // ART_RUNTIME_JIT_JIT_H_