blob: 0bd4f7dd1b12bba4deba0e95d24323e90cf9f1dd [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
74 void DoneCompiling(ArtMethod* method, Thread* self)
75 SHARED_REQUIRES(Locks::mutator_lock_)
76 REQUIRES(!lock_);
77
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +010078 // Allocate and write code and its metadata to the code cache.
79 uint8_t* CommitCode(Thread* self,
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010080 ArtMethod* method,
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +010081 const uint8_t* mapping_table,
82 const uint8_t* vmap_table,
83 const uint8_t* gc_map,
84 size_t frame_size_in_bytes,
85 size_t core_spill_mask,
86 size_t fp_spill_mask,
87 const uint8_t* code,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +000088 size_t code_size,
89 bool osr)
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010090 SHARED_REQUIRES(Locks::mutator_lock_)
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +010091 REQUIRES(!lock_);
92
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010093 // Return true if the code cache contains this pc.
94 bool ContainsPc(const void* pc) const;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080095
Nicolas Geoffraya5891e82015-11-06 14:18:27 +000096 // Return true if the code cache contains this method.
97 bool ContainsMethod(ArtMethod* method) REQUIRES(!lock_);
98
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010099 // 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 +0100100 uint8_t* ReserveData(Thread* self, size_t size)
101 SHARED_REQUIRES(Locks::mutator_lock_)
102 REQUIRES(!lock_);
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100103
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000104 // 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 Chartier2cebb242015-04-21 16:50:40 -0700109 // Add a data array of size (end - begin) with the associated contents, returns null if there
Mathieu Chartierbce416f2015-03-23 12:37:35 -0700110 // is no more room.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800111 uint8_t* AddDataArray(Thread* self, const uint8_t* begin, const uint8_t* end)
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100112 SHARED_REQUIRES(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -0700113 REQUIRES(!lock_);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800114
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100115 CodeCacheBitmap* GetLiveBitmap() const {
116 return live_bitmap_.get();
117 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800118
Nicolas Geoffray35122442016-03-02 12:05:30 +0000119 // Return whether we should do a full collection given the current state of the cache.
120 bool ShouldDoFullCollection()
121 REQUIRES(lock_)
122 SHARED_REQUIRES(Locks::mutator_lock_);
123
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100124 // Perform a collection on the code cache.
125 void GarbageCollectCache(Thread* self)
126 REQUIRES(!lock_)
127 SHARED_REQUIRES(Locks::mutator_lock_);
128
129 // Given the 'pc', try to find the JIT compiled code associated with it.
130 // Return null if 'pc' is not in the code cache. 'method' is passed for
131 // sanity check.
132 OatQuickMethodHeader* LookupMethodHeader(uintptr_t pc, ArtMethod* method)
133 REQUIRES(!lock_)
134 SHARED_REQUIRES(Locks::mutator_lock_);
135
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000136 OatQuickMethodHeader* LookupOsrMethodHeader(ArtMethod* method)
137 REQUIRES(!lock_)
138 SHARED_REQUIRES(Locks::mutator_lock_);
139
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000140 // Remove all methods in our cache that were allocated by 'alloc'.
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100141 void RemoveMethodsIn(Thread* self, const LinearAlloc& alloc)
142 REQUIRES(!lock_)
143 REQUIRES(Locks::classlinker_classes_lock_)
144 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800145
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000146 // Create a 'ProfileInfo' for 'method'. If 'retry_allocation' is true,
147 // will collect and retry if the first allocation is unsuccessful.
148 ProfilingInfo* AddProfilingInfo(Thread* self,
149 ArtMethod* method,
150 const std::vector<uint32_t>& entries,
151 bool retry_allocation)
152 REQUIRES(!lock_)
153 SHARED_REQUIRES(Locks::mutator_lock_);
154
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000155 bool OwnsSpace(const void* mspace) const NO_THREAD_SAFETY_ANALYSIS {
156 return mspace == code_mspace_ || mspace == data_mspace_;
157 }
158
159 void* MoreCore(const void* mspace, intptr_t increment);
160
Calin Juravle66f55232015-12-08 15:09:10 +0000161 // Adds to `methods` all the compiled ArtMethods which are part of any of the given dex locations.
Calin Juravleb4eddd22016-01-13 15:52:33 -0800162 void GetCompiledArtMethods(const std::set<std::string>& dex_base_locations,
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000163 std::vector<ArtMethod*>& methods)
Calin Juravle31f2c152015-10-23 17:56:15 +0100164 REQUIRES(!lock_)
165 SHARED_REQUIRES(Locks::mutator_lock_);
166
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000167 uint64_t GetLastUpdateTimeNs() const;
Calin Juravle31f2c152015-10-23 17:56:15 +0100168
Nicolas Geoffrayaee21562015-12-15 16:39:44 +0000169 size_t GetCurrentCapacity() REQUIRES(!lock_) {
170 MutexLock lock(Thread::Current(), lock_);
171 return current_capacity_;
172 }
173
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000174 size_t GetMemorySizeOfCodePointer(const void* ptr) REQUIRES(!lock_);
175
Nicolas Geoffrayb88d59e2016-02-17 11:31:49 +0000176 void InvalidateCompiledCodeFor(ArtMethod* method, const OatQuickMethodHeader* code)
177 REQUIRES(!lock_)
178 SHARED_REQUIRES(Locks::mutator_lock_);
179
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +0000180 void Dump(std::ostream& os) REQUIRES(!lock_);
181
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800182 private:
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000183 // Take ownership of maps.
184 JitCodeCache(MemMap* code_map,
185 MemMap* data_map,
186 size_t initial_code_capacity,
187 size_t initial_data_capacity,
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000188 size_t max_capacity,
189 bool garbage_collect_code);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800190
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100191 // Internal version of 'CommitCode' that will not retry if the
192 // allocation fails. Return null if the allocation fails.
193 uint8_t* CommitCodeInternal(Thread* self,
194 ArtMethod* method,
195 const uint8_t* mapping_table,
196 const uint8_t* vmap_table,
197 const uint8_t* gc_map,
198 size_t frame_size_in_bytes,
199 size_t core_spill_mask,
200 size_t fp_spill_mask,
201 const uint8_t* code,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000202 size_t code_size,
203 bool osr)
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100204 REQUIRES(!lock_)
205 SHARED_REQUIRES(Locks::mutator_lock_);
206
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000207 ProfilingInfo* AddProfilingInfoInternal(Thread* self,
208 ArtMethod* method,
209 const std::vector<uint32_t>& entries)
Nicolas Geoffray1e7da9b2016-03-01 14:11:40 +0000210 REQUIRES(lock_)
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000211 SHARED_REQUIRES(Locks::mutator_lock_);
212
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100213 // If a collection is in progress, wait for it to finish. Return
214 // whether the thread actually waited.
215 bool WaitForPotentialCollectionToComplete(Thread* self)
216 REQUIRES(lock_) REQUIRES(!Locks::mutator_lock_);
217
218 // Free in the mspace allocations taken by 'method'.
219 void FreeCode(const void* code_ptr, ArtMethod* method) REQUIRES(lock_);
220
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000221 // Number of bytes allocated in the code cache.
222 size_t CodeCacheSizeLocked() REQUIRES(lock_);
223
224 // Number of bytes allocated in the data cache.
225 size_t DataCacheSizeLocked() REQUIRES(lock_);
226
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000227 // Notify all waiting threads that a collection is done.
228 void NotifyCollectionDone(Thread* self) REQUIRES(lock_);
229
230 // Try to increase the current capacity of the code cache. Return whether we
231 // succeeded at doing so.
232 bool IncreaseCodeCacheCapacity() REQUIRES(lock_);
233
234 // Set the footprint limit of the code cache.
235 void SetFootprintLimit(size_t new_footprint) REQUIRES(lock_);
236
Nicolas Geoffray35122442016-03-02 12:05:30 +0000237 void DoCollection(Thread* self, bool collect_profiling_info)
Nicolas Geoffray8d372502016-02-23 13:56:43 +0000238 REQUIRES(!lock_)
239 SHARED_REQUIRES(Locks::mutator_lock_);
240
Nicolas Geoffray9abb2972016-03-04 14:32:59 +0000241 void RemoveUnmarkedCode(Thread* self)
Nicolas Geoffray8d372502016-02-23 13:56:43 +0000242 REQUIRES(!lock_)
243 SHARED_REQUIRES(Locks::mutator_lock_);
244
245 void MarkCompiledCodeOnThreadStacks(Thread* self)
246 REQUIRES(!lock_)
247 SHARED_REQUIRES(Locks::mutator_lock_);
248
Nicolas Geoffray35122442016-03-02 12:05:30 +0000249 bool CheckLiveCompiledCodeHasProfilingInfo()
250 REQUIRES(lock_)
251 SHARED_REQUIRES(Locks::mutator_lock_);
252
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +0000253 void FreeCode(uint8_t* code) REQUIRES(lock_);
254 uint8_t* AllocateCode(size_t code_size) REQUIRES(lock_);
255 void FreeData(uint8_t* data) REQUIRES(lock_);
256 uint8_t* AllocateData(size_t data_size) REQUIRES(lock_);
257
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100258 // Lock for guarding allocations, collections, and the method_code_map_.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800259 Mutex lock_;
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100260 // Condition to wait on during collection.
261 ConditionVariable lock_cond_ GUARDED_BY(lock_);
262 // Whether there is a code cache collection in progress.
263 bool collection_in_progress_ GUARDED_BY(lock_);
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100264 // Mem map which holds code.
265 std::unique_ptr<MemMap> code_map_;
266 // Mem map which holds data (stack maps and profiling info).
267 std::unique_ptr<MemMap> data_map_;
268 // The opaque mspace for allocating code.
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100269 void* code_mspace_ GUARDED_BY(lock_);
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100270 // The opaque mspace for allocating data.
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100271 void* data_mspace_ GUARDED_BY(lock_);
272 // Bitmap for collecting code and data.
273 std::unique_ptr<CodeCacheBitmap> live_bitmap_;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000274 // Holds compiled code associated to the ArtMethod.
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100275 SafeMap<const void*, ArtMethod*> method_code_map_ GUARDED_BY(lock_);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000276 // Holds osr compiled code associated to the ArtMethod.
277 SafeMap<ArtMethod*, const void*> osr_code_map_ GUARDED_BY(lock_);
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000278 // ProfilingInfo objects we have allocated.
279 std::vector<ProfilingInfo*> profiling_infos_ GUARDED_BY(lock_);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800280
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000281 // The maximum capacity in bytes this code cache can go to.
282 size_t max_capacity_ GUARDED_BY(lock_);
283
284 // The current capacity in bytes of the code cache.
285 size_t current_capacity_ GUARDED_BY(lock_);
286
287 // The current footprint in bytes of the code portion of the code cache.
288 size_t code_end_ GUARDED_BY(lock_);
289
290 // The current footprint in bytes of the data portion of the code cache.
291 size_t data_end_ GUARDED_BY(lock_);
292
Nicolas Geoffray35122442016-03-02 12:05:30 +0000293 // Whether the last collection round increased the code cache.
294 bool last_collection_increased_code_cache_ GUARDED_BY(lock_);
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000295
Calin Juravle31f2c152015-10-23 17:56:15 +0100296 // Last time the the code_cache was updated.
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000297 // It is atomic to avoid locking when reading it.
298 Atomic<uint64_t> last_update_time_ns_;
Calin Juravle31f2c152015-10-23 17:56:15 +0100299
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000300 // Whether we can do garbage collection.
301 const bool garbage_collect_code_;
302
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +0000303 // The size in bytes of used memory for the data portion of the code cache.
304 size_t used_memory_for_data_ GUARDED_BY(lock_);
305
306 // The size in bytes of used memory for the code portion of the code cache.
307 size_t used_memory_for_code_ GUARDED_BY(lock_);
308
Nicolas Geoffray0a522232016-01-19 09:34:58 +0000309 // Number of compilations done throughout the lifetime of the JIT.
310 size_t number_of_compilations_ GUARDED_BY(lock_);
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +0000311
312 // Number of compilations for on-stack-replacement done throughout the lifetime of the JIT.
Nicolas Geoffrayfcdd7292016-02-25 13:27:47 +0000313 size_t number_of_osr_compilations_ GUARDED_BY(lock_);
Nicolas Geoffray0a522232016-01-19 09:34:58 +0000314
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +0000315 // Number of deoptimizations done throughout the lifetime of the JIT.
316 size_t number_of_deoptimizations_ GUARDED_BY(lock_);
317
318 // Number of code cache collections done throughout the lifetime of the JIT.
319 size_t number_of_collections_ GUARDED_BY(lock_);
320
Mathieu Chartier3130cdf2015-05-03 15:20:23 -0700321 DISALLOW_IMPLICIT_CONSTRUCTORS(JitCodeCache);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800322};
323
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800324} // namespace jit
325} // namespace art
326
327#endif // ART_RUNTIME_JIT_JIT_CODE_CACHE_H_