Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 1 | /* |
| 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 Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 22 | #include "atomic.h" |
| 23 | #include "base/macros.h" |
| 24 | #include "base/mutex.h" |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 25 | #include "base/timing_logger.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 26 | #include "gc_root.h" |
| 27 | #include "jni.h" |
| 28 | #include "object_callbacks.h" |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 29 | #include "offline_profiling_info.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 30 | #include "thread_pool.h" |
| 31 | |
| 32 | namespace art { |
| 33 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 34 | class ArtMethod; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 35 | class CompilerCallbacks; |
| 36 | struct RuntimeArgumentMap; |
| 37 | |
| 38 | namespace jit { |
| 39 | |
| 40 | class JitCodeCache; |
| 41 | class JitInstrumentationCache; |
| 42 | class JitOptions; |
| 43 | |
| 44 | class Jit { |
| 45 | public: |
| 46 | static constexpr bool kStressMode = kIsDebugBuild; |
Nicolas Geoffray | 4e915fb | 2015-10-28 17:39:47 +0000 | [diff] [blame] | 47 | static constexpr size_t kDefaultCompileThreshold = kStressMode ? 2 : 500; |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 48 | static constexpr size_t kDefaultWarmupThreshold = kDefaultCompileThreshold / 2; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 49 | |
| 50 | virtual ~Jit(); |
| 51 | static Jit* Create(JitOptions* options, std::string* error_msg); |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame^] | 52 | bool CompileMethod(ArtMethod* method, Thread* self, bool osr) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 53 | SHARED_REQUIRES(Locks::mutator_lock_); |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame^] | 54 | void CreateInstrumentationCache(size_t compile_threshold, |
| 55 | size_t warmup_threshold, |
| 56 | size_t osr_threshold); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 57 | 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 Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 68 | // 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 Chartier | a50f9cf | 2015-09-25 11:34:45 -0700 | [diff] [blame] | 73 | JitInstrumentationCache* GetInstrumentationCache() const { |
| 74 | return instrumentation_cache_.get(); |
| 75 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 76 | |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 77 | void StartProfileSaver(const std::string& filename, const std::vector<std::string>& code_paths); |
| 78 | void StopProfileSaver(); |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 79 | |
Nicolas Geoffray | aee2156 | 2015-12-15 16:39:44 +0000 | [diff] [blame] | 80 | void DumpForSigQuit(std::ostream& os) { |
| 81 | DumpInfo(os); |
| 82 | } |
| 83 | |
Tamas Berghammer | 160e6df | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 84 | static void NewTypeLoadedIfUsingJit(mirror::Class* type) |
| 85 | SHARED_REQUIRES(Locks::mutator_lock_); |
| 86 | |
Tamas Berghammer | fffbee4 | 2016-01-15 13:09:34 +0000 | [diff] [blame] | 87 | // 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 Chandra | 05d2415 | 2016-01-05 17:43:17 -0800 | [diff] [blame] | 91 | bool JitAtFirstUse(); |
| 92 | |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame^] | 93 | // 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 Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 104 | 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 Geoffray | a25dce9 | 2016-01-12 16:41:10 +0000 | [diff] [blame] | 111 | void* (*jit_load_)(CompilerCallbacks**, bool*); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 112 | void (*jit_unload_)(void*); |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame^] | 113 | bool (*jit_compile_method_)(void*, ArtMethod*, Thread*, bool); |
Tamas Berghammer | fffbee4 | 2016-01-15 13:09:34 +0000 | [diff] [blame] | 114 | void (*jit_types_loaded_)(void*, mirror::Class**, size_t count); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 115 | |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 116 | // Performance monitoring. |
| 117 | bool dump_info_on_shutdown_; |
| 118 | CumulativeLogger cumulative_timings_; |
| 119 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 120 | 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 Chartier | 3130cdf | 2015-05-03 15:20:23 -0700 | [diff] [blame] | 123 | |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 124 | bool save_profiling_info_; |
Nicolas Geoffray | a25dce9 | 2016-01-12 16:41:10 +0000 | [diff] [blame] | 125 | bool generate_debug_info_; |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 126 | |
Mathieu Chartier | 3130cdf | 2015-05-03 15:20:23 -0700 | [diff] [blame] | 127 | DISALLOW_COPY_AND_ASSIGN(Jit); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 128 | }; |
| 129 | |
| 130 | class JitOptions { |
| 131 | public: |
| 132 | static JitOptions* CreateFromRuntimeArguments(const RuntimeArgumentMap& options); |
| 133 | size_t GetCompileThreshold() const { |
| 134 | return compile_threshold_; |
| 135 | } |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 136 | size_t GetWarmupThreshold() const { |
| 137 | return warmup_threshold_; |
| 138 | } |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame^] | 139 | size_t GetOsrThreshold() const { |
| 140 | return osr_threshold_; |
| 141 | } |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 142 | size_t GetCodeCacheInitialCapacity() const { |
| 143 | return code_cache_initial_capacity_; |
| 144 | } |
| 145 | size_t GetCodeCacheMaxCapacity() const { |
| 146 | return code_cache_max_capacity_; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 147 | } |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 148 | bool DumpJitInfoOnShutdown() const { |
| 149 | return dump_info_on_shutdown_; |
| 150 | } |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 151 | bool GetSaveProfilingInfo() const { |
| 152 | return save_profiling_info_; |
| 153 | } |
Mathieu Chartier | 455f67c | 2015-03-17 13:48:29 -0700 | [diff] [blame] | 154 | bool UseJIT() const { |
| 155 | return use_jit_; |
| 156 | } |
| 157 | void SetUseJIT(bool b) { |
| 158 | use_jit_ = b; |
| 159 | } |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 160 | void SetSaveProfilingInfo(bool b) { |
| 161 | save_profiling_info_ = b; |
| 162 | } |
Siva Chandra | 05d2415 | 2016-01-05 17:43:17 -0800 | [diff] [blame] | 163 | void SetJitAtFirstUse() { |
| 164 | use_jit_ = true; |
| 165 | compile_threshold_ = 0; |
| 166 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 167 | |
| 168 | private: |
Mathieu Chartier | 455f67c | 2015-03-17 13:48:29 -0700 | [diff] [blame] | 169 | bool use_jit_; |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 170 | size_t code_cache_initial_capacity_; |
| 171 | size_t code_cache_max_capacity_; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 172 | size_t compile_threshold_; |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 173 | size_t warmup_threshold_; |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame^] | 174 | size_t osr_threshold_; |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 175 | bool dump_info_on_shutdown_; |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 176 | bool save_profiling_info_; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 177 | |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 178 | JitOptions() |
| 179 | : use_jit_(false), |
| 180 | code_cache_initial_capacity_(0), |
| 181 | code_cache_max_capacity_(0), |
| 182 | compile_threshold_(0), |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 183 | dump_info_on_shutdown_(false), |
| 184 | save_profiling_info_(false) { } |
Mathieu Chartier | 3130cdf | 2015-05-03 15:20:23 -0700 | [diff] [blame] | 185 | |
| 186 | DISALLOW_COPY_AND_ASSIGN(JitOptions); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 187 | }; |
| 188 | |
| 189 | } // namespace jit |
| 190 | } // namespace art |
| 191 | |
| 192 | #endif // ART_RUNTIME_JIT_JIT_H_ |