blob: a6c8c5299b41b1940e2c2f0e8f540a7fe5adf4dc [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 Geoffray8d372502016-02-23 13:56:43 +0000126 has_done_full_collection_(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
549void JitCodeCache::RemoveUnusedCode(Thread* self) {
550 // Clear the osr map, chances are most of the code in it is now dead.
551 {
552 MutexLock mu(self, lock_);
553 osr_code_map_.clear();
554 }
555
556 // Run a checkpoint on all threads to mark the JIT compiled code they are running.
557 MarkCompiledCodeOnThreadStacks(self);
558
559 // Iterate over all compiled code and remove entries that are not marked and not
560 // the entrypoint of their corresponding ArtMethod.
561 {
562 MutexLock mu(self, lock_);
563 ScopedCodeCacheWrite scc(code_map_.get());
564 for (auto it = method_code_map_.begin(); it != method_code_map_.end();) {
565 const void* code_ptr = it->first;
566 ArtMethod* method = it->second;
567 uintptr_t allocation = FromCodeToAllocation(code_ptr);
568 const OatQuickMethodHeader* method_header = OatQuickMethodHeader::FromCodePointer(code_ptr);
569 if ((method->GetEntryPointFromQuickCompiledCode() != method_header->GetEntryPoint()) &&
570 !GetLiveBitmap()->Test(allocation)) {
571 FreeCode(code_ptr, method);
572 it = method_code_map_.erase(it);
573 } else {
574 ++it;
575 }
576 }
577 }
578}
579
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000580void JitCodeCache::GarbageCollectCache(Thread* self) {
Nicolas Geoffray8d372502016-02-23 13:56:43 +0000581 if (!garbage_collect_code_) {
582 MutexLock mu(self, lock_);
583 IncreaseCodeCacheCapacity();
584 return;
585 }
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100586
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000587 // Wait for an existing collection, or let everyone know we are starting one.
588 {
589 ScopedThreadSuspension sts(self, kSuspended);
590 MutexLock mu(self, lock_);
591 if (WaitForPotentialCollectionToComplete(self)) {
592 return;
593 } else {
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000594 live_bitmap_.reset(CodeCacheBitmap::Create(
595 "code-cache-bitmap",
596 reinterpret_cast<uintptr_t>(code_map_->Begin()),
597 reinterpret_cast<uintptr_t>(code_map_->Begin() + current_capacity_ / 2)));
Nicolas Geoffray8d372502016-02-23 13:56:43 +0000598 collection_in_progress_ = true;
599 }
600 }
601
602 // Check if we want to do a full collection.
603 bool do_full_collection = true;
604 {
605 MutexLock mu(self, lock_);
606 if (current_capacity_ == max_capacity_) {
607 // Always do a full collection when the code cache is full.
608 do_full_collection = true;
609 } else if (current_capacity_ < kReservedCapacity) {
610 // Do a partial collection until we hit the reserved capacity limit.
611 do_full_collection = false;
612 } else if (has_done_full_collection_) {
613 // Do a partial collection if we have done a full collection in the last
614 // collection round.
615 do_full_collection = false;
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000616 }
617 }
618
619 if (!kIsDebugBuild || VLOG_IS_ON(jit)) {
Nicolas Geoffray8d372502016-02-23 13:56:43 +0000620 LOG(INFO) << "Do "
621 << (do_full_collection ? "full" : "partial")
622 << " code cache collection, code="
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000623 << PrettySize(CodeCacheSize())
624 << ", data=" << PrettySize(DataCacheSize());
625 }
Nicolas Geoffray8d372502016-02-23 13:56:43 +0000626
627 if (do_full_collection) {
628 DoFullCollection(self);
629 } else {
630 RemoveUnusedCode(self);
631 }
632
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100633 {
634 MutexLock mu(self, lock_);
Nicolas Geoffray8d372502016-02-23 13:56:43 +0000635 if (!do_full_collection) {
636 has_done_full_collection_ = false;
637 IncreaseCodeCacheCapacity();
638 } else {
639 has_done_full_collection_ = true;
640 }
641 live_bitmap_.reset(nullptr);
642 NotifyCollectionDone(self);
643 }
644
645 if (!kIsDebugBuild || VLOG_IS_ON(jit)) {
646 LOG(INFO) << "After code cache collection, code="
647 << PrettySize(CodeCacheSize())
648 << ", data=" << PrettySize(DataCacheSize());
649 }
650}
651
652void JitCodeCache::DoFullCollection(Thread* self) {
653 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
654 {
655 MutexLock mu(self, lock_);
656 // Walk over all compiled methods and set the entry points of these
657 // methods to interpreter.
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100658 for (auto& it : method_code_map_) {
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000659 instrumentation->UpdateMethodsCode(it.second, GetQuickToInterpreterBridge());
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100660 }
Nicolas Geoffray8d372502016-02-23 13:56:43 +0000661
662 // Clear the profiling info of methods that are not being compiled.
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000663 for (ProfilingInfo* info : profiling_infos_) {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100664 if (!info->IsMethodBeingCompiled()) {
665 info->GetMethod()->SetProfilingInfo(nullptr);
666 }
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000667 }
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000668
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000669 // Empty osr method map, as osr compiled code will be deleted (except the ones
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000670 // on thread stacks).
671 osr_code_map_.clear();
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100672 }
673
674 // Run a checkpoint on all threads to mark the JIT compiled code they are running.
Nicolas Geoffray8d372502016-02-23 13:56:43 +0000675 MarkCompiledCodeOnThreadStacks(self);
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100676
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100677 {
678 MutexLock mu(self, lock_);
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000679 // Free unused compiled code, and restore the entry point of used compiled code.
680 {
681 ScopedCodeCacheWrite scc(code_map_.get());
682 for (auto it = method_code_map_.begin(); it != method_code_map_.end();) {
683 const void* code_ptr = it->first;
684 ArtMethod* method = it->second;
685 uintptr_t allocation = FromCodeToAllocation(code_ptr);
686 const OatQuickMethodHeader* method_header = OatQuickMethodHeader::FromCodePointer(code_ptr);
687 if (GetLiveBitmap()->Test(allocation)) {
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000688 instrumentation->UpdateMethodsCode(method, method_header->GetEntryPoint());
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000689 ++it;
690 } else {
691 method->ClearCounter();
692 DCHECK_NE(method->GetEntryPointFromQuickCompiledCode(), method_header->GetEntryPoint());
693 FreeCode(code_ptr, method);
694 it = method_code_map_.erase(it);
695 }
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100696 }
697 }
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000698
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100699 // Free all profiling infos of methods that were not being compiled.
700 auto profiling_kept_end = std::remove_if(profiling_infos_.begin(), profiling_infos_.end(),
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +0000701 [this] (ProfilingInfo* info) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100702 if (info->GetMethod()->GetProfilingInfo(sizeof(void*)) == nullptr) {
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +0000703 FreeData(reinterpret_cast<uint8_t*>(info));
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100704 return true;
705 }
706 return false;
707 });
708 profiling_infos_.erase(profiling_kept_end, profiling_infos_.end());
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100709 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800710}
711
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100712
713OatQuickMethodHeader* JitCodeCache::LookupMethodHeader(uintptr_t pc, ArtMethod* method) {
714 static_assert(kRuntimeISA != kThumb2, "kThumb2 cannot be a runtime ISA");
715 if (kRuntimeISA == kArm) {
716 // On Thumb-2, the pc is offset by one.
717 --pc;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800718 }
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100719 if (!ContainsPc(reinterpret_cast<const void*>(pc))) {
720 return nullptr;
721 }
722
723 MutexLock mu(Thread::Current(), lock_);
724 if (method_code_map_.empty()) {
725 return nullptr;
726 }
727 auto it = method_code_map_.lower_bound(reinterpret_cast<const void*>(pc));
728 --it;
729
730 const void* code_ptr = it->first;
731 OatQuickMethodHeader* method_header = OatQuickMethodHeader::FromCodePointer(code_ptr);
732 if (!method_header->Contains(pc)) {
733 return nullptr;
734 }
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +0000735 if (kIsDebugBuild && method != nullptr) {
736 DCHECK_EQ(it->second, method)
737 << PrettyMethod(method) << " " << PrettyMethod(it->second) << " " << std::hex << pc;
738 }
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100739 return method_header;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800740}
741
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000742OatQuickMethodHeader* JitCodeCache::LookupOsrMethodHeader(ArtMethod* method) {
743 MutexLock mu(Thread::Current(), lock_);
744 auto it = osr_code_map_.find(method);
745 if (it == osr_code_map_.end()) {
746 return nullptr;
747 }
748 return OatQuickMethodHeader::FromCodePointer(it->second);
749}
750
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000751ProfilingInfo* JitCodeCache::AddProfilingInfo(Thread* self,
752 ArtMethod* method,
753 const std::vector<uint32_t>& entries,
Nicolas Geoffray1e7da9b2016-03-01 14:11:40 +0000754 bool retry_allocation)
755 // No thread safety analysis as we are using TryLock/Unlock explicitly.
756 NO_THREAD_SAFETY_ANALYSIS {
757 ProfilingInfo* info = nullptr;
758 if (!retry_allocation) {
759 // If we are allocating for the interpreter, just try to lock, to avoid
760 // lock contention with the JIT.
761 if (lock_.ExclusiveTryLock(self)) {
762 info = AddProfilingInfoInternal(self, method, entries);
763 lock_.ExclusiveUnlock(self);
764 }
765 } else {
766 {
767 MutexLock mu(self, lock_);
768 info = AddProfilingInfoInternal(self, method, entries);
769 }
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000770
Nicolas Geoffray1e7da9b2016-03-01 14:11:40 +0000771 if (info == nullptr) {
772 GarbageCollectCache(self);
773 MutexLock mu(self, lock_);
774 info = AddProfilingInfoInternal(self, method, entries);
775 }
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000776 }
777 return info;
778}
779
Nicolas Geoffray1e7da9b2016-03-01 14:11:40 +0000780ProfilingInfo* JitCodeCache::AddProfilingInfoInternal(Thread* self ATTRIBUTE_UNUSED,
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000781 ArtMethod* method,
782 const std::vector<uint32_t>& entries) {
783 size_t profile_info_size = RoundUp(
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100784 sizeof(ProfilingInfo) + sizeof(InlineCache) * entries.size(),
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000785 sizeof(void*));
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000786
787 // Check whether some other thread has concurrently created it.
788 ProfilingInfo* info = method->GetProfilingInfo(sizeof(void*));
789 if (info != nullptr) {
790 return info;
791 }
792
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +0000793 uint8_t* data = AllocateData(profile_info_size);
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000794 if (data == nullptr) {
795 return nullptr;
796 }
797 info = new (data) ProfilingInfo(method, entries);
Nicolas Geoffray07f35642016-01-04 16:06:51 +0000798
799 // Make sure other threads see the data in the profiling info object before the
800 // store in the ArtMethod's ProfilingInfo pointer.
801 QuasiAtomic::ThreadFenceRelease();
802
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000803 method->SetProfilingInfo(info);
804 profiling_infos_.push_back(info);
805 return info;
806}
807
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000808// NO_THREAD_SAFETY_ANALYSIS as this is called from mspace code, at which point the lock
809// is already held.
810void* JitCodeCache::MoreCore(const void* mspace, intptr_t increment) NO_THREAD_SAFETY_ANALYSIS {
811 if (code_mspace_ == mspace) {
812 size_t result = code_end_;
813 code_end_ += increment;
814 return reinterpret_cast<void*>(result + code_map_->Begin());
815 } else {
816 DCHECK_EQ(data_mspace_, mspace);
817 size_t result = data_end_;
818 data_end_ += increment;
819 return reinterpret_cast<void*>(result + data_map_->Begin());
820 }
821}
822
Calin Juravleb4eddd22016-01-13 15:52:33 -0800823void JitCodeCache::GetCompiledArtMethods(const std::set<std::string>& dex_base_locations,
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000824 std::vector<ArtMethod*>& methods) {
Calin Juravle31f2c152015-10-23 17:56:15 +0100825 MutexLock mu(Thread::Current(), lock_);
826 for (auto it : method_code_map_) {
Calin Juravle66f55232015-12-08 15:09:10 +0000827 if (ContainsElement(dex_base_locations, it.second->GetDexFile()->GetBaseLocation())) {
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000828 methods.push_back(it.second);
Calin Juravle31f2c152015-10-23 17:56:15 +0100829 }
830 }
831}
832
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000833uint64_t JitCodeCache::GetLastUpdateTimeNs() const {
834 return last_update_time_ns_.LoadAcquire();
Calin Juravle31f2c152015-10-23 17:56:15 +0100835}
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100836
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000837bool JitCodeCache::NotifyCompilationOf(ArtMethod* method, Thread* self, bool osr) {
838 if (!osr && ContainsPc(method->GetEntryPointFromQuickCompiledCode())) {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100839 return false;
840 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000841
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000842 MutexLock mu(self, lock_);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000843 if (osr && (osr_code_map_.find(method) != osr_code_map_.end())) {
844 return false;
845 }
Nicolas Geoffrayc26f1282016-01-29 11:41:25 +0000846 ProfilingInfo* info = method->GetProfilingInfo(sizeof(void*));
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100847 if (info == nullptr || info->IsMethodBeingCompiled()) {
848 return false;
849 }
850 info->SetIsMethodBeingCompiled(true);
851 return true;
852}
853
854void JitCodeCache::DoneCompiling(ArtMethod* method, Thread* self ATTRIBUTE_UNUSED) {
855 ProfilingInfo* info = method->GetProfilingInfo(sizeof(void*));
856 DCHECK(info->IsMethodBeingCompiled());
857 info->SetIsMethodBeingCompiled(false);
858}
859
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000860size_t JitCodeCache::GetMemorySizeOfCodePointer(const void* ptr) {
861 MutexLock mu(Thread::Current(), lock_);
862 return mspace_usable_size(reinterpret_cast<const void*>(FromCodeToAllocation(ptr)));
863}
864
Nicolas Geoffrayb88d59e2016-02-17 11:31:49 +0000865void JitCodeCache::InvalidateCompiledCodeFor(ArtMethod* method,
866 const OatQuickMethodHeader* header) {
867 if (method->GetEntryPointFromQuickCompiledCode() == header->GetEntryPoint()) {
868 // The entrypoint is the one to invalidate, so we just update
869 // it to the interpreter entry point and clear the counter to get the method
870 // Jitted again.
871 Runtime::Current()->GetInstrumentation()->UpdateMethodsCode(
872 method, GetQuickToInterpreterBridge());
873 method->ClearCounter();
874 } else {
875 MutexLock mu(Thread::Current(), lock_);
876 auto it = osr_code_map_.find(method);
877 if (it != osr_code_map_.end() && OatQuickMethodHeader::FromCodePointer(it->second) == header) {
878 // Remove the OSR method, to avoid using it again.
879 osr_code_map_.erase(it);
880 }
881 }
882}
883
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +0000884uint8_t* JitCodeCache::AllocateCode(size_t code_size) {
885 size_t alignment = GetInstructionSetAlignment(kRuntimeISA);
886 uint8_t* result = reinterpret_cast<uint8_t*>(
887 mspace_memalign(code_mspace_, alignment, code_size));
888 size_t header_size = RoundUp(sizeof(OatQuickMethodHeader), alignment);
889 // Ensure the header ends up at expected instruction alignment.
890 DCHECK_ALIGNED_PARAM(reinterpret_cast<uintptr_t>(result + header_size), alignment);
891 used_memory_for_code_ += mspace_usable_size(result);
892 return result;
893}
894
895void JitCodeCache::FreeCode(uint8_t* code) {
896 used_memory_for_code_ -= mspace_usable_size(code);
897 mspace_free(code_mspace_, code);
898}
899
900uint8_t* JitCodeCache::AllocateData(size_t data_size) {
901 void* result = mspace_malloc(data_mspace_, data_size);
902 used_memory_for_data_ += mspace_usable_size(result);
903 return reinterpret_cast<uint8_t*>(result);
904}
905
906void JitCodeCache::FreeData(uint8_t* data) {
907 used_memory_for_data_ -= mspace_usable_size(data);
908 mspace_free(data_mspace_, data);
909}
910
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800911} // namespace jit
912} // namespace art