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_CODE_CACHE_H_ |
| 18 | #define ART_RUNTIME_JIT_JIT_CODE_CACHE_H_ |
| 19 | |
| 20 | #include "instrumentation.h" |
| 21 | |
| 22 | #include "atomic.h" |
| 23 | #include "base/macros.h" |
| 24 | #include "base/mutex.h" |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 25 | #include "gc/accounting/bitmap.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 26 | #include "gc_root.h" |
| 27 | #include "jni.h" |
| 28 | #include "oat_file.h" |
| 29 | #include "object_callbacks.h" |
| 30 | #include "safe_map.h" |
| 31 | #include "thread_pool.h" |
| 32 | |
| 33 | namespace art { |
| 34 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 35 | class ArtMethod; |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 36 | class LinearAlloc; |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 37 | class ProfilingInfo; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 38 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 39 | namespace jit { |
| 40 | |
| 41 | class JitInstrumentationCache; |
| 42 | |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 43 | // Alignment in bits that will suit all architectures. |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 44 | static constexpr int kJitCodeAlignment = 16; |
| 45 | using CodeCacheBitmap = gc::accounting::MemoryRangeBitmap<kJitCodeAlignment>; |
| 46 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 47 | class JitCodeCache { |
| 48 | public: |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 49 | static constexpr size_t kMaxCapacity = 64 * MB; |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 50 | // Put the default to a very low amount for debug builds to stress the code cache |
| 51 | // collection. |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 52 | static constexpr size_t kInitialCapacity = kIsDebugBuild ? 16 * KB : 64 * KB; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 53 | |
Mathieu Chartier | bce416f | 2015-03-23 12:37:35 -0700 | [diff] [blame] | 54 | // Create the code cache with a code + data capacity equal to "capacity", error message is passed |
| 55 | // in the out arg error_msg. |
Nicolas Geoffray | a25dce9 | 2016-01-12 16:41:10 +0000 | [diff] [blame] | 56 | static JitCodeCache* Create(size_t initial_capacity, |
| 57 | size_t max_capacity, |
| 58 | bool generate_debug_info, |
| 59 | std::string* error_msg); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 60 | |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 61 | // Number of bytes allocated in the code cache. |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 62 | size_t CodeCacheSize() REQUIRES(!lock_); |
| 63 | |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 64 | // Number of bytes allocated in the data cache. |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 65 | size_t DataCacheSize() REQUIRES(!lock_); |
| 66 | |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 67 | // Number of compiled code in the code cache. Note that this is not the number |
| 68 | // of methods that got JIT compiled, as we might have collected some. |
| 69 | size_t NumberOfCompiledCode() REQUIRES(!lock_); |
| 70 | |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 71 | bool NotifyCompilationOf(ArtMethod* method, Thread* self) |
| 72 | SHARED_REQUIRES(Locks::mutator_lock_) |
| 73 | REQUIRES(!lock_); |
| 74 | |
| 75 | void DoneCompiling(ArtMethod* method, Thread* self) |
| 76 | SHARED_REQUIRES(Locks::mutator_lock_) |
| 77 | REQUIRES(!lock_); |
| 78 | |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 79 | // Allocate and write code and its metadata to the code cache. |
| 80 | uint8_t* CommitCode(Thread* self, |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 81 | ArtMethod* method, |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 82 | const uint8_t* mapping_table, |
| 83 | const uint8_t* vmap_table, |
| 84 | const uint8_t* gc_map, |
| 85 | size_t frame_size_in_bytes, |
| 86 | size_t core_spill_mask, |
| 87 | size_t fp_spill_mask, |
| 88 | const uint8_t* code, |
| 89 | size_t code_size) |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 90 | SHARED_REQUIRES(Locks::mutator_lock_) |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 91 | REQUIRES(!lock_); |
| 92 | |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 93 | // Return true if the code cache contains this pc. |
| 94 | bool ContainsPc(const void* pc) const; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 95 | |
Nicolas Geoffray | a5891e8 | 2015-11-06 14:18:27 +0000 | [diff] [blame] | 96 | // Return true if the code cache contains this method. |
| 97 | bool ContainsMethod(ArtMethod* method) REQUIRES(!lock_); |
| 98 | |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 99 | // Reserve a region of data of size at least "size". Returns null if there is no more room. |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 100 | uint8_t* ReserveData(Thread* self, size_t size) |
| 101 | SHARED_REQUIRES(Locks::mutator_lock_) |
| 102 | REQUIRES(!lock_); |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 103 | |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 104 | // Clear data from the data portion of the code cache. |
| 105 | void ClearData(Thread* self, void* data) |
| 106 | SHARED_REQUIRES(Locks::mutator_lock_) |
| 107 | REQUIRES(!lock_); |
| 108 | |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 109 | // Add a data array of size (end - begin) with the associated contents, returns null if there |
Mathieu Chartier | bce416f | 2015-03-23 12:37:35 -0700 | [diff] [blame] | 110 | // is no more room. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 111 | uint8_t* AddDataArray(Thread* self, const uint8_t* begin, const uint8_t* end) |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 112 | SHARED_REQUIRES(Locks::mutator_lock_) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 113 | REQUIRES(!lock_); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 114 | |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 115 | CodeCacheBitmap* GetLiveBitmap() const { |
| 116 | return live_bitmap_.get(); |
| 117 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 118 | |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 119 | // Perform a collection on the code cache. |
| 120 | void GarbageCollectCache(Thread* self) |
| 121 | REQUIRES(!lock_) |
| 122 | SHARED_REQUIRES(Locks::mutator_lock_); |
| 123 | |
| 124 | // Given the 'pc', try to find the JIT compiled code associated with it. |
| 125 | // Return null if 'pc' is not in the code cache. 'method' is passed for |
| 126 | // sanity check. |
| 127 | OatQuickMethodHeader* LookupMethodHeader(uintptr_t pc, ArtMethod* method) |
| 128 | REQUIRES(!lock_) |
| 129 | SHARED_REQUIRES(Locks::mutator_lock_); |
| 130 | |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 131 | // Remove all methods in our cache that were allocated by 'alloc'. |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 132 | void RemoveMethodsIn(Thread* self, const LinearAlloc& alloc) |
| 133 | REQUIRES(!lock_) |
| 134 | REQUIRES(Locks::classlinker_classes_lock_) |
| 135 | SHARED_REQUIRES(Locks::mutator_lock_); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 136 | |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 137 | // Create a 'ProfileInfo' for 'method'. If 'retry_allocation' is true, |
| 138 | // will collect and retry if the first allocation is unsuccessful. |
| 139 | ProfilingInfo* AddProfilingInfo(Thread* self, |
| 140 | ArtMethod* method, |
| 141 | const std::vector<uint32_t>& entries, |
| 142 | bool retry_allocation) |
| 143 | REQUIRES(!lock_) |
| 144 | SHARED_REQUIRES(Locks::mutator_lock_); |
| 145 | |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 146 | bool OwnsSpace(const void* mspace) const NO_THREAD_SAFETY_ANALYSIS { |
| 147 | return mspace == code_mspace_ || mspace == data_mspace_; |
| 148 | } |
| 149 | |
| 150 | void* MoreCore(const void* mspace, intptr_t increment); |
| 151 | |
Calin Juravle | 66f5523 | 2015-12-08 15:09:10 +0000 | [diff] [blame] | 152 | // Adds to `methods` all the compiled ArtMethods which are part of any of the given dex locations. |
| 153 | void GetCompiledArtMethods(const std::set<const std::string>& dex_base_locations, |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 154 | std::vector<ArtMethod*>& methods) |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 155 | REQUIRES(!lock_) |
| 156 | SHARED_REQUIRES(Locks::mutator_lock_); |
| 157 | |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 158 | uint64_t GetLastUpdateTimeNs() const; |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 159 | |
Nicolas Geoffray | aee2156 | 2015-12-15 16:39:44 +0000 | [diff] [blame] | 160 | size_t GetCurrentCapacity() REQUIRES(!lock_) { |
| 161 | MutexLock lock(Thread::Current(), lock_); |
| 162 | return current_capacity_; |
| 163 | } |
| 164 | |
Nicolas Geoffray | a25dce9 | 2016-01-12 16:41:10 +0000 | [diff] [blame] | 165 | size_t GetMemorySizeOfCodePointer(const void* ptr) REQUIRES(!lock_); |
| 166 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 167 | private: |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 168 | // Take ownership of maps. |
| 169 | JitCodeCache(MemMap* code_map, |
| 170 | MemMap* data_map, |
| 171 | size_t initial_code_capacity, |
| 172 | size_t initial_data_capacity, |
Nicolas Geoffray | a25dce9 | 2016-01-12 16:41:10 +0000 | [diff] [blame] | 173 | size_t max_capacity, |
| 174 | bool garbage_collect_code); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 175 | |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 176 | // Internal version of 'CommitCode' that will not retry if the |
| 177 | // allocation fails. Return null if the allocation fails. |
| 178 | uint8_t* CommitCodeInternal(Thread* self, |
| 179 | ArtMethod* method, |
| 180 | const uint8_t* mapping_table, |
| 181 | const uint8_t* vmap_table, |
| 182 | const uint8_t* gc_map, |
| 183 | size_t frame_size_in_bytes, |
| 184 | size_t core_spill_mask, |
| 185 | size_t fp_spill_mask, |
| 186 | const uint8_t* code, |
| 187 | size_t code_size) |
| 188 | REQUIRES(!lock_) |
| 189 | SHARED_REQUIRES(Locks::mutator_lock_); |
| 190 | |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 191 | ProfilingInfo* AddProfilingInfoInternal(Thread* self, |
| 192 | ArtMethod* method, |
| 193 | const std::vector<uint32_t>& entries) |
| 194 | REQUIRES(!lock_) |
| 195 | SHARED_REQUIRES(Locks::mutator_lock_); |
| 196 | |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 197 | // If a collection is in progress, wait for it to finish. Return |
| 198 | // whether the thread actually waited. |
| 199 | bool WaitForPotentialCollectionToComplete(Thread* self) |
| 200 | REQUIRES(lock_) REQUIRES(!Locks::mutator_lock_); |
| 201 | |
| 202 | // Free in the mspace allocations taken by 'method'. |
| 203 | void FreeCode(const void* code_ptr, ArtMethod* method) REQUIRES(lock_); |
| 204 | |
Nicolas Geoffray | a5891e8 | 2015-11-06 14:18:27 +0000 | [diff] [blame] | 205 | // Number of bytes allocated in the code cache. |
| 206 | size_t CodeCacheSizeLocked() REQUIRES(lock_); |
| 207 | |
| 208 | // Number of bytes allocated in the data cache. |
| 209 | size_t DataCacheSizeLocked() REQUIRES(lock_); |
| 210 | |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 211 | // Notify all waiting threads that a collection is done. |
| 212 | void NotifyCollectionDone(Thread* self) REQUIRES(lock_); |
| 213 | |
| 214 | // Try to increase the current capacity of the code cache. Return whether we |
| 215 | // succeeded at doing so. |
| 216 | bool IncreaseCodeCacheCapacity() REQUIRES(lock_); |
| 217 | |
| 218 | // Set the footprint limit of the code cache. |
| 219 | void SetFootprintLimit(size_t new_footprint) REQUIRES(lock_); |
| 220 | |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 221 | // Lock for guarding allocations, collections, and the method_code_map_. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 222 | Mutex lock_; |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 223 | // Condition to wait on during collection. |
| 224 | ConditionVariable lock_cond_ GUARDED_BY(lock_); |
| 225 | // Whether there is a code cache collection in progress. |
| 226 | bool collection_in_progress_ GUARDED_BY(lock_); |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 227 | // Mem map which holds code. |
| 228 | std::unique_ptr<MemMap> code_map_; |
| 229 | // Mem map which holds data (stack maps and profiling info). |
| 230 | std::unique_ptr<MemMap> data_map_; |
| 231 | // The opaque mspace for allocating code. |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 232 | void* code_mspace_ GUARDED_BY(lock_); |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 233 | // The opaque mspace for allocating data. |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 234 | void* data_mspace_ GUARDED_BY(lock_); |
| 235 | // Bitmap for collecting code and data. |
| 236 | std::unique_ptr<CodeCacheBitmap> live_bitmap_; |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 237 | // This map holds compiled code associated to the ArtMethod. |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 238 | SafeMap<const void*, ArtMethod*> method_code_map_ GUARDED_BY(lock_); |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 239 | // ProfilingInfo objects we have allocated. |
| 240 | std::vector<ProfilingInfo*> profiling_infos_ GUARDED_BY(lock_); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 241 | |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 242 | // The maximum capacity in bytes this code cache can go to. |
| 243 | size_t max_capacity_ GUARDED_BY(lock_); |
| 244 | |
| 245 | // The current capacity in bytes of the code cache. |
| 246 | size_t current_capacity_ GUARDED_BY(lock_); |
| 247 | |
| 248 | // The current footprint in bytes of the code portion of the code cache. |
| 249 | size_t code_end_ GUARDED_BY(lock_); |
| 250 | |
| 251 | // The current footprint in bytes of the data portion of the code cache. |
| 252 | size_t data_end_ GUARDED_BY(lock_); |
| 253 | |
| 254 | // Whether a collection has already been done on the current capacity. |
| 255 | bool has_done_one_collection_ GUARDED_BY(lock_); |
| 256 | |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 257 | // Last time the the code_cache was updated. |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 258 | // It is atomic to avoid locking when reading it. |
| 259 | Atomic<uint64_t> last_update_time_ns_; |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 260 | |
Nicolas Geoffray | a25dce9 | 2016-01-12 16:41:10 +0000 | [diff] [blame] | 261 | // Whether we can do garbage collection. |
| 262 | const bool garbage_collect_code_; |
| 263 | |
Mathieu Chartier | 3130cdf | 2015-05-03 15:20:23 -0700 | [diff] [blame] | 264 | DISALLOW_IMPLICIT_CONSTRUCTORS(JitCodeCache); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 265 | }; |
| 266 | |
| 267 | |
| 268 | } // namespace jit |
| 269 | } // namespace art |
| 270 | |
| 271 | #endif // ART_RUNTIME_JIT_JIT_CODE_CACHE_H_ |