blob: 36d21612624640a6ebbd3492fbe8a7d44deb2cee [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 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 */
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070016
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -070017#include "dlmalloc_space-inl.h"
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070018
Andreas Gampe170331f2017-12-07 18:41:03 -080019#include "base/logging.h" // For VLOG.
Vladimir Marko80afd022015-05-19 18:08:00 +010020#include "base/time_utils.h"
David Sehrc431b9d2018-03-02 12:01:51 -080021#include "base/utils.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070022#include "gc/accounting/card_table.h"
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070023#include "gc/accounting/space_bitmap-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070024#include "gc/heap.h"
Nicolas Geoffray0a3be162015-11-18 11:15:22 +000025#include "jit/jit.h"
26#include "jit/jit_code_cache.h"
Evgenii Stepanov1e133742015-05-20 12:30:59 -070027#include "memory_tool_malloc_space-inl.h"
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070028#include "mirror/class-inl.h"
Mathieu Chartier0f72e412013-09-06 16:40:01 -070029#include "mirror/object-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080030#include "runtime.h"
Andreas Gampe508fdf32017-06-05 16:42:13 -070031#include "scoped_thread_state_change-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080032#include "thread.h"
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070033#include "thread_list.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070034
Carl Shapiro69759ea2011-07-21 18:13:35 -070035namespace art {
Ian Rogers1d54e732013-05-02 21:10:01 -070036namespace gc {
37namespace space {
Ian Rogers30fab402012-01-23 15:43:46 -080038
Ian Rogers6fac4472014-02-25 17:01:10 -080039static constexpr bool kPrefetchDuringDlMallocFreeList = true;
40
Vladimir Markoc34bebf2018-08-16 16:12:49 +010041DlMallocSpace::DlMallocSpace(MemMap&& mem_map,
42 size_t initial_size,
43 const std::string& name,
44 void* mspace,
45 uint8_t* begin,
46 uint8_t* end,
47 uint8_t* limit,
48 size_t growth_limit,
49 bool can_move_objects,
50 size_t starting_size)
51 : MallocSpace(name,
52 std::move(mem_map),
53 begin,
54 end,
55 limit,
56 growth_limit,
57 /* create_bitmaps */ true,
58 can_move_objects,
Mathieu Chartier31f44142014-04-08 14:40:03 -070059 starting_size, initial_size),
60 mspace_(mspace) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070061 CHECK(mspace != nullptr);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070062}
63
Vladimir Markoc34bebf2018-08-16 16:12:49 +010064DlMallocSpace* DlMallocSpace::CreateFromMemMap(MemMap&& mem_map,
65 const std::string& name,
66 size_t starting_size,
67 size_t initial_size,
68 size_t growth_limit,
69 size_t capacity,
Mathieu Chartier31f44142014-04-08 14:40:03 -070070 bool can_move_objects) {
Vladimir Markoc34bebf2018-08-16 16:12:49 +010071 DCHECK(mem_map.IsValid());
72 void* mspace = CreateMspace(mem_map.Begin(), starting_size, initial_size);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080073 if (mspace == nullptr) {
Ian Rogers30fab402012-01-23 15:43:46 -080074 LOG(ERROR) << "Failed to initialize mspace for alloc space (" << name << ")";
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080075 return nullptr;
Ian Rogers30fab402012-01-23 15:43:46 -080076 }
77
lzang1385de732014-02-21 14:15:01 +080078 // Protect memory beyond the starting size. morecore will add r/w permissions when necessory
Vladimir Markoc34bebf2018-08-16 16:12:49 +010079 uint8_t* end = mem_map.Begin() + starting_size;
lzang1385de732014-02-21 14:15:01 +080080 if (capacity - starting_size > 0) {
Mathieu Chartier3425d022017-10-03 16:22:05 -070081 CheckedCall(mprotect, name.c_str(), end, capacity - starting_size, PROT_NONE);
Ian Rogers30fab402012-01-23 15:43:46 -080082 }
83
84 // Everything is set so record in immutable structure and leave
Vladimir Markoc34bebf2018-08-16 16:12:49 +010085 uint8_t* begin = mem_map.Begin();
Evgenii Stepanov1e133742015-05-20 12:30:59 -070086 if (Runtime::Current()->IsRunningOnMemoryTool()) {
87 return new MemoryToolMallocSpace<DlMallocSpace, kDefaultMemoryToolRedZoneBytes, true, false>(
Vladimir Markoc34bebf2018-08-16 16:12:49 +010088 std::move(mem_map),
89 initial_size,
90 name,
91 mspace,
92 begin,
93 end,
94 begin + capacity, growth_limit,
95 can_move_objects,
96 starting_size);
Ian Rogers1d54e732013-05-02 21:10:01 -070097 } else {
Vladimir Markoc34bebf2018-08-16 16:12:49 +010098 return new DlMallocSpace(std::move(mem_map),
99 initial_size,
100 name,
101 mspace,
102 begin,
103 end,
104 begin + capacity,
105 growth_limit,
106 can_move_objects,
107 starting_size);
Ian Rogers1d54e732013-05-02 21:10:01 -0700108 }
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800109}
110
Mathieu Chartier31f44142014-04-08 14:40:03 -0700111DlMallocSpace* DlMallocSpace::Create(const std::string& name, size_t initial_size,
Ian Rogers13735952014-10-08 12:43:28 -0700112 size_t growth_limit, size_t capacity, uint8_t* requested_begin,
Mathieu Chartier31f44142014-04-08 14:40:03 -0700113 bool can_move_objects) {
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800114 uint64_t start_time = 0;
115 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
116 start_time = NanoTime();
117 LOG(INFO) << "DlMallocSpace::Create entering " << name
118 << " initial_size=" << PrettySize(initial_size)
119 << " growth_limit=" << PrettySize(growth_limit)
120 << " capacity=" << PrettySize(capacity)
121 << " requested_begin=" << reinterpret_cast<void*>(requested_begin);
122 }
123
124 // Memory we promise to dlmalloc before it asks for morecore.
125 // Note: making this value large means that large allocations are unlikely to succeed as dlmalloc
126 // will ask for this memory from sys_alloc which will fail as the footprint (this value plus the
127 // size of the large allocation) will be greater than the footprint limit.
128 size_t starting_size = kPageSize;
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100129 MemMap mem_map =
130 CreateMemMap(name, starting_size, &initial_size, &growth_limit, &capacity, requested_begin);
131 if (!mem_map.IsValid()) {
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800132 LOG(ERROR) << "Failed to create mem map for alloc space (" << name << ") of size "
133 << PrettySize(capacity);
134 return nullptr;
135 }
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100136 DlMallocSpace* space = CreateFromMemMap(std::move(mem_map),
137 name,
138 starting_size,
139 initial_size,
140 growth_limit,
141 capacity,
142 can_move_objects);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700143 // We start out with only the initial size possibly containing objects.
Ian Rogers30fab402012-01-23 15:43:46 -0800144 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700145 LOG(INFO) << "DlMallocSpace::Create exiting (" << PrettyDuration(NanoTime() - start_time)
Ian Rogers3bb17a62012-01-27 23:56:44 -0800146 << " ) " << *space;
Ian Rogers30fab402012-01-23 15:43:46 -0800147 }
148 return space;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700149}
150
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700151void* DlMallocSpace::CreateMspace(void* begin, size_t morecore_start, size_t initial_size) {
Ian Rogers30fab402012-01-23 15:43:46 -0800152 // clear errno to allow PLOG on error
Carl Shapiro69759ea2011-07-21 18:13:35 -0700153 errno = 0;
Ian Rogers3bb17a62012-01-27 23:56:44 -0800154 // create mspace using our backing storage starting at begin and with a footprint of
155 // morecore_start. Don't use an internal dlmalloc lock (as we already hold heap lock). When
156 // morecore_start bytes of memory is exhaused morecore will be called.
157 void* msp = create_mspace_with_base(begin, morecore_start, false /*locked*/);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800158 if (msp != nullptr) {
Ian Rogers30fab402012-01-23 15:43:46 -0800159 // Do not allow morecore requests to succeed beyond the initial size of the heap
Ian Rogers3bb17a62012-01-27 23:56:44 -0800160 mspace_set_footprint_limit(msp, initial_size);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700161 } else {
Ian Rogers30fab402012-01-23 15:43:46 -0800162 PLOG(ERROR) << "create_mspace_with_base failed";
Carl Shapiro69759ea2011-07-21 18:13:35 -0700163 }
164 return msp;
165}
166
Ian Rogers6fac4472014-02-25 17:01:10 -0800167mirror::Object* DlMallocSpace::AllocWithGrowth(Thread* self, size_t num_bytes,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700168 size_t* bytes_allocated, size_t* usable_size,
169 size_t* bytes_tl_bulk_allocated) {
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700170 mirror::Object* result;
171 {
172 MutexLock mu(self, lock_);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700173 // Grow as much as possible within the space.
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700174 size_t max_allowed = Capacity();
175 mspace_set_footprint_limit(mspace_, max_allowed);
176 // Try the allocation.
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700177 result = AllocWithoutGrowthLocked(self, num_bytes, bytes_allocated, usable_size,
178 bytes_tl_bulk_allocated);
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700179 // Shrink back down as small as possible.
180 size_t footprint = mspace_footprint(mspace_);
181 mspace_set_footprint_limit(mspace_, footprint);
jeffhaoc1160702011-10-27 15:48:45 -0700182 }
Mathieu Chartier661974a2014-01-09 11:23:53 -0800183 if (result != nullptr) {
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700184 // Zero freshly allocated memory, done while not holding the space's lock.
185 memset(result, 0, num_bytes);
Mathieu Chartier661974a2014-01-09 11:23:53 -0800186 // Check that the result is contained in the space.
187 CHECK(!kDebugSpaces || Contains(result));
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700188 }
Ian Rogers30fab402012-01-23 15:43:46 -0800189 return result;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700190}
191
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100192MallocSpace* DlMallocSpace::CreateInstance(MemMap&& mem_map,
193 const std::string& name,
194 void* allocator,
195 uint8_t* begin,
196 uint8_t* end,
197 uint8_t* limit,
198 size_t growth_limit,
Mathieu Chartier31f44142014-04-08 14:40:03 -0700199 bool can_move_objects) {
Evgenii Stepanov1e133742015-05-20 12:30:59 -0700200 if (Runtime::Current()->IsRunningOnMemoryTool()) {
201 return new MemoryToolMallocSpace<DlMallocSpace, kDefaultMemoryToolRedZoneBytes, true, false>(
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100202 std::move(mem_map),
203 initial_size_,
204 name,
205 allocator,
206 begin,
207 end,
208 limit,
209 growth_limit,
210 can_move_objects,
211 starting_size_);
Andreas Gamped7576322014-10-24 22:13:45 -0700212 } else {
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100213 return new DlMallocSpace(std::move(mem_map),
214 initial_size_,
215 name,
216 allocator,
217 begin,
218 end,
219 limit,
220 growth_limit,
221 can_move_objects,
222 starting_size_);
Andreas Gamped7576322014-10-24 22:13:45 -0700223 }
Mathieu Chartier0f72e412013-09-06 16:40:01 -0700224}
225
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800226size_t DlMallocSpace::Free(Thread* self, mirror::Object* ptr) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700227 MutexLock mu(self, lock_);
Mathieu Chartier8e9a1492012-10-04 12:25:40 -0700228 if (kDebugSpaces) {
Mathieu Chartier661974a2014-01-09 11:23:53 -0800229 CHECK(ptr != nullptr);
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700230 CHECK(Contains(ptr)) << "Free (" << ptr << ") not in bounds of heap " << *this;
231 }
Ian Rogers6fac4472014-02-25 17:01:10 -0800232 const size_t bytes_freed = AllocationSizeNonvirtual(ptr, nullptr);
Mathieu Chartier0f72e412013-09-06 16:40:01 -0700233 if (kRecentFreeCount > 0) {
234 RegisterRecentFree(ptr);
235 }
Ian Rogers30fab402012-01-23 15:43:46 -0800236 mspace_free(mspace_, ptr);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700237 return bytes_freed;
Ian Rogers30fab402012-01-23 15:43:46 -0800238}
239
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800240size_t DlMallocSpace::FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700241 DCHECK(ptrs != nullptr);
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800242
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700243 // Don't need the lock to calculate the size of the freed pointers.
244 size_t bytes_freed = 0;
245 for (size_t i = 0; i < num_ptrs; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800246 mirror::Object* ptr = ptrs[i];
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800247 const size_t look_ahead = 8;
248 if (kPrefetchDuringDlMallocFreeList && i + look_ahead < num_ptrs) {
249 // The head of chunk for the allocation is sizeof(size_t) behind the allocation.
250 __builtin_prefetch(reinterpret_cast<char*>(ptrs[i + look_ahead]) - sizeof(size_t));
251 }
Ian Rogers6fac4472014-02-25 17:01:10 -0800252 bytes_freed += AllocationSizeNonvirtual(ptr, nullptr);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700253 }
254
Mathieu Chartier0f72e412013-09-06 16:40:01 -0700255 if (kRecentFreeCount > 0) {
256 MutexLock mu(self, lock_);
257 for (size_t i = 0; i < num_ptrs; i++) {
258 RegisterRecentFree(ptrs[i]);
259 }
260 }
261
Mathieu Chartier8e9a1492012-10-04 12:25:40 -0700262 if (kDebugSpaces) {
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700263 size_t num_broken_ptrs = 0;
264 for (size_t i = 0; i < num_ptrs; i++) {
265 if (!Contains(ptrs[i])) {
266 num_broken_ptrs++;
267 LOG(ERROR) << "FreeList[" << i << "] (" << ptrs[i] << ") not in bounds of heap " << *this;
268 } else {
269 size_t size = mspace_usable_size(ptrs[i]);
270 memset(ptrs[i], 0xEF, size);
271 }
Ian Rogers30fab402012-01-23 15:43:46 -0800272 }
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700273 CHECK_EQ(num_broken_ptrs, 0u);
Ian Rogers30fab402012-01-23 15:43:46 -0800274 }
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800275
276 {
277 MutexLock mu(self, lock_);
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800278 mspace_bulk_free(mspace_, reinterpret_cast<void**>(ptrs), num_ptrs);
279 return bytes_freed;
280 }
Ian Rogers30fab402012-01-23 15:43:46 -0800281}
282
Ian Rogers48931882013-01-22 14:35:16 -0800283size_t DlMallocSpace::Trim() {
Ian Rogers50b35e22012-10-04 10:09:15 -0700284 MutexLock mu(Thread::Current(), lock_);
Elliott Hughes9eebd3b2012-06-08 13:56:31 -0700285 // Trim to release memory at the end of the space.
286 mspace_trim(mspace_, 0);
287 // Visit space looking for page-sized holes to advise the kernel we don't need.
Ian Rogers48931882013-01-22 14:35:16 -0800288 size_t reclaimed = 0;
Ian Rogers1d54e732013-05-02 21:10:01 -0700289 mspace_inspect_all(mspace_, DlmallocMadviseCallback, &reclaimed);
Ian Rogers48931882013-01-22 14:35:16 -0800290 return reclaimed;
Elliott Hughes9eebd3b2012-06-08 13:56:31 -0700291}
Ian Rogers30fab402012-01-23 15:43:46 -0800292
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700293void DlMallocSpace::Walk(void(*callback)(void *start, void *end, size_t num_bytes, void* callback_arg),
Ian Rogers30fab402012-01-23 15:43:46 -0800294 void* arg) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700295 MutexLock mu(Thread::Current(), lock_);
Ian Rogers30fab402012-01-23 15:43:46 -0800296 mspace_inspect_all(mspace_, callback, arg);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700297 callback(nullptr, nullptr, 0, arg); // Indicate end of a space.
Ian Rogers30fab402012-01-23 15:43:46 -0800298}
299
Hiroshi Yamauchi09b07a92013-07-15 13:17:06 -0700300size_t DlMallocSpace::GetFootprint() {
301 MutexLock mu(Thread::Current(), lock_);
302 return mspace_footprint(mspace_);
303}
304
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700305size_t DlMallocSpace::GetFootprintLimit() {
Ian Rogers50b35e22012-10-04 10:09:15 -0700306 MutexLock mu(Thread::Current(), lock_);
Ian Rogers30fab402012-01-23 15:43:46 -0800307 return mspace_footprint_limit(mspace_);
308}
309
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700310void DlMallocSpace::SetFootprintLimit(size_t new_size) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700311 MutexLock mu(Thread::Current(), lock_);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700312 VLOG(heap) << "DlMallocSpace::SetFootprintLimit " << PrettySize(new_size);
Ian Rogers30fab402012-01-23 15:43:46 -0800313 // Compare against the actual footprint, rather than the Size(), because the heap may not have
314 // grown all the way to the allowed size yet.
Ian Rogers30fab402012-01-23 15:43:46 -0800315 size_t current_space_size = mspace_footprint(mspace_);
316 if (new_size < current_space_size) {
317 // Don't let the space grow any more.
318 new_size = current_space_size;
319 }
320 mspace_set_footprint_limit(mspace_, new_size);
321}
322
Hiroshi Yamauchibe031ff2013-10-08 16:42:37 -0700323uint64_t DlMallocSpace::GetBytesAllocated() {
Hiroshi Yamauchi4ce1f002013-11-18 14:49:09 -0800324 MutexLock mu(Thread::Current(), lock_);
325 size_t bytes_allocated = 0;
326 mspace_inspect_all(mspace_, DlmallocBytesAllocatedCallback, &bytes_allocated);
327 return bytes_allocated;
Hiroshi Yamauchibe031ff2013-10-08 16:42:37 -0700328}
329
330uint64_t DlMallocSpace::GetObjectsAllocated() {
Hiroshi Yamauchi4ce1f002013-11-18 14:49:09 -0800331 MutexLock mu(Thread::Current(), lock_);
332 size_t objects_allocated = 0;
333 mspace_inspect_all(mspace_, DlmallocObjectsAllocatedCallback, &objects_allocated);
334 return objects_allocated;
Hiroshi Yamauchibe031ff2013-10-08 16:42:37 -0700335}
336
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800337void DlMallocSpace::Clear() {
Mathieu Chartier31f44142014-04-08 14:40:03 -0700338 size_t footprint_limit = GetFootprintLimit();
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800339 madvise(GetMemMap()->Begin(), GetMemMap()->Size(), MADV_DONTNEED);
Mathieu Chartier31f44142014-04-08 14:40:03 -0700340 live_bitmap_->Clear();
341 mark_bitmap_->Clear();
Ian Rogersbe2a1df2014-07-10 00:56:36 -0700342 SetEnd(Begin() + starting_size_);
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100343 mspace_ = CreateMspace(mem_map_.Begin(), starting_size_, initial_size_);
Mathieu Chartier31f44142014-04-08 14:40:03 -0700344 SetFootprintLimit(footprint_limit);
Mathieu Chartier15d34022014-02-26 17:16:38 -0800345}
346
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700347#ifndef NDEBUG
348void DlMallocSpace::CheckMoreCoreForPrecondition() {
349 lock_.AssertHeld(Thread::Current());
350}
351#endif
352
Hiroshi Yamauchi654dd482014-07-09 12:54:32 -0700353static void MSpaceChunkCallback(void* start, void* end, size_t used_bytes, void* arg) {
354 size_t chunk_size = reinterpret_cast<uint8_t*>(end) - reinterpret_cast<uint8_t*>(start);
355 if (used_bytes < chunk_size) {
356 size_t chunk_free_bytes = chunk_size - used_bytes;
357 size_t& max_contiguous_allocation = *reinterpret_cast<size_t*>(arg);
358 max_contiguous_allocation = std::max(max_contiguous_allocation, chunk_free_bytes);
359 }
360}
361
Mathieu Chartierf1d666e2015-09-03 16:13:34 -0700362void DlMallocSpace::LogFragmentationAllocFailure(std::ostream& os,
363 size_t failed_alloc_bytes ATTRIBUTE_UNUSED) {
364 Thread* const self = Thread::Current();
Hiroshi Yamauchi654dd482014-07-09 12:54:32 -0700365 size_t max_contiguous_allocation = 0;
366 // To allow the Walk/InspectAll() to exclusively-lock the mutator
367 // lock, temporarily release the shared access to the mutator
368 // lock here by transitioning to the suspended state.
369 Locks::mutator_lock_->AssertSharedHeld(self);
Mathieu Chartierf1d666e2015-09-03 16:13:34 -0700370 ScopedThreadSuspension sts(self, kSuspended);
Hiroshi Yamauchi654dd482014-07-09 12:54:32 -0700371 Walk(MSpaceChunkCallback, &max_contiguous_allocation);
Hiroshi Yamauchi654dd482014-07-09 12:54:32 -0700372 os << "; failed due to fragmentation (largest possible contiguous allocation "
373 << max_contiguous_allocation << " bytes)";
374}
375
Ian Rogers1d54e732013-05-02 21:10:01 -0700376} // namespace space
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800377
378namespace allocator {
379
380// Implement the dlmalloc morecore callback.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700381void* ArtDlMallocMoreCore(void* mspace, intptr_t increment) REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000382 Runtime* runtime = Runtime::Current();
383 Heap* heap = runtime->GetHeap();
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800384 ::art::gc::space::DlMallocSpace* dlmalloc_space = heap->GetDlMallocSpace();
385 // Support for multiple DlMalloc provided by a slow path.
386 if (UNLIKELY(dlmalloc_space == nullptr || dlmalloc_space->GetMspace() != mspace)) {
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000387 if (LIKELY(runtime->GetJit() != nullptr)) {
388 jit::JitCodeCache* code_cache = runtime->GetJit()->GetCodeCache();
389 if (code_cache->OwnsSpace(mspace)) {
390 return code_cache->MoreCore(mspace, increment);
391 }
392 }
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800393 dlmalloc_space = nullptr;
394 for (space::ContinuousSpace* space : heap->GetContinuousSpaces()) {
395 if (space->IsDlMallocSpace()) {
396 ::art::gc::space::DlMallocSpace* cur_dlmalloc_space = space->AsDlMallocSpace();
397 if (cur_dlmalloc_space->GetMspace() == mspace) {
398 dlmalloc_space = cur_dlmalloc_space;
399 break;
400 }
401 }
402 }
403 CHECK(dlmalloc_space != nullptr) << "Couldn't find DlmMallocSpace with mspace=" << mspace;
404 }
405 return dlmalloc_space->MoreCore(increment);
406}
407
408} // namespace allocator
409
Ian Rogers1d54e732013-05-02 21:10:01 -0700410} // namespace gc
Carl Shapiro69759ea2011-07-21 18:13:35 -0700411} // namespace art