blob: 1021db0ad8f60e2c5fcf1f6bac7054783e86f89a [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
274static void FillRootTable(uint8_t* roots_data, Handle<mirror::ObjectArray<mirror::Object>> roots)
275 REQUIRES_SHARED(Locks::mutator_lock_) {
276 GcRoot<mirror::Object>* gc_roots = reinterpret_cast<GcRoot<mirror::Object>*>(roots_data);
277 uint32_t length = roots->GetLength();
278 // Put all roots in `roots_data`.
279 for (uint32_t i = 0; i < length; ++i) {
280 ObjPtr<mirror::Object> object = roots->Get(i);
281 if (kIsDebugBuild) {
282 // Ensure the string is strongly interned. b/32995596
283 CHECK(object->IsString());
284 ObjPtr<mirror::String> str = reinterpret_cast<mirror::String*>(object.Ptr());
285 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
286 CHECK(class_linker->GetInternTable()->LookupStrong(Thread::Current(), str) != nullptr);
287 }
288 gc_roots[i] = GcRoot<mirror::Object>(object);
289 }
290 // Store the length of the table at the end. This will allow fetching it from a `stack_map`
291 // pointer.
292 reinterpret_cast<uint32_t*>(gc_roots + length)[0] = length;
293}
294
295static uint8_t* GetRootTable(const void* code_ptr, uint32_t* number_of_roots = nullptr) {
296 OatQuickMethodHeader* method_header = OatQuickMethodHeader::FromCodePointer(code_ptr);
297 uint8_t* data = method_header->GetOptimizedCodeInfoPtr();
298 uint32_t roots = GetNumberOfRoots(data);
299 if (number_of_roots != nullptr) {
300 *number_of_roots = roots;
301 }
302 return data - ComputeRootTableSize(roots);
303}
304
305void JitCodeCache::SweepRootTables(IsMarkedVisitor* visitor) {
306 MutexLock mu(Thread::Current(), lock_);
307 for (const auto& entry : method_code_map_) {
308 uint32_t number_of_roots = 0;
309 uint8_t* roots_data = GetRootTable(entry.first, &number_of_roots);
310 GcRoot<mirror::Object>* roots = reinterpret_cast<GcRoot<mirror::Object>*>(roots_data);
311 for (uint32_t i = 0; i < number_of_roots; ++i) {
312 // This does not need a read barrier because this is called by GC.
313 mirror::Object* object = roots[i].Read<kWithoutReadBarrier>();
314 DCHECK(object != nullptr);
315 mirror::Object* new_object = visitor->IsMarked(object);
316 // We know the string is marked because it's a strongly-interned string that
317 // is always alive. The IsMarked implementation of the CMS collector returns
318 // null for newly allocated objects, but we know those haven't moved. Therefore,
319 // only update the entry if we get a different non-null string.
320 // TODO: Do not use IsMarked for j.l.Class, and adjust once we move this method
321 // out of the weak access/creation pause. b/32167580
322 if (new_object != nullptr && new_object != object) {
323 DCHECK(new_object->IsString());
324 roots[i] = GcRoot<mirror::Object>(new_object);
325 }
326 }
327 }
Nicolas Geoffraye51ca8b2016-11-22 14:49:31 +0000328 // Walk over inline caches to clear entries containing unloaded classes.
329 for (ProfilingInfo* info : profiling_infos_) {
330 for (size_t i = 0; i < info->number_of_inline_caches_; ++i) {
331 InlineCache* cache = &info->cache_[i];
332 for (size_t j = 0; j < InlineCache::kIndividualCacheSize; ++j) {
333 // This does not need a read barrier because this is called by GC.
334 mirror::Class* cls = cache->classes_[j].Read<kWithoutReadBarrier>();
335 if (cls != nullptr) {
336 // Look at the classloader of the class to know if it has been
337 // unloaded.
338 // This does not need a read barrier because this is called by GC.
339 mirror::Object* class_loader =
340 cls->GetClassLoader<kDefaultVerifyFlags, kWithoutReadBarrier>();
341 if (visitor->IsMarked(class_loader) != nullptr) {
342 // The class loader is live, update the entry if the class has moved.
343 mirror::Class* new_cls = down_cast<mirror::Class*>(visitor->IsMarked(cls));
344 // Note that new_object can be null for CMS and newly allocated objects.
345 if (new_cls != nullptr && new_cls != cls) {
346 cache->classes_[j] = GcRoot<mirror::Class>(new_cls);
347 }
348 } else {
349 // The class loader is not live, clear the entry.
350 cache->classes_[j] = GcRoot<mirror::Class>(nullptr);
351 }
352 }
353 }
354 }
355 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000356}
357
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100358void JitCodeCache::FreeCode(const void* code_ptr, ArtMethod* method ATTRIBUTE_UNUSED) {
359 uintptr_t allocation = FromCodeToAllocation(code_ptr);
David Srbecky5cc349f2015-12-18 15:04:48 +0000360 // Notify native debugger that we are about to remove the code.
361 // It does nothing if we are not using native debugger.
362 DeleteJITCodeEntryForAddress(reinterpret_cast<uintptr_t>(code_ptr));
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000363 FreeData(GetRootTable(code_ptr));
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +0000364 FreeCode(reinterpret_cast<uint8_t*>(allocation));
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100365}
366
367void JitCodeCache::RemoveMethodsIn(Thread* self, const LinearAlloc& alloc) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800368 ScopedTrace trace(__PRETTY_FUNCTION__);
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100369 MutexLock mu(self, lock_);
370 // We do not check if a code cache GC is in progress, as this method comes
371 // with the classlinker_classes_lock_ held, and suspending ourselves could
372 // lead to a deadlock.
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000373 {
374 ScopedCodeCacheWrite scc(code_map_.get());
375 for (auto it = method_code_map_.begin(); it != method_code_map_.end();) {
376 if (alloc.ContainsUnsafe(it->second)) {
377 FreeCode(it->first, it->second);
378 it = method_code_map_.erase(it);
379 } else {
380 ++it;
381 }
382 }
383 }
Nicolas Geoffraya9b91312016-02-17 09:49:19 +0000384 for (auto it = osr_code_map_.begin(); it != osr_code_map_.end();) {
385 if (alloc.ContainsUnsafe(it->first)) {
386 // Note that the code has already been removed in the loop above.
387 it = osr_code_map_.erase(it);
388 } else {
389 ++it;
390 }
391 }
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000392 for (auto it = profiling_infos_.begin(); it != profiling_infos_.end();) {
393 ProfilingInfo* info = *it;
394 if (alloc.ContainsUnsafe(info->GetMethod())) {
395 info->GetMethod()->SetProfilingInfo(nullptr);
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +0000396 FreeData(reinterpret_cast<uint8_t*>(info));
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000397 it = profiling_infos_.erase(it);
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100398 } else {
399 ++it;
400 }
401 }
402}
403
Nicolas Geoffraye51ca8b2016-11-22 14:49:31 +0000404bool JitCodeCache::IsWeakAccessEnabled(Thread* self) const {
405 return kUseReadBarrier
406 ? self->GetWeakRefAccessEnabled()
407 : is_weak_access_enabled_.LoadSequentiallyConsistent();
408}
409
410void JitCodeCache::WaitUntilInlineCacheAccessible(Thread* self) {
411 if (IsWeakAccessEnabled(self)) {
412 return;
413 }
414 ScopedThreadSuspension sts(self, kWaitingWeakGcRootRead);
Nicolas Geoffrayb6e20ae2016-03-07 14:29:04 +0000415 MutexLock mu(self, lock_);
Nicolas Geoffraye51ca8b2016-11-22 14:49:31 +0000416 while (!IsWeakAccessEnabled(self)) {
417 inline_cache_cond_.Wait(self);
418 }
419}
420
421void JitCodeCache::BroadcastForInlineCacheAccess() {
422 Thread* self = Thread::Current();
423 MutexLock mu(self, lock_);
424 inline_cache_cond_.Broadcast(self);
425}
426
427void JitCodeCache::AllowInlineCacheAccess() {
428 DCHECK(!kUseReadBarrier);
429 is_weak_access_enabled_.StoreSequentiallyConsistent(true);
430 BroadcastForInlineCacheAccess();
431}
432
433void JitCodeCache::DisallowInlineCacheAccess() {
434 DCHECK(!kUseReadBarrier);
435 is_weak_access_enabled_.StoreSequentiallyConsistent(false);
436}
437
438void JitCodeCache::CopyInlineCacheInto(const InlineCache& ic,
439 Handle<mirror::ObjectArray<mirror::Class>> array) {
440 WaitUntilInlineCacheAccessible(Thread::Current());
441 // Note that we don't need to lock `lock_` here, the compiler calling
442 // this method has already ensured the inline cache will not be deleted.
443 for (size_t in_cache = 0, in_array = 0;
444 in_cache < InlineCache::kIndividualCacheSize;
445 ++in_cache) {
446 mirror::Class* object = ic.classes_[in_cache].Read();
447 if (object != nullptr) {
448 array->Set(in_array++, object);
Nicolas Geoffrayb6e20ae2016-03-07 14:29:04 +0000449 }
450 }
451}
452
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100453uint8_t* JitCodeCache::CommitCodeInternal(Thread* self,
454 ArtMethod* method,
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000455 uint8_t* stack_map,
456 uint8_t* roots_data,
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100457 size_t frame_size_in_bytes,
458 size_t core_spill_mask,
459 size_t fp_spill_mask,
460 const uint8_t* code,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000461 size_t code_size,
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000462 bool osr,
463 Handle<mirror::ObjectArray<mirror::Object>> roots) {
464 DCHECK(stack_map != nullptr);
Nicolas Geoffray1e7de6c2015-10-21 12:07:31 +0100465 size_t alignment = GetInstructionSetAlignment(kRuntimeISA);
466 // Ensure the header ends up at expected instruction alignment.
467 size_t header_size = RoundUp(sizeof(OatQuickMethodHeader), alignment);
468 size_t total_size = header_size + code_size;
469
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100470 OatQuickMethodHeader* method_header = nullptr;
Nicolas Geoffray1e7de6c2015-10-21 12:07:31 +0100471 uint8_t* code_ptr = nullptr;
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +0000472 uint8_t* memory = nullptr;
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100473 {
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000474 ScopedThreadSuspension sts(self, kSuspended);
475 MutexLock mu(self, lock_);
476 WaitForPotentialCollectionToComplete(self);
477 {
478 ScopedCodeCacheWrite scc(code_map_.get());
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +0000479 memory = AllocateCode(total_size);
480 if (memory == nullptr) {
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000481 return nullptr;
482 }
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +0000483 code_ptr = memory + header_size;
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000484
485 std::copy(code, code + code_size, code_ptr);
486 method_header = OatQuickMethodHeader::FromCodePointer(code_ptr);
487 new (method_header) OatQuickMethodHeader(
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000488 code_ptr - stack_map,
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000489 frame_size_in_bytes,
490 core_spill_mask,
491 fp_spill_mask,
492 code_size);
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100493 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100494
Roland Levillain32430262016-02-01 15:23:20 +0000495 FlushInstructionCache(reinterpret_cast<char*>(code_ptr),
496 reinterpret_cast<char*>(code_ptr + code_size));
Nicolas Geoffray0a522232016-01-19 09:34:58 +0000497 number_of_compilations_++;
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100498 }
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000499 // We need to update the entry point in the runnable state for the instrumentation.
500 {
501 MutexLock mu(self, lock_);
502 method_code_map_.Put(code_ptr, method);
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000503 // Fill the root table before updating the entry point.
504 FillRootTable(roots_data, roots);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000505 if (osr) {
Nicolas Geoffrayfcdd7292016-02-25 13:27:47 +0000506 number_of_osr_compilations_++;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000507 osr_code_map_.Put(method, code_ptr);
Nicolas Geoffray480d5102016-04-18 12:09:30 +0100508 } else {
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000509 Runtime::Current()->GetInstrumentation()->UpdateMethodsCode(
510 method, method_header->GetEntryPoint());
511 }
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000512 if (collection_in_progress_) {
513 // We need to update the live bitmap if there is a GC to ensure it sees this new
514 // code.
515 GetLiveBitmap()->AtomicTestAndSet(FromCodeToAllocation(code_ptr));
516 }
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000517 last_update_time_ns_.StoreRelease(NanoTime());
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000518 VLOG(jit)
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100519 << "JIT added (osr=" << std::boolalpha << osr << std::noboolalpha << ") "
David Sehr709b0702016-10-13 09:12:37 -0700520 << ArtMethod::PrettyMethod(method) << "@" << method
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000521 << " ccache_size=" << PrettySize(CodeCacheSizeLocked()) << ": "
522 << " dcache_size=" << PrettySize(DataCacheSizeLocked()) << ": "
523 << reinterpret_cast<const void*>(method_header->GetEntryPoint()) << ","
524 << reinterpret_cast<const void*>(method_header->GetEntryPoint() + method_header->code_size_);
Nicolas Geoffray933330a2016-03-16 14:20:06 +0000525 histogram_code_memory_use_.AddValue(code_size);
526 if (code_size > kCodeSizeLogThreshold) {
527 LOG(INFO) << "JIT allocated "
528 << PrettySize(code_size)
529 << " for compiled code of "
David Sehr709b0702016-10-13 09:12:37 -0700530 << ArtMethod::PrettyMethod(method);
Nicolas Geoffray933330a2016-03-16 14:20:06 +0000531 }
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000532 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100533
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100534 return reinterpret_cast<uint8_t*>(method_header);
535}
536
537size_t JitCodeCache::CodeCacheSize() {
538 MutexLock mu(Thread::Current(), lock_);
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000539 return CodeCacheSizeLocked();
540}
541
542size_t JitCodeCache::CodeCacheSizeLocked() {
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +0000543 return used_memory_for_code_;
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100544}
545
546size_t JitCodeCache::DataCacheSize() {
547 MutexLock mu(Thread::Current(), lock_);
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000548 return DataCacheSizeLocked();
549}
550
551size_t JitCodeCache::DataCacheSizeLocked() {
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +0000552 return used_memory_for_data_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800553}
554
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000555void JitCodeCache::ClearData(Thread* self, void* data) {
556 MutexLock mu(self, lock_);
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +0000557 FreeData(reinterpret_cast<uint8_t*>(data));
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000558}
559
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000560void JitCodeCache::ReserveData(Thread* self,
561 size_t stack_map_size,
562 size_t number_of_roots,
563 ArtMethod* method,
564 uint8_t** stack_map_data,
565 uint8_t** roots_data) {
566 size_t table_size = ComputeRootTableSize(number_of_roots);
567 size_t size = RoundUp(stack_map_size + table_size, sizeof(void*));
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100568 uint8_t* result = nullptr;
569
570 {
571 ScopedThreadSuspension sts(self, kSuspended);
572 MutexLock mu(self, lock_);
573 WaitForPotentialCollectionToComplete(self);
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +0000574 result = AllocateData(size);
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100575 }
576
577 if (result == nullptr) {
578 // Retry.
579 GarbageCollectCache(self);
580 ScopedThreadSuspension sts(self, kSuspended);
581 MutexLock mu(self, lock_);
582 WaitForPotentialCollectionToComplete(self);
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +0000583 result = AllocateData(size);
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100584 }
585
Nicolas Geoffray933330a2016-03-16 14:20:06 +0000586 MutexLock mu(self, lock_);
587 histogram_stack_map_memory_use_.AddValue(size);
588 if (size > kStackMapSizeLogThreshold) {
589 LOG(INFO) << "JIT allocated "
590 << PrettySize(size)
591 << " for stack maps of "
David Sehr709b0702016-10-13 09:12:37 -0700592 << ArtMethod::PrettyMethod(method);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800593 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000594 *roots_data = result;
595 *stack_map_data = result + table_size;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800596}
597
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100598class MarkCodeVisitor FINAL : public StackVisitor {
599 public:
600 MarkCodeVisitor(Thread* thread_in, JitCodeCache* code_cache_in)
601 : StackVisitor(thread_in, nullptr, StackVisitor::StackWalkKind::kSkipInlinedFrames),
602 code_cache_(code_cache_in),
603 bitmap_(code_cache_->GetLiveBitmap()) {}
604
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700605 bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100606 const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader();
607 if (method_header == nullptr) {
608 return true;
609 }
610 const void* code = method_header->GetCode();
611 if (code_cache_->ContainsPc(code)) {
612 // Use the atomic set version, as multiple threads are executing this code.
613 bitmap_->AtomicTestAndSet(FromCodeToAllocation(code));
614 }
615 return true;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800616 }
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100617
618 private:
619 JitCodeCache* const code_cache_;
620 CodeCacheBitmap* const bitmap_;
621};
622
623class MarkCodeClosure FINAL : public Closure {
624 public:
625 MarkCodeClosure(JitCodeCache* code_cache, Barrier* barrier)
626 : code_cache_(code_cache), barrier_(barrier) {}
627
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700628 void Run(Thread* thread) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800629 ScopedTrace trace(__PRETTY_FUNCTION__);
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100630 DCHECK(thread == Thread::Current() || thread->IsSuspended());
631 MarkCodeVisitor visitor(thread, code_cache_);
632 visitor.WalkStack();
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +0000633 if (kIsDebugBuild) {
634 // The stack walking code queries the side instrumentation stack if it
635 // sees an instrumentation exit pc, so the JIT code of methods in that stack
636 // must have been seen. We sanity check this below.
637 for (const instrumentation::InstrumentationStackFrame& frame
638 : *thread->GetInstrumentationStack()) {
639 // The 'method_' in InstrumentationStackFrame is the one that has return_pc_ in
640 // its stack frame, it is not the method owning return_pc_. We just pass null to
641 // LookupMethodHeader: the method is only checked against in debug builds.
642 OatQuickMethodHeader* method_header =
643 code_cache_->LookupMethodHeader(frame.return_pc_, nullptr);
644 if (method_header != nullptr) {
645 const void* code = method_header->GetCode();
646 CHECK(code_cache_->GetLiveBitmap()->Test(FromCodeToAllocation(code)));
647 }
648 }
649 }
Mathieu Chartier10d25082015-10-28 18:36:09 -0700650 barrier_->Pass(Thread::Current());
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800651 }
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100652
653 private:
654 JitCodeCache* const code_cache_;
655 Barrier* const barrier_;
656};
657
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000658void JitCodeCache::NotifyCollectionDone(Thread* self) {
659 collection_in_progress_ = false;
660 lock_cond_.Broadcast(self);
661}
662
663void JitCodeCache::SetFootprintLimit(size_t new_footprint) {
664 size_t per_space_footprint = new_footprint / 2;
665 DCHECK(IsAlignedParam(per_space_footprint, kPageSize));
666 DCHECK_EQ(per_space_footprint * 2, new_footprint);
667 mspace_set_footprint_limit(data_mspace_, per_space_footprint);
668 {
669 ScopedCodeCacheWrite scc(code_map_.get());
670 mspace_set_footprint_limit(code_mspace_, per_space_footprint);
671 }
672}
673
674bool JitCodeCache::IncreaseCodeCacheCapacity() {
675 if (current_capacity_ == max_capacity_) {
676 return false;
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100677 }
678
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000679 // Double the capacity if we're below 1MB, or increase it by 1MB if
680 // we're above.
681 if (current_capacity_ < 1 * MB) {
682 current_capacity_ *= 2;
683 } else {
684 current_capacity_ += 1 * MB;
685 }
686 if (current_capacity_ > max_capacity_) {
687 current_capacity_ = max_capacity_;
688 }
689
690 if (!kIsDebugBuild || VLOG_IS_ON(jit)) {
691 LOG(INFO) << "Increasing code cache capacity to " << PrettySize(current_capacity_);
692 }
693
694 SetFootprintLimit(current_capacity_);
695
696 return true;
697}
698
Nicolas Geoffray8d372502016-02-23 13:56:43 +0000699void JitCodeCache::MarkCompiledCodeOnThreadStacks(Thread* self) {
700 Barrier barrier(0);
701 size_t threads_running_checkpoint = 0;
702 MarkCodeClosure closure(this, &barrier);
703 threads_running_checkpoint = Runtime::Current()->GetThreadList()->RunCheckpoint(&closure);
704 // Now that we have run our checkpoint, move to a suspended state and wait
705 // for other threads to run the checkpoint.
706 ScopedThreadSuspension sts(self, kSuspended);
707 if (threads_running_checkpoint != 0) {
708 barrier.Increment(self, threads_running_checkpoint);
709 }
710}
711
Nicolas Geoffray35122442016-03-02 12:05:30 +0000712bool JitCodeCache::ShouldDoFullCollection() {
713 if (current_capacity_ == max_capacity_) {
714 // Always do a full collection when the code cache is full.
715 return true;
716 } else if (current_capacity_ < kReservedCapacity) {
717 // Always do partial collection when the code cache size is below the reserved
718 // capacity.
719 return false;
720 } else if (last_collection_increased_code_cache_) {
721 // This time do a full collection.
722 return true;
723 } else {
724 // This time do a partial collection.
725 return false;
Nicolas Geoffray8d372502016-02-23 13:56:43 +0000726 }
727}
728
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000729void JitCodeCache::GarbageCollectCache(Thread* self) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800730 ScopedTrace trace(__FUNCTION__);
Nicolas Geoffray8d372502016-02-23 13:56:43 +0000731 if (!garbage_collect_code_) {
732 MutexLock mu(self, lock_);
733 IncreaseCodeCacheCapacity();
734 return;
735 }
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100736
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000737 // Wait for an existing collection, or let everyone know we are starting one.
738 {
739 ScopedThreadSuspension sts(self, kSuspended);
740 MutexLock mu(self, lock_);
741 if (WaitForPotentialCollectionToComplete(self)) {
742 return;
743 } else {
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +0000744 number_of_collections_++;
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000745 live_bitmap_.reset(CodeCacheBitmap::Create(
746 "code-cache-bitmap",
747 reinterpret_cast<uintptr_t>(code_map_->Begin()),
748 reinterpret_cast<uintptr_t>(code_map_->Begin() + current_capacity_ / 2)));
Nicolas Geoffray8d372502016-02-23 13:56:43 +0000749 collection_in_progress_ = true;
750 }
751 }
752
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +0000753 TimingLogger logger("JIT code cache timing logger", true, VLOG_IS_ON(jit));
Nicolas Geoffray8d372502016-02-23 13:56:43 +0000754 {
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +0000755 TimingLogger::ScopedTiming st("Code cache collection", &logger);
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000756
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +0000757 bool do_full_collection = false;
758 {
759 MutexLock mu(self, lock_);
760 do_full_collection = ShouldDoFullCollection();
Nicolas Geoffraya96917a2016-03-01 22:18:02 +0000761 }
762
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +0000763 if (!kIsDebugBuild || VLOG_IS_ON(jit)) {
764 LOG(INFO) << "Do "
765 << (do_full_collection ? "full" : "partial")
766 << " code cache collection, code="
767 << PrettySize(CodeCacheSize())
768 << ", data=" << PrettySize(DataCacheSize());
769 }
Nicolas Geoffray35122442016-03-02 12:05:30 +0000770
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +0000771 DoCollection(self, /* collect_profiling_info */ do_full_collection);
772
773 if (!kIsDebugBuild || VLOG_IS_ON(jit)) {
774 LOG(INFO) << "After code cache collection, code="
775 << PrettySize(CodeCacheSize())
776 << ", data=" << PrettySize(DataCacheSize());
777 }
778
779 {
780 MutexLock mu(self, lock_);
781
782 // Increase the code cache only when we do partial collections.
783 // TODO: base this strategy on how full the code cache is?
784 if (do_full_collection) {
785 last_collection_increased_code_cache_ = false;
786 } else {
787 last_collection_increased_code_cache_ = true;
788 IncreaseCodeCacheCapacity();
Nicolas Geoffray35122442016-03-02 12:05:30 +0000789 }
790
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +0000791 bool next_collection_will_be_full = ShouldDoFullCollection();
792
793 // Start polling the liveness of compiled code to prepare for the next full collection.
Nicolas Geoffray480d5102016-04-18 12:09:30 +0100794 if (next_collection_will_be_full) {
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +0000795 // Save the entry point of methods we have compiled, and update the entry
796 // point of those methods to the interpreter. If the method is invoked, the
797 // interpreter will update its entry point to the compiled code and call it.
798 for (ProfilingInfo* info : profiling_infos_) {
799 const void* entry_point = info->GetMethod()->GetEntryPointFromQuickCompiledCode();
800 if (ContainsPc(entry_point)) {
801 info->SetSavedEntryPoint(entry_point);
Nicolas Geoffray480d5102016-04-18 12:09:30 +0100802 Runtime::Current()->GetInstrumentation()->UpdateMethodsCode(
803 info->GetMethod(), GetQuickToInterpreterBridge());
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +0000804 }
805 }
806
807 DCHECK(CheckLiveCompiledCodeHasProfilingInfo());
808 }
809 live_bitmap_.reset(nullptr);
810 NotifyCollectionDone(self);
Nicolas Geoffray35122442016-03-02 12:05:30 +0000811 }
Nicolas Geoffray35122442016-03-02 12:05:30 +0000812 }
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +0000813 Runtime::Current()->GetJit()->AddTimingLogger(logger);
Nicolas Geoffray35122442016-03-02 12:05:30 +0000814}
815
Nicolas Geoffray9abb2972016-03-04 14:32:59 +0000816void JitCodeCache::RemoveUnmarkedCode(Thread* self) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800817 ScopedTrace trace(__FUNCTION__);
Nicolas Geoffray35122442016-03-02 12:05:30 +0000818 MutexLock mu(self, lock_);
819 ScopedCodeCacheWrite scc(code_map_.get());
Nicolas Geoffray9abb2972016-03-04 14:32:59 +0000820 // Iterate over all compiled code and remove entries that are not marked.
Nicolas Geoffray35122442016-03-02 12:05:30 +0000821 for (auto it = method_code_map_.begin(); it != method_code_map_.end();) {
822 const void* code_ptr = it->first;
823 ArtMethod* method = it->second;
824 uintptr_t allocation = FromCodeToAllocation(code_ptr);
Nicolas Geoffray9abb2972016-03-04 14:32:59 +0000825 if (GetLiveBitmap()->Test(allocation)) {
Nicolas Geoffray35122442016-03-02 12:05:30 +0000826 ++it;
827 } else {
Nicolas Geoffray35122442016-03-02 12:05:30 +0000828 FreeCode(code_ptr, method);
829 it = method_code_map_.erase(it);
830 }
831 }
832}
833
834void JitCodeCache::DoCollection(Thread* self, bool collect_profiling_info) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800835 ScopedTrace trace(__FUNCTION__);
Nicolas Geoffray35122442016-03-02 12:05:30 +0000836 {
837 MutexLock mu(self, lock_);
838 if (collect_profiling_info) {
839 // Clear the profiling info of methods that do not have compiled code as entrypoint.
840 // Also remove the saved entry point from the ProfilingInfo objects.
841 for (ProfilingInfo* info : profiling_infos_) {
842 const void* ptr = info->GetMethod()->GetEntryPointFromQuickCompiledCode();
Nicolas Geoffrayb6e20ae2016-03-07 14:29:04 +0000843 if (!ContainsPc(ptr) && !info->IsInUseByCompiler()) {
Nicolas Geoffray35122442016-03-02 12:05:30 +0000844 info->GetMethod()->SetProfilingInfo(nullptr);
845 }
Nicolas Geoffrayb9a639d2016-03-22 11:25:20 +0000846
847 if (info->GetSavedEntryPoint() != nullptr) {
848 info->SetSavedEntryPoint(nullptr);
849 // We are going to move this method back to interpreter. Clear the counter now to
850 // give it a chance to be hot again.
851 info->GetMethod()->ClearCounter();
852 }
Nicolas Geoffray35122442016-03-02 12:05:30 +0000853 }
854 } else if (kIsDebugBuild) {
855 // Sanity check that the profiling infos do not have a dangling entry point.
856 for (ProfilingInfo* info : profiling_infos_) {
857 DCHECK(info->GetSavedEntryPoint() == nullptr);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100858 }
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000859 }
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000860
Nicolas Geoffray9abb2972016-03-04 14:32:59 +0000861 // Mark compiled code that are entrypoints of ArtMethods. Compiled code that is not
862 // an entry point is either:
863 // - an osr compiled code, that will be removed if not in a thread call stack.
864 // - discarded compiled code, that will be removed if not in a thread call stack.
865 for (const auto& it : method_code_map_) {
866 ArtMethod* method = it.second;
867 const void* code_ptr = it.first;
868 const OatQuickMethodHeader* method_header = OatQuickMethodHeader::FromCodePointer(code_ptr);
869 if (method_header->GetEntryPoint() == method->GetEntryPointFromQuickCompiledCode()) {
870 GetLiveBitmap()->AtomicTestAndSet(FromCodeToAllocation(code_ptr));
871 }
872 }
873
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +0000874 // Empty osr method map, as osr compiled code will be deleted (except the ones
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000875 // on thread stacks).
876 osr_code_map_.clear();
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100877 }
878
879 // Run a checkpoint on all threads to mark the JIT compiled code they are running.
Nicolas Geoffray8d372502016-02-23 13:56:43 +0000880 MarkCompiledCodeOnThreadStacks(self);
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100881
Nicolas Geoffray9abb2972016-03-04 14:32:59 +0000882 // At this point, mutator threads are still running, and entrypoints of methods can
883 // change. We do know they cannot change to a code cache entry that is not marked,
884 // therefore we can safely remove those entries.
885 RemoveUnmarkedCode(self);
Nicolas Geoffraya96917a2016-03-01 22:18:02 +0000886
Nicolas Geoffray35122442016-03-02 12:05:30 +0000887 if (collect_profiling_info) {
Nicolas Geoffraycf48fa02016-07-30 22:49:11 +0100888 ScopedThreadSuspension sts(self, kSuspended);
Nicolas Geoffray35122442016-03-02 12:05:30 +0000889 MutexLock mu(self, lock_);
890 // Free all profiling infos of methods not compiled nor being compiled.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100891 auto profiling_kept_end = std::remove_if(profiling_infos_.begin(), profiling_infos_.end(),
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +0000892 [this] (ProfilingInfo* info) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffray35122442016-03-02 12:05:30 +0000893 const void* ptr = info->GetMethod()->GetEntryPointFromQuickCompiledCode();
Nicolas Geoffray511e41b2016-03-02 17:09:35 +0000894 // We have previously cleared the ProfilingInfo pointer in the ArtMethod in the hope
895 // that the compiled code would not get revived. As mutator threads run concurrently,
896 // they may have revived the compiled code, and now we are in the situation where
897 // a method has compiled code but no ProfilingInfo.
898 // We make sure compiled methods have a ProfilingInfo object. It is needed for
899 // code cache collection.
Andreas Gampe542451c2016-07-26 09:02:02 -0700900 if (ContainsPc(ptr) &&
901 info->GetMethod()->GetProfilingInfo(kRuntimePointerSize) == nullptr) {
Nicolas Geoffray35122442016-03-02 12:05:30 +0000902 info->GetMethod()->SetProfilingInfo(info);
Andreas Gampe542451c2016-07-26 09:02:02 -0700903 } else if (info->GetMethod()->GetProfilingInfo(kRuntimePointerSize) != info) {
Nicolas Geoffray35122442016-03-02 12:05:30 +0000904 // No need for this ProfilingInfo object anymore.
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +0000905 FreeData(reinterpret_cast<uint8_t*>(info));
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100906 return true;
907 }
908 return false;
909 });
910 profiling_infos_.erase(profiling_kept_end, profiling_infos_.end());
Nicolas Geoffray35122442016-03-02 12:05:30 +0000911 DCHECK(CheckLiveCompiledCodeHasProfilingInfo());
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100912 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800913}
914
Nicolas Geoffray35122442016-03-02 12:05:30 +0000915bool JitCodeCache::CheckLiveCompiledCodeHasProfilingInfo() {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800916 ScopedTrace trace(__FUNCTION__);
Nicolas Geoffray35122442016-03-02 12:05:30 +0000917 // Check that methods we have compiled do have a ProfilingInfo object. We would
918 // have memory leaks of compiled code otherwise.
919 for (const auto& it : method_code_map_) {
920 ArtMethod* method = it.second;
Andreas Gampe542451c2016-07-26 09:02:02 -0700921 if (method->GetProfilingInfo(kRuntimePointerSize) == nullptr) {
Nicolas Geoffray35122442016-03-02 12:05:30 +0000922 const void* code_ptr = it.first;
923 const OatQuickMethodHeader* method_header = OatQuickMethodHeader::FromCodePointer(code_ptr);
924 if (method_header->GetEntryPoint() == method->GetEntryPointFromQuickCompiledCode()) {
925 // If the code is not dead, then we have a problem. Note that this can even
926 // happen just after a collection, as mutator threads are running in parallel
927 // and could deoptimize an existing compiled code.
928 return false;
929 }
930 }
931 }
932 return true;
933}
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100934
935OatQuickMethodHeader* JitCodeCache::LookupMethodHeader(uintptr_t pc, ArtMethod* method) {
936 static_assert(kRuntimeISA != kThumb2, "kThumb2 cannot be a runtime ISA");
937 if (kRuntimeISA == kArm) {
938 // On Thumb-2, the pc is offset by one.
939 --pc;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800940 }
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100941 if (!ContainsPc(reinterpret_cast<const void*>(pc))) {
942 return nullptr;
943 }
944
945 MutexLock mu(Thread::Current(), lock_);
946 if (method_code_map_.empty()) {
947 return nullptr;
948 }
949 auto it = method_code_map_.lower_bound(reinterpret_cast<const void*>(pc));
950 --it;
951
952 const void* code_ptr = it->first;
953 OatQuickMethodHeader* method_header = OatQuickMethodHeader::FromCodePointer(code_ptr);
954 if (!method_header->Contains(pc)) {
955 return nullptr;
956 }
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +0000957 if (kIsDebugBuild && method != nullptr) {
958 DCHECK_EQ(it->second, method)
David Sehr709b0702016-10-13 09:12:37 -0700959 << ArtMethod::PrettyMethod(method) << " " << ArtMethod::PrettyMethod(it->second) << " "
960 << std::hex << pc;
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +0000961 }
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100962 return method_header;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800963}
964
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000965OatQuickMethodHeader* JitCodeCache::LookupOsrMethodHeader(ArtMethod* method) {
966 MutexLock mu(Thread::Current(), lock_);
967 auto it = osr_code_map_.find(method);
968 if (it == osr_code_map_.end()) {
969 return nullptr;
970 }
971 return OatQuickMethodHeader::FromCodePointer(it->second);
972}
973
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000974ProfilingInfo* JitCodeCache::AddProfilingInfo(Thread* self,
975 ArtMethod* method,
976 const std::vector<uint32_t>& entries,
Nicolas Geoffray1e7da9b2016-03-01 14:11:40 +0000977 bool retry_allocation)
978 // No thread safety analysis as we are using TryLock/Unlock explicitly.
979 NO_THREAD_SAFETY_ANALYSIS {
980 ProfilingInfo* info = nullptr;
981 if (!retry_allocation) {
982 // If we are allocating for the interpreter, just try to lock, to avoid
983 // lock contention with the JIT.
984 if (lock_.ExclusiveTryLock(self)) {
985 info = AddProfilingInfoInternal(self, method, entries);
986 lock_.ExclusiveUnlock(self);
987 }
988 } else {
989 {
990 MutexLock mu(self, lock_);
991 info = AddProfilingInfoInternal(self, method, entries);
992 }
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000993
Nicolas Geoffray1e7da9b2016-03-01 14:11:40 +0000994 if (info == nullptr) {
995 GarbageCollectCache(self);
996 MutexLock mu(self, lock_);
997 info = AddProfilingInfoInternal(self, method, entries);
998 }
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000999 }
1000 return info;
1001}
1002
Nicolas Geoffray1e7da9b2016-03-01 14:11:40 +00001003ProfilingInfo* JitCodeCache::AddProfilingInfoInternal(Thread* self ATTRIBUTE_UNUSED,
Nicolas Geoffray26705e22015-10-28 12:50:11 +00001004 ArtMethod* method,
1005 const std::vector<uint32_t>& entries) {
1006 size_t profile_info_size = RoundUp(
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01001007 sizeof(ProfilingInfo) + sizeof(InlineCache) * entries.size(),
Nicolas Geoffray26705e22015-10-28 12:50:11 +00001008 sizeof(void*));
Nicolas Geoffray26705e22015-10-28 12:50:11 +00001009
1010 // Check whether some other thread has concurrently created it.
Andreas Gampe542451c2016-07-26 09:02:02 -07001011 ProfilingInfo* info = method->GetProfilingInfo(kRuntimePointerSize);
Nicolas Geoffray26705e22015-10-28 12:50:11 +00001012 if (info != nullptr) {
1013 return info;
1014 }
1015
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +00001016 uint8_t* data = AllocateData(profile_info_size);
Nicolas Geoffray26705e22015-10-28 12:50:11 +00001017 if (data == nullptr) {
1018 return nullptr;
1019 }
1020 info = new (data) ProfilingInfo(method, entries);
Nicolas Geoffray07f35642016-01-04 16:06:51 +00001021
1022 // Make sure other threads see the data in the profiling info object before the
1023 // store in the ArtMethod's ProfilingInfo pointer.
1024 QuasiAtomic::ThreadFenceRelease();
1025
Nicolas Geoffray26705e22015-10-28 12:50:11 +00001026 method->SetProfilingInfo(info);
1027 profiling_infos_.push_back(info);
Nicolas Geoffray933330a2016-03-16 14:20:06 +00001028 histogram_profiling_info_memory_use_.AddValue(profile_info_size);
Nicolas Geoffray26705e22015-10-28 12:50:11 +00001029 return info;
1030}
1031
Nicolas Geoffray0a3be162015-11-18 11:15:22 +00001032// NO_THREAD_SAFETY_ANALYSIS as this is called from mspace code, at which point the lock
1033// is already held.
1034void* JitCodeCache::MoreCore(const void* mspace, intptr_t increment) NO_THREAD_SAFETY_ANALYSIS {
1035 if (code_mspace_ == mspace) {
1036 size_t result = code_end_;
1037 code_end_ += increment;
1038 return reinterpret_cast<void*>(result + code_map_->Begin());
1039 } else {
1040 DCHECK_EQ(data_mspace_, mspace);
1041 size_t result = data_end_;
1042 data_end_ += increment;
1043 return reinterpret_cast<void*>(result + data_map_->Begin());
1044 }
1045}
1046
Calin Juravle99629622016-04-19 16:33:46 +01001047void JitCodeCache::GetProfiledMethods(const std::set<std::string>& dex_base_locations,
1048 std::vector<MethodReference>& methods) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001049 ScopedTrace trace(__FUNCTION__);
Calin Juravle31f2c152015-10-23 17:56:15 +01001050 MutexLock mu(Thread::Current(), lock_);
Calin Juravle99629622016-04-19 16:33:46 +01001051 for (const ProfilingInfo* info : profiling_infos_) {
1052 ArtMethod* method = info->GetMethod();
1053 const DexFile* dex_file = method->GetDexFile();
1054 if (ContainsElement(dex_base_locations, dex_file->GetBaseLocation())) {
1055 methods.emplace_back(dex_file, method->GetDexMethodIndex());
Calin Juravle31f2c152015-10-23 17:56:15 +01001056 }
1057 }
1058}
1059
Calin Juravle4d77b6a2015-12-01 18:38:09 +00001060uint64_t JitCodeCache::GetLastUpdateTimeNs() const {
1061 return last_update_time_ns_.LoadAcquire();
Calin Juravle31f2c152015-10-23 17:56:15 +01001062}
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01001063
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +01001064bool JitCodeCache::IsOsrCompiled(ArtMethod* method) {
1065 MutexLock mu(Thread::Current(), lock_);
1066 return osr_code_map_.find(method) != osr_code_map_.end();
1067}
1068
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00001069bool JitCodeCache::NotifyCompilationOf(ArtMethod* method, Thread* self, bool osr) {
1070 if (!osr && ContainsPc(method->GetEntryPointFromQuickCompiledCode())) {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01001071 return false;
1072 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001073
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001074 MutexLock mu(self, lock_);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00001075 if (osr && (osr_code_map_.find(method) != osr_code_map_.end())) {
1076 return false;
1077 }
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +00001078
Andreas Gampe542451c2016-07-26 09:02:02 -07001079 ProfilingInfo* info = method->GetProfilingInfo(kRuntimePointerSize);
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +00001080 if (info == nullptr) {
David Sehr709b0702016-10-13 09:12:37 -07001081 VLOG(jit) << method->PrettyMethod() << " needs a ProfilingInfo to be compiled";
Nicolas Geoffrayb9a639d2016-03-22 11:25:20 +00001082 // Because the counter is not atomic, there are some rare cases where we may not
1083 // hit the threshold for creating the ProfilingInfo. Reset the counter now to
1084 // "correct" this.
1085 method->ClearCounter();
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01001086 return false;
1087 }
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +00001088
buzbee454b3b62016-04-07 14:42:47 -07001089 if (info->IsMethodBeingCompiled(osr)) {
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +00001090 return false;
1091 }
1092
buzbee454b3b62016-04-07 14:42:47 -07001093 info->SetIsMethodBeingCompiled(true, osr);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01001094 return true;
1095}
1096
Nicolas Geoffray07e3ca92016-03-11 09:57:57 +00001097ProfilingInfo* JitCodeCache::NotifyCompilerUse(ArtMethod* method, Thread* self) {
Nicolas Geoffrayb6e20ae2016-03-07 14:29:04 +00001098 MutexLock mu(self, lock_);
Andreas Gampe542451c2016-07-26 09:02:02 -07001099 ProfilingInfo* info = method->GetProfilingInfo(kRuntimePointerSize);
Nicolas Geoffrayb6e20ae2016-03-07 14:29:04 +00001100 if (info != nullptr) {
1101 info->IncrementInlineUse();
1102 }
Nicolas Geoffray07e3ca92016-03-11 09:57:57 +00001103 return info;
Nicolas Geoffrayb6e20ae2016-03-07 14:29:04 +00001104}
1105
Nicolas Geoffray07e3ca92016-03-11 09:57:57 +00001106void JitCodeCache::DoneCompilerUse(ArtMethod* method, Thread* self) {
Nicolas Geoffrayb6e20ae2016-03-07 14:29:04 +00001107 MutexLock mu(self, lock_);
Andreas Gampe542451c2016-07-26 09:02:02 -07001108 ProfilingInfo* info = method->GetProfilingInfo(kRuntimePointerSize);
Nicolas Geoffray07e3ca92016-03-11 09:57:57 +00001109 DCHECK(info != nullptr);
1110 info->DecrementInlineUse();
Nicolas Geoffrayb6e20ae2016-03-07 14:29:04 +00001111}
1112
buzbee454b3b62016-04-07 14:42:47 -07001113void JitCodeCache::DoneCompiling(ArtMethod* method, Thread* self ATTRIBUTE_UNUSED, bool osr) {
Andreas Gampe542451c2016-07-26 09:02:02 -07001114 ProfilingInfo* info = method->GetProfilingInfo(kRuntimePointerSize);
buzbee454b3b62016-04-07 14:42:47 -07001115 DCHECK(info->IsMethodBeingCompiled(osr));
1116 info->SetIsMethodBeingCompiled(false, osr);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01001117}
1118
Nicolas Geoffraya25dce92016-01-12 16:41:10 +00001119size_t JitCodeCache::GetMemorySizeOfCodePointer(const void* ptr) {
1120 MutexLock mu(Thread::Current(), lock_);
1121 return mspace_usable_size(reinterpret_cast<const void*>(FromCodeToAllocation(ptr)));
1122}
1123
Nicolas Geoffrayb88d59e2016-02-17 11:31:49 +00001124void JitCodeCache::InvalidateCompiledCodeFor(ArtMethod* method,
1125 const OatQuickMethodHeader* header) {
Andreas Gampe542451c2016-07-26 09:02:02 -07001126 ProfilingInfo* profiling_info = method->GetProfilingInfo(kRuntimePointerSize);
Nicolas Geoffray35122442016-03-02 12:05:30 +00001127 if ((profiling_info != nullptr) &&
1128 (profiling_info->GetSavedEntryPoint() == header->GetEntryPoint())) {
1129 // Prevent future uses of the compiled code.
1130 profiling_info->SetSavedEntryPoint(nullptr);
1131 }
1132
Nicolas Geoffrayb88d59e2016-02-17 11:31:49 +00001133 if (method->GetEntryPointFromQuickCompiledCode() == header->GetEntryPoint()) {
1134 // The entrypoint is the one to invalidate, so we just update
1135 // it to the interpreter entry point and clear the counter to get the method
1136 // Jitted again.
1137 Runtime::Current()->GetInstrumentation()->UpdateMethodsCode(
1138 method, GetQuickToInterpreterBridge());
1139 method->ClearCounter();
1140 } else {
1141 MutexLock mu(Thread::Current(), lock_);
1142 auto it = osr_code_map_.find(method);
1143 if (it != osr_code_map_.end() && OatQuickMethodHeader::FromCodePointer(it->second) == header) {
1144 // Remove the OSR method, to avoid using it again.
1145 osr_code_map_.erase(it);
1146 }
1147 }
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +00001148 MutexLock mu(Thread::Current(), lock_);
1149 number_of_deoptimizations_++;
Nicolas Geoffrayb88d59e2016-02-17 11:31:49 +00001150}
1151
Nicolas Geoffray38ea9bd2016-02-19 16:25:57 +00001152uint8_t* JitCodeCache::AllocateCode(size_t code_size) {
1153 size_t alignment = GetInstructionSetAlignment(kRuntimeISA);
1154 uint8_t* result = reinterpret_cast<uint8_t*>(
1155 mspace_memalign(code_mspace_, alignment, code_size));
1156 size_t header_size = RoundUp(sizeof(OatQuickMethodHeader), alignment);
1157 // Ensure the header ends up at expected instruction alignment.
1158 DCHECK_ALIGNED_PARAM(reinterpret_cast<uintptr_t>(result + header_size), alignment);
1159 used_memory_for_code_ += mspace_usable_size(result);
1160 return result;
1161}
1162
1163void JitCodeCache::FreeCode(uint8_t* code) {
1164 used_memory_for_code_ -= mspace_usable_size(code);
1165 mspace_free(code_mspace_, code);
1166}
1167
1168uint8_t* JitCodeCache::AllocateData(size_t data_size) {
1169 void* result = mspace_malloc(data_mspace_, data_size);
1170 used_memory_for_data_ += mspace_usable_size(result);
1171 return reinterpret_cast<uint8_t*>(result);
1172}
1173
1174void JitCodeCache::FreeData(uint8_t* data) {
1175 used_memory_for_data_ -= mspace_usable_size(data);
1176 mspace_free(data_mspace_, data);
1177}
1178
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +00001179void JitCodeCache::Dump(std::ostream& os) {
1180 MutexLock mu(Thread::Current(), lock_);
1181 os << "Current JIT code cache size: " << PrettySize(used_memory_for_code_) << "\n"
1182 << "Current JIT data cache size: " << PrettySize(used_memory_for_data_) << "\n"
1183 << "Current JIT capacity: " << PrettySize(current_capacity_) << "\n"
1184 << "Current number of JIT code cache entries: " << method_code_map_.size() << "\n"
1185 << "Total number of JIT compilations: " << number_of_compilations_ << "\n"
1186 << "Total number of JIT compilations for on stack replacement: "
1187 << number_of_osr_compilations_ << "\n"
1188 << "Total number of deoptimizations: " << number_of_deoptimizations_ << "\n"
1189 << "Total number of JIT code cache collections: " << number_of_collections_ << std::endl;
Nicolas Geoffray933330a2016-03-16 14:20:06 +00001190 histogram_stack_map_memory_use_.PrintMemoryUse(os);
1191 histogram_code_memory_use_.PrintMemoryUse(os);
1192 histogram_profiling_info_memory_use_.PrintMemoryUse(os);
Nicolas Geoffraybcd94c82016-03-03 13:23:33 +00001193}
1194
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001195} // namespace jit
1196} // namespace art