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" |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 26 | #include "gc/allocator/dlmalloc.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 27 | #include "gc_root.h" |
| 28 | #include "jni.h" |
| 29 | #include "oat_file.h" |
| 30 | #include "object_callbacks.h" |
| 31 | #include "safe_map.h" |
| 32 | #include "thread_pool.h" |
| 33 | |
| 34 | namespace art { |
| 35 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 36 | class ArtMethod; |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 37 | class LinearAlloc; |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 38 | class ProfilingInfo; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 39 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 40 | namespace jit { |
| 41 | |
| 42 | class JitInstrumentationCache; |
| 43 | |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 44 | // Alignment that will suit all architectures. |
| 45 | static constexpr int kJitCodeAlignment = 16; |
| 46 | using CodeCacheBitmap = gc::accounting::MemoryRangeBitmap<kJitCodeAlignment>; |
| 47 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 48 | class JitCodeCache { |
| 49 | public: |
| 50 | static constexpr size_t kMaxCapacity = 1 * GB; |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 51 | // Put the default to a very low amount for debug builds to stress the code cache |
| 52 | // collection. |
| 53 | static constexpr size_t kDefaultCapacity = kIsDebugBuild ? 20 * KB : 2 * MB; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 54 | |
Mathieu Chartier | bce416f | 2015-03-23 12:37:35 -0700 | [diff] [blame] | 55 | // Create the code cache with a code + data capacity equal to "capacity", error message is passed |
| 56 | // in the out arg error_msg. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 57 | static JitCodeCache* Create(size_t capacity, std::string* error_msg); |
| 58 | |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 59 | // Number of bytes allocated in the code cache. |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 60 | size_t CodeCacheSize() REQUIRES(!lock_); |
| 61 | |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 62 | // Number of bytes allocated in the data cache. |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 63 | size_t DataCacheSize() REQUIRES(!lock_); |
| 64 | |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 65 | // Number of compiled code in the code cache. Note that this is not the number |
| 66 | // of methods that got JIT compiled, as we might have collected some. |
| 67 | size_t NumberOfCompiledCode() REQUIRES(!lock_); |
| 68 | |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 69 | // Allocate and write code and its metadata to the code cache. |
| 70 | uint8_t* CommitCode(Thread* self, |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 71 | ArtMethod* method, |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 72 | const uint8_t* mapping_table, |
| 73 | const uint8_t* vmap_table, |
| 74 | const uint8_t* gc_map, |
| 75 | size_t frame_size_in_bytes, |
| 76 | size_t core_spill_mask, |
| 77 | size_t fp_spill_mask, |
| 78 | const uint8_t* code, |
| 79 | size_t code_size) |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 80 | SHARED_REQUIRES(Locks::mutator_lock_) |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 81 | REQUIRES(!lock_); |
| 82 | |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 83 | // Return true if the code cache contains this pc. |
| 84 | bool ContainsPc(const void* pc) const; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 85 | |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 86 | // 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] | 87 | uint8_t* ReserveData(Thread* self, size_t size) |
| 88 | SHARED_REQUIRES(Locks::mutator_lock_) |
| 89 | REQUIRES(!lock_); |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 90 | |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 91 | // 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] | 92 | // is no more room. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 93 | 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] | 94 | SHARED_REQUIRES(Locks::mutator_lock_) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 95 | REQUIRES(!lock_); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 96 | |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 97 | CodeCacheBitmap* GetLiveBitmap() const { |
| 98 | return live_bitmap_.get(); |
| 99 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 100 | |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 101 | // Perform a collection on the code cache. |
| 102 | void GarbageCollectCache(Thread* self) |
| 103 | REQUIRES(!lock_) |
| 104 | SHARED_REQUIRES(Locks::mutator_lock_); |
| 105 | |
| 106 | // Given the 'pc', try to find the JIT compiled code associated with it. |
| 107 | // Return null if 'pc' is not in the code cache. 'method' is passed for |
| 108 | // sanity check. |
| 109 | OatQuickMethodHeader* LookupMethodHeader(uintptr_t pc, ArtMethod* method) |
| 110 | REQUIRES(!lock_) |
| 111 | SHARED_REQUIRES(Locks::mutator_lock_); |
| 112 | |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 113 | // Remove all methods in our cache that were allocated by 'alloc'. |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 114 | void RemoveMethodsIn(Thread* self, const LinearAlloc& alloc) |
| 115 | REQUIRES(!lock_) |
| 116 | REQUIRES(Locks::classlinker_classes_lock_) |
| 117 | SHARED_REQUIRES(Locks::mutator_lock_); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 118 | |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 119 | // Create a 'ProfileInfo' for 'method'. If 'retry_allocation' is true, |
| 120 | // will collect and retry if the first allocation is unsuccessful. |
| 121 | ProfilingInfo* AddProfilingInfo(Thread* self, |
| 122 | ArtMethod* method, |
| 123 | const std::vector<uint32_t>& entries, |
| 124 | bool retry_allocation) |
| 125 | REQUIRES(!lock_) |
| 126 | SHARED_REQUIRES(Locks::mutator_lock_); |
| 127 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 128 | private: |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 129 | // Take ownership of code_mem_map. |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 130 | JitCodeCache(MemMap* code_map, MemMap* data_map); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 131 | |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 132 | // Internal version of 'CommitCode' that will not retry if the |
| 133 | // allocation fails. Return null if the allocation fails. |
| 134 | uint8_t* CommitCodeInternal(Thread* self, |
| 135 | ArtMethod* method, |
| 136 | const uint8_t* mapping_table, |
| 137 | const uint8_t* vmap_table, |
| 138 | const uint8_t* gc_map, |
| 139 | size_t frame_size_in_bytes, |
| 140 | size_t core_spill_mask, |
| 141 | size_t fp_spill_mask, |
| 142 | const uint8_t* code, |
| 143 | size_t code_size) |
| 144 | REQUIRES(!lock_) |
| 145 | SHARED_REQUIRES(Locks::mutator_lock_); |
| 146 | |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 147 | ProfilingInfo* AddProfilingInfoInternal(Thread* self, |
| 148 | ArtMethod* method, |
| 149 | const std::vector<uint32_t>& entries) |
| 150 | REQUIRES(!lock_) |
| 151 | SHARED_REQUIRES(Locks::mutator_lock_); |
| 152 | |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 153 | // If a collection is in progress, wait for it to finish. Return |
| 154 | // whether the thread actually waited. |
| 155 | bool WaitForPotentialCollectionToComplete(Thread* self) |
| 156 | REQUIRES(lock_) REQUIRES(!Locks::mutator_lock_); |
| 157 | |
| 158 | // Free in the mspace allocations taken by 'method'. |
| 159 | void FreeCode(const void* code_ptr, ArtMethod* method) REQUIRES(lock_); |
| 160 | |
| 161 | // Lock for guarding allocations, collections, and the method_code_map_. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 162 | Mutex lock_; |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 163 | // Condition to wait on during collection. |
| 164 | ConditionVariable lock_cond_ GUARDED_BY(lock_); |
| 165 | // Whether there is a code cache collection in progress. |
| 166 | bool collection_in_progress_ GUARDED_BY(lock_); |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 167 | // Mem map which holds code. |
| 168 | std::unique_ptr<MemMap> code_map_; |
| 169 | // Mem map which holds data (stack maps and profiling info). |
| 170 | std::unique_ptr<MemMap> data_map_; |
| 171 | // The opaque mspace for allocating code. |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 172 | void* code_mspace_ GUARDED_BY(lock_); |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 173 | // The opaque mspace for allocating data. |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 174 | void* data_mspace_ GUARDED_BY(lock_); |
| 175 | // Bitmap for collecting code and data. |
| 176 | std::unique_ptr<CodeCacheBitmap> live_bitmap_; |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 177 | // This map holds compiled code associated to the ArtMethod. |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 178 | SafeMap<const void*, ArtMethod*> method_code_map_ GUARDED_BY(lock_); |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 179 | // ProfilingInfo objects we have allocated. |
| 180 | std::vector<ProfilingInfo*> profiling_infos_ GUARDED_BY(lock_); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 181 | |
Mathieu Chartier | 3130cdf | 2015-05-03 15:20:23 -0700 | [diff] [blame] | 182 | DISALLOW_IMPLICIT_CONSTRUCTORS(JitCodeCache); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 183 | }; |
| 184 | |
| 185 | |
| 186 | } // namespace jit |
| 187 | } // namespace art |
| 188 | |
| 189 | #endif // ART_RUNTIME_JIT_JIT_CODE_CACHE_H_ |