blob: 4500dbdf67e88f5291c55270284646655fda776e [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#include "jit_code_cache.h"
18
19#include <sstream>
20
Mathieu Chartiere401d142015-04-22 13:56:20 -070021#include "art_method-inl.h"
Calin Juravle66f55232015-12-08 15:09:10 +000022#include "base/stl_util.h"
Calin Juravle31f2c152015-10-23 17:56:15 +010023#include "base/time_utils.h"
David Srbecky5cc349f2015-12-18 15:04:48 +000024#include "debugger_interface.h"
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010025#include "entrypoints/runtime_asm_entrypoints.h"
26#include "gc/accounting/bitmap-inl.h"
Nicolas Geoffray26705e22015-10-28 12:50:11 +000027#include "jit/profiling_info.h"
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010028#include "linear_alloc.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080029#include "mem_map.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080030#include "oat_file-inl.h"
Nicolas Geoffray62623402015-10-28 19:15:05 +000031#include "scoped_thread_state_change.h"
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010032#include "thread_list.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080033
34namespace art {
35namespace jit {
36
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +010037static constexpr int kProtAll = PROT_READ | PROT_WRITE | PROT_EXEC;
38static constexpr int kProtData = PROT_READ | PROT_WRITE;
39static constexpr int kProtCode = PROT_READ | PROT_EXEC;
40
41#define CHECKED_MPROTECT(memory, size, prot) \
42 do { \
43 int rc = mprotect(memory, size, prot); \
44 if (UNLIKELY(rc != 0)) { \
45 errno = rc; \
46 PLOG(FATAL) << "Failed to mprotect jit code cache"; \
47 } \
48 } while (false) \
49
Nicolas Geoffray0a3be162015-11-18 11:15:22 +000050JitCodeCache* JitCodeCache::Create(size_t initial_capacity,
51 size_t max_capacity,
Nicolas Geoffraya25dce92016-01-12 16:41:10 +000052 bool generate_debug_info,
Nicolas Geoffray0a3be162015-11-18 11:15:22 +000053 std::string* error_msg) {
54 CHECK_GE(max_capacity, initial_capacity);
Nicolas Geoffraya25dce92016-01-12 16:41:10 +000055
56 // Generating debug information is mostly for using the 'perf' tool, which does
57 // not work with ashmem.
58 bool use_ashmem = !generate_debug_info;
59 // With 'perf', we want a 1-1 mapping between an address and a method.
60 bool garbage_collect_code = !generate_debug_info;
61
Nicolas Geoffray0a3be162015-11-18 11:15:22 +000062 // We need to have 32 bit offsets from method headers in code cache which point to things
63 // in the data cache. If the maps are more than 4G apart, having multiple maps wouldn't work.
64 // Ensure we're below 1 GB to be safe.
65 if (max_capacity > 1 * GB) {
66 std::ostringstream oss;
67 oss << "Maxium code cache capacity is limited to 1 GB, "
68 << PrettySize(max_capacity) << " is too big";
69 *error_msg = oss.str();
70 return nullptr;
71 }
72
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080073 std::string error_str;
74 // Map name specific for android_os_Debug.cpp accounting.
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +010075 MemMap* data_map = MemMap::MapAnonymous(
Nicolas Geoffraya25dce92016-01-12 16:41:10 +000076 "data-code-cache", nullptr, max_capacity, kProtAll, false, false, &error_str, use_ashmem);
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +010077 if (data_map == nullptr) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080078 std::ostringstream oss;
Nicolas Geoffray0a3be162015-11-18 11:15:22 +000079 oss << "Failed to create read write execute cache: " << error_str << " size=" << max_capacity;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080080 *error_msg = oss.str();
81 return nullptr;
82 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +010083
Nicolas Geoffray0a3be162015-11-18 11:15:22 +000084 // Align both capacities to page size, as that's the unit mspaces use.
85 initial_capacity = RoundDown(initial_capacity, 2 * kPageSize);
86 max_capacity = RoundDown(max_capacity, 2 * kPageSize);
87
Nicolas Geoffray4e915fb2015-10-28 17:39:47 +000088 // Data cache is 1 / 2 of the map.
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +010089 // TODO: Make this variable?
Nicolas Geoffray0a3be162015-11-18 11:15:22 +000090 size_t data_size = max_capacity / 2;
91 size_t code_size = max_capacity - data_size;
92 DCHECK_EQ(code_size + data_size, max_capacity);
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +010093 uint8_t* divider = data_map->Begin() + data_size;
94
Nicolas Geoffraya25dce92016-01-12 16:41:10 +000095 MemMap* code_map =
96 data_map->RemapAtEnd(divider, "jit-code-cache", kProtAll, &error_str, use_ashmem);
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +010097 if (code_map == nullptr) {
98 std::ostringstream oss;
Nicolas Geoffray0a3be162015-11-18 11:15:22 +000099 oss << "Failed to create read write execute cache: " << error_str << " size=" << max_capacity;
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100100 *error_msg = oss.str();
101 return nullptr;
102 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100103 DCHECK_EQ(code_map->Begin(), divider);
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000104 data_size = initial_capacity / 2;
105 code_size = initial_capacity - data_size;
106 DCHECK_EQ(code_size + data_size, initial_capacity);
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000107 return new JitCodeCache(
Nicolas Geoffrayc3fec4c2016-01-14 16:16:35 +0000108 code_map, data_map, code_size, data_size, max_capacity, garbage_collect_code);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800109}
110
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000111JitCodeCache::JitCodeCache(MemMap* code_map,
112 MemMap* data_map,
113 size_t initial_code_capacity,
114 size_t initial_data_capacity,
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000115 size_t max_capacity,
116 bool garbage_collect_code)
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100117 : lock_("Jit code cache", kJitCodeCacheLock),
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100118 lock_cond_("Jit code cache variable", lock_),
119 collection_in_progress_(false),
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100120 code_map_(code_map),
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000121 data_map_(data_map),
122 max_capacity_(max_capacity),
123 current_capacity_(initial_code_capacity + initial_data_capacity),
124 code_end_(initial_code_capacity),
125 data_end_(initial_data_capacity),
Nicolas Geoffray7273a5d2016-02-29 15:35:39 +0000126 last_collection_increased_code_cache_(false),
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000127 last_update_time_ns_(0),
Nicolas Geoffray0a522232016-01-19 09:34:58 +0000128 garbage_collect_code_(garbage_collect_code),
Nicolas Geoffrayb0d22082016-02-24 17:18:25 +0000129 used_memory_for_data_(0),
130 used_memory_for_code_(0),
Nicolas Geoffrayfcdd7292016-02-25 13:27:47 +0000131 number_of_compilations_(0),
132 number_of_osr_compilations_(0) {
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100133
Nicolas Geoffrayc3fec4c2016-01-14 16:16:35 +0000134 DCHECK_GE(max_capacity, initial_code_capacity + initial_data_capacity);
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000135 code_mspace_ = create_mspace_with_base(code_map_->Begin(), code_end_, false /*locked*/);
136 data_mspace_ = create_mspace_with_base(data_map_->Begin(), data_end_, false /*locked*/);
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100137
138 if (code_mspace_ == nullptr || data_mspace_ == nullptr) {
139 PLOG(FATAL) << "create_mspace_with_base failed";
140 }
141
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000142 SetFootprintLimit(current_capacity_);
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100143
144 CHECKED_MPROTECT(code_map_->Begin(), code_map_->Size(), kProtCode);
145 CHECKED_MPROTECT(data_map_->Begin(), data_map_->Size(), kProtData);
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100146
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000147 VLOG(jit) << "Created jit code cache: initial data size="
148 << PrettySize(initial_data_capacity)
149 << ", initial code size="
150 << PrettySize(initial_code_capacity);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800151}
152
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100153bool JitCodeCache::ContainsPc(const void* ptr) const {
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100154 return code_map_->Begin() <= ptr && ptr < code_map_->End();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800155}
156
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000157bool JitCodeCache::ContainsMethod(ArtMethod* method) {
158 MutexLock mu(Thread::Current(), lock_);
159 for (auto& it : method_code_map_) {
160 if (it.second == method) {
161 return true;
162 }
163 }
164 return false;
165}
166
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100167class ScopedCodeCacheWrite {
168 public:
169 explicit ScopedCodeCacheWrite(MemMap* code_map) : code_map_(code_map) {
170 CHECKED_MPROTECT(code_map_->Begin(), code_map_->Size(), kProtAll);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800171 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100172 ~ScopedCodeCacheWrite() {
173 CHECKED_MPROTECT(code_map_->Begin(), code_map_->Size(), kProtCode);
174 }
175 private:
176 MemMap* const code_map_;
177
178 DISALLOW_COPY_AND_ASSIGN(ScopedCodeCacheWrite);
179};
180
181uint8_t* JitCodeCache::CommitCode(Thread* self,
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100182 ArtMethod* method,
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100183 const uint8_t* mapping_table,
184 const uint8_t* vmap_table,
185 const uint8_t* gc_map,
186 size_t frame_size_in_bytes,
187 size_t core_spill_mask,
188 size_t fp_spill_mask,
189 const uint8_t* code,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000190 size_t code_size,
191 bool osr) {
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100192 uint8_t* result = CommitCodeInternal(self,
193 method,
194 mapping_table,
195 vmap_table,
196 gc_map,
197 frame_size_in_bytes,
198 core_spill_mask,
199 fp_spill_mask,
200 code,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000201 code_size,
202 osr);
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100203 if (result == nullptr) {
204 // Retry.
205 GarbageCollectCache(self);
206 result = CommitCodeInternal(self,
207 method,
208 mapping_table,
209 vmap_table,
210 gc_map,
211 frame_size_in_bytes,
212 core_spill_mask,
213 fp_spill_mask,
214 code,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000215 code_size,
216 osr);
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100217 }
218 return result;
219}
220
221bool JitCodeCache::WaitForPotentialCollectionToComplete(Thread* self) {
222 bool in_collection = false;
223 while (collection_in_progress_) {
224 in_collection = true;
225 lock_cond_.Wait(self);
226 }
227 return in_collection;
228}
229
230static uintptr_t FromCodeToAllocation(const void* code) {
231 size_t alignment = GetInstructionSetAlignment(kRuntimeISA);
232 return reinterpret_cast<uintptr_t>(code) - RoundUp(sizeof(OatQuickMethodHeader), alignment);
233}
234
235void JitCodeCache::FreeCode(const void* code_ptr, ArtMethod* method ATTRIBUTE_UNUSED) {
236 uintptr_t allocation = FromCodeToAllocation(code_ptr);
237 const OatQuickMethodHeader* method_header = OatQuickMethodHeader::FromCodePointer(code_ptr);
David Srbecky5cc349f2015-12-18 15:04:48 +0000238 // Notify native debugger that we are about to remove the code.
239 // It does nothing if we are not using native debugger.
240 DeleteJITCodeEntryForAddress(reinterpret_cast<uintptr_t>(code_ptr));
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +0000241
242 FreeData(const_cast<uint8_t*>(method_header->GetNativeGcMap()));
243 FreeData(const_cast<uint8_t*>(method_header->GetMappingTable()));
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100244 // Use the offset directly to prevent sanity check that the method is
245 // compiled with optimizing.
246 // TODO(ngeoffray): Clean up.
247 if (method_header->vmap_table_offset_ != 0) {
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +0000248 const uint8_t* data = method_header->code_ - method_header->vmap_table_offset_;
249 FreeData(const_cast<uint8_t*>(data));
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100250 }
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +0000251 FreeCode(reinterpret_cast<uint8_t*>(allocation));
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100252}
253
254void JitCodeCache::RemoveMethodsIn(Thread* self, const LinearAlloc& alloc) {
255 MutexLock mu(self, lock_);
256 // We do not check if a code cache GC is in progress, as this method comes
257 // with the classlinker_classes_lock_ held, and suspending ourselves could
258 // lead to a deadlock.
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000259 {
260 ScopedCodeCacheWrite scc(code_map_.get());
261 for (auto it = method_code_map_.begin(); it != method_code_map_.end();) {
262 if (alloc.ContainsUnsafe(it->second)) {
263 FreeCode(it->first, it->second);
264 it = method_code_map_.erase(it);
265 } else {
266 ++it;
267 }
268 }
269 }
Nicolas Geoffraya9b91312016-02-17 09:49:19 +0000270 for (auto it = osr_code_map_.begin(); it != osr_code_map_.end();) {
271 if (alloc.ContainsUnsafe(it->first)) {
272 // Note that the code has already been removed in the loop above.
273 it = osr_code_map_.erase(it);
274 } else {
275 ++it;
276 }
277 }
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000278 for (auto it = profiling_infos_.begin(); it != profiling_infos_.end();) {
279 ProfilingInfo* info = *it;
280 if (alloc.ContainsUnsafe(info->GetMethod())) {
281 info->GetMethod()->SetProfilingInfo(nullptr);
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +0000282 FreeData(reinterpret_cast<uint8_t*>(info));
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000283 it = profiling_infos_.erase(it);
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100284 } else {
285 ++it;
286 }
287 }
288}
289
290uint8_t* JitCodeCache::CommitCodeInternal(Thread* self,
291 ArtMethod* method,
292 const uint8_t* mapping_table,
293 const uint8_t* vmap_table,
294 const uint8_t* gc_map,
295 size_t frame_size_in_bytes,
296 size_t core_spill_mask,
297 size_t fp_spill_mask,
298 const uint8_t* code,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000299 size_t code_size,
300 bool osr) {
Nicolas Geoffray1e7de6c2015-10-21 12:07:31 +0100301 size_t alignment = GetInstructionSetAlignment(kRuntimeISA);
302 // Ensure the header ends up at expected instruction alignment.
303 size_t header_size = RoundUp(sizeof(OatQuickMethodHeader), alignment);
304 size_t total_size = header_size + code_size;
305
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100306 OatQuickMethodHeader* method_header = nullptr;
Nicolas Geoffray1e7de6c2015-10-21 12:07:31 +0100307 uint8_t* code_ptr = nullptr;
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +0000308 uint8_t* memory = nullptr;
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100309 {
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000310 ScopedThreadSuspension sts(self, kSuspended);
311 MutexLock mu(self, lock_);
312 WaitForPotentialCollectionToComplete(self);
313 {
314 ScopedCodeCacheWrite scc(code_map_.get());
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +0000315 memory = AllocateCode(total_size);
316 if (memory == nullptr) {
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000317 return nullptr;
318 }
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +0000319 code_ptr = memory + header_size;
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000320
321 std::copy(code, code + code_size, code_ptr);
322 method_header = OatQuickMethodHeader::FromCodePointer(code_ptr);
323 new (method_header) OatQuickMethodHeader(
324 (mapping_table == nullptr) ? 0 : code_ptr - mapping_table,
325 (vmap_table == nullptr) ? 0 : code_ptr - vmap_table,
326 (gc_map == nullptr) ? 0 : code_ptr - gc_map,
327 frame_size_in_bytes,
328 core_spill_mask,
329 fp_spill_mask,
330 code_size);
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100331 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100332
Roland Levillain32430262016-02-01 15:23:20 +0000333 FlushInstructionCache(reinterpret_cast<char*>(code_ptr),
334 reinterpret_cast<char*>(code_ptr + code_size));
Nicolas Geoffray0a522232016-01-19 09:34:58 +0000335 number_of_compilations_++;
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100336 }
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000337 // We need to update the entry point in the runnable state for the instrumentation.
338 {
339 MutexLock mu(self, lock_);
340 method_code_map_.Put(code_ptr, method);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000341 if (osr) {
Nicolas Geoffrayfcdd7292016-02-25 13:27:47 +0000342 number_of_osr_compilations_++;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000343 osr_code_map_.Put(method, code_ptr);
344 } else {
345 Runtime::Current()->GetInstrumentation()->UpdateMethodsCode(
346 method, method_header->GetEntryPoint());
347 }
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000348 if (collection_in_progress_) {
349 // We need to update the live bitmap if there is a GC to ensure it sees this new
350 // code.
351 GetLiveBitmap()->AtomicTestAndSet(FromCodeToAllocation(code_ptr));
352 }
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000353 last_update_time_ns_.StoreRelease(NanoTime());
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000354 VLOG(jit)
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000355 << "JIT added (osr = " << std::boolalpha << osr << std::noboolalpha << ") "
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000356 << PrettyMethod(method) << "@" << method
357 << " ccache_size=" << PrettySize(CodeCacheSizeLocked()) << ": "
358 << " dcache_size=" << PrettySize(DataCacheSizeLocked()) << ": "
359 << reinterpret_cast<const void*>(method_header->GetEntryPoint()) << ","
360 << reinterpret_cast<const void*>(method_header->GetEntryPoint() + method_header->code_size_);
361 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100362
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100363 return reinterpret_cast<uint8_t*>(method_header);
364}
365
Nicolas Geoffray0a522232016-01-19 09:34:58 +0000366size_t JitCodeCache::NumberOfCompilations() {
367 MutexLock mu(Thread::Current(), lock_);
368 return number_of_compilations_;
369}
370
Nicolas Geoffrayfcdd7292016-02-25 13:27:47 +0000371size_t JitCodeCache::NumberOfOsrCompilations() {
372 MutexLock mu(Thread::Current(), lock_);
373 return number_of_osr_compilations_;
374}
375
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100376size_t JitCodeCache::CodeCacheSize() {
377 MutexLock mu(Thread::Current(), lock_);
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000378 return CodeCacheSizeLocked();
379}
380
381size_t JitCodeCache::CodeCacheSizeLocked() {
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +0000382 return used_memory_for_code_;
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100383}
384
385size_t JitCodeCache::DataCacheSize() {
386 MutexLock mu(Thread::Current(), lock_);
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000387 return DataCacheSizeLocked();
388}
389
390size_t JitCodeCache::DataCacheSizeLocked() {
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +0000391 return used_memory_for_data_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800392}
393
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100394size_t JitCodeCache::NumberOfCompiledCode() {
395 MutexLock mu(Thread::Current(), lock_);
396 return method_code_map_.size();
397}
398
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000399void JitCodeCache::ClearData(Thread* self, void* data) {
400 MutexLock mu(self, lock_);
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +0000401 FreeData(reinterpret_cast<uint8_t*>(data));
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000402}
403
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100404uint8_t* JitCodeCache::ReserveData(Thread* self, size_t size) {
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100405 size = RoundUp(size, sizeof(void*));
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100406 uint8_t* result = nullptr;
407
408 {
409 ScopedThreadSuspension sts(self, kSuspended);
410 MutexLock mu(self, lock_);
411 WaitForPotentialCollectionToComplete(self);
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +0000412 result = AllocateData(size);
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100413 }
414
415 if (result == nullptr) {
416 // Retry.
417 GarbageCollectCache(self);
418 ScopedThreadSuspension sts(self, kSuspended);
419 MutexLock mu(self, lock_);
420 WaitForPotentialCollectionToComplete(self);
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +0000421 result = AllocateData(size);
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100422 }
423
424 return result;
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100425}
426
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800427uint8_t* JitCodeCache::AddDataArray(Thread* self, const uint8_t* begin, const uint8_t* end) {
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100428 uint8_t* result = ReserveData(self, end - begin);
429 if (result == nullptr) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800430 return nullptr; // Out of space in the data cache.
431 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100432 std::copy(begin, end, result);
433 return result;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800434}
435
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100436class MarkCodeVisitor FINAL : public StackVisitor {
437 public:
438 MarkCodeVisitor(Thread* thread_in, JitCodeCache* code_cache_in)
439 : StackVisitor(thread_in, nullptr, StackVisitor::StackWalkKind::kSkipInlinedFrames),
440 code_cache_(code_cache_in),
441 bitmap_(code_cache_->GetLiveBitmap()) {}
442
443 bool VisitFrame() OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
444 const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader();
445 if (method_header == nullptr) {
446 return true;
447 }
448 const void* code = method_header->GetCode();
449 if (code_cache_->ContainsPc(code)) {
450 // Use the atomic set version, as multiple threads are executing this code.
451 bitmap_->AtomicTestAndSet(FromCodeToAllocation(code));
452 }
453 return true;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800454 }
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100455
456 private:
457 JitCodeCache* const code_cache_;
458 CodeCacheBitmap* const bitmap_;
459};
460
461class MarkCodeClosure FINAL : public Closure {
462 public:
463 MarkCodeClosure(JitCodeCache* code_cache, Barrier* barrier)
464 : code_cache_(code_cache), barrier_(barrier) {}
465
466 void Run(Thread* thread) OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
467 DCHECK(thread == Thread::Current() || thread->IsSuspended());
468 MarkCodeVisitor visitor(thread, code_cache_);
469 visitor.WalkStack();
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +0000470 if (kIsDebugBuild) {
471 // The stack walking code queries the side instrumentation stack if it
472 // sees an instrumentation exit pc, so the JIT code of methods in that stack
473 // must have been seen. We sanity check this below.
474 for (const instrumentation::InstrumentationStackFrame& frame
475 : *thread->GetInstrumentationStack()) {
476 // The 'method_' in InstrumentationStackFrame is the one that has return_pc_ in
477 // its stack frame, it is not the method owning return_pc_. We just pass null to
478 // LookupMethodHeader: the method is only checked against in debug builds.
479 OatQuickMethodHeader* method_header =
480 code_cache_->LookupMethodHeader(frame.return_pc_, nullptr);
481 if (method_header != nullptr) {
482 const void* code = method_header->GetCode();
483 CHECK(code_cache_->GetLiveBitmap()->Test(FromCodeToAllocation(code)));
484 }
485 }
486 }
Mathieu Chartier10d25082015-10-28 18:36:09 -0700487 barrier_->Pass(Thread::Current());
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800488 }
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100489
490 private:
491 JitCodeCache* const code_cache_;
492 Barrier* const barrier_;
493};
494
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000495void JitCodeCache::NotifyCollectionDone(Thread* self) {
496 collection_in_progress_ = false;
497 lock_cond_.Broadcast(self);
498}
499
500void JitCodeCache::SetFootprintLimit(size_t new_footprint) {
501 size_t per_space_footprint = new_footprint / 2;
502 DCHECK(IsAlignedParam(per_space_footprint, kPageSize));
503 DCHECK_EQ(per_space_footprint * 2, new_footprint);
504 mspace_set_footprint_limit(data_mspace_, per_space_footprint);
505 {
506 ScopedCodeCacheWrite scc(code_map_.get());
507 mspace_set_footprint_limit(code_mspace_, per_space_footprint);
508 }
509}
510
511bool JitCodeCache::IncreaseCodeCacheCapacity() {
512 if (current_capacity_ == max_capacity_) {
513 return false;
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100514 }
515
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000516 // Double the capacity if we're below 1MB, or increase it by 1MB if
517 // we're above.
518 if (current_capacity_ < 1 * MB) {
519 current_capacity_ *= 2;
520 } else {
521 current_capacity_ += 1 * MB;
522 }
523 if (current_capacity_ > max_capacity_) {
524 current_capacity_ = max_capacity_;
525 }
526
527 if (!kIsDebugBuild || VLOG_IS_ON(jit)) {
528 LOG(INFO) << "Increasing code cache capacity to " << PrettySize(current_capacity_);
529 }
530
531 SetFootprintLimit(current_capacity_);
532
533 return true;
534}
535
Nicolas Geoffray8d372502016-02-23 13:56:43 +0000536void JitCodeCache::MarkCompiledCodeOnThreadStacks(Thread* self) {
537 Barrier barrier(0);
538 size_t threads_running_checkpoint = 0;
539 MarkCodeClosure closure(this, &barrier);
540 threads_running_checkpoint = Runtime::Current()->GetThreadList()->RunCheckpoint(&closure);
541 // Now that we have run our checkpoint, move to a suspended state and wait
542 // for other threads to run the checkpoint.
543 ScopedThreadSuspension sts(self, kSuspended);
544 if (threads_running_checkpoint != 0) {
545 barrier.Increment(self, threads_running_checkpoint);
546 }
547}
548
Nicolas Geoffray7273a5d2016-02-29 15:35:39 +0000549bool JitCodeCache::ShouldDoFullCollection() {
550 if (current_capacity_ == max_capacity_) {
551 // Always do a full collection when the code cache is full.
552 return true;
553 } else if (current_capacity_ < kReservedCapacity) {
554 // Always do partial collection when the code cache size is below the reserved
555 // capacity.
556 return false;
557 } else if (last_collection_increased_code_cache_) {
558 // This time do a full collection.
559 return true;
560 } else {
561 // This time do a partial collection.
562 return false;
Nicolas Geoffray8d372502016-02-23 13:56:43 +0000563 }
564}
565
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000566void JitCodeCache::GarbageCollectCache(Thread* self) {
Nicolas Geoffray8d372502016-02-23 13:56:43 +0000567 if (!garbage_collect_code_) {
568 MutexLock mu(self, lock_);
569 IncreaseCodeCacheCapacity();
570 return;
571 }
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100572
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000573 // Wait for an existing collection, or let everyone know we are starting one.
574 {
575 ScopedThreadSuspension sts(self, kSuspended);
576 MutexLock mu(self, lock_);
577 if (WaitForPotentialCollectionToComplete(self)) {
578 return;
579 } else {
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000580 live_bitmap_.reset(CodeCacheBitmap::Create(
581 "code-cache-bitmap",
582 reinterpret_cast<uintptr_t>(code_map_->Begin()),
583 reinterpret_cast<uintptr_t>(code_map_->Begin() + current_capacity_ / 2)));
Nicolas Geoffray8d372502016-02-23 13:56:43 +0000584 collection_in_progress_ = true;
585 }
586 }
587
Nicolas Geoffray7273a5d2016-02-29 15:35:39 +0000588 bool do_full_collection = false;
Nicolas Geoffray8d372502016-02-23 13:56:43 +0000589 {
590 MutexLock mu(self, lock_);
Nicolas Geoffray7273a5d2016-02-29 15:35:39 +0000591 do_full_collection = ShouldDoFullCollection();
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000592 }
593
594 if (!kIsDebugBuild || VLOG_IS_ON(jit)) {
Nicolas Geoffray8d372502016-02-23 13:56:43 +0000595 LOG(INFO) << "Do "
596 << (do_full_collection ? "full" : "partial")
597 << " code cache collection, code="
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000598 << PrettySize(CodeCacheSize())
599 << ", data=" << PrettySize(DataCacheSize());
600 }
Nicolas Geoffray8d372502016-02-23 13:56:43 +0000601
Nicolas Geoffray7273a5d2016-02-29 15:35:39 +0000602 DoCollection(self, /* collect_profiling_info */ do_full_collection);
Nicolas Geoffray8d372502016-02-23 13:56:43 +0000603
604 if (!kIsDebugBuild || VLOG_IS_ON(jit)) {
605 LOG(INFO) << "After code cache collection, code="
606 << PrettySize(CodeCacheSize())
607 << ", data=" << PrettySize(DataCacheSize());
608 }
Nicolas Geoffray8d372502016-02-23 13:56:43 +0000609
Nicolas Geoffray8d372502016-02-23 13:56:43 +0000610 {
611 MutexLock mu(self, lock_);
Nicolas Geoffray7273a5d2016-02-29 15:35:39 +0000612
613 // Increase the code cache only when we do partial collections.
614 // TODO: base this strategy on how full the code cache is?
615 if (do_full_collection) {
616 last_collection_increased_code_cache_ = false;
617 } else {
618 last_collection_increased_code_cache_ = true;
619 IncreaseCodeCacheCapacity();
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100620 }
Nicolas Geoffray8d372502016-02-23 13:56:43 +0000621
Nicolas Geoffray7273a5d2016-02-29 15:35:39 +0000622 bool next_collection_will_be_full = ShouldDoFullCollection();
623
624 // Start polling the liveness of compiled code to prepare for the next full collection.
625 if (next_collection_will_be_full) {
626 // Save the entry point of methods we have compiled, and update the entry
627 // point of those methods to the interpreter. If the method is invoked, the
628 // interpreter will update its entry point to the compiled code and call it.
629 for (ProfilingInfo* info : profiling_infos_) {
630 const void* entry_point = info->GetMethod()->GetEntryPointFromQuickCompiledCode();
631 if (ContainsPc(entry_point)) {
632 info->SetSavedEntryPoint(entry_point);
633 info->GetMethod()->SetEntryPointFromQuickCompiledCode(GetQuickToInterpreterBridge());
634 }
635 }
636
637 if (kIsDebugBuild) {
638 // Check that methods we have compiled do have a ProfilingInfo object. We would
639 // have memory leaks of compiled code otherwise.
640 for (const auto& it : method_code_map_) {
641 DCHECK(it.second->GetProfilingInfo(sizeof(void*)) != nullptr);
642 }
643 }
644 }
645 live_bitmap_.reset(nullptr);
646 NotifyCollectionDone(self);
647 }
648}
649
650void JitCodeCache::RemoveUnusedAndUnmarkedCode(Thread* self) {
651 MutexLock mu(self, lock_);
652 ScopedCodeCacheWrite scc(code_map_.get());
653 // Iterate over all compiled code and remove entries that are not marked and not
654 // the entrypoint of their corresponding ArtMethod.
655 for (auto it = method_code_map_.begin(); it != method_code_map_.end();) {
656 const void* code_ptr = it->first;
657 ArtMethod* method = it->second;
658 uintptr_t allocation = FromCodeToAllocation(code_ptr);
659 const OatQuickMethodHeader* method_header = OatQuickMethodHeader::FromCodePointer(code_ptr);
660 const void* entrypoint = method->GetEntryPointFromQuickCompiledCode();
661 if ((entrypoint == method_header->GetEntryPoint()) || GetLiveBitmap()->Test(allocation)) {
662 ++it;
663 } else {
664 if (entrypoint == GetQuickToInterpreterBridge()) {
665 method->ClearCounter();
666 }
667 FreeCode(code_ptr, method);
668 it = method_code_map_.erase(it);
669 }
670 }
671}
672
673void JitCodeCache::DoCollection(Thread* self, bool collect_profiling_info) {
674 {
675 MutexLock mu(self, lock_);
676 if (collect_profiling_info) {
677 // Clear the profiling info of methods that do not have compiled code as entrypoint.
678 // Also remove the saved entry point from the ProfilingInfo objects.
679 for (ProfilingInfo* info : profiling_infos_) {
680 const void* ptr = info->GetMethod()->GetEntryPointFromQuickCompiledCode();
681 if (!ContainsPc(ptr) && !info->IsMethodBeingCompiled()) {
682 info->GetMethod()->SetProfilingInfo(nullptr);
683 }
684 info->SetSavedEntryPoint(nullptr);
685 }
686 } else if (kIsDebugBuild) {
687 // Sanity check that the profiling infos do not have a dangling entry point.
688 for (ProfilingInfo* info : profiling_infos_) {
689 DCHECK(info->GetSavedEntryPoint() == nullptr);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100690 }
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000691 }
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000692
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000693 // Empty osr method map, as osr compiled code will be deleted (except the ones
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000694 // on thread stacks).
695 osr_code_map_.clear();
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100696 }
697
698 // Run a checkpoint on all threads to mark the JIT compiled code they are running.
Nicolas Geoffray8d372502016-02-23 13:56:43 +0000699 MarkCompiledCodeOnThreadStacks(self);
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100700
Nicolas Geoffray7273a5d2016-02-29 15:35:39 +0000701 // Remove compiled code that is not the entrypoint of their method and not in the call
702 // stack.
703 RemoveUnusedAndUnmarkedCode(self);
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000704
Nicolas Geoffray7273a5d2016-02-29 15:35:39 +0000705 if (collect_profiling_info) {
706 MutexLock mu(self, lock_);
707 // Free all profiling infos of methods not compiled nor being compiled.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100708 auto profiling_kept_end = std::remove_if(profiling_infos_.begin(), profiling_infos_.end(),
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +0000709 [this] (ProfilingInfo* info) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffray7273a5d2016-02-29 15:35:39 +0000710 const void* ptr = info->GetMethod()->GetEntryPointFromQuickCompiledCode();
711 if (ContainsPc(ptr) && info->GetMethod()->GetProfilingInfo(sizeof(void*)) == nullptr) {
712 // Make sure compiled methods have a ProfilingInfo object. It is needed for
713 // code cache collection.
714 info->GetMethod()->SetProfilingInfo(info);
715 } else if (info->GetMethod()->GetProfilingInfo(sizeof(void*)) != info) {
716 // No need for this ProfilingInfo object anymore.
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +0000717 FreeData(reinterpret_cast<uint8_t*>(info));
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100718 return true;
719 }
720 return false;
721 });
722 profiling_infos_.erase(profiling_kept_end, profiling_infos_.end());
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100723 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800724}
725
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100726
727OatQuickMethodHeader* JitCodeCache::LookupMethodHeader(uintptr_t pc, ArtMethod* method) {
728 static_assert(kRuntimeISA != kThumb2, "kThumb2 cannot be a runtime ISA");
729 if (kRuntimeISA == kArm) {
730 // On Thumb-2, the pc is offset by one.
731 --pc;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800732 }
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100733 if (!ContainsPc(reinterpret_cast<const void*>(pc))) {
734 return nullptr;
735 }
736
737 MutexLock mu(Thread::Current(), lock_);
738 if (method_code_map_.empty()) {
739 return nullptr;
740 }
741 auto it = method_code_map_.lower_bound(reinterpret_cast<const void*>(pc));
742 --it;
743
744 const void* code_ptr = it->first;
745 OatQuickMethodHeader* method_header = OatQuickMethodHeader::FromCodePointer(code_ptr);
746 if (!method_header->Contains(pc)) {
747 return nullptr;
748 }
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +0000749 if (kIsDebugBuild && method != nullptr) {
750 DCHECK_EQ(it->second, method)
751 << PrettyMethod(method) << " " << PrettyMethod(it->second) << " " << std::hex << pc;
752 }
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100753 return method_header;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800754}
755
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000756OatQuickMethodHeader* JitCodeCache::LookupOsrMethodHeader(ArtMethod* method) {
757 MutexLock mu(Thread::Current(), lock_);
758 auto it = osr_code_map_.find(method);
759 if (it == osr_code_map_.end()) {
760 return nullptr;
761 }
762 return OatQuickMethodHeader::FromCodePointer(it->second);
763}
764
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000765ProfilingInfo* JitCodeCache::AddProfilingInfo(Thread* self,
766 ArtMethod* method,
767 const std::vector<uint32_t>& entries,
768 bool retry_allocation) {
769 ProfilingInfo* info = AddProfilingInfoInternal(self, method, entries);
770
771 if (info == nullptr && retry_allocation) {
772 GarbageCollectCache(self);
773 info = AddProfilingInfoInternal(self, method, entries);
774 }
775 return info;
776}
777
778ProfilingInfo* JitCodeCache::AddProfilingInfoInternal(Thread* self,
779 ArtMethod* method,
780 const std::vector<uint32_t>& entries) {
781 size_t profile_info_size = RoundUp(
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100782 sizeof(ProfilingInfo) + sizeof(InlineCache) * entries.size(),
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000783 sizeof(void*));
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000784 MutexLock mu(self, lock_);
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000785
786 // Check whether some other thread has concurrently created it.
787 ProfilingInfo* info = method->GetProfilingInfo(sizeof(void*));
788 if (info != nullptr) {
789 return info;
790 }
791
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +0000792 uint8_t* data = AllocateData(profile_info_size);
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000793 if (data == nullptr) {
794 return nullptr;
795 }
796 info = new (data) ProfilingInfo(method, entries);
Nicolas Geoffray07f35642016-01-04 16:06:51 +0000797
798 // Make sure other threads see the data in the profiling info object before the
799 // store in the ArtMethod's ProfilingInfo pointer.
800 QuasiAtomic::ThreadFenceRelease();
801
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000802 method->SetProfilingInfo(info);
803 profiling_infos_.push_back(info);
804 return info;
805}
806
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000807// NO_THREAD_SAFETY_ANALYSIS as this is called from mspace code, at which point the lock
808// is already held.
809void* JitCodeCache::MoreCore(const void* mspace, intptr_t increment) NO_THREAD_SAFETY_ANALYSIS {
810 if (code_mspace_ == mspace) {
811 size_t result = code_end_;
812 code_end_ += increment;
813 return reinterpret_cast<void*>(result + code_map_->Begin());
814 } else {
815 DCHECK_EQ(data_mspace_, mspace);
816 size_t result = data_end_;
817 data_end_ += increment;
818 return reinterpret_cast<void*>(result + data_map_->Begin());
819 }
820}
821
Calin Juravleb4eddd22016-01-13 15:52:33 -0800822void JitCodeCache::GetCompiledArtMethods(const std::set<std::string>& dex_base_locations,
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000823 std::vector<ArtMethod*>& methods) {
Calin Juravle31f2c152015-10-23 17:56:15 +0100824 MutexLock mu(Thread::Current(), lock_);
825 for (auto it : method_code_map_) {
Calin Juravle66f55232015-12-08 15:09:10 +0000826 if (ContainsElement(dex_base_locations, it.second->GetDexFile()->GetBaseLocation())) {
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000827 methods.push_back(it.second);
Calin Juravle31f2c152015-10-23 17:56:15 +0100828 }
829 }
830}
831
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000832uint64_t JitCodeCache::GetLastUpdateTimeNs() const {
833 return last_update_time_ns_.LoadAcquire();
Calin Juravle31f2c152015-10-23 17:56:15 +0100834}
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100835
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000836bool JitCodeCache::NotifyCompilationOf(ArtMethod* method, Thread* self, bool osr) {
837 if (!osr && ContainsPc(method->GetEntryPointFromQuickCompiledCode())) {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100838 return false;
839 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000840
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000841 MutexLock mu(self, lock_);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000842 if (osr && (osr_code_map_.find(method) != osr_code_map_.end())) {
843 return false;
844 }
Nicolas Geoffrayc26f1282016-01-29 11:41:25 +0000845 ProfilingInfo* info = method->GetProfilingInfo(sizeof(void*));
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100846 if (info == nullptr || info->IsMethodBeingCompiled()) {
847 return false;
848 }
849 info->SetIsMethodBeingCompiled(true);
850 return true;
851}
852
853void JitCodeCache::DoneCompiling(ArtMethod* method, Thread* self ATTRIBUTE_UNUSED) {
854 ProfilingInfo* info = method->GetProfilingInfo(sizeof(void*));
855 DCHECK(info->IsMethodBeingCompiled());
856 info->SetIsMethodBeingCompiled(false);
857}
858
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000859size_t JitCodeCache::GetMemorySizeOfCodePointer(const void* ptr) {
860 MutexLock mu(Thread::Current(), lock_);
861 return mspace_usable_size(reinterpret_cast<const void*>(FromCodeToAllocation(ptr)));
862}
863
Nicolas Geoffrayb88d59e2016-02-17 11:31:49 +0000864void JitCodeCache::InvalidateCompiledCodeFor(ArtMethod* method,
865 const OatQuickMethodHeader* header) {
Nicolas Geoffray7273a5d2016-02-29 15:35:39 +0000866 ProfilingInfo* profiling_info = method->GetProfilingInfo(sizeof(void*));
867 if ((profiling_info != nullptr) &&
868 (profiling_info->GetSavedEntryPoint() == header->GetEntryPoint())) {
869 // Prevent future uses of the compiled code.
870 profiling_info->SetSavedEntryPoint(nullptr);
871 }
872
Nicolas Geoffrayb88d59e2016-02-17 11:31:49 +0000873 if (method->GetEntryPointFromQuickCompiledCode() == header->GetEntryPoint()) {
874 // The entrypoint is the one to invalidate, so we just update
875 // it to the interpreter entry point and clear the counter to get the method
876 // Jitted again.
877 Runtime::Current()->GetInstrumentation()->UpdateMethodsCode(
878 method, GetQuickToInterpreterBridge());
879 method->ClearCounter();
880 } else {
881 MutexLock mu(Thread::Current(), lock_);
882 auto it = osr_code_map_.find(method);
883 if (it != osr_code_map_.end() && OatQuickMethodHeader::FromCodePointer(it->second) == header) {
884 // Remove the OSR method, to avoid using it again.
885 osr_code_map_.erase(it);
886 }
887 }
888}
889
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +0000890uint8_t* JitCodeCache::AllocateCode(size_t code_size) {
891 size_t alignment = GetInstructionSetAlignment(kRuntimeISA);
892 uint8_t* result = reinterpret_cast<uint8_t*>(
893 mspace_memalign(code_mspace_, alignment, code_size));
894 size_t header_size = RoundUp(sizeof(OatQuickMethodHeader), alignment);
895 // Ensure the header ends up at expected instruction alignment.
896 DCHECK_ALIGNED_PARAM(reinterpret_cast<uintptr_t>(result + header_size), alignment);
897 used_memory_for_code_ += mspace_usable_size(result);
898 return result;
899}
900
901void JitCodeCache::FreeCode(uint8_t* code) {
902 used_memory_for_code_ -= mspace_usable_size(code);
903 mspace_free(code_mspace_, code);
904}
905
906uint8_t* JitCodeCache::AllocateData(size_t data_size) {
907 void* result = mspace_malloc(data_mspace_, data_size);
908 used_memory_for_data_ += mspace_usable_size(result);
909 return reinterpret_cast<uint8_t*>(result);
910}
911
912void JitCodeCache::FreeData(uint8_t* data) {
913 used_memory_for_data_ -= mspace_usable_size(data);
914 mspace_free(data_mspace_, data);
915}
916
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800917} // namespace jit
918} // namespace art