blob: ac53532126250e2618f937217934c6ddb7943a75 [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_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 Geoffray1dad3f62015-10-23 14:59:54 +010025#include "gc/accounting/bitmap.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080026#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
33namespace art {
34
Mathieu Chartiere401d142015-04-22 13:56:20 -070035class ArtMethod;
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010036class LinearAlloc;
Nicolas Geoffray26705e22015-10-28 12:50:11 +000037class ProfilingInfo;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080038
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080039namespace jit {
40
41class JitInstrumentationCache;
42
Nicolas Geoffray0a3be162015-11-18 11:15:22 +000043// Alignment in bits that will suit all architectures.
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010044static constexpr int kJitCodeAlignment = 16;
45using CodeCacheBitmap = gc::accounting::MemoryRangeBitmap<kJitCodeAlignment>;
46
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080047class JitCodeCache {
48 public:
Nicolas Geoffray0a3be162015-11-18 11:15:22 +000049 static constexpr size_t kMaxCapacity = 64 * MB;
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010050 // Put the default to a very low amount for debug builds to stress the code cache
51 // collection.
Nicolas Geoffray0a3be162015-11-18 11:15:22 +000052 static constexpr size_t kInitialCapacity = kIsDebugBuild ? 16 * KB : 64 * KB;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080053
Mathieu Chartierbce416f2015-03-23 12:37:35 -070054 // Create the code cache with a code + data capacity equal to "capacity", error message is passed
55 // in the out arg error_msg.
Nicolas Geoffraya25dce92016-01-12 16:41:10 +000056 static JitCodeCache* Create(size_t initial_capacity,
57 size_t max_capacity,
58 bool generate_debug_info,
59 std::string* error_msg);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080060
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010061 // Number of bytes allocated in the code cache.
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +010062 size_t CodeCacheSize() REQUIRES(!lock_);
63
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010064 // Number of bytes allocated in the data cache.
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +010065 size_t DataCacheSize() REQUIRES(!lock_);
66
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010067 // 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 Geoffray0a522232016-01-19 09:34:58 +000071 // Number of compilations done throughout the lifetime of the JIT.
72 size_t NumberOfCompilations() REQUIRES(!lock_);
73
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +000074 bool NotifyCompilationOf(ArtMethod* method, Thread* self, bool osr)
Nicolas Geoffray73be1e82015-09-17 15:22:56 +010075 SHARED_REQUIRES(Locks::mutator_lock_)
76 REQUIRES(!lock_);
77
78 void DoneCompiling(ArtMethod* method, Thread* self)
79 SHARED_REQUIRES(Locks::mutator_lock_)
80 REQUIRES(!lock_);
81
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +010082 // Allocate and write code and its metadata to the code cache.
83 uint8_t* CommitCode(Thread* self,
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010084 ArtMethod* method,
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +010085 const uint8_t* mapping_table,
86 const uint8_t* vmap_table,
87 const uint8_t* gc_map,
88 size_t frame_size_in_bytes,
89 size_t core_spill_mask,
90 size_t fp_spill_mask,
91 const uint8_t* code,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +000092 size_t code_size,
93 bool osr)
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010094 SHARED_REQUIRES(Locks::mutator_lock_)
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +010095 REQUIRES(!lock_);
96
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010097 // Return true if the code cache contains this pc.
98 bool ContainsPc(const void* pc) const;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080099
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000100 // Return true if the code cache contains this method.
101 bool ContainsMethod(ArtMethod* method) REQUIRES(!lock_);
102
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100103 // Reserve a region of data of size at least "size". Returns null if there is no more room.
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100104 uint8_t* ReserveData(Thread* self, size_t size)
105 SHARED_REQUIRES(Locks::mutator_lock_)
106 REQUIRES(!lock_);
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100107
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000108 // Clear data from the data portion of the code cache.
109 void ClearData(Thread* self, void* data)
110 SHARED_REQUIRES(Locks::mutator_lock_)
111 REQUIRES(!lock_);
112
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700113 // Add a data array of size (end - begin) with the associated contents, returns null if there
Mathieu Chartierbce416f2015-03-23 12:37:35 -0700114 // is no more room.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800115 uint8_t* AddDataArray(Thread* self, const uint8_t* begin, const uint8_t* end)
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100116 SHARED_REQUIRES(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -0700117 REQUIRES(!lock_);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800118
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100119 CodeCacheBitmap* GetLiveBitmap() const {
120 return live_bitmap_.get();
121 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800122
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100123 // Perform a collection on the code cache.
124 void GarbageCollectCache(Thread* self)
125 REQUIRES(!lock_)
126 SHARED_REQUIRES(Locks::mutator_lock_);
127
128 // Given the 'pc', try to find the JIT compiled code associated with it.
129 // Return null if 'pc' is not in the code cache. 'method' is passed for
130 // sanity check.
131 OatQuickMethodHeader* LookupMethodHeader(uintptr_t pc, ArtMethod* method)
132 REQUIRES(!lock_)
133 SHARED_REQUIRES(Locks::mutator_lock_);
134
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000135 OatQuickMethodHeader* LookupOsrMethodHeader(ArtMethod* method)
136 REQUIRES(!lock_)
137 SHARED_REQUIRES(Locks::mutator_lock_);
138
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000139 // Remove all methods in our cache that were allocated by 'alloc'.
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100140 void RemoveMethodsIn(Thread* self, const LinearAlloc& alloc)
141 REQUIRES(!lock_)
142 REQUIRES(Locks::classlinker_classes_lock_)
143 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800144
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000145 // Create a 'ProfileInfo' for 'method'. If 'retry_allocation' is true,
146 // will collect and retry if the first allocation is unsuccessful.
147 ProfilingInfo* AddProfilingInfo(Thread* self,
148 ArtMethod* method,
149 const std::vector<uint32_t>& entries,
150 bool retry_allocation)
151 REQUIRES(!lock_)
152 SHARED_REQUIRES(Locks::mutator_lock_);
153
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000154 bool OwnsSpace(const void* mspace) const NO_THREAD_SAFETY_ANALYSIS {
155 return mspace == code_mspace_ || mspace == data_mspace_;
156 }
157
158 void* MoreCore(const void* mspace, intptr_t increment);
159
Calin Juravle66f55232015-12-08 15:09:10 +0000160 // Adds to `methods` all the compiled ArtMethods which are part of any of the given dex locations.
Calin Juravleb4eddd22016-01-13 15:52:33 -0800161 void GetCompiledArtMethods(const std::set<std::string>& dex_base_locations,
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000162 std::vector<ArtMethod*>& methods)
Calin Juravle31f2c152015-10-23 17:56:15 +0100163 REQUIRES(!lock_)
164 SHARED_REQUIRES(Locks::mutator_lock_);
165
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000166 uint64_t GetLastUpdateTimeNs() const;
Calin Juravle31f2c152015-10-23 17:56:15 +0100167
Nicolas Geoffrayaee21562015-12-15 16:39:44 +0000168 size_t GetCurrentCapacity() REQUIRES(!lock_) {
169 MutexLock lock(Thread::Current(), lock_);
170 return current_capacity_;
171 }
172
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000173 size_t GetMemorySizeOfCodePointer(const void* ptr) REQUIRES(!lock_);
174
Nicolas Geoffrayb88d59e2016-02-17 11:31:49 +0000175 void InvalidateCompiledCodeFor(ArtMethod* method, const OatQuickMethodHeader* code)
176 REQUIRES(!lock_)
177 SHARED_REQUIRES(Locks::mutator_lock_);
178
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800179 private:
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000180 // Take ownership of maps.
181 JitCodeCache(MemMap* code_map,
182 MemMap* data_map,
183 size_t initial_code_capacity,
184 size_t initial_data_capacity,
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000185 size_t max_capacity,
186 bool garbage_collect_code);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800187
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100188 // Internal version of 'CommitCode' that will not retry if the
189 // allocation fails. Return null if the allocation fails.
190 uint8_t* CommitCodeInternal(Thread* self,
191 ArtMethod* method,
192 const uint8_t* mapping_table,
193 const uint8_t* vmap_table,
194 const uint8_t* gc_map,
195 size_t frame_size_in_bytes,
196 size_t core_spill_mask,
197 size_t fp_spill_mask,
198 const uint8_t* code,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000199 size_t code_size,
200 bool osr)
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100201 REQUIRES(!lock_)
202 SHARED_REQUIRES(Locks::mutator_lock_);
203
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000204 ProfilingInfo* AddProfilingInfoInternal(Thread* self,
205 ArtMethod* method,
206 const std::vector<uint32_t>& entries)
207 REQUIRES(!lock_)
208 SHARED_REQUIRES(Locks::mutator_lock_);
209
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100210 // If a collection is in progress, wait for it to finish. Return
211 // whether the thread actually waited.
212 bool WaitForPotentialCollectionToComplete(Thread* self)
213 REQUIRES(lock_) REQUIRES(!Locks::mutator_lock_);
214
215 // Free in the mspace allocations taken by 'method'.
216 void FreeCode(const void* code_ptr, ArtMethod* method) REQUIRES(lock_);
217
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000218 // Number of bytes allocated in the code cache.
219 size_t CodeCacheSizeLocked() REQUIRES(lock_);
220
221 // Number of bytes allocated in the data cache.
222 size_t DataCacheSizeLocked() REQUIRES(lock_);
223
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000224 // Notify all waiting threads that a collection is done.
225 void NotifyCollectionDone(Thread* self) REQUIRES(lock_);
226
227 // Try to increase the current capacity of the code cache. Return whether we
228 // succeeded at doing so.
229 bool IncreaseCodeCacheCapacity() REQUIRES(lock_);
230
231 // Set the footprint limit of the code cache.
232 void SetFootprintLimit(size_t new_footprint) REQUIRES(lock_);
233
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100234 // Lock for guarding allocations, collections, and the method_code_map_.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800235 Mutex lock_;
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100236 // Condition to wait on during collection.
237 ConditionVariable lock_cond_ GUARDED_BY(lock_);
238 // Whether there is a code cache collection in progress.
239 bool collection_in_progress_ GUARDED_BY(lock_);
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100240 // Mem map which holds code.
241 std::unique_ptr<MemMap> code_map_;
242 // Mem map which holds data (stack maps and profiling info).
243 std::unique_ptr<MemMap> data_map_;
244 // The opaque mspace for allocating code.
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100245 void* code_mspace_ GUARDED_BY(lock_);
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100246 // The opaque mspace for allocating data.
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100247 void* data_mspace_ GUARDED_BY(lock_);
248 // Bitmap for collecting code and data.
249 std::unique_ptr<CodeCacheBitmap> live_bitmap_;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000250 // Holds compiled code associated to the ArtMethod.
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100251 SafeMap<const void*, ArtMethod*> method_code_map_ GUARDED_BY(lock_);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000252 // Holds osr compiled code associated to the ArtMethod.
253 SafeMap<ArtMethod*, const void*> osr_code_map_ GUARDED_BY(lock_);
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000254 // ProfilingInfo objects we have allocated.
255 std::vector<ProfilingInfo*> profiling_infos_ GUARDED_BY(lock_);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800256
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000257 // The maximum capacity in bytes this code cache can go to.
258 size_t max_capacity_ GUARDED_BY(lock_);
259
260 // The current capacity in bytes of the code cache.
261 size_t current_capacity_ GUARDED_BY(lock_);
262
263 // The current footprint in bytes of the code portion of the code cache.
264 size_t code_end_ GUARDED_BY(lock_);
265
266 // The current footprint in bytes of the data portion of the code cache.
267 size_t data_end_ GUARDED_BY(lock_);
268
269 // Whether a collection has already been done on the current capacity.
270 bool has_done_one_collection_ GUARDED_BY(lock_);
271
Calin Juravle31f2c152015-10-23 17:56:15 +0100272 // Last time the the code_cache was updated.
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000273 // It is atomic to avoid locking when reading it.
274 Atomic<uint64_t> last_update_time_ns_;
Calin Juravle31f2c152015-10-23 17:56:15 +0100275
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000276 // Whether we can do garbage collection.
277 const bool garbage_collect_code_;
278
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +0000279 // The size in bytes of used memory for the data portion of the code cache.
280 size_t used_memory_for_data_ GUARDED_BY(lock_);
281
282 // The size in bytes of used memory for the code portion of the code cache.
283 size_t used_memory_for_code_ GUARDED_BY(lock_);
284
285 void FreeCode(uint8_t* code) REQUIRES(lock_);
286 uint8_t* AllocateCode(size_t code_size) REQUIRES(lock_);
287 void FreeData(uint8_t* data) REQUIRES(lock_);
288 uint8_t* AllocateData(size_t data_size) REQUIRES(lock_);
289
Nicolas Geoffray0a522232016-01-19 09:34:58 +0000290 // Number of compilations done throughout the lifetime of the JIT.
291 size_t number_of_compilations_ GUARDED_BY(lock_);
292
Mathieu Chartier3130cdf2015-05-03 15:20:23 -0700293 DISALLOW_IMPLICIT_CONSTRUCTORS(JitCodeCache);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800294};
295
296
297} // namespace jit
298} // namespace art
299
300#endif // ART_RUNTIME_JIT_JIT_CODE_CACHE_H_