blob: 98dd70dcf98d02acdedeca58ff8a3116aff17242 [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 Geoffray7ca4b772016-02-23 13:52:01 +000052 static constexpr size_t kInitialCapacity = kIsDebugBuild ? 8 * KB : 64 * KB;
Nicolas Geoffray65b83d82016-02-22 13:14:04 +000053
54 // By default, do not GC until reaching 256KB.
55 static constexpr size_t kReservedCapacity = kInitialCapacity * 4;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080056
Mathieu Chartierbce416f2015-03-23 12:37:35 -070057 // Create the code cache with a code + data capacity equal to "capacity", error message is passed
58 // in the out arg error_msg.
Nicolas Geoffraya25dce92016-01-12 16:41:10 +000059 static JitCodeCache* Create(size_t initial_capacity,
60 size_t max_capacity,
61 bool generate_debug_info,
62 std::string* error_msg);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080063
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010064 // Number of bytes allocated in the code cache.
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +010065 size_t CodeCacheSize() REQUIRES(!lock_);
66
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010067 // Number of bytes allocated in the data cache.
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +010068 size_t DataCacheSize() REQUIRES(!lock_);
69
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +000070 bool NotifyCompilationOf(ArtMethod* method, Thread* self, bool osr)
Nicolas Geoffray73be1e82015-09-17 15:22:56 +010071 SHARED_REQUIRES(Locks::mutator_lock_)
72 REQUIRES(!lock_);
73
Nicolas Geoffray07e3ca92016-03-11 09:57:57 +000074 // Notify to the code cache that the compiler wants to use the
75 // profiling info of `method` to drive optimizations,
76 // and therefore ensure the returned profiling info object is not
77 // collected.
78 ProfilingInfo* NotifyCompilerUse(ArtMethod* method, Thread* self)
Nicolas Geoffrayb6e20ae2016-03-07 14:29:04 +000079 SHARED_REQUIRES(Locks::mutator_lock_)
80 REQUIRES(!lock_);
81
Nicolas Geoffray73be1e82015-09-17 15:22:56 +010082 void DoneCompiling(ArtMethod* method, Thread* self)
83 SHARED_REQUIRES(Locks::mutator_lock_)
84 REQUIRES(!lock_);
85
Nicolas Geoffray07e3ca92016-03-11 09:57:57 +000086 void DoneCompilerUse(ArtMethod* method, Thread* self)
Nicolas Geoffrayb6e20ae2016-03-07 14:29:04 +000087 SHARED_REQUIRES(Locks::mutator_lock_)
88 REQUIRES(!lock_);
89
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +010090 // Allocate and write code and its metadata to the code cache.
91 uint8_t* CommitCode(Thread* self,
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010092 ArtMethod* method,
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +010093 const uint8_t* mapping_table,
94 const uint8_t* vmap_table,
95 const uint8_t* gc_map,
96 size_t frame_size_in_bytes,
97 size_t core_spill_mask,
98 size_t fp_spill_mask,
99 const uint8_t* code,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000100 size_t code_size,
101 bool osr)
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100102 SHARED_REQUIRES(Locks::mutator_lock_)
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100103 REQUIRES(!lock_);
104
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100105 // Return true if the code cache contains this pc.
106 bool ContainsPc(const void* pc) const;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800107
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000108 // Return true if the code cache contains this method.
109 bool ContainsMethod(ArtMethod* method) REQUIRES(!lock_);
110
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100111 // 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 +0100112 uint8_t* ReserveData(Thread* self, size_t size)
113 SHARED_REQUIRES(Locks::mutator_lock_)
114 REQUIRES(!lock_);
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100115
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000116 // Clear data from the data portion of the code cache.
117 void ClearData(Thread* self, void* data)
118 SHARED_REQUIRES(Locks::mutator_lock_)
119 REQUIRES(!lock_);
120
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700121 // Add a data array of size (end - begin) with the associated contents, returns null if there
Mathieu Chartierbce416f2015-03-23 12:37:35 -0700122 // is no more room.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800123 uint8_t* AddDataArray(Thread* self, const uint8_t* begin, const uint8_t* end)
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100124 SHARED_REQUIRES(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -0700125 REQUIRES(!lock_);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800126
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100127 CodeCacheBitmap* GetLiveBitmap() const {
128 return live_bitmap_.get();
129 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800130
Nicolas Geoffray35122442016-03-02 12:05:30 +0000131 // Return whether we should do a full collection given the current state of the cache.
132 bool ShouldDoFullCollection()
133 REQUIRES(lock_)
134 SHARED_REQUIRES(Locks::mutator_lock_);
135
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100136 // Perform a collection on the code cache.
137 void GarbageCollectCache(Thread* self)
138 REQUIRES(!lock_)
139 SHARED_REQUIRES(Locks::mutator_lock_);
140
141 // Given the 'pc', try to find the JIT compiled code associated with it.
142 // Return null if 'pc' is not in the code cache. 'method' is passed for
143 // sanity check.
144 OatQuickMethodHeader* LookupMethodHeader(uintptr_t pc, ArtMethod* method)
145 REQUIRES(!lock_)
146 SHARED_REQUIRES(Locks::mutator_lock_);
147
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000148 OatQuickMethodHeader* LookupOsrMethodHeader(ArtMethod* method)
149 REQUIRES(!lock_)
150 SHARED_REQUIRES(Locks::mutator_lock_);
151
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000152 // Remove all methods in our cache that were allocated by 'alloc'.
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100153 void RemoveMethodsIn(Thread* self, const LinearAlloc& alloc)
154 REQUIRES(!lock_)
155 REQUIRES(Locks::classlinker_classes_lock_)
156 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800157
Nicolas Geoffrayb6e20ae2016-03-07 14:29:04 +0000158 void ClearGcRootsInInlineCaches(Thread* self) REQUIRES(!lock_);
159
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000160 // Create a 'ProfileInfo' for 'method'. If 'retry_allocation' is true,
161 // will collect and retry if the first allocation is unsuccessful.
162 ProfilingInfo* AddProfilingInfo(Thread* self,
163 ArtMethod* method,
164 const std::vector<uint32_t>& entries,
165 bool retry_allocation)
166 REQUIRES(!lock_)
167 SHARED_REQUIRES(Locks::mutator_lock_);
168
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000169 bool OwnsSpace(const void* mspace) const NO_THREAD_SAFETY_ANALYSIS {
170 return mspace == code_mspace_ || mspace == data_mspace_;
171 }
172
173 void* MoreCore(const void* mspace, intptr_t increment);
174
Calin Juravle66f55232015-12-08 15:09:10 +0000175 // Adds to `methods` all the compiled ArtMethods which are part of any of the given dex locations.
Calin Juravleb4eddd22016-01-13 15:52:33 -0800176 void GetCompiledArtMethods(const std::set<std::string>& dex_base_locations,
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000177 std::vector<ArtMethod*>& methods)
Calin Juravle31f2c152015-10-23 17:56:15 +0100178 REQUIRES(!lock_)
179 SHARED_REQUIRES(Locks::mutator_lock_);
180
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000181 uint64_t GetLastUpdateTimeNs() const;
Calin Juravle31f2c152015-10-23 17:56:15 +0100182
Nicolas Geoffrayaee21562015-12-15 16:39:44 +0000183 size_t GetCurrentCapacity() REQUIRES(!lock_) {
184 MutexLock lock(Thread::Current(), lock_);
185 return current_capacity_;
186 }
187
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000188 size_t GetMemorySizeOfCodePointer(const void* ptr) REQUIRES(!lock_);
189
Nicolas Geoffrayb88d59e2016-02-17 11:31:49 +0000190 void InvalidateCompiledCodeFor(ArtMethod* method, const OatQuickMethodHeader* code)
191 REQUIRES(!lock_)
192 SHARED_REQUIRES(Locks::mutator_lock_);
193
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +0000194 void Dump(std::ostream& os) REQUIRES(!lock_);
195
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800196 private:
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000197 // Take ownership of maps.
198 JitCodeCache(MemMap* code_map,
199 MemMap* data_map,
200 size_t initial_code_capacity,
201 size_t initial_data_capacity,
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000202 size_t max_capacity,
203 bool garbage_collect_code);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800204
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100205 // Internal version of 'CommitCode' that will not retry if the
206 // allocation fails. Return null if the allocation fails.
207 uint8_t* CommitCodeInternal(Thread* self,
208 ArtMethod* method,
209 const uint8_t* mapping_table,
210 const uint8_t* vmap_table,
211 const uint8_t* gc_map,
212 size_t frame_size_in_bytes,
213 size_t core_spill_mask,
214 size_t fp_spill_mask,
215 const uint8_t* code,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000216 size_t code_size,
217 bool osr)
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100218 REQUIRES(!lock_)
219 SHARED_REQUIRES(Locks::mutator_lock_);
220
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000221 ProfilingInfo* AddProfilingInfoInternal(Thread* self,
222 ArtMethod* method,
223 const std::vector<uint32_t>& entries)
Nicolas Geoffray1e7da9b2016-03-01 14:11:40 +0000224 REQUIRES(lock_)
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000225 SHARED_REQUIRES(Locks::mutator_lock_);
226
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100227 // If a collection is in progress, wait for it to finish. Return
228 // whether the thread actually waited.
229 bool WaitForPotentialCollectionToComplete(Thread* self)
230 REQUIRES(lock_) REQUIRES(!Locks::mutator_lock_);
231
232 // Free in the mspace allocations taken by 'method'.
233 void FreeCode(const void* code_ptr, ArtMethod* method) REQUIRES(lock_);
234
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000235 // Number of bytes allocated in the code cache.
236 size_t CodeCacheSizeLocked() REQUIRES(lock_);
237
238 // Number of bytes allocated in the data cache.
239 size_t DataCacheSizeLocked() REQUIRES(lock_);
240
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000241 // Notify all waiting threads that a collection is done.
242 void NotifyCollectionDone(Thread* self) REQUIRES(lock_);
243
244 // Try to increase the current capacity of the code cache. Return whether we
245 // succeeded at doing so.
246 bool IncreaseCodeCacheCapacity() REQUIRES(lock_);
247
248 // Set the footprint limit of the code cache.
249 void SetFootprintLimit(size_t new_footprint) REQUIRES(lock_);
250
Nicolas Geoffray35122442016-03-02 12:05:30 +0000251 void DoCollection(Thread* self, bool collect_profiling_info)
Nicolas Geoffray8d372502016-02-23 13:56:43 +0000252 REQUIRES(!lock_)
253 SHARED_REQUIRES(Locks::mutator_lock_);
254
Nicolas Geoffray9abb2972016-03-04 14:32:59 +0000255 void RemoveUnmarkedCode(Thread* self)
Nicolas Geoffray8d372502016-02-23 13:56:43 +0000256 REQUIRES(!lock_)
257 SHARED_REQUIRES(Locks::mutator_lock_);
258
259 void MarkCompiledCodeOnThreadStacks(Thread* self)
260 REQUIRES(!lock_)
261 SHARED_REQUIRES(Locks::mutator_lock_);
262
Nicolas Geoffray35122442016-03-02 12:05:30 +0000263 bool CheckLiveCompiledCodeHasProfilingInfo()
264 REQUIRES(lock_)
265 SHARED_REQUIRES(Locks::mutator_lock_);
266
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +0000267 void FreeCode(uint8_t* code) REQUIRES(lock_);
268 uint8_t* AllocateCode(size_t code_size) REQUIRES(lock_);
269 void FreeData(uint8_t* data) REQUIRES(lock_);
270 uint8_t* AllocateData(size_t data_size) REQUIRES(lock_);
271
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100272 // Lock for guarding allocations, collections, and the method_code_map_.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800273 Mutex lock_;
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100274 // Condition to wait on during collection.
275 ConditionVariable lock_cond_ GUARDED_BY(lock_);
276 // Whether there is a code cache collection in progress.
277 bool collection_in_progress_ GUARDED_BY(lock_);
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100278 // Mem map which holds code.
279 std::unique_ptr<MemMap> code_map_;
280 // Mem map which holds data (stack maps and profiling info).
281 std::unique_ptr<MemMap> data_map_;
282 // The opaque mspace for allocating code.
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100283 void* code_mspace_ GUARDED_BY(lock_);
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100284 // The opaque mspace for allocating data.
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100285 void* data_mspace_ GUARDED_BY(lock_);
286 // Bitmap for collecting code and data.
287 std::unique_ptr<CodeCacheBitmap> live_bitmap_;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000288 // Holds compiled code associated to the ArtMethod.
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100289 SafeMap<const void*, ArtMethod*> method_code_map_ GUARDED_BY(lock_);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000290 // Holds osr compiled code associated to the ArtMethod.
291 SafeMap<ArtMethod*, const void*> osr_code_map_ GUARDED_BY(lock_);
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000292 // ProfilingInfo objects we have allocated.
293 std::vector<ProfilingInfo*> profiling_infos_ GUARDED_BY(lock_);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800294
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000295 // The maximum capacity in bytes this code cache can go to.
296 size_t max_capacity_ GUARDED_BY(lock_);
297
298 // The current capacity in bytes of the code cache.
299 size_t current_capacity_ GUARDED_BY(lock_);
300
301 // The current footprint in bytes of the code portion of the code cache.
302 size_t code_end_ GUARDED_BY(lock_);
303
304 // The current footprint in bytes of the data portion of the code cache.
305 size_t data_end_ GUARDED_BY(lock_);
306
Nicolas Geoffray35122442016-03-02 12:05:30 +0000307 // Whether the last collection round increased the code cache.
308 bool last_collection_increased_code_cache_ GUARDED_BY(lock_);
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000309
Calin Juravle31f2c152015-10-23 17:56:15 +0100310 // Last time the the code_cache was updated.
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000311 // It is atomic to avoid locking when reading it.
312 Atomic<uint64_t> last_update_time_ns_;
Calin Juravle31f2c152015-10-23 17:56:15 +0100313
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000314 // Whether we can do garbage collection.
315 const bool garbage_collect_code_;
316
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +0000317 // The size in bytes of used memory for the data portion of the code cache.
318 size_t used_memory_for_data_ GUARDED_BY(lock_);
319
320 // The size in bytes of used memory for the code portion of the code cache.
321 size_t used_memory_for_code_ GUARDED_BY(lock_);
322
Nicolas Geoffray0a522232016-01-19 09:34:58 +0000323 // Number of compilations done throughout the lifetime of the JIT.
324 size_t number_of_compilations_ GUARDED_BY(lock_);
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +0000325
326 // Number of compilations for on-stack-replacement done throughout the lifetime of the JIT.
Nicolas Geoffrayfcdd7292016-02-25 13:27:47 +0000327 size_t number_of_osr_compilations_ GUARDED_BY(lock_);
Nicolas Geoffray0a522232016-01-19 09:34:58 +0000328
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +0000329 // Number of deoptimizations done throughout the lifetime of the JIT.
330 size_t number_of_deoptimizations_ GUARDED_BY(lock_);
331
332 // Number of code cache collections done throughout the lifetime of the JIT.
333 size_t number_of_collections_ GUARDED_BY(lock_);
334
Mathieu Chartier3130cdf2015-05-03 15:20:23 -0700335 DISALLOW_IMPLICIT_CONSTRUCTORS(JitCodeCache);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800336};
337
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800338} // namespace jit
339} // namespace art
340
341#endif // ART_RUNTIME_JIT_JIT_CODE_CACHE_H_