blob: 478b1645977ac8b6eedc27ee0de724805fee4d26 [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 Geoffray0a522232016-01-19 09:34:58 +0000131 number_of_compilations_(0) {
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100132
Nicolas Geoffrayc3fec4c2016-01-14 16:16:35 +0000133 DCHECK_GE(max_capacity, initial_code_capacity + initial_data_capacity);
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000134 code_mspace_ = create_mspace_with_base(code_map_->Begin(), code_end_, false /*locked*/);
135 data_mspace_ = create_mspace_with_base(data_map_->Begin(), data_end_, false /*locked*/);
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100136
137 if (code_mspace_ == nullptr || data_mspace_ == nullptr) {
138 PLOG(FATAL) << "create_mspace_with_base failed";
139 }
140
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000141 SetFootprintLimit(current_capacity_);
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100142
143 CHECKED_MPROTECT(code_map_->Begin(), code_map_->Size(), kProtCode);
144 CHECKED_MPROTECT(data_map_->Begin(), data_map_->Size(), kProtData);
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100145
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000146 VLOG(jit) << "Created jit code cache: initial data size="
147 << PrettySize(initial_data_capacity)
148 << ", initial code size="
149 << PrettySize(initial_code_capacity);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800150}
151
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100152bool JitCodeCache::ContainsPc(const void* ptr) const {
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100153 return code_map_->Begin() <= ptr && ptr < code_map_->End();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800154}
155
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000156bool JitCodeCache::ContainsMethod(ArtMethod* method) {
157 MutexLock mu(Thread::Current(), lock_);
158 for (auto& it : method_code_map_) {
159 if (it.second == method) {
160 return true;
161 }
162 }
163 return false;
164}
165
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100166class ScopedCodeCacheWrite {
167 public:
168 explicit ScopedCodeCacheWrite(MemMap* code_map) : code_map_(code_map) {
169 CHECKED_MPROTECT(code_map_->Begin(), code_map_->Size(), kProtAll);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800170 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100171 ~ScopedCodeCacheWrite() {
172 CHECKED_MPROTECT(code_map_->Begin(), code_map_->Size(), kProtCode);
173 }
174 private:
175 MemMap* const code_map_;
176
177 DISALLOW_COPY_AND_ASSIGN(ScopedCodeCacheWrite);
178};
179
180uint8_t* JitCodeCache::CommitCode(Thread* self,
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100181 ArtMethod* method,
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100182 const uint8_t* mapping_table,
183 const uint8_t* vmap_table,
184 const uint8_t* gc_map,
185 size_t frame_size_in_bytes,
186 size_t core_spill_mask,
187 size_t fp_spill_mask,
188 const uint8_t* code,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000189 size_t code_size,
190 bool osr) {
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100191 uint8_t* result = CommitCodeInternal(self,
192 method,
193 mapping_table,
194 vmap_table,
195 gc_map,
196 frame_size_in_bytes,
197 core_spill_mask,
198 fp_spill_mask,
199 code,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000200 code_size,
201 osr);
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100202 if (result == nullptr) {
203 // Retry.
204 GarbageCollectCache(self);
205 result = CommitCodeInternal(self,
206 method,
207 mapping_table,
208 vmap_table,
209 gc_map,
210 frame_size_in_bytes,
211 core_spill_mask,
212 fp_spill_mask,
213 code,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000214 code_size,
215 osr);
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100216 }
217 return result;
218}
219
220bool JitCodeCache::WaitForPotentialCollectionToComplete(Thread* self) {
221 bool in_collection = false;
222 while (collection_in_progress_) {
223 in_collection = true;
224 lock_cond_.Wait(self);
225 }
226 return in_collection;
227}
228
229static uintptr_t FromCodeToAllocation(const void* code) {
230 size_t alignment = GetInstructionSetAlignment(kRuntimeISA);
231 return reinterpret_cast<uintptr_t>(code) - RoundUp(sizeof(OatQuickMethodHeader), alignment);
232}
233
234void JitCodeCache::FreeCode(const void* code_ptr, ArtMethod* method ATTRIBUTE_UNUSED) {
235 uintptr_t allocation = FromCodeToAllocation(code_ptr);
236 const OatQuickMethodHeader* method_header = OatQuickMethodHeader::FromCodePointer(code_ptr);
David Srbecky5cc349f2015-12-18 15:04:48 +0000237 // Notify native debugger that we are about to remove the code.
238 // It does nothing if we are not using native debugger.
239 DeleteJITCodeEntryForAddress(reinterpret_cast<uintptr_t>(code_ptr));
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +0000240
241 FreeData(const_cast<uint8_t*>(method_header->GetNativeGcMap()));
242 FreeData(const_cast<uint8_t*>(method_header->GetMappingTable()));
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100243 // Use the offset directly to prevent sanity check that the method is
244 // compiled with optimizing.
245 // TODO(ngeoffray): Clean up.
246 if (method_header->vmap_table_offset_ != 0) {
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +0000247 const uint8_t* data = method_header->code_ - method_header->vmap_table_offset_;
248 FreeData(const_cast<uint8_t*>(data));
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100249 }
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +0000250 FreeCode(reinterpret_cast<uint8_t*>(allocation));
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100251}
252
253void JitCodeCache::RemoveMethodsIn(Thread* self, const LinearAlloc& alloc) {
254 MutexLock mu(self, lock_);
255 // We do not check if a code cache GC is in progress, as this method comes
256 // with the classlinker_classes_lock_ held, and suspending ourselves could
257 // lead to a deadlock.
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000258 {
259 ScopedCodeCacheWrite scc(code_map_.get());
260 for (auto it = method_code_map_.begin(); it != method_code_map_.end();) {
261 if (alloc.ContainsUnsafe(it->second)) {
262 FreeCode(it->first, it->second);
263 it = method_code_map_.erase(it);
264 } else {
265 ++it;
266 }
267 }
268 }
Nicolas Geoffraya9b91312016-02-17 09:49:19 +0000269 for (auto it = osr_code_map_.begin(); it != osr_code_map_.end();) {
270 if (alloc.ContainsUnsafe(it->first)) {
271 // Note that the code has already been removed in the loop above.
272 it = osr_code_map_.erase(it);
273 } else {
274 ++it;
275 }
276 }
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000277 for (auto it = profiling_infos_.begin(); it != profiling_infos_.end();) {
278 ProfilingInfo* info = *it;
279 if (alloc.ContainsUnsafe(info->GetMethod())) {
280 info->GetMethod()->SetProfilingInfo(nullptr);
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +0000281 FreeData(reinterpret_cast<uint8_t*>(info));
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000282 it = profiling_infos_.erase(it);
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100283 } else {
284 ++it;
285 }
286 }
287}
288
289uint8_t* JitCodeCache::CommitCodeInternal(Thread* self,
290 ArtMethod* method,
291 const uint8_t* mapping_table,
292 const uint8_t* vmap_table,
293 const uint8_t* gc_map,
294 size_t frame_size_in_bytes,
295 size_t core_spill_mask,
296 size_t fp_spill_mask,
297 const uint8_t* code,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000298 size_t code_size,
299 bool osr) {
Nicolas Geoffray1e7de6c2015-10-21 12:07:31 +0100300 size_t alignment = GetInstructionSetAlignment(kRuntimeISA);
301 // Ensure the header ends up at expected instruction alignment.
302 size_t header_size = RoundUp(sizeof(OatQuickMethodHeader), alignment);
303 size_t total_size = header_size + code_size;
304
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100305 OatQuickMethodHeader* method_header = nullptr;
Nicolas Geoffray1e7de6c2015-10-21 12:07:31 +0100306 uint8_t* code_ptr = nullptr;
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +0000307 uint8_t* memory = nullptr;
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100308 {
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000309 ScopedThreadSuspension sts(self, kSuspended);
310 MutexLock mu(self, lock_);
311 WaitForPotentialCollectionToComplete(self);
312 {
313 ScopedCodeCacheWrite scc(code_map_.get());
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +0000314 memory = AllocateCode(total_size);
315 if (memory == nullptr) {
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000316 return nullptr;
317 }
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +0000318 code_ptr = memory + header_size;
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000319
320 std::copy(code, code + code_size, code_ptr);
321 method_header = OatQuickMethodHeader::FromCodePointer(code_ptr);
322 new (method_header) OatQuickMethodHeader(
323 (mapping_table == nullptr) ? 0 : code_ptr - mapping_table,
324 (vmap_table == nullptr) ? 0 : code_ptr - vmap_table,
325 (gc_map == nullptr) ? 0 : code_ptr - gc_map,
326 frame_size_in_bytes,
327 core_spill_mask,
328 fp_spill_mask,
329 code_size);
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100330 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100331
Roland Levillain32430262016-02-01 15:23:20 +0000332 FlushInstructionCache(reinterpret_cast<char*>(code_ptr),
333 reinterpret_cast<char*>(code_ptr + code_size));
Nicolas Geoffray0a522232016-01-19 09:34:58 +0000334 number_of_compilations_++;
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100335 }
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000336 // We need to update the entry point in the runnable state for the instrumentation.
337 {
338 MutexLock mu(self, lock_);
339 method_code_map_.Put(code_ptr, method);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000340 if (osr) {
341 osr_code_map_.Put(method, code_ptr);
342 } else {
343 Runtime::Current()->GetInstrumentation()->UpdateMethodsCode(
344 method, method_header->GetEntryPoint());
345 }
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000346 if (collection_in_progress_) {
347 // We need to update the live bitmap if there is a GC to ensure it sees this new
348 // code.
349 GetLiveBitmap()->AtomicTestAndSet(FromCodeToAllocation(code_ptr));
350 }
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000351 last_update_time_ns_.StoreRelease(NanoTime());
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000352 VLOG(jit)
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000353 << "JIT added (osr = " << std::boolalpha << osr << std::noboolalpha << ") "
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000354 << PrettyMethod(method) << "@" << method
355 << " ccache_size=" << PrettySize(CodeCacheSizeLocked()) << ": "
356 << " dcache_size=" << PrettySize(DataCacheSizeLocked()) << ": "
357 << reinterpret_cast<const void*>(method_header->GetEntryPoint()) << ","
358 << reinterpret_cast<const void*>(method_header->GetEntryPoint() + method_header->code_size_);
359 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100360
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100361 return reinterpret_cast<uint8_t*>(method_header);
362}
363
Nicolas Geoffray0a522232016-01-19 09:34:58 +0000364size_t JitCodeCache::NumberOfCompilations() {
365 MutexLock mu(Thread::Current(), lock_);
366 return number_of_compilations_;
367}
368
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100369size_t JitCodeCache::CodeCacheSize() {
370 MutexLock mu(Thread::Current(), lock_);
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000371 return CodeCacheSizeLocked();
372}
373
374size_t JitCodeCache::CodeCacheSizeLocked() {
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +0000375 return used_memory_for_code_;
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100376}
377
378size_t JitCodeCache::DataCacheSize() {
379 MutexLock mu(Thread::Current(), lock_);
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000380 return DataCacheSizeLocked();
381}
382
383size_t JitCodeCache::DataCacheSizeLocked() {
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +0000384 return used_memory_for_data_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800385}
386
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100387size_t JitCodeCache::NumberOfCompiledCode() {
388 MutexLock mu(Thread::Current(), lock_);
389 return method_code_map_.size();
390}
391
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000392void JitCodeCache::ClearData(Thread* self, void* data) {
393 MutexLock mu(self, lock_);
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +0000394 FreeData(reinterpret_cast<uint8_t*>(data));
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000395}
396
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100397uint8_t* JitCodeCache::ReserveData(Thread* self, size_t size) {
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100398 size = RoundUp(size, sizeof(void*));
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100399 uint8_t* result = nullptr;
400
401 {
402 ScopedThreadSuspension sts(self, kSuspended);
403 MutexLock mu(self, lock_);
404 WaitForPotentialCollectionToComplete(self);
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +0000405 result = AllocateData(size);
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100406 }
407
408 if (result == nullptr) {
409 // Retry.
410 GarbageCollectCache(self);
411 ScopedThreadSuspension sts(self, kSuspended);
412 MutexLock mu(self, lock_);
413 WaitForPotentialCollectionToComplete(self);
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +0000414 result = AllocateData(size);
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100415 }
416
417 return result;
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100418}
419
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800420uint8_t* JitCodeCache::AddDataArray(Thread* self, const uint8_t* begin, const uint8_t* end) {
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100421 uint8_t* result = ReserveData(self, end - begin);
422 if (result == nullptr) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800423 return nullptr; // Out of space in the data cache.
424 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100425 std::copy(begin, end, result);
426 return result;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800427}
428
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100429class MarkCodeVisitor FINAL : public StackVisitor {
430 public:
431 MarkCodeVisitor(Thread* thread_in, JitCodeCache* code_cache_in)
432 : StackVisitor(thread_in, nullptr, StackVisitor::StackWalkKind::kSkipInlinedFrames),
433 code_cache_(code_cache_in),
434 bitmap_(code_cache_->GetLiveBitmap()) {}
435
436 bool VisitFrame() OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
437 const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader();
438 if (method_header == nullptr) {
439 return true;
440 }
441 const void* code = method_header->GetCode();
442 if (code_cache_->ContainsPc(code)) {
443 // Use the atomic set version, as multiple threads are executing this code.
444 bitmap_->AtomicTestAndSet(FromCodeToAllocation(code));
445 }
446 return true;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800447 }
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100448
449 private:
450 JitCodeCache* const code_cache_;
451 CodeCacheBitmap* const bitmap_;
452};
453
454class MarkCodeClosure FINAL : public Closure {
455 public:
456 MarkCodeClosure(JitCodeCache* code_cache, Barrier* barrier)
457 : code_cache_(code_cache), barrier_(barrier) {}
458
459 void Run(Thread* thread) OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
460 DCHECK(thread == Thread::Current() || thread->IsSuspended());
461 MarkCodeVisitor visitor(thread, code_cache_);
462 visitor.WalkStack();
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +0000463 if (kIsDebugBuild) {
464 // The stack walking code queries the side instrumentation stack if it
465 // sees an instrumentation exit pc, so the JIT code of methods in that stack
466 // must have been seen. We sanity check this below.
467 for (const instrumentation::InstrumentationStackFrame& frame
468 : *thread->GetInstrumentationStack()) {
469 // The 'method_' in InstrumentationStackFrame is the one that has return_pc_ in
470 // its stack frame, it is not the method owning return_pc_. We just pass null to
471 // LookupMethodHeader: the method is only checked against in debug builds.
472 OatQuickMethodHeader* method_header =
473 code_cache_->LookupMethodHeader(frame.return_pc_, nullptr);
474 if (method_header != nullptr) {
475 const void* code = method_header->GetCode();
476 CHECK(code_cache_->GetLiveBitmap()->Test(FromCodeToAllocation(code)));
477 }
478 }
479 }
Mathieu Chartier10d25082015-10-28 18:36:09 -0700480 barrier_->Pass(Thread::Current());
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800481 }
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100482
483 private:
484 JitCodeCache* const code_cache_;
485 Barrier* const barrier_;
486};
487
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000488void JitCodeCache::NotifyCollectionDone(Thread* self) {
489 collection_in_progress_ = false;
490 lock_cond_.Broadcast(self);
491}
492
493void JitCodeCache::SetFootprintLimit(size_t new_footprint) {
494 size_t per_space_footprint = new_footprint / 2;
495 DCHECK(IsAlignedParam(per_space_footprint, kPageSize));
496 DCHECK_EQ(per_space_footprint * 2, new_footprint);
497 mspace_set_footprint_limit(data_mspace_, per_space_footprint);
498 {
499 ScopedCodeCacheWrite scc(code_map_.get());
500 mspace_set_footprint_limit(code_mspace_, per_space_footprint);
501 }
502}
503
504bool JitCodeCache::IncreaseCodeCacheCapacity() {
505 if (current_capacity_ == max_capacity_) {
506 return false;
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100507 }
508
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000509 // Double the capacity if we're below 1MB, or increase it by 1MB if
510 // we're above.
511 if (current_capacity_ < 1 * MB) {
512 current_capacity_ *= 2;
513 } else {
514 current_capacity_ += 1 * MB;
515 }
516 if (current_capacity_ > max_capacity_) {
517 current_capacity_ = max_capacity_;
518 }
519
520 if (!kIsDebugBuild || VLOG_IS_ON(jit)) {
521 LOG(INFO) << "Increasing code cache capacity to " << PrettySize(current_capacity_);
522 }
523
524 SetFootprintLimit(current_capacity_);
525
526 return true;
527}
528
Nicolas Geoffray8d372502016-02-23 13:56:43 +0000529void JitCodeCache::MarkCompiledCodeOnThreadStacks(Thread* self) {
530 Barrier barrier(0);
531 size_t threads_running_checkpoint = 0;
532 MarkCodeClosure closure(this, &barrier);
533 threads_running_checkpoint = Runtime::Current()->GetThreadList()->RunCheckpoint(&closure);
534 // Now that we have run our checkpoint, move to a suspended state and wait
535 // for other threads to run the checkpoint.
536 ScopedThreadSuspension sts(self, kSuspended);
537 if (threads_running_checkpoint != 0) {
538 barrier.Increment(self, threads_running_checkpoint);
539 }
540}
541
542void JitCodeCache::RemoveUnusedCode(Thread* self) {
543 // Clear the osr map, chances are most of the code in it is now dead.
544 {
545 MutexLock mu(self, lock_);
546 osr_code_map_.clear();
547 }
548
549 // Run a checkpoint on all threads to mark the JIT compiled code they are running.
550 MarkCompiledCodeOnThreadStacks(self);
551
552 // Iterate over all compiled code and remove entries that are not marked and not
553 // the entrypoint of their corresponding ArtMethod.
554 {
555 MutexLock mu(self, lock_);
556 ScopedCodeCacheWrite scc(code_map_.get());
557 for (auto it = method_code_map_.begin(); it != method_code_map_.end();) {
558 const void* code_ptr = it->first;
559 ArtMethod* method = it->second;
560 uintptr_t allocation = FromCodeToAllocation(code_ptr);
561 const OatQuickMethodHeader* method_header = OatQuickMethodHeader::FromCodePointer(code_ptr);
562 if ((method->GetEntryPointFromQuickCompiledCode() != method_header->GetEntryPoint()) &&
563 !GetLiveBitmap()->Test(allocation)) {
564 FreeCode(code_ptr, method);
565 it = method_code_map_.erase(it);
566 } else {
567 ++it;
568 }
569 }
570 }
571}
572
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000573void JitCodeCache::GarbageCollectCache(Thread* self) {
Nicolas Geoffray8d372502016-02-23 13:56:43 +0000574 if (!garbage_collect_code_) {
575 MutexLock mu(self, lock_);
576 IncreaseCodeCacheCapacity();
577 return;
578 }
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100579
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000580 // Wait for an existing collection, or let everyone know we are starting one.
581 {
582 ScopedThreadSuspension sts(self, kSuspended);
583 MutexLock mu(self, lock_);
584 if (WaitForPotentialCollectionToComplete(self)) {
585 return;
586 } else {
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000587 live_bitmap_.reset(CodeCacheBitmap::Create(
588 "code-cache-bitmap",
589 reinterpret_cast<uintptr_t>(code_map_->Begin()),
590 reinterpret_cast<uintptr_t>(code_map_->Begin() + current_capacity_ / 2)));
Nicolas Geoffray8d372502016-02-23 13:56:43 +0000591 collection_in_progress_ = true;
592 }
593 }
594
595 // Check if we want to do a full collection.
596 bool do_full_collection = true;
597 {
598 MutexLock mu(self, lock_);
599 if (current_capacity_ == max_capacity_) {
600 // Always do a full collection when the code cache is full.
601 do_full_collection = true;
602 } else if (current_capacity_ < kReservedCapacity) {
603 // Do a partial collection until we hit the reserved capacity limit.
604 do_full_collection = false;
605 } else if (has_done_full_collection_) {
606 // Do a partial collection if we have done a full collection in the last
607 // collection round.
608 do_full_collection = false;
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000609 }
610 }
611
612 if (!kIsDebugBuild || VLOG_IS_ON(jit)) {
Nicolas Geoffray8d372502016-02-23 13:56:43 +0000613 LOG(INFO) << "Do "
614 << (do_full_collection ? "full" : "partial")
615 << " code cache collection, code="
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000616 << PrettySize(CodeCacheSize())
617 << ", data=" << PrettySize(DataCacheSize());
618 }
Nicolas Geoffray8d372502016-02-23 13:56:43 +0000619
620 if (do_full_collection) {
621 DoFullCollection(self);
622 } else {
623 RemoveUnusedCode(self);
624 }
625
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100626 {
627 MutexLock mu(self, lock_);
Nicolas Geoffray8d372502016-02-23 13:56:43 +0000628 if (!do_full_collection) {
629 has_done_full_collection_ = false;
630 IncreaseCodeCacheCapacity();
631 } else {
632 has_done_full_collection_ = true;
633 }
634 live_bitmap_.reset(nullptr);
635 NotifyCollectionDone(self);
636 }
637
638 if (!kIsDebugBuild || VLOG_IS_ON(jit)) {
639 LOG(INFO) << "After code cache collection, code="
640 << PrettySize(CodeCacheSize())
641 << ", data=" << PrettySize(DataCacheSize());
642 }
643}
644
645void JitCodeCache::DoFullCollection(Thread* self) {
646 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
647 {
648 MutexLock mu(self, lock_);
649 // Walk over all compiled methods and set the entry points of these
650 // methods to interpreter.
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100651 for (auto& it : method_code_map_) {
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000652 instrumentation->UpdateMethodsCode(it.second, GetQuickToInterpreterBridge());
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100653 }
Nicolas Geoffray8d372502016-02-23 13:56:43 +0000654
655 // Clear the profiling info of methods that are not being compiled.
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000656 for (ProfilingInfo* info : profiling_infos_) {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100657 if (!info->IsMethodBeingCompiled()) {
658 info->GetMethod()->SetProfilingInfo(nullptr);
659 }
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000660 }
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000661
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000662 // Empty osr method map, as osr compiled code will be deleted (except the ones
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000663 // on thread stacks).
664 osr_code_map_.clear();
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100665 }
666
667 // Run a checkpoint on all threads to mark the JIT compiled code they are running.
Nicolas Geoffray8d372502016-02-23 13:56:43 +0000668 MarkCompiledCodeOnThreadStacks(self);
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100669
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100670 {
671 MutexLock mu(self, lock_);
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000672 // Free unused compiled code, and restore the entry point of used compiled code.
673 {
674 ScopedCodeCacheWrite scc(code_map_.get());
675 for (auto it = method_code_map_.begin(); it != method_code_map_.end();) {
676 const void* code_ptr = it->first;
677 ArtMethod* method = it->second;
678 uintptr_t allocation = FromCodeToAllocation(code_ptr);
679 const OatQuickMethodHeader* method_header = OatQuickMethodHeader::FromCodePointer(code_ptr);
680 if (GetLiveBitmap()->Test(allocation)) {
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000681 instrumentation->UpdateMethodsCode(method, method_header->GetEntryPoint());
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000682 ++it;
683 } else {
684 method->ClearCounter();
685 DCHECK_NE(method->GetEntryPointFromQuickCompiledCode(), method_header->GetEntryPoint());
686 FreeCode(code_ptr, method);
687 it = method_code_map_.erase(it);
688 }
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100689 }
690 }
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000691
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100692 // Free all profiling infos of methods that were not being compiled.
693 auto profiling_kept_end = std::remove_if(profiling_infos_.begin(), profiling_infos_.end(),
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +0000694 [this] (ProfilingInfo* info) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100695 if (info->GetMethod()->GetProfilingInfo(sizeof(void*)) == nullptr) {
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +0000696 FreeData(reinterpret_cast<uint8_t*>(info));
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100697 return true;
698 }
699 return false;
700 });
701 profiling_infos_.erase(profiling_kept_end, profiling_infos_.end());
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100702 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800703}
704
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100705
706OatQuickMethodHeader* JitCodeCache::LookupMethodHeader(uintptr_t pc, ArtMethod* method) {
707 static_assert(kRuntimeISA != kThumb2, "kThumb2 cannot be a runtime ISA");
708 if (kRuntimeISA == kArm) {
709 // On Thumb-2, the pc is offset by one.
710 --pc;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800711 }
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100712 if (!ContainsPc(reinterpret_cast<const void*>(pc))) {
713 return nullptr;
714 }
715
716 MutexLock mu(Thread::Current(), lock_);
717 if (method_code_map_.empty()) {
718 return nullptr;
719 }
720 auto it = method_code_map_.lower_bound(reinterpret_cast<const void*>(pc));
721 --it;
722
723 const void* code_ptr = it->first;
724 OatQuickMethodHeader* method_header = OatQuickMethodHeader::FromCodePointer(code_ptr);
725 if (!method_header->Contains(pc)) {
726 return nullptr;
727 }
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +0000728 if (kIsDebugBuild && method != nullptr) {
729 DCHECK_EQ(it->second, method)
730 << PrettyMethod(method) << " " << PrettyMethod(it->second) << " " << std::hex << pc;
731 }
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100732 return method_header;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800733}
734
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000735OatQuickMethodHeader* JitCodeCache::LookupOsrMethodHeader(ArtMethod* method) {
736 MutexLock mu(Thread::Current(), lock_);
737 auto it = osr_code_map_.find(method);
738 if (it == osr_code_map_.end()) {
739 return nullptr;
740 }
741 return OatQuickMethodHeader::FromCodePointer(it->second);
742}
743
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000744ProfilingInfo* JitCodeCache::AddProfilingInfo(Thread* self,
745 ArtMethod* method,
746 const std::vector<uint32_t>& entries,
747 bool retry_allocation) {
748 ProfilingInfo* info = AddProfilingInfoInternal(self, method, entries);
749
750 if (info == nullptr && retry_allocation) {
751 GarbageCollectCache(self);
752 info = AddProfilingInfoInternal(self, method, entries);
753 }
754 return info;
755}
756
757ProfilingInfo* JitCodeCache::AddProfilingInfoInternal(Thread* self,
758 ArtMethod* method,
759 const std::vector<uint32_t>& entries) {
760 size_t profile_info_size = RoundUp(
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100761 sizeof(ProfilingInfo) + sizeof(InlineCache) * entries.size(),
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000762 sizeof(void*));
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000763 MutexLock mu(self, lock_);
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000764
765 // Check whether some other thread has concurrently created it.
766 ProfilingInfo* info = method->GetProfilingInfo(sizeof(void*));
767 if (info != nullptr) {
768 return info;
769 }
770
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +0000771 uint8_t* data = AllocateData(profile_info_size);
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000772 if (data == nullptr) {
773 return nullptr;
774 }
775 info = new (data) ProfilingInfo(method, entries);
Nicolas Geoffray07f35642016-01-04 16:06:51 +0000776
777 // Make sure other threads see the data in the profiling info object before the
778 // store in the ArtMethod's ProfilingInfo pointer.
779 QuasiAtomic::ThreadFenceRelease();
780
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000781 method->SetProfilingInfo(info);
782 profiling_infos_.push_back(info);
783 return info;
784}
785
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000786// NO_THREAD_SAFETY_ANALYSIS as this is called from mspace code, at which point the lock
787// is already held.
788void* JitCodeCache::MoreCore(const void* mspace, intptr_t increment) NO_THREAD_SAFETY_ANALYSIS {
789 if (code_mspace_ == mspace) {
790 size_t result = code_end_;
791 code_end_ += increment;
792 return reinterpret_cast<void*>(result + code_map_->Begin());
793 } else {
794 DCHECK_EQ(data_mspace_, mspace);
795 size_t result = data_end_;
796 data_end_ += increment;
797 return reinterpret_cast<void*>(result + data_map_->Begin());
798 }
799}
800
Calin Juravleb4eddd22016-01-13 15:52:33 -0800801void JitCodeCache::GetCompiledArtMethods(const std::set<std::string>& dex_base_locations,
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000802 std::vector<ArtMethod*>& methods) {
Calin Juravle31f2c152015-10-23 17:56:15 +0100803 MutexLock mu(Thread::Current(), lock_);
804 for (auto it : method_code_map_) {
Calin Juravle66f55232015-12-08 15:09:10 +0000805 if (ContainsElement(dex_base_locations, it.second->GetDexFile()->GetBaseLocation())) {
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000806 methods.push_back(it.second);
Calin Juravle31f2c152015-10-23 17:56:15 +0100807 }
808 }
809}
810
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000811uint64_t JitCodeCache::GetLastUpdateTimeNs() const {
812 return last_update_time_ns_.LoadAcquire();
Calin Juravle31f2c152015-10-23 17:56:15 +0100813}
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100814
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000815bool JitCodeCache::NotifyCompilationOf(ArtMethod* method, Thread* self, bool osr) {
816 if (!osr && ContainsPc(method->GetEntryPointFromQuickCompiledCode())) {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100817 return false;
818 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000819
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000820 MutexLock mu(self, lock_);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000821 if (osr && (osr_code_map_.find(method) != osr_code_map_.end())) {
822 return false;
823 }
Nicolas Geoffrayc26f1282016-01-29 11:41:25 +0000824 ProfilingInfo* info = method->GetProfilingInfo(sizeof(void*));
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100825 if (info == nullptr || info->IsMethodBeingCompiled()) {
826 return false;
827 }
828 info->SetIsMethodBeingCompiled(true);
829 return true;
830}
831
832void JitCodeCache::DoneCompiling(ArtMethod* method, Thread* self ATTRIBUTE_UNUSED) {
833 ProfilingInfo* info = method->GetProfilingInfo(sizeof(void*));
834 DCHECK(info->IsMethodBeingCompiled());
835 info->SetIsMethodBeingCompiled(false);
836}
837
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000838size_t JitCodeCache::GetMemorySizeOfCodePointer(const void* ptr) {
839 MutexLock mu(Thread::Current(), lock_);
840 return mspace_usable_size(reinterpret_cast<const void*>(FromCodeToAllocation(ptr)));
841}
842
Nicolas Geoffrayb88d59e2016-02-17 11:31:49 +0000843void JitCodeCache::InvalidateCompiledCodeFor(ArtMethod* method,
844 const OatQuickMethodHeader* header) {
845 if (method->GetEntryPointFromQuickCompiledCode() == header->GetEntryPoint()) {
846 // The entrypoint is the one to invalidate, so we just update
847 // it to the interpreter entry point and clear the counter to get the method
848 // Jitted again.
849 Runtime::Current()->GetInstrumentation()->UpdateMethodsCode(
850 method, GetQuickToInterpreterBridge());
851 method->ClearCounter();
852 } else {
853 MutexLock mu(Thread::Current(), lock_);
854 auto it = osr_code_map_.find(method);
855 if (it != osr_code_map_.end() && OatQuickMethodHeader::FromCodePointer(it->second) == header) {
856 // Remove the OSR method, to avoid using it again.
857 osr_code_map_.erase(it);
858 }
859 }
860}
861
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +0000862uint8_t* JitCodeCache::AllocateCode(size_t code_size) {
863 size_t alignment = GetInstructionSetAlignment(kRuntimeISA);
864 uint8_t* result = reinterpret_cast<uint8_t*>(
865 mspace_memalign(code_mspace_, alignment, code_size));
866 size_t header_size = RoundUp(sizeof(OatQuickMethodHeader), alignment);
867 // Ensure the header ends up at expected instruction alignment.
868 DCHECK_ALIGNED_PARAM(reinterpret_cast<uintptr_t>(result + header_size), alignment);
869 used_memory_for_code_ += mspace_usable_size(result);
870 return result;
871}
872
873void JitCodeCache::FreeCode(uint8_t* code) {
874 used_memory_for_code_ -= mspace_usable_size(code);
875 mspace_free(code_mspace_, code);
876}
877
878uint8_t* JitCodeCache::AllocateData(size_t data_size) {
879 void* result = mspace_malloc(data_mspace_, data_size);
880 used_memory_for_data_ += mspace_usable_size(result);
881 return reinterpret_cast<uint8_t*>(result);
882}
883
884void JitCodeCache::FreeData(uint8_t* data) {
885 used_memory_for_data_ -= mspace_usable_size(data);
886 mspace_free(data_mspace_, data);
887}
888
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800889} // namespace jit
890} // namespace art