blob: 2ae989a239d6df0c0ff6feb0a99899b50cebcf1f [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"
Andreas Gampe542451c2016-07-26 09:02:02 -070022#include "base/enums.h"
Calin Juravle66f55232015-12-08 15:09:10 +000023#include "base/stl_util.h"
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -080024#include "base/systrace.h"
Calin Juravle31f2c152015-10-23 17:56:15 +010025#include "base/time_utils.h"
David Srbecky5cc349f2015-12-18 15:04:48 +000026#include "debugger_interface.h"
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010027#include "entrypoints/runtime_asm_entrypoints.h"
28#include "gc/accounting/bitmap-inl.h"
Nicolas Geoffraycf48fa02016-07-30 22:49:11 +010029#include "gc/scoped_gc_critical_section.h"
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +000030#include "jit/jit.h"
Nicolas Geoffray26705e22015-10-28 12:50:11 +000031#include "jit/profiling_info.h"
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010032#include "linear_alloc.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080033#include "mem_map.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080034#include "oat_file-inl.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070035#include "scoped_thread_state_change-inl.h"
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010036#include "thread_list.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080037
38namespace art {
39namespace jit {
40
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +010041static constexpr int kProtAll = PROT_READ | PROT_WRITE | PROT_EXEC;
42static constexpr int kProtData = PROT_READ | PROT_WRITE;
43static constexpr int kProtCode = PROT_READ | PROT_EXEC;
44
Nicolas Geoffray933330a2016-03-16 14:20:06 +000045static constexpr size_t kCodeSizeLogThreshold = 50 * KB;
46static constexpr size_t kStackMapSizeLogThreshold = 50 * KB;
47
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +010048#define CHECKED_MPROTECT(memory, size, prot) \
49 do { \
50 int rc = mprotect(memory, size, prot); \
51 if (UNLIKELY(rc != 0)) { \
52 errno = rc; \
53 PLOG(FATAL) << "Failed to mprotect jit code cache"; \
54 } \
55 } while (false) \
56
Nicolas Geoffray0a3be162015-11-18 11:15:22 +000057JitCodeCache* JitCodeCache::Create(size_t initial_capacity,
58 size_t max_capacity,
Nicolas Geoffraya25dce92016-01-12 16:41:10 +000059 bool generate_debug_info,
Nicolas Geoffray0a3be162015-11-18 11:15:22 +000060 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -080061 ScopedTrace trace(__PRETTY_FUNCTION__);
Nicolas Geoffray0a3be162015-11-18 11:15:22 +000062 CHECK_GE(max_capacity, initial_capacity);
Nicolas Geoffraya25dce92016-01-12 16:41:10 +000063
64 // Generating debug information is mostly for using the 'perf' tool, which does
65 // not work with ashmem.
66 bool use_ashmem = !generate_debug_info;
67 // With 'perf', we want a 1-1 mapping between an address and a method.
68 bool garbage_collect_code = !generate_debug_info;
69
Nicolas Geoffray0a3be162015-11-18 11:15:22 +000070 // We need to have 32 bit offsets from method headers in code cache which point to things
71 // in the data cache. If the maps are more than 4G apart, having multiple maps wouldn't work.
72 // Ensure we're below 1 GB to be safe.
73 if (max_capacity > 1 * GB) {
74 std::ostringstream oss;
75 oss << "Maxium code cache capacity is limited to 1 GB, "
76 << PrettySize(max_capacity) << " is too big";
77 *error_msg = oss.str();
78 return nullptr;
79 }
80
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080081 std::string error_str;
82 // Map name specific for android_os_Debug.cpp accounting.
Nicolas Geoffray132d8362016-11-16 09:19:42 +000083 // Map in low 4gb to simplify accessing root tables for x86_64.
84 // We could do PC-relative addressing to avoid this problem, but that
85 // would require reserving code and data area before submitting, which
86 // means more windows for the code memory to be RWX.
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +010087 MemMap* data_map = MemMap::MapAnonymous(
Nicolas Geoffray132d8362016-11-16 09:19:42 +000088 "data-code-cache", nullptr,
89 max_capacity,
90 kProtAll,
91 /* low_4gb */ true,
92 /* reuse */ false,
93 &error_str,
94 use_ashmem);
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +010095 if (data_map == nullptr) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080096 std::ostringstream oss;
Nicolas Geoffray0a3be162015-11-18 11:15:22 +000097 oss << "Failed to create read write execute cache: " << error_str << " size=" << max_capacity;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080098 *error_msg = oss.str();
99 return nullptr;
100 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100101
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000102 // Align both capacities to page size, as that's the unit mspaces use.
103 initial_capacity = RoundDown(initial_capacity, 2 * kPageSize);
104 max_capacity = RoundDown(max_capacity, 2 * kPageSize);
105
Nicolas Geoffray4e915fb2015-10-28 17:39:47 +0000106 // Data cache is 1 / 2 of the map.
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100107 // TODO: Make this variable?
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000108 size_t data_size = max_capacity / 2;
109 size_t code_size = max_capacity - data_size;
110 DCHECK_EQ(code_size + data_size, max_capacity);
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100111 uint8_t* divider = data_map->Begin() + data_size;
112
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000113 MemMap* code_map =
114 data_map->RemapAtEnd(divider, "jit-code-cache", kProtAll, &error_str, use_ashmem);
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100115 if (code_map == nullptr) {
116 std::ostringstream oss;
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000117 oss << "Failed to create read write execute cache: " << error_str << " size=" << max_capacity;
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100118 *error_msg = oss.str();
119 return nullptr;
120 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100121 DCHECK_EQ(code_map->Begin(), divider);
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000122 data_size = initial_capacity / 2;
123 code_size = initial_capacity - data_size;
124 DCHECK_EQ(code_size + data_size, initial_capacity);
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000125 return new JitCodeCache(
Nicolas Geoffrayc3fec4c2016-01-14 16:16:35 +0000126 code_map, data_map, code_size, data_size, max_capacity, garbage_collect_code);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800127}
128
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000129JitCodeCache::JitCodeCache(MemMap* code_map,
130 MemMap* data_map,
131 size_t initial_code_capacity,
132 size_t initial_data_capacity,
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000133 size_t max_capacity,
134 bool garbage_collect_code)
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100135 : lock_("Jit code cache", kJitCodeCacheLock),
Nicolas Geoffraye51ca8b2016-11-22 14:49:31 +0000136 lock_cond_("Jit code cache condition variable", lock_),
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100137 collection_in_progress_(false),
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100138 code_map_(code_map),
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000139 data_map_(data_map),
140 max_capacity_(max_capacity),
141 current_capacity_(initial_code_capacity + initial_data_capacity),
142 code_end_(initial_code_capacity),
143 data_end_(initial_data_capacity),
Nicolas Geoffray35122442016-03-02 12:05:30 +0000144 last_collection_increased_code_cache_(false),
Nicolas Geoffraya25dce92016-01-12 16:41:10 +0000145 last_update_time_ns_(0),
Nicolas Geoffray0a522232016-01-19 09:34:58 +0000146 garbage_collect_code_(garbage_collect_code),
Nicolas Geoffrayb0d22082016-02-24 17:18:25 +0000147 used_memory_for_data_(0),
148 used_memory_for_code_(0),
Nicolas Geoffrayfcdd7292016-02-25 13:27:47 +0000149 number_of_compilations_(0),
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +0000150 number_of_osr_compilations_(0),
151 number_of_deoptimizations_(0),
Nicolas Geoffray933330a2016-03-16 14:20:06 +0000152 number_of_collections_(0),
153 histogram_stack_map_memory_use_("Memory used for stack maps", 16),
154 histogram_code_memory_use_("Memory used for compiled code", 16),
Nicolas Geoffraye51ca8b2016-11-22 14:49:31 +0000155 histogram_profiling_info_memory_use_("Memory used for profiling info", 16),
156 is_weak_access_enabled_(true),
157 inline_cache_cond_("Jit inline cache condition variable", lock_) {
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100158
Nicolas Geoffrayc3fec4c2016-01-14 16:16:35 +0000159 DCHECK_GE(max_capacity, initial_code_capacity + initial_data_capacity);
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000160 code_mspace_ = create_mspace_with_base(code_map_->Begin(), code_end_, false /*locked*/);
161 data_mspace_ = create_mspace_with_base(data_map_->Begin(), data_end_, false /*locked*/);
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100162
163 if (code_mspace_ == nullptr || data_mspace_ == nullptr) {
164 PLOG(FATAL) << "create_mspace_with_base failed";
165 }
166
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000167 SetFootprintLimit(current_capacity_);
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100168
169 CHECKED_MPROTECT(code_map_->Begin(), code_map_->Size(), kProtCode);
170 CHECKED_MPROTECT(data_map_->Begin(), data_map_->Size(), kProtData);
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100171
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000172 VLOG(jit) << "Created jit code cache: initial data size="
173 << PrettySize(initial_data_capacity)
174 << ", initial code size="
175 << PrettySize(initial_code_capacity);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800176}
177
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100178bool JitCodeCache::ContainsPc(const void* ptr) const {
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100179 return code_map_->Begin() <= ptr && ptr < code_map_->End();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800180}
181
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000182bool JitCodeCache::ContainsMethod(ArtMethod* method) {
183 MutexLock mu(Thread::Current(), lock_);
184 for (auto& it : method_code_map_) {
185 if (it.second == method) {
186 return true;
187 }
188 }
189 return false;
190}
191
Mathieu Chartier33fbf372016-03-07 13:48:08 -0800192class ScopedCodeCacheWrite : ScopedTrace {
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100193 public:
Mathieu Chartier33fbf372016-03-07 13:48:08 -0800194 explicit ScopedCodeCacheWrite(MemMap* code_map)
195 : ScopedTrace("ScopedCodeCacheWrite"),
196 code_map_(code_map) {
197 ScopedTrace trace("mprotect all");
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100198 CHECKED_MPROTECT(code_map_->Begin(), code_map_->Size(), kProtAll);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800199 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100200 ~ScopedCodeCacheWrite() {
Mathieu Chartier33fbf372016-03-07 13:48:08 -0800201 ScopedTrace trace("mprotect code");
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100202 CHECKED_MPROTECT(code_map_->Begin(), code_map_->Size(), kProtCode);
203 }
204 private:
205 MemMap* const code_map_;
206
207 DISALLOW_COPY_AND_ASSIGN(ScopedCodeCacheWrite);
208};
209
210uint8_t* JitCodeCache::CommitCode(Thread* self,
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100211 ArtMethod* method,
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000212 uint8_t* stack_map,
213 uint8_t* roots_data,
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100214 size_t frame_size_in_bytes,
215 size_t core_spill_mask,
216 size_t fp_spill_mask,
217 const uint8_t* code,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000218 size_t code_size,
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000219 bool osr,
220 Handle<mirror::ObjectArray<mirror::Object>> roots) {
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100221 uint8_t* result = CommitCodeInternal(self,
222 method,
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000223 stack_map,
224 roots_data,
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100225 frame_size_in_bytes,
226 core_spill_mask,
227 fp_spill_mask,
228 code,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000229 code_size,
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000230 osr,
231 roots);
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100232 if (result == nullptr) {
233 // Retry.
234 GarbageCollectCache(self);
235 result = CommitCodeInternal(self,
236 method,
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000237 stack_map,
238 roots_data,
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100239 frame_size_in_bytes,
240 core_spill_mask,
241 fp_spill_mask,
242 code,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000243 code_size,
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000244 osr,
245 roots);
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100246 }
247 return result;
248}
249
250bool JitCodeCache::WaitForPotentialCollectionToComplete(Thread* self) {
251 bool in_collection = false;
252 while (collection_in_progress_) {
253 in_collection = true;
254 lock_cond_.Wait(self);
255 }
256 return in_collection;
257}
258
259static uintptr_t FromCodeToAllocation(const void* code) {
260 size_t alignment = GetInstructionSetAlignment(kRuntimeISA);
261 return reinterpret_cast<uintptr_t>(code) - RoundUp(sizeof(OatQuickMethodHeader), alignment);
262}
263
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000264static uint32_t ComputeRootTableSize(uint32_t number_of_roots) {
265 return sizeof(uint32_t) + number_of_roots * sizeof(GcRoot<mirror::Object>);
266}
267
268static uint32_t GetNumberOfRoots(const uint8_t* stack_map) {
269 // The length of the table is stored just before the stack map (and therefore at the end of
270 // the table itself), in order to be able to fetch it from a `stack_map` pointer.
271 return reinterpret_cast<const uint32_t*>(stack_map)[-1];
272}
273
Mathieu Chartier7a704be2016-11-22 13:24:40 -0800274static void FillRootTableLength(uint8_t* roots_data, uint32_t length) {
275 // Store the length of the table at the end. This will allow fetching it from a `stack_map`
276 // pointer.
277 reinterpret_cast<uint32_t*>(roots_data)[length] = length;
278}
279
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000280static void FillRootTable(uint8_t* roots_data, Handle<mirror::ObjectArray<mirror::Object>> roots)
281 REQUIRES_SHARED(Locks::mutator_lock_) {
282 GcRoot<mirror::Object>* gc_roots = reinterpret_cast<GcRoot<mirror::Object>*>(roots_data);
Mathieu Chartier7a704be2016-11-22 13:24:40 -0800283 const uint32_t length = roots->GetLength();
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000284 // Put all roots in `roots_data`.
285 for (uint32_t i = 0; i < length; ++i) {
286 ObjPtr<mirror::Object> object = roots->Get(i);
287 if (kIsDebugBuild) {
288 // Ensure the string is strongly interned. b/32995596
289 CHECK(object->IsString());
290 ObjPtr<mirror::String> str = reinterpret_cast<mirror::String*>(object.Ptr());
291 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
292 CHECK(class_linker->GetInternTable()->LookupStrong(Thread::Current(), str) != nullptr);
293 }
294 gc_roots[i] = GcRoot<mirror::Object>(object);
295 }
Mathieu Chartier7a704be2016-11-22 13:24:40 -0800296 FillRootTableLength(roots_data, length);
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000297}
298
299static uint8_t* GetRootTable(const void* code_ptr, uint32_t* number_of_roots = nullptr) {
300 OatQuickMethodHeader* method_header = OatQuickMethodHeader::FromCodePointer(code_ptr);
301 uint8_t* data = method_header->GetOptimizedCodeInfoPtr();
302 uint32_t roots = GetNumberOfRoots(data);
303 if (number_of_roots != nullptr) {
304 *number_of_roots = roots;
305 }
306 return data - ComputeRootTableSize(roots);
307}
308
309void JitCodeCache::SweepRootTables(IsMarkedVisitor* visitor) {
310 MutexLock mu(Thread::Current(), lock_);
311 for (const auto& entry : method_code_map_) {
312 uint32_t number_of_roots = 0;
313 uint8_t* roots_data = GetRootTable(entry.first, &number_of_roots);
314 GcRoot<mirror::Object>* roots = reinterpret_cast<GcRoot<mirror::Object>*>(roots_data);
315 for (uint32_t i = 0; i < number_of_roots; ++i) {
316 // This does not need a read barrier because this is called by GC.
317 mirror::Object* object = roots[i].Read<kWithoutReadBarrier>();
318 DCHECK(object != nullptr);
319 mirror::Object* new_object = visitor->IsMarked(object);
320 // We know the string is marked because it's a strongly-interned string that
321 // is always alive. The IsMarked implementation of the CMS collector returns
322 // null for newly allocated objects, but we know those haven't moved. Therefore,
323 // only update the entry if we get a different non-null string.
324 // TODO: Do not use IsMarked for j.l.Class, and adjust once we move this method
325 // out of the weak access/creation pause. b/32167580
326 if (new_object != nullptr && new_object != object) {
327 DCHECK(new_object->IsString());
328 roots[i] = GcRoot<mirror::Object>(new_object);
329 }
330 }
331 }
Nicolas Geoffraye51ca8b2016-11-22 14:49:31 +0000332 // Walk over inline caches to clear entries containing unloaded classes.
333 for (ProfilingInfo* info : profiling_infos_) {
334 for (size_t i = 0; i < info->number_of_inline_caches_; ++i) {
335 InlineCache* cache = &info->cache_[i];
336 for (size_t j = 0; j < InlineCache::kIndividualCacheSize; ++j) {
337 // This does not need a read barrier because this is called by GC.
338 mirror::Class* cls = cache->classes_[j].Read<kWithoutReadBarrier>();
339 if (cls != nullptr) {
340 // Look at the classloader of the class to know if it has been
341 // unloaded.
342 // This does not need a read barrier because this is called by GC.
343 mirror::Object* class_loader =
344 cls->GetClassLoader<kDefaultVerifyFlags, kWithoutReadBarrier>();
Nicolas Geoffrayb84defb2016-11-30 16:02:16 +0000345 if (class_loader == nullptr || visitor->IsMarked(class_loader) != nullptr) {
Nicolas Geoffraye51ca8b2016-11-22 14:49:31 +0000346 // The class loader is live, update the entry if the class has moved.
347 mirror::Class* new_cls = down_cast<mirror::Class*>(visitor->IsMarked(cls));
348 // Note that new_object can be null for CMS and newly allocated objects.
349 if (new_cls != nullptr && new_cls != cls) {
350 cache->classes_[j] = GcRoot<mirror::Class>(new_cls);
351 }
352 } else {
353 // The class loader is not live, clear the entry.
354 cache->classes_[j] = GcRoot<mirror::Class>(nullptr);
355 }
356 }
357 }
358 }
359 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000360}
361
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100362void JitCodeCache::FreeCode(const void* code_ptr, ArtMethod* method ATTRIBUTE_UNUSED) {
363 uintptr_t allocation = FromCodeToAllocation(code_ptr);
David Srbecky5cc349f2015-12-18 15:04:48 +0000364 // Notify native debugger that we are about to remove the code.
365 // It does nothing if we are not using native debugger.
366 DeleteJITCodeEntryForAddress(reinterpret_cast<uintptr_t>(code_ptr));
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000367 FreeData(GetRootTable(code_ptr));
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +0000368 FreeCode(reinterpret_cast<uint8_t*>(allocation));
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100369}
370
371void JitCodeCache::RemoveMethodsIn(Thread* self, const LinearAlloc& alloc) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800372 ScopedTrace trace(__PRETTY_FUNCTION__);
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100373 MutexLock mu(self, lock_);
374 // We do not check if a code cache GC is in progress, as this method comes
375 // with the classlinker_classes_lock_ held, and suspending ourselves could
376 // lead to a deadlock.
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000377 {
378 ScopedCodeCacheWrite scc(code_map_.get());
379 for (auto it = method_code_map_.begin(); it != method_code_map_.end();) {
380 if (alloc.ContainsUnsafe(it->second)) {
381 FreeCode(it->first, it->second);
382 it = method_code_map_.erase(it);
383 } else {
384 ++it;
385 }
386 }
387 }
Nicolas Geoffraya9b91312016-02-17 09:49:19 +0000388 for (auto it = osr_code_map_.begin(); it != osr_code_map_.end();) {
389 if (alloc.ContainsUnsafe(it->first)) {
390 // Note that the code has already been removed in the loop above.
391 it = osr_code_map_.erase(it);
392 } else {
393 ++it;
394 }
395 }
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000396 for (auto it = profiling_infos_.begin(); it != profiling_infos_.end();) {
397 ProfilingInfo* info = *it;
398 if (alloc.ContainsUnsafe(info->GetMethod())) {
399 info->GetMethod()->SetProfilingInfo(nullptr);
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +0000400 FreeData(reinterpret_cast<uint8_t*>(info));
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000401 it = profiling_infos_.erase(it);
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100402 } else {
403 ++it;
404 }
405 }
406}
407
Nicolas Geoffraye51ca8b2016-11-22 14:49:31 +0000408bool JitCodeCache::IsWeakAccessEnabled(Thread* self) const {
409 return kUseReadBarrier
410 ? self->GetWeakRefAccessEnabled()
411 : is_weak_access_enabled_.LoadSequentiallyConsistent();
412}
413
414void JitCodeCache::WaitUntilInlineCacheAccessible(Thread* self) {
415 if (IsWeakAccessEnabled(self)) {
416 return;
417 }
418 ScopedThreadSuspension sts(self, kWaitingWeakGcRootRead);
Nicolas Geoffrayb6e20ae2016-03-07 14:29:04 +0000419 MutexLock mu(self, lock_);
Nicolas Geoffraye51ca8b2016-11-22 14:49:31 +0000420 while (!IsWeakAccessEnabled(self)) {
421 inline_cache_cond_.Wait(self);
422 }
423}
424
425void JitCodeCache::BroadcastForInlineCacheAccess() {
426 Thread* self = Thread::Current();
427 MutexLock mu(self, lock_);
428 inline_cache_cond_.Broadcast(self);
429}
430
431void JitCodeCache::AllowInlineCacheAccess() {
432 DCHECK(!kUseReadBarrier);
433 is_weak_access_enabled_.StoreSequentiallyConsistent(true);
434 BroadcastForInlineCacheAccess();
435}
436
437void JitCodeCache::DisallowInlineCacheAccess() {
438 DCHECK(!kUseReadBarrier);
439 is_weak_access_enabled_.StoreSequentiallyConsistent(false);
440}
441
442void JitCodeCache::CopyInlineCacheInto(const InlineCache& ic,
443 Handle<mirror::ObjectArray<mirror::Class>> array) {
444 WaitUntilInlineCacheAccessible(Thread::Current());
445 // Note that we don't need to lock `lock_` here, the compiler calling
446 // this method has already ensured the inline cache will not be deleted.
447 for (size_t in_cache = 0, in_array = 0;
448 in_cache < InlineCache::kIndividualCacheSize;
449 ++in_cache) {
450 mirror::Class* object = ic.classes_[in_cache].Read();
451 if (object != nullptr) {
452 array->Set(in_array++, object);
Nicolas Geoffrayb6e20ae2016-03-07 14:29:04 +0000453 }
454 }
455}
456
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100457uint8_t* JitCodeCache::CommitCodeInternal(Thread* self,
458 ArtMethod* method,
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000459 uint8_t* stack_map,
460 uint8_t* roots_data,
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100461 size_t frame_size_in_bytes,
462 size_t core_spill_mask,
463 size_t fp_spill_mask,
464 const uint8_t* code,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000465 size_t code_size,
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000466 bool osr,
467 Handle<mirror::ObjectArray<mirror::Object>> roots) {
468 DCHECK(stack_map != nullptr);
Nicolas Geoffray1e7de6c2015-10-21 12:07:31 +0100469 size_t alignment = GetInstructionSetAlignment(kRuntimeISA);
470 // Ensure the header ends up at expected instruction alignment.
471 size_t header_size = RoundUp(sizeof(OatQuickMethodHeader), alignment);
472 size_t total_size = header_size + code_size;
Mathieu Chartier7a704be2016-11-22 13:24:40 -0800473 const uint32_t num_roots = roots->GetLength();
Nicolas Geoffray1e7de6c2015-10-21 12:07:31 +0100474
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100475 OatQuickMethodHeader* method_header = nullptr;
Nicolas Geoffray1e7de6c2015-10-21 12:07:31 +0100476 uint8_t* code_ptr = nullptr;
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +0000477 uint8_t* memory = nullptr;
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100478 {
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000479 ScopedThreadSuspension sts(self, kSuspended);
480 MutexLock mu(self, lock_);
481 WaitForPotentialCollectionToComplete(self);
482 {
483 ScopedCodeCacheWrite scc(code_map_.get());
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +0000484 memory = AllocateCode(total_size);
485 if (memory == nullptr) {
Mathieu Chartier7a704be2016-11-22 13:24:40 -0800486 // Fill root table length so that ClearData works correctly in case of failure. Otherwise
487 // the length will be 0 and cause incorrect DCHECK failure.
488 FillRootTableLength(roots_data, num_roots);
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000489 return nullptr;
490 }
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +0000491 code_ptr = memory + header_size;
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000492
493 std::copy(code, code + code_size, code_ptr);
494 method_header = OatQuickMethodHeader::FromCodePointer(code_ptr);
495 new (method_header) OatQuickMethodHeader(
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000496 code_ptr - stack_map,
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000497 frame_size_in_bytes,
498 core_spill_mask,
499 fp_spill_mask,
500 code_size);
Artem Udovichenkob18a6692016-11-17 10:51:58 +0300501 // Flush caches before we remove write permission because on some ARMv8 hardware,
502 // flushing caches require write permissions.
503 //
504 // For reference, here are kernel patches discussing about this issue:
505 // https://android.googlesource.com/kernel/msm/%2B/0e7f7bcc3fc87489cda5aa6aff8ce40eed912279
506 // https://patchwork.kernel.org/patch/9047921/
507 FlushInstructionCache(reinterpret_cast<char*>(code_ptr),
508 reinterpret_cast<char*>(code_ptr + code_size));
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100509 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100510
Nicolas Geoffray0a522232016-01-19 09:34:58 +0000511 number_of_compilations_++;
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100512 }
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000513 // We need to update the entry point in the runnable state for the instrumentation.
514 {
515 MutexLock mu(self, lock_);
516 method_code_map_.Put(code_ptr, method);
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000517 // Fill the root table before updating the entry point.
518 FillRootTable(roots_data, roots);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000519 if (osr) {
Nicolas Geoffrayfcdd7292016-02-25 13:27:47 +0000520 number_of_osr_compilations_++;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000521 osr_code_map_.Put(method, code_ptr);
Nicolas Geoffray480d5102016-04-18 12:09:30 +0100522 } else {
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000523 Runtime::Current()->GetInstrumentation()->UpdateMethodsCode(
524 method, method_header->GetEntryPoint());
525 }
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000526 if (collection_in_progress_) {
527 // We need to update the live bitmap if there is a GC to ensure it sees this new
528 // code.
529 GetLiveBitmap()->AtomicTestAndSet(FromCodeToAllocation(code_ptr));
530 }
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000531 last_update_time_ns_.StoreRelease(NanoTime());
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000532 VLOG(jit)
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100533 << "JIT added (osr=" << std::boolalpha << osr << std::noboolalpha << ") "
David Sehr709b0702016-10-13 09:12:37 -0700534 << ArtMethod::PrettyMethod(method) << "@" << method
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000535 << " ccache_size=" << PrettySize(CodeCacheSizeLocked()) << ": "
536 << " dcache_size=" << PrettySize(DataCacheSizeLocked()) << ": "
537 << reinterpret_cast<const void*>(method_header->GetEntryPoint()) << ","
538 << reinterpret_cast<const void*>(method_header->GetEntryPoint() + method_header->code_size_);
Nicolas Geoffray933330a2016-03-16 14:20:06 +0000539 histogram_code_memory_use_.AddValue(code_size);
540 if (code_size > kCodeSizeLogThreshold) {
541 LOG(INFO) << "JIT allocated "
542 << PrettySize(code_size)
543 << " for compiled code of "
David Sehr709b0702016-10-13 09:12:37 -0700544 << ArtMethod::PrettyMethod(method);
Nicolas Geoffray933330a2016-03-16 14:20:06 +0000545 }
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000546 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100547
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100548 return reinterpret_cast<uint8_t*>(method_header);
549}
550
551size_t JitCodeCache::CodeCacheSize() {
552 MutexLock mu(Thread::Current(), lock_);
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000553 return CodeCacheSizeLocked();
554}
555
556size_t JitCodeCache::CodeCacheSizeLocked() {
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +0000557 return used_memory_for_code_;
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100558}
559
560size_t JitCodeCache::DataCacheSize() {
561 MutexLock mu(Thread::Current(), lock_);
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000562 return DataCacheSizeLocked();
563}
564
565size_t JitCodeCache::DataCacheSizeLocked() {
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +0000566 return used_memory_for_data_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800567}
568
Nicolas Geoffrayf46501c2016-11-22 13:45:36 +0000569static const uint8_t* FromStackMapToRoots(const uint8_t* stack_map_data) {
570 return stack_map_data - ComputeRootTableSize(GetNumberOfRoots(stack_map_data));
571}
572
573void JitCodeCache::ClearData(Thread* self,
574 uint8_t* stack_map_data,
575 uint8_t* roots_data) {
576 DCHECK_EQ(FromStackMapToRoots(stack_map_data), roots_data);
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000577 MutexLock mu(self, lock_);
Nicolas Geoffrayf46501c2016-11-22 13:45:36 +0000578 FreeData(reinterpret_cast<uint8_t*>(roots_data));
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000579}
580
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000581void JitCodeCache::ReserveData(Thread* self,
582 size_t stack_map_size,
583 size_t number_of_roots,
584 ArtMethod* method,
585 uint8_t** stack_map_data,
586 uint8_t** roots_data) {
587 size_t table_size = ComputeRootTableSize(number_of_roots);
588 size_t size = RoundUp(stack_map_size + table_size, sizeof(void*));
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100589 uint8_t* result = nullptr;
590
591 {
592 ScopedThreadSuspension sts(self, kSuspended);
593 MutexLock mu(self, lock_);
594 WaitForPotentialCollectionToComplete(self);
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +0000595 result = AllocateData(size);
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100596 }
597
598 if (result == nullptr) {
599 // Retry.
600 GarbageCollectCache(self);
601 ScopedThreadSuspension sts(self, kSuspended);
602 MutexLock mu(self, lock_);
603 WaitForPotentialCollectionToComplete(self);
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +0000604 result = AllocateData(size);
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100605 }
606
Nicolas Geoffray933330a2016-03-16 14:20:06 +0000607 MutexLock mu(self, lock_);
608 histogram_stack_map_memory_use_.AddValue(size);
609 if (size > kStackMapSizeLogThreshold) {
610 LOG(INFO) << "JIT allocated "
611 << PrettySize(size)
612 << " for stack maps of "
David Sehr709b0702016-10-13 09:12:37 -0700613 << ArtMethod::PrettyMethod(method);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800614 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000615 *roots_data = result;
616 *stack_map_data = result + table_size;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800617}
618
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100619class MarkCodeVisitor FINAL : public StackVisitor {
620 public:
621 MarkCodeVisitor(Thread* thread_in, JitCodeCache* code_cache_in)
622 : StackVisitor(thread_in, nullptr, StackVisitor::StackWalkKind::kSkipInlinedFrames),
623 code_cache_(code_cache_in),
624 bitmap_(code_cache_->GetLiveBitmap()) {}
625
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700626 bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100627 const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader();
628 if (method_header == nullptr) {
629 return true;
630 }
631 const void* code = method_header->GetCode();
632 if (code_cache_->ContainsPc(code)) {
633 // Use the atomic set version, as multiple threads are executing this code.
634 bitmap_->AtomicTestAndSet(FromCodeToAllocation(code));
635 }
636 return true;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800637 }
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100638
639 private:
640 JitCodeCache* const code_cache_;
641 CodeCacheBitmap* const bitmap_;
642};
643
644class MarkCodeClosure FINAL : public Closure {
645 public:
646 MarkCodeClosure(JitCodeCache* code_cache, Barrier* barrier)
647 : code_cache_(code_cache), barrier_(barrier) {}
648
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700649 void Run(Thread* thread) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800650 ScopedTrace trace(__PRETTY_FUNCTION__);
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100651 DCHECK(thread == Thread::Current() || thread->IsSuspended());
652 MarkCodeVisitor visitor(thread, code_cache_);
653 visitor.WalkStack();
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +0000654 if (kIsDebugBuild) {
655 // The stack walking code queries the side instrumentation stack if it
656 // sees an instrumentation exit pc, so the JIT code of methods in that stack
657 // must have been seen. We sanity check this below.
658 for (const instrumentation::InstrumentationStackFrame& frame
659 : *thread->GetInstrumentationStack()) {
660 // The 'method_' in InstrumentationStackFrame is the one that has return_pc_ in
661 // its stack frame, it is not the method owning return_pc_. We just pass null to
662 // LookupMethodHeader: the method is only checked against in debug builds.
663 OatQuickMethodHeader* method_header =
664 code_cache_->LookupMethodHeader(frame.return_pc_, nullptr);
665 if (method_header != nullptr) {
666 const void* code = method_header->GetCode();
667 CHECK(code_cache_->GetLiveBitmap()->Test(FromCodeToAllocation(code)));
668 }
669 }
670 }
Mathieu Chartier10d25082015-10-28 18:36:09 -0700671 barrier_->Pass(Thread::Current());
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800672 }
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100673
674 private:
675 JitCodeCache* const code_cache_;
676 Barrier* const barrier_;
677};
678
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000679void JitCodeCache::NotifyCollectionDone(Thread* self) {
680 collection_in_progress_ = false;
681 lock_cond_.Broadcast(self);
682}
683
684void JitCodeCache::SetFootprintLimit(size_t new_footprint) {
685 size_t per_space_footprint = new_footprint / 2;
686 DCHECK(IsAlignedParam(per_space_footprint, kPageSize));
687 DCHECK_EQ(per_space_footprint * 2, new_footprint);
688 mspace_set_footprint_limit(data_mspace_, per_space_footprint);
689 {
690 ScopedCodeCacheWrite scc(code_map_.get());
691 mspace_set_footprint_limit(code_mspace_, per_space_footprint);
692 }
693}
694
695bool JitCodeCache::IncreaseCodeCacheCapacity() {
696 if (current_capacity_ == max_capacity_) {
697 return false;
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100698 }
699
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000700 // Double the capacity if we're below 1MB, or increase it by 1MB if
701 // we're above.
702 if (current_capacity_ < 1 * MB) {
703 current_capacity_ *= 2;
704 } else {
705 current_capacity_ += 1 * MB;
706 }
707 if (current_capacity_ > max_capacity_) {
708 current_capacity_ = max_capacity_;
709 }
710
711 if (!kIsDebugBuild || VLOG_IS_ON(jit)) {
712 LOG(INFO) << "Increasing code cache capacity to " << PrettySize(current_capacity_);
713 }
714
715 SetFootprintLimit(current_capacity_);
716
717 return true;
718}
719
Nicolas Geoffray8d372502016-02-23 13:56:43 +0000720void JitCodeCache::MarkCompiledCodeOnThreadStacks(Thread* self) {
721 Barrier barrier(0);
722 size_t threads_running_checkpoint = 0;
723 MarkCodeClosure closure(this, &barrier);
724 threads_running_checkpoint = Runtime::Current()->GetThreadList()->RunCheckpoint(&closure);
725 // Now that we have run our checkpoint, move to a suspended state and wait
726 // for other threads to run the checkpoint.
727 ScopedThreadSuspension sts(self, kSuspended);
728 if (threads_running_checkpoint != 0) {
729 barrier.Increment(self, threads_running_checkpoint);
730 }
731}
732
Nicolas Geoffray35122442016-03-02 12:05:30 +0000733bool JitCodeCache::ShouldDoFullCollection() {
734 if (current_capacity_ == max_capacity_) {
735 // Always do a full collection when the code cache is full.
736 return true;
737 } else if (current_capacity_ < kReservedCapacity) {
738 // Always do partial collection when the code cache size is below the reserved
739 // capacity.
740 return false;
741 } else if (last_collection_increased_code_cache_) {
742 // This time do a full collection.
743 return true;
744 } else {
745 // This time do a partial collection.
746 return false;
Nicolas Geoffray8d372502016-02-23 13:56:43 +0000747 }
748}
749
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000750void JitCodeCache::GarbageCollectCache(Thread* self) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800751 ScopedTrace trace(__FUNCTION__);
Nicolas Geoffray8d372502016-02-23 13:56:43 +0000752 if (!garbage_collect_code_) {
753 MutexLock mu(self, lock_);
754 IncreaseCodeCacheCapacity();
755 return;
756 }
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100757
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000758 // Wait for an existing collection, or let everyone know we are starting one.
759 {
760 ScopedThreadSuspension sts(self, kSuspended);
761 MutexLock mu(self, lock_);
762 if (WaitForPotentialCollectionToComplete(self)) {
763 return;
764 } else {
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +0000765 number_of_collections_++;
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000766 live_bitmap_.reset(CodeCacheBitmap::Create(
767 "code-cache-bitmap",
768 reinterpret_cast<uintptr_t>(code_map_->Begin()),
769 reinterpret_cast<uintptr_t>(code_map_->Begin() + current_capacity_ / 2)));
Nicolas Geoffray8d372502016-02-23 13:56:43 +0000770 collection_in_progress_ = true;
771 }
772 }
773
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +0000774 TimingLogger logger("JIT code cache timing logger", true, VLOG_IS_ON(jit));
Nicolas Geoffray8d372502016-02-23 13:56:43 +0000775 {
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +0000776 TimingLogger::ScopedTiming st("Code cache collection", &logger);
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000777
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +0000778 bool do_full_collection = false;
779 {
780 MutexLock mu(self, lock_);
781 do_full_collection = ShouldDoFullCollection();
Nicolas Geoffraya96917a2016-03-01 22:18:02 +0000782 }
783
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +0000784 if (!kIsDebugBuild || VLOG_IS_ON(jit)) {
785 LOG(INFO) << "Do "
786 << (do_full_collection ? "full" : "partial")
787 << " code cache collection, code="
788 << PrettySize(CodeCacheSize())
789 << ", data=" << PrettySize(DataCacheSize());
790 }
Nicolas Geoffray35122442016-03-02 12:05:30 +0000791
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +0000792 DoCollection(self, /* collect_profiling_info */ do_full_collection);
793
794 if (!kIsDebugBuild || VLOG_IS_ON(jit)) {
795 LOG(INFO) << "After code cache collection, code="
796 << PrettySize(CodeCacheSize())
797 << ", data=" << PrettySize(DataCacheSize());
798 }
799
800 {
801 MutexLock mu(self, lock_);
802
803 // Increase the code cache only when we do partial collections.
804 // TODO: base this strategy on how full the code cache is?
805 if (do_full_collection) {
806 last_collection_increased_code_cache_ = false;
807 } else {
808 last_collection_increased_code_cache_ = true;
809 IncreaseCodeCacheCapacity();
Nicolas Geoffray35122442016-03-02 12:05:30 +0000810 }
811
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +0000812 bool next_collection_will_be_full = ShouldDoFullCollection();
813
814 // Start polling the liveness of compiled code to prepare for the next full collection.
Nicolas Geoffray480d5102016-04-18 12:09:30 +0100815 if (next_collection_will_be_full) {
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +0000816 // Save the entry point of methods we have compiled, and update the entry
817 // point of those methods to the interpreter. If the method is invoked, the
818 // interpreter will update its entry point to the compiled code and call it.
819 for (ProfilingInfo* info : profiling_infos_) {
820 const void* entry_point = info->GetMethod()->GetEntryPointFromQuickCompiledCode();
821 if (ContainsPc(entry_point)) {
822 info->SetSavedEntryPoint(entry_point);
Nicolas Geoffray480d5102016-04-18 12:09:30 +0100823 Runtime::Current()->GetInstrumentation()->UpdateMethodsCode(
824 info->GetMethod(), GetQuickToInterpreterBridge());
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +0000825 }
826 }
827
828 DCHECK(CheckLiveCompiledCodeHasProfilingInfo());
829 }
830 live_bitmap_.reset(nullptr);
831 NotifyCollectionDone(self);
Nicolas Geoffray35122442016-03-02 12:05:30 +0000832 }
Nicolas Geoffray35122442016-03-02 12:05:30 +0000833 }
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +0000834 Runtime::Current()->GetJit()->AddTimingLogger(logger);
Nicolas Geoffray35122442016-03-02 12:05:30 +0000835}
836
Nicolas Geoffray9abb2972016-03-04 14:32:59 +0000837void JitCodeCache::RemoveUnmarkedCode(Thread* self) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800838 ScopedTrace trace(__FUNCTION__);
Nicolas Geoffray35122442016-03-02 12:05:30 +0000839 MutexLock mu(self, lock_);
840 ScopedCodeCacheWrite scc(code_map_.get());
Nicolas Geoffray9abb2972016-03-04 14:32:59 +0000841 // Iterate over all compiled code and remove entries that are not marked.
Nicolas Geoffray35122442016-03-02 12:05:30 +0000842 for (auto it = method_code_map_.begin(); it != method_code_map_.end();) {
843 const void* code_ptr = it->first;
844 ArtMethod* method = it->second;
845 uintptr_t allocation = FromCodeToAllocation(code_ptr);
Nicolas Geoffray9abb2972016-03-04 14:32:59 +0000846 if (GetLiveBitmap()->Test(allocation)) {
Nicolas Geoffray35122442016-03-02 12:05:30 +0000847 ++it;
848 } else {
Nicolas Geoffray35122442016-03-02 12:05:30 +0000849 FreeCode(code_ptr, method);
850 it = method_code_map_.erase(it);
851 }
852 }
853}
854
855void JitCodeCache::DoCollection(Thread* self, bool collect_profiling_info) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800856 ScopedTrace trace(__FUNCTION__);
Nicolas Geoffray35122442016-03-02 12:05:30 +0000857 {
858 MutexLock mu(self, lock_);
859 if (collect_profiling_info) {
860 // Clear the profiling info of methods that do not have compiled code as entrypoint.
861 // Also remove the saved entry point from the ProfilingInfo objects.
862 for (ProfilingInfo* info : profiling_infos_) {
863 const void* ptr = info->GetMethod()->GetEntryPointFromQuickCompiledCode();
Nicolas Geoffrayb6e20ae2016-03-07 14:29:04 +0000864 if (!ContainsPc(ptr) && !info->IsInUseByCompiler()) {
Nicolas Geoffray35122442016-03-02 12:05:30 +0000865 info->GetMethod()->SetProfilingInfo(nullptr);
866 }
Nicolas Geoffrayb9a639d2016-03-22 11:25:20 +0000867
868 if (info->GetSavedEntryPoint() != nullptr) {
869 info->SetSavedEntryPoint(nullptr);
870 // We are going to move this method back to interpreter. Clear the counter now to
871 // give it a chance to be hot again.
872 info->GetMethod()->ClearCounter();
873 }
Nicolas Geoffray35122442016-03-02 12:05:30 +0000874 }
875 } else if (kIsDebugBuild) {
876 // Sanity check that the profiling infos do not have a dangling entry point.
877 for (ProfilingInfo* info : profiling_infos_) {
878 DCHECK(info->GetSavedEntryPoint() == nullptr);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100879 }
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000880 }
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000881
Nicolas Geoffray9abb2972016-03-04 14:32:59 +0000882 // Mark compiled code that are entrypoints of ArtMethods. Compiled code that is not
883 // an entry point is either:
884 // - an osr compiled code, that will be removed if not in a thread call stack.
885 // - discarded compiled code, that will be removed if not in a thread call stack.
886 for (const auto& it : method_code_map_) {
887 ArtMethod* method = it.second;
888 const void* code_ptr = it.first;
889 const OatQuickMethodHeader* method_header = OatQuickMethodHeader::FromCodePointer(code_ptr);
890 if (method_header->GetEntryPoint() == method->GetEntryPointFromQuickCompiledCode()) {
891 GetLiveBitmap()->AtomicTestAndSet(FromCodeToAllocation(code_ptr));
892 }
893 }
894
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000895 // Empty osr method map, as osr compiled code will be deleted (except the ones
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000896 // on thread stacks).
897 osr_code_map_.clear();
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100898 }
899
900 // Run a checkpoint on all threads to mark the JIT compiled code they are running.
Nicolas Geoffray8d372502016-02-23 13:56:43 +0000901 MarkCompiledCodeOnThreadStacks(self);
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100902
Nicolas Geoffray9abb2972016-03-04 14:32:59 +0000903 // At this point, mutator threads are still running, and entrypoints of methods can
904 // change. We do know they cannot change to a code cache entry that is not marked,
905 // therefore we can safely remove those entries.
906 RemoveUnmarkedCode(self);
Nicolas Geoffraya96917a2016-03-01 22:18:02 +0000907
Nicolas Geoffray35122442016-03-02 12:05:30 +0000908 if (collect_profiling_info) {
Nicolas Geoffraycf48fa02016-07-30 22:49:11 +0100909 ScopedThreadSuspension sts(self, kSuspended);
Nicolas Geoffray35122442016-03-02 12:05:30 +0000910 MutexLock mu(self, lock_);
911 // Free all profiling infos of methods not compiled nor being compiled.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100912 auto profiling_kept_end = std::remove_if(profiling_infos_.begin(), profiling_infos_.end(),
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +0000913 [this] (ProfilingInfo* info) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffray35122442016-03-02 12:05:30 +0000914 const void* ptr = info->GetMethod()->GetEntryPointFromQuickCompiledCode();
Nicolas Geoffray511e41b2016-03-02 17:09:35 +0000915 // We have previously cleared the ProfilingInfo pointer in the ArtMethod in the hope
916 // that the compiled code would not get revived. As mutator threads run concurrently,
917 // they may have revived the compiled code, and now we are in the situation where
918 // a method has compiled code but no ProfilingInfo.
919 // We make sure compiled methods have a ProfilingInfo object. It is needed for
920 // code cache collection.
Andreas Gampe542451c2016-07-26 09:02:02 -0700921 if (ContainsPc(ptr) &&
922 info->GetMethod()->GetProfilingInfo(kRuntimePointerSize) == nullptr) {
Nicolas Geoffray35122442016-03-02 12:05:30 +0000923 info->GetMethod()->SetProfilingInfo(info);
Andreas Gampe542451c2016-07-26 09:02:02 -0700924 } else if (info->GetMethod()->GetProfilingInfo(kRuntimePointerSize) != info) {
Nicolas Geoffray35122442016-03-02 12:05:30 +0000925 // No need for this ProfilingInfo object anymore.
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +0000926 FreeData(reinterpret_cast<uint8_t*>(info));
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100927 return true;
928 }
929 return false;
930 });
931 profiling_infos_.erase(profiling_kept_end, profiling_infos_.end());
Nicolas Geoffray35122442016-03-02 12:05:30 +0000932 DCHECK(CheckLiveCompiledCodeHasProfilingInfo());
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100933 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800934}
935
Nicolas Geoffray35122442016-03-02 12:05:30 +0000936bool JitCodeCache::CheckLiveCompiledCodeHasProfilingInfo() {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800937 ScopedTrace trace(__FUNCTION__);
Nicolas Geoffray35122442016-03-02 12:05:30 +0000938 // Check that methods we have compiled do have a ProfilingInfo object. We would
939 // have memory leaks of compiled code otherwise.
940 for (const auto& it : method_code_map_) {
941 ArtMethod* method = it.second;
Andreas Gampe542451c2016-07-26 09:02:02 -0700942 if (method->GetProfilingInfo(kRuntimePointerSize) == nullptr) {
Nicolas Geoffray35122442016-03-02 12:05:30 +0000943 const void* code_ptr = it.first;
944 const OatQuickMethodHeader* method_header = OatQuickMethodHeader::FromCodePointer(code_ptr);
945 if (method_header->GetEntryPoint() == method->GetEntryPointFromQuickCompiledCode()) {
946 // If the code is not dead, then we have a problem. Note that this can even
947 // happen just after a collection, as mutator threads are running in parallel
948 // and could deoptimize an existing compiled code.
949 return false;
950 }
951 }
952 }
953 return true;
954}
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100955
956OatQuickMethodHeader* JitCodeCache::LookupMethodHeader(uintptr_t pc, ArtMethod* method) {
957 static_assert(kRuntimeISA != kThumb2, "kThumb2 cannot be a runtime ISA");
958 if (kRuntimeISA == kArm) {
959 // On Thumb-2, the pc is offset by one.
960 --pc;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800961 }
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100962 if (!ContainsPc(reinterpret_cast<const void*>(pc))) {
963 return nullptr;
964 }
965
966 MutexLock mu(Thread::Current(), lock_);
967 if (method_code_map_.empty()) {
968 return nullptr;
969 }
970 auto it = method_code_map_.lower_bound(reinterpret_cast<const void*>(pc));
971 --it;
972
973 const void* code_ptr = it->first;
974 OatQuickMethodHeader* method_header = OatQuickMethodHeader::FromCodePointer(code_ptr);
975 if (!method_header->Contains(pc)) {
976 return nullptr;
977 }
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +0000978 if (kIsDebugBuild && method != nullptr) {
979 DCHECK_EQ(it->second, method)
David Sehr709b0702016-10-13 09:12:37 -0700980 << ArtMethod::PrettyMethod(method) << " " << ArtMethod::PrettyMethod(it->second) << " "
981 << std::hex << pc;
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +0000982 }
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100983 return method_header;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800984}
985
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000986OatQuickMethodHeader* JitCodeCache::LookupOsrMethodHeader(ArtMethod* method) {
987 MutexLock mu(Thread::Current(), lock_);
988 auto it = osr_code_map_.find(method);
989 if (it == osr_code_map_.end()) {
990 return nullptr;
991 }
992 return OatQuickMethodHeader::FromCodePointer(it->second);
993}
994
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000995ProfilingInfo* JitCodeCache::AddProfilingInfo(Thread* self,
996 ArtMethod* method,
997 const std::vector<uint32_t>& entries,
Nicolas Geoffray1e7da9b2016-03-01 14:11:40 +0000998 bool retry_allocation)
999 // No thread safety analysis as we are using TryLock/Unlock explicitly.
1000 NO_THREAD_SAFETY_ANALYSIS {
1001 ProfilingInfo* info = nullptr;
1002 if (!retry_allocation) {
1003 // If we are allocating for the interpreter, just try to lock, to avoid
1004 // lock contention with the JIT.
1005 if (lock_.ExclusiveTryLock(self)) {
1006 info = AddProfilingInfoInternal(self, method, entries);
1007 lock_.ExclusiveUnlock(self);
1008 }
1009 } else {
1010 {
1011 MutexLock mu(self, lock_);
1012 info = AddProfilingInfoInternal(self, method, entries);
1013 }
Nicolas Geoffray26705e22015-10-28 12:50:11 +00001014
Nicolas Geoffray1e7da9b2016-03-01 14:11:40 +00001015 if (info == nullptr) {
1016 GarbageCollectCache(self);
1017 MutexLock mu(self, lock_);
1018 info = AddProfilingInfoInternal(self, method, entries);
1019 }
Nicolas Geoffray26705e22015-10-28 12:50:11 +00001020 }
1021 return info;
1022}
1023
Nicolas Geoffray1e7da9b2016-03-01 14:11:40 +00001024ProfilingInfo* JitCodeCache::AddProfilingInfoInternal(Thread* self ATTRIBUTE_UNUSED,
Nicolas Geoffray26705e22015-10-28 12:50:11 +00001025 ArtMethod* method,
1026 const std::vector<uint32_t>& entries) {
1027 size_t profile_info_size = RoundUp(
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01001028 sizeof(ProfilingInfo) + sizeof(InlineCache) * entries.size(),
Nicolas Geoffray26705e22015-10-28 12:50:11 +00001029 sizeof(void*));
Nicolas Geoffray26705e22015-10-28 12:50:11 +00001030
1031 // Check whether some other thread has concurrently created it.
Andreas Gampe542451c2016-07-26 09:02:02 -07001032 ProfilingInfo* info = method->GetProfilingInfo(kRuntimePointerSize);
Nicolas Geoffray26705e22015-10-28 12:50:11 +00001033 if (info != nullptr) {
1034 return info;
1035 }
1036
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +00001037 uint8_t* data = AllocateData(profile_info_size);
Nicolas Geoffray26705e22015-10-28 12:50:11 +00001038 if (data == nullptr) {
1039 return nullptr;
1040 }
1041 info = new (data) ProfilingInfo(method, entries);
Nicolas Geoffray07f35642016-01-04 16:06:51 +00001042
1043 // Make sure other threads see the data in the profiling info object before the
1044 // store in the ArtMethod's ProfilingInfo pointer.
1045 QuasiAtomic::ThreadFenceRelease();
1046
Nicolas Geoffray26705e22015-10-28 12:50:11 +00001047 method->SetProfilingInfo(info);
1048 profiling_infos_.push_back(info);
Nicolas Geoffray933330a2016-03-16 14:20:06 +00001049 histogram_profiling_info_memory_use_.AddValue(profile_info_size);
Nicolas Geoffray26705e22015-10-28 12:50:11 +00001050 return info;
1051}
1052
Nicolas Geoffray0a3be162015-11-18 11:15:22 +00001053// NO_THREAD_SAFETY_ANALYSIS as this is called from mspace code, at which point the lock
1054// is already held.
1055void* JitCodeCache::MoreCore(const void* mspace, intptr_t increment) NO_THREAD_SAFETY_ANALYSIS {
1056 if (code_mspace_ == mspace) {
1057 size_t result = code_end_;
1058 code_end_ += increment;
1059 return reinterpret_cast<void*>(result + code_map_->Begin());
1060 } else {
1061 DCHECK_EQ(data_mspace_, mspace);
1062 size_t result = data_end_;
1063 data_end_ += increment;
1064 return reinterpret_cast<void*>(result + data_map_->Begin());
1065 }
1066}
1067
Calin Juravle99629622016-04-19 16:33:46 +01001068void JitCodeCache::GetProfiledMethods(const std::set<std::string>& dex_base_locations,
1069 std::vector<MethodReference>& methods) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001070 ScopedTrace trace(__FUNCTION__);
Calin Juravle31f2c152015-10-23 17:56:15 +01001071 MutexLock mu(Thread::Current(), lock_);
Calin Juravle99629622016-04-19 16:33:46 +01001072 for (const ProfilingInfo* info : profiling_infos_) {
1073 ArtMethod* method = info->GetMethod();
1074 const DexFile* dex_file = method->GetDexFile();
1075 if (ContainsElement(dex_base_locations, dex_file->GetBaseLocation())) {
1076 methods.emplace_back(dex_file, method->GetDexMethodIndex());
Calin Juravle31f2c152015-10-23 17:56:15 +01001077 }
1078 }
1079}
1080
Calin Juravle4d77b6a2015-12-01 18:38:09 +00001081uint64_t JitCodeCache::GetLastUpdateTimeNs() const {
1082 return last_update_time_ns_.LoadAcquire();
Calin Juravle31f2c152015-10-23 17:56:15 +01001083}
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01001084
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +01001085bool JitCodeCache::IsOsrCompiled(ArtMethod* method) {
1086 MutexLock mu(Thread::Current(), lock_);
1087 return osr_code_map_.find(method) != osr_code_map_.end();
1088}
1089
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00001090bool JitCodeCache::NotifyCompilationOf(ArtMethod* method, Thread* self, bool osr) {
1091 if (!osr && ContainsPc(method->GetEntryPointFromQuickCompiledCode())) {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01001092 return false;
1093 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001094
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001095 MutexLock mu(self, lock_);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00001096 if (osr && (osr_code_map_.find(method) != osr_code_map_.end())) {
1097 return false;
1098 }
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +00001099
Andreas Gampe542451c2016-07-26 09:02:02 -07001100 ProfilingInfo* info = method->GetProfilingInfo(kRuntimePointerSize);
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +00001101 if (info == nullptr) {
David Sehr709b0702016-10-13 09:12:37 -07001102 VLOG(jit) << method->PrettyMethod() << " needs a ProfilingInfo to be compiled";
Nicolas Geoffrayb9a639d2016-03-22 11:25:20 +00001103 // Because the counter is not atomic, there are some rare cases where we may not
1104 // hit the threshold for creating the ProfilingInfo. Reset the counter now to
1105 // "correct" this.
1106 method->ClearCounter();
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01001107 return false;
1108 }
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +00001109
buzbee454b3b62016-04-07 14:42:47 -07001110 if (info->IsMethodBeingCompiled(osr)) {
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +00001111 return false;
1112 }
1113
buzbee454b3b62016-04-07 14:42:47 -07001114 info->SetIsMethodBeingCompiled(true, osr);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01001115 return true;
1116}
1117
Nicolas Geoffray07e3ca92016-03-11 09:57:57 +00001118ProfilingInfo* JitCodeCache::NotifyCompilerUse(ArtMethod* method, Thread* self) {
Nicolas Geoffrayb6e20ae2016-03-07 14:29:04 +00001119 MutexLock mu(self, lock_);
Andreas Gampe542451c2016-07-26 09:02:02 -07001120 ProfilingInfo* info = method->GetProfilingInfo(kRuntimePointerSize);
Nicolas Geoffrayb6e20ae2016-03-07 14:29:04 +00001121 if (info != nullptr) {
1122 info->IncrementInlineUse();
1123 }
Nicolas Geoffray07e3ca92016-03-11 09:57:57 +00001124 return info;
Nicolas Geoffrayb6e20ae2016-03-07 14:29:04 +00001125}
1126
Nicolas Geoffray07e3ca92016-03-11 09:57:57 +00001127void JitCodeCache::DoneCompilerUse(ArtMethod* method, Thread* self) {
Nicolas Geoffrayb6e20ae2016-03-07 14:29:04 +00001128 MutexLock mu(self, lock_);
Andreas Gampe542451c2016-07-26 09:02:02 -07001129 ProfilingInfo* info = method->GetProfilingInfo(kRuntimePointerSize);
Nicolas Geoffray07e3ca92016-03-11 09:57:57 +00001130 DCHECK(info != nullptr);
1131 info->DecrementInlineUse();
Nicolas Geoffrayb6e20ae2016-03-07 14:29:04 +00001132}
1133
buzbee454b3b62016-04-07 14:42:47 -07001134void JitCodeCache::DoneCompiling(ArtMethod* method, Thread* self ATTRIBUTE_UNUSED, bool osr) {
Andreas Gampe542451c2016-07-26 09:02:02 -07001135 ProfilingInfo* info = method->GetProfilingInfo(kRuntimePointerSize);
buzbee454b3b62016-04-07 14:42:47 -07001136 DCHECK(info->IsMethodBeingCompiled(osr));
1137 info->SetIsMethodBeingCompiled(false, osr);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01001138}
1139
Nicolas Geoffraya25dce92016-01-12 16:41:10 +00001140size_t JitCodeCache::GetMemorySizeOfCodePointer(const void* ptr) {
1141 MutexLock mu(Thread::Current(), lock_);
1142 return mspace_usable_size(reinterpret_cast<const void*>(FromCodeToAllocation(ptr)));
1143}
1144
Nicolas Geoffrayb88d59e2016-02-17 11:31:49 +00001145void JitCodeCache::InvalidateCompiledCodeFor(ArtMethod* method,
1146 const OatQuickMethodHeader* header) {
Andreas Gampe542451c2016-07-26 09:02:02 -07001147 ProfilingInfo* profiling_info = method->GetProfilingInfo(kRuntimePointerSize);
Nicolas Geoffray35122442016-03-02 12:05:30 +00001148 if ((profiling_info != nullptr) &&
1149 (profiling_info->GetSavedEntryPoint() == header->GetEntryPoint())) {
1150 // Prevent future uses of the compiled code.
1151 profiling_info->SetSavedEntryPoint(nullptr);
1152 }
1153
Nicolas Geoffrayb88d59e2016-02-17 11:31:49 +00001154 if (method->GetEntryPointFromQuickCompiledCode() == header->GetEntryPoint()) {
1155 // The entrypoint is the one to invalidate, so we just update
1156 // it to the interpreter entry point and clear the counter to get the method
1157 // Jitted again.
1158 Runtime::Current()->GetInstrumentation()->UpdateMethodsCode(
1159 method, GetQuickToInterpreterBridge());
1160 method->ClearCounter();
1161 } else {
1162 MutexLock mu(Thread::Current(), lock_);
1163 auto it = osr_code_map_.find(method);
1164 if (it != osr_code_map_.end() && OatQuickMethodHeader::FromCodePointer(it->second) == header) {
1165 // Remove the OSR method, to avoid using it again.
1166 osr_code_map_.erase(it);
1167 }
1168 }
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +00001169 MutexLock mu(Thread::Current(), lock_);
1170 number_of_deoptimizations_++;
Nicolas Geoffrayb88d59e2016-02-17 11:31:49 +00001171}
1172
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +00001173uint8_t* JitCodeCache::AllocateCode(size_t code_size) {
1174 size_t alignment = GetInstructionSetAlignment(kRuntimeISA);
1175 uint8_t* result = reinterpret_cast<uint8_t*>(
1176 mspace_memalign(code_mspace_, alignment, code_size));
1177 size_t header_size = RoundUp(sizeof(OatQuickMethodHeader), alignment);
1178 // Ensure the header ends up at expected instruction alignment.
1179 DCHECK_ALIGNED_PARAM(reinterpret_cast<uintptr_t>(result + header_size), alignment);
1180 used_memory_for_code_ += mspace_usable_size(result);
1181 return result;
1182}
1183
1184void JitCodeCache::FreeCode(uint8_t* code) {
1185 used_memory_for_code_ -= mspace_usable_size(code);
1186 mspace_free(code_mspace_, code);
1187}
1188
1189uint8_t* JitCodeCache::AllocateData(size_t data_size) {
1190 void* result = mspace_malloc(data_mspace_, data_size);
1191 used_memory_for_data_ += mspace_usable_size(result);
1192 return reinterpret_cast<uint8_t*>(result);
1193}
1194
1195void JitCodeCache::FreeData(uint8_t* data) {
1196 used_memory_for_data_ -= mspace_usable_size(data);
1197 mspace_free(data_mspace_, data);
1198}
1199
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +00001200void JitCodeCache::Dump(std::ostream& os) {
1201 MutexLock mu(Thread::Current(), lock_);
1202 os << "Current JIT code cache size: " << PrettySize(used_memory_for_code_) << "\n"
1203 << "Current JIT data cache size: " << PrettySize(used_memory_for_data_) << "\n"
1204 << "Current JIT capacity: " << PrettySize(current_capacity_) << "\n"
1205 << "Current number of JIT code cache entries: " << method_code_map_.size() << "\n"
1206 << "Total number of JIT compilations: " << number_of_compilations_ << "\n"
1207 << "Total number of JIT compilations for on stack replacement: "
1208 << number_of_osr_compilations_ << "\n"
1209 << "Total number of deoptimizations: " << number_of_deoptimizations_ << "\n"
1210 << "Total number of JIT code cache collections: " << number_of_collections_ << std::endl;
Nicolas Geoffray933330a2016-03-16 14:20:06 +00001211 histogram_stack_map_memory_use_.PrintMemoryUse(os);
1212 histogram_code_memory_use_.PrintMemoryUse(os);
1213 histogram_profiling_info_memory_use_.PrintMemoryUse(os);
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +00001214}
1215
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001216} // namespace jit
1217} // namespace art