blob: 0ceb17ae747d9027f3d8dca727748a6ab6ffd00f [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 Geoffray0a3be162015-11-18 11:15:22 +000056 static JitCodeCache* Create(size_t initial_capacity, size_t max_capacity, std::string* error_msg);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080057
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010058 // Number of bytes allocated in the code cache.
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +010059 size_t CodeCacheSize() REQUIRES(!lock_);
60
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010061 // Number of bytes allocated in the data cache.
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +010062 size_t DataCacheSize() REQUIRES(!lock_);
63
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010064 // 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 Geoffray73be1e82015-09-17 15:22:56 +010068 bool NotifyCompilationOf(ArtMethod* method, Thread* self)
69 SHARED_REQUIRES(Locks::mutator_lock_)
70 REQUIRES(!lock_);
71
72 void DoneCompiling(ArtMethod* method, Thread* self)
73 SHARED_REQUIRES(Locks::mutator_lock_)
74 REQUIRES(!lock_);
75
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +010076 // Allocate and write code and its metadata to the code cache.
77 uint8_t* CommitCode(Thread* self,
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010078 ArtMethod* method,
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +010079 const uint8_t* mapping_table,
80 const uint8_t* vmap_table,
81 const uint8_t* gc_map,
82 size_t frame_size_in_bytes,
83 size_t core_spill_mask,
84 size_t fp_spill_mask,
85 const uint8_t* code,
86 size_t code_size)
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010087 SHARED_REQUIRES(Locks::mutator_lock_)
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +010088 REQUIRES(!lock_);
89
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010090 // Return true if the code cache contains this pc.
91 bool ContainsPc(const void* pc) const;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080092
Nicolas Geoffraya5891e82015-11-06 14:18:27 +000093 // Return true if the code cache contains this method.
94 bool ContainsMethod(ArtMethod* method) REQUIRES(!lock_);
95
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010096 // 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 +010097 uint8_t* ReserveData(Thread* self, size_t size)
98 SHARED_REQUIRES(Locks::mutator_lock_)
99 REQUIRES(!lock_);
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100100
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000101 // Clear data from the data portion of the code cache.
102 void ClearData(Thread* self, void* data)
103 SHARED_REQUIRES(Locks::mutator_lock_)
104 REQUIRES(!lock_);
105
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700106 // Add a data array of size (end - begin) with the associated contents, returns null if there
Mathieu Chartierbce416f2015-03-23 12:37:35 -0700107 // is no more room.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800108 uint8_t* AddDataArray(Thread* self, const uint8_t* begin, const uint8_t* end)
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100109 SHARED_REQUIRES(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -0700110 REQUIRES(!lock_);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800111
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100112 CodeCacheBitmap* GetLiveBitmap() const {
113 return live_bitmap_.get();
114 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800115
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100116 // Perform a collection on the code cache.
117 void GarbageCollectCache(Thread* self)
118 REQUIRES(!lock_)
119 SHARED_REQUIRES(Locks::mutator_lock_);
120
121 // Given the 'pc', try to find the JIT compiled code associated with it.
122 // Return null if 'pc' is not in the code cache. 'method' is passed for
123 // sanity check.
124 OatQuickMethodHeader* LookupMethodHeader(uintptr_t pc, ArtMethod* method)
125 REQUIRES(!lock_)
126 SHARED_REQUIRES(Locks::mutator_lock_);
127
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000128 // Remove all methods in our cache that were allocated by 'alloc'.
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100129 void RemoveMethodsIn(Thread* self, const LinearAlloc& alloc)
130 REQUIRES(!lock_)
131 REQUIRES(Locks::classlinker_classes_lock_)
132 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800133
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000134 // Create a 'ProfileInfo' for 'method'. If 'retry_allocation' is true,
135 // will collect and retry if the first allocation is unsuccessful.
136 ProfilingInfo* AddProfilingInfo(Thread* self,
137 ArtMethod* method,
138 const std::vector<uint32_t>& entries,
139 bool retry_allocation)
140 REQUIRES(!lock_)
141 SHARED_REQUIRES(Locks::mutator_lock_);
142
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000143 bool OwnsSpace(const void* mspace) const NO_THREAD_SAFETY_ANALYSIS {
144 return mspace == code_mspace_ || mspace == data_mspace_;
145 }
146
147 void* MoreCore(const void* mspace, intptr_t increment);
148
Calin Juravle66f55232015-12-08 15:09:10 +0000149 // Adds to `methods` all the compiled ArtMethods which are part of any of the given dex locations.
150 void GetCompiledArtMethods(const std::set<const std::string>& dex_base_locations,
151 std::set<ArtMethod*>& methods)
Calin Juravle31f2c152015-10-23 17:56:15 +0100152 REQUIRES(!lock_)
153 SHARED_REQUIRES(Locks::mutator_lock_);
154
155 uint64_t GetLastUpdateTimeNs() REQUIRES(!lock_);
156
Nicolas Geoffrayaee21562015-12-15 16:39:44 +0000157 size_t GetCurrentCapacity() REQUIRES(!lock_) {
158 MutexLock lock(Thread::Current(), lock_);
159 return current_capacity_;
160 }
161
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800162 private:
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000163 // Take ownership of maps.
164 JitCodeCache(MemMap* code_map,
165 MemMap* data_map,
166 size_t initial_code_capacity,
167 size_t initial_data_capacity,
168 size_t max_capacity);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800169
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100170 // Internal version of 'CommitCode' that will not retry if the
171 // allocation fails. Return null if the allocation fails.
172 uint8_t* CommitCodeInternal(Thread* self,
173 ArtMethod* method,
174 const uint8_t* mapping_table,
175 const uint8_t* vmap_table,
176 const uint8_t* gc_map,
177 size_t frame_size_in_bytes,
178 size_t core_spill_mask,
179 size_t fp_spill_mask,
180 const uint8_t* code,
181 size_t code_size)
182 REQUIRES(!lock_)
183 SHARED_REQUIRES(Locks::mutator_lock_);
184
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000185 ProfilingInfo* AddProfilingInfoInternal(Thread* self,
186 ArtMethod* method,
187 const std::vector<uint32_t>& entries)
188 REQUIRES(!lock_)
189 SHARED_REQUIRES(Locks::mutator_lock_);
190
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100191 // If a collection is in progress, wait for it to finish. Return
192 // whether the thread actually waited.
193 bool WaitForPotentialCollectionToComplete(Thread* self)
194 REQUIRES(lock_) REQUIRES(!Locks::mutator_lock_);
195
196 // Free in the mspace allocations taken by 'method'.
197 void FreeCode(const void* code_ptr, ArtMethod* method) REQUIRES(lock_);
198
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000199 // Number of bytes allocated in the code cache.
200 size_t CodeCacheSizeLocked() REQUIRES(lock_);
201
202 // Number of bytes allocated in the data cache.
203 size_t DataCacheSizeLocked() REQUIRES(lock_);
204
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000205 // Notify all waiting threads that a collection is done.
206 void NotifyCollectionDone(Thread* self) REQUIRES(lock_);
207
208 // Try to increase the current capacity of the code cache. Return whether we
209 // succeeded at doing so.
210 bool IncreaseCodeCacheCapacity() REQUIRES(lock_);
211
212 // Set the footprint limit of the code cache.
213 void SetFootprintLimit(size_t new_footprint) REQUIRES(lock_);
214
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100215 // Lock for guarding allocations, collections, and the method_code_map_.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800216 Mutex lock_;
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100217 // Condition to wait on during collection.
218 ConditionVariable lock_cond_ GUARDED_BY(lock_);
219 // Whether there is a code cache collection in progress.
220 bool collection_in_progress_ GUARDED_BY(lock_);
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100221 // Mem map which holds code.
222 std::unique_ptr<MemMap> code_map_;
223 // Mem map which holds data (stack maps and profiling info).
224 std::unique_ptr<MemMap> data_map_;
225 // The opaque mspace for allocating code.
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100226 void* code_mspace_ GUARDED_BY(lock_);
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100227 // The opaque mspace for allocating data.
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100228 void* data_mspace_ GUARDED_BY(lock_);
229 // Bitmap for collecting code and data.
230 std::unique_ptr<CodeCacheBitmap> live_bitmap_;
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000231 // This map holds compiled code associated to the ArtMethod.
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100232 SafeMap<const void*, ArtMethod*> method_code_map_ GUARDED_BY(lock_);
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000233 // ProfilingInfo objects we have allocated.
234 std::vector<ProfilingInfo*> profiling_infos_ GUARDED_BY(lock_);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800235
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000236 // The maximum capacity in bytes this code cache can go to.
237 size_t max_capacity_ GUARDED_BY(lock_);
238
239 // The current capacity in bytes of the code cache.
240 size_t current_capacity_ GUARDED_BY(lock_);
241
242 // The current footprint in bytes of the code portion of the code cache.
243 size_t code_end_ GUARDED_BY(lock_);
244
245 // The current footprint in bytes of the data portion of the code cache.
246 size_t data_end_ GUARDED_BY(lock_);
247
248 // Whether a collection has already been done on the current capacity.
249 bool has_done_one_collection_ GUARDED_BY(lock_);
250
Calin Juravle31f2c152015-10-23 17:56:15 +0100251 // Last time the the code_cache was updated.
252 uint64_t last_update_time_ns_ GUARDED_BY(lock_);
253
Mathieu Chartier3130cdf2015-05-03 15:20:23 -0700254 DISALLOW_IMPLICIT_CONSTRUCTORS(JitCodeCache);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800255};
256
257
258} // namespace jit
259} // namespace art
260
261#endif // ART_RUNTIME_JIT_JIT_CODE_CACHE_H_