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; |
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 | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame^] | 43 | // Alignment that will suit all architectures. |
| 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: |
| 49 | static constexpr size_t kMaxCapacity = 1 * GB; |
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. |
| 52 | static constexpr size_t kDefaultCapacity = kIsDebugBuild ? 20 * KB : 2 * MB; |
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. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 56 | static JitCodeCache* Create(size_t capacity, std::string* error_msg); |
| 57 | |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame^] | 58 | // Number of bytes allocated in the code cache. |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 59 | size_t CodeCacheSize() REQUIRES(!lock_); |
| 60 | |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame^] | 61 | // Number of bytes allocated in the data cache. |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 62 | size_t DataCacheSize() REQUIRES(!lock_); |
| 63 | |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame^] | 64 | // Number of compiled code in the code cache. Note that this is not the number |
| 65 | // of methods that got JIT compiled, as we might have collected some. |
| 66 | size_t NumberOfCompiledCode() REQUIRES(!lock_); |
| 67 | |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 68 | // Allocate and write code and its metadata to the code cache. |
| 69 | uint8_t* CommitCode(Thread* self, |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame^] | 70 | ArtMethod* method, |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 71 | const uint8_t* mapping_table, |
| 72 | const uint8_t* vmap_table, |
| 73 | const uint8_t* gc_map, |
| 74 | size_t frame_size_in_bytes, |
| 75 | size_t core_spill_mask, |
| 76 | size_t fp_spill_mask, |
| 77 | const uint8_t* code, |
| 78 | size_t code_size) |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame^] | 79 | SHARED_REQUIRES(Locks::mutator_lock_) |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 80 | REQUIRES(!lock_); |
| 81 | |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame^] | 82 | // Return true if the code cache contains this pc. |
| 83 | bool ContainsPc(const void* pc) const; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 84 | |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 85 | // 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^] | 86 | uint8_t* ReserveData(Thread* self, size_t size) |
| 87 | SHARED_REQUIRES(Locks::mutator_lock_) |
| 88 | REQUIRES(!lock_); |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 89 | |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 90 | // 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] | 91 | // is no more room. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 92 | 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^] | 93 | SHARED_REQUIRES(Locks::mutator_lock_) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 94 | REQUIRES(!lock_); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 95 | |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame^] | 96 | CodeCacheBitmap* GetLiveBitmap() const { |
| 97 | return live_bitmap_.get(); |
| 98 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 99 | |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame^] | 100 | // Perform a collection on the code cache. |
| 101 | void GarbageCollectCache(Thread* self) |
| 102 | REQUIRES(!lock_) |
| 103 | SHARED_REQUIRES(Locks::mutator_lock_); |
| 104 | |
| 105 | // Given the 'pc', try to find the JIT compiled code associated with it. |
| 106 | // Return null if 'pc' is not in the code cache. 'method' is passed for |
| 107 | // sanity check. |
| 108 | OatQuickMethodHeader* LookupMethodHeader(uintptr_t pc, ArtMethod* method) |
| 109 | REQUIRES(!lock_) |
| 110 | SHARED_REQUIRES(Locks::mutator_lock_); |
| 111 | |
| 112 | void RemoveMethodsIn(Thread* self, const LinearAlloc& alloc) |
| 113 | REQUIRES(!lock_) |
| 114 | REQUIRES(Locks::classlinker_classes_lock_) |
| 115 | SHARED_REQUIRES(Locks::mutator_lock_); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 116 | |
| 117 | private: |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame^] | 118 | // Take ownership of code_mem_map. |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 119 | JitCodeCache(MemMap* code_map, MemMap* data_map); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 120 | |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame^] | 121 | // Internal version of 'CommitCode' that will not retry if the |
| 122 | // allocation fails. Return null if the allocation fails. |
| 123 | uint8_t* CommitCodeInternal(Thread* self, |
| 124 | ArtMethod* method, |
| 125 | const uint8_t* mapping_table, |
| 126 | const uint8_t* vmap_table, |
| 127 | const uint8_t* gc_map, |
| 128 | size_t frame_size_in_bytes, |
| 129 | size_t core_spill_mask, |
| 130 | size_t fp_spill_mask, |
| 131 | const uint8_t* code, |
| 132 | size_t code_size) |
| 133 | REQUIRES(!lock_) |
| 134 | SHARED_REQUIRES(Locks::mutator_lock_); |
| 135 | |
| 136 | // If a collection is in progress, wait for it to finish. Return |
| 137 | // whether the thread actually waited. |
| 138 | bool WaitForPotentialCollectionToComplete(Thread* self) |
| 139 | REQUIRES(lock_) REQUIRES(!Locks::mutator_lock_); |
| 140 | |
| 141 | // Free in the mspace allocations taken by 'method'. |
| 142 | void FreeCode(const void* code_ptr, ArtMethod* method) REQUIRES(lock_); |
| 143 | |
| 144 | // Lock for guarding allocations, collections, and the method_code_map_. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 145 | Mutex lock_; |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame^] | 146 | // Condition to wait on during collection. |
| 147 | ConditionVariable lock_cond_ GUARDED_BY(lock_); |
| 148 | // Whether there is a code cache collection in progress. |
| 149 | bool collection_in_progress_ GUARDED_BY(lock_); |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 150 | // Mem map which holds code. |
| 151 | std::unique_ptr<MemMap> code_map_; |
| 152 | // Mem map which holds data (stack maps and profiling info). |
| 153 | std::unique_ptr<MemMap> data_map_; |
| 154 | // The opaque mspace for allocating code. |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame^] | 155 | void* code_mspace_ GUARDED_BY(lock_); |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 156 | // The opaque mspace for allocating data. |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame^] | 157 | void* data_mspace_ GUARDED_BY(lock_); |
| 158 | // Bitmap for collecting code and data. |
| 159 | std::unique_ptr<CodeCacheBitmap> live_bitmap_; |
| 160 | // This map holds compiled code associated to the ArtMethod |
| 161 | SafeMap<const void*, ArtMethod*> method_code_map_ GUARDED_BY(lock_); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 162 | |
Mathieu Chartier | 3130cdf | 2015-05-03 15:20:23 -0700 | [diff] [blame] | 163 | DISALLOW_IMPLICIT_CONSTRUCTORS(JitCodeCache); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 164 | }; |
| 165 | |
| 166 | |
| 167 | } // namespace jit |
| 168 | } // namespace art |
| 169 | |
| 170 | #endif // ART_RUNTIME_JIT_JIT_CODE_CACHE_H_ |