Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 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 Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 16 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 17 | #include "dlmalloc_space.h" |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 18 | |
Hiroshi Yamauchi | 50b2928 | 2013-07-30 13:58:37 -0700 | [diff] [blame] | 19 | #include "dlmalloc_space-inl.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 20 | #include "gc/accounting/card_table.h" |
| 21 | #include "gc/heap.h" |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 22 | #include "mirror/class-inl.h" |
Mathieu Chartier | 0f72e41 | 2013-09-06 16:40:01 -0700 | [diff] [blame] | 23 | #include "mirror/object-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 24 | #include "runtime.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 25 | #include "thread.h" |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 26 | #include "thread_list.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 27 | #include "utils.h" |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 28 | #include "valgrind_malloc_space-inl.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 29 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 30 | namespace art { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 31 | namespace gc { |
| 32 | namespace space { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 33 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 34 | static constexpr bool kPrefetchDuringDlMallocFreeList = true; |
| 35 | |
| 36 | template class ValgrindMallocSpace<DlMallocSpace, void*>; |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 37 | |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 38 | DlMallocSpace::DlMallocSpace(const std::string& name, MemMap* mem_map, void* mspace, byte* begin, |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 39 | byte* end, byte* limit, size_t growth_limit) |
| 40 | : MallocSpace(name, mem_map, begin, end, limit, growth_limit), |
Hiroshi Yamauchi | e48780b | 2013-12-17 17:19:53 -0800 | [diff] [blame] | 41 | mspace_(mspace), mspace_for_alloc_(mspace) { |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 42 | CHECK(mspace != NULL); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 45 | DlMallocSpace* DlMallocSpace::CreateFromMemMap(MemMap* mem_map, const std::string& name, |
| 46 | size_t starting_size, |
| 47 | size_t initial_size, size_t growth_limit, |
| 48 | size_t capacity) { |
| 49 | DCHECK(mem_map != nullptr); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 50 | void* mspace = CreateMspace(mem_map->Begin(), starting_size, initial_size); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 51 | if (mspace == nullptr) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 52 | LOG(ERROR) << "Failed to initialize mspace for alloc space (" << name << ")"; |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 53 | return nullptr; |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 54 | } |
| 55 | |
lzang1 | 385de73 | 2014-02-21 14:15:01 +0800 | [diff] [blame] | 56 | // Protect memory beyond the starting size. morecore will add r/w permissions when necessory |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 57 | byte* end = mem_map->Begin() + starting_size; |
lzang1 | 385de73 | 2014-02-21 14:15:01 +0800 | [diff] [blame] | 58 | if (capacity - starting_size > 0) { |
| 59 | CHECK_MEMORY_CALL(mprotect, (end, capacity - starting_size, PROT_NONE), name); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | // Everything is set so record in immutable structure and leave |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 63 | byte* begin = mem_map->Begin(); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 64 | if (RUNNING_ON_VALGRIND > 0) { |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 65 | return new ValgrindMallocSpace<DlMallocSpace, void*>( |
Hiroshi Yamauchi | 7cb7bbc | 2013-11-18 17:27:37 -0800 | [diff] [blame] | 66 | name, mem_map, mspace, begin, end, begin + capacity, growth_limit, initial_size); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 67 | } else { |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 68 | return new DlMallocSpace(name, mem_map, mspace, begin, end, begin + capacity, growth_limit); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 69 | } |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | DlMallocSpace* DlMallocSpace::Create(const std::string& name, size_t initial_size, size_t growth_limit, |
| 73 | size_t capacity, byte* requested_begin) { |
| 74 | uint64_t start_time = 0; |
| 75 | if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) { |
| 76 | start_time = NanoTime(); |
| 77 | LOG(INFO) << "DlMallocSpace::Create entering " << name |
| 78 | << " initial_size=" << PrettySize(initial_size) |
| 79 | << " growth_limit=" << PrettySize(growth_limit) |
| 80 | << " capacity=" << PrettySize(capacity) |
| 81 | << " requested_begin=" << reinterpret_cast<void*>(requested_begin); |
| 82 | } |
| 83 | |
| 84 | // Memory we promise to dlmalloc before it asks for morecore. |
| 85 | // Note: making this value large means that large allocations are unlikely to succeed as dlmalloc |
| 86 | // will ask for this memory from sys_alloc which will fail as the footprint (this value plus the |
| 87 | // size of the large allocation) will be greater than the footprint limit. |
| 88 | size_t starting_size = kPageSize; |
| 89 | MemMap* mem_map = CreateMemMap(name, starting_size, &initial_size, &growth_limit, &capacity, |
| 90 | requested_begin); |
| 91 | if (mem_map == nullptr) { |
| 92 | LOG(ERROR) << "Failed to create mem map for alloc space (" << name << ") of size " |
| 93 | << PrettySize(capacity); |
| 94 | return nullptr; |
| 95 | } |
| 96 | DlMallocSpace* space = CreateFromMemMap(mem_map, name, starting_size, initial_size, |
| 97 | growth_limit, capacity); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 98 | // We start out with only the initial size possibly containing objects. |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 99 | if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) { |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 100 | LOG(INFO) << "DlMallocSpace::Create exiting (" << PrettyDuration(NanoTime() - start_time) |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 101 | << " ) " << *space; |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 102 | } |
| 103 | return space; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 106 | void* DlMallocSpace::CreateMspace(void* begin, size_t morecore_start, size_t initial_size) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 107 | // clear errno to allow PLOG on error |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 108 | errno = 0; |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 109 | // create mspace using our backing storage starting at begin and with a footprint of |
| 110 | // morecore_start. Don't use an internal dlmalloc lock (as we already hold heap lock). When |
| 111 | // morecore_start bytes of memory is exhaused morecore will be called. |
| 112 | void* msp = create_mspace_with_base(begin, morecore_start, false /*locked*/); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 113 | if (msp != nullptr) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 114 | // Do not allow morecore requests to succeed beyond the initial size of the heap |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 115 | mspace_set_footprint_limit(msp, initial_size); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 116 | } else { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 117 | PLOG(ERROR) << "create_mspace_with_base failed"; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 118 | } |
| 119 | return msp; |
| 120 | } |
| 121 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 122 | mirror::Object* DlMallocSpace::AllocWithGrowth(Thread* self, size_t num_bytes, |
| 123 | size_t* bytes_allocated, size_t* usable_size) { |
Hiroshi Yamauchi | 50b2928 | 2013-07-30 13:58:37 -0700 | [diff] [blame] | 124 | mirror::Object* result; |
| 125 | { |
| 126 | MutexLock mu(self, lock_); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 127 | // Grow as much as possible within the space. |
Hiroshi Yamauchi | 50b2928 | 2013-07-30 13:58:37 -0700 | [diff] [blame] | 128 | size_t max_allowed = Capacity(); |
| 129 | mspace_set_footprint_limit(mspace_, max_allowed); |
| 130 | // Try the allocation. |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 131 | result = AllocWithoutGrowthLocked(self, num_bytes, bytes_allocated, usable_size); |
Hiroshi Yamauchi | 50b2928 | 2013-07-30 13:58:37 -0700 | [diff] [blame] | 132 | // Shrink back down as small as possible. |
| 133 | size_t footprint = mspace_footprint(mspace_); |
| 134 | mspace_set_footprint_limit(mspace_, footprint); |
jeffhao | c116070 | 2011-10-27 15:48:45 -0700 | [diff] [blame] | 135 | } |
Hiroshi Yamauchi | 50b2928 | 2013-07-30 13:58:37 -0700 | [diff] [blame] | 136 | if (result != NULL) { |
| 137 | // Zero freshly allocated memory, done while not holding the space's lock. |
| 138 | memset(result, 0, num_bytes); |
| 139 | } |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 140 | // Return the new allocation or NULL. |
Mathieu Chartier | 8e9a149 | 2012-10-04 12:25:40 -0700 | [diff] [blame] | 141 | CHECK(!kDebugSpaces || result == NULL || Contains(result)); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 142 | return result; |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 145 | MallocSpace* DlMallocSpace::CreateInstance(const std::string& name, MemMap* mem_map, |
| 146 | void* allocator, byte* begin, byte* end, |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 147 | byte* limit, size_t growth_limit) { |
| 148 | return new DlMallocSpace(name, mem_map, allocator, begin, end, limit, growth_limit); |
Mathieu Chartier | 0f72e41 | 2013-09-06 16:40:01 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 151 | size_t DlMallocSpace::Free(Thread* self, mirror::Object* ptr) { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 152 | MutexLock mu(self, lock_); |
Mathieu Chartier | 8e9a149 | 2012-10-04 12:25:40 -0700 | [diff] [blame] | 153 | if (kDebugSpaces) { |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 154 | CHECK(ptr != NULL); |
| 155 | CHECK(Contains(ptr)) << "Free (" << ptr << ") not in bounds of heap " << *this; |
| 156 | } |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 157 | const size_t bytes_freed = AllocationSizeNonvirtual(ptr, nullptr); |
Mathieu Chartier | 0f72e41 | 2013-09-06 16:40:01 -0700 | [diff] [blame] | 158 | if (kRecentFreeCount > 0) { |
| 159 | RegisterRecentFree(ptr); |
| 160 | } |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 161 | mspace_free(mspace_, ptr); |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 162 | return bytes_freed; |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 163 | } |
| 164 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 165 | size_t DlMallocSpace::FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs) { |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 166 | DCHECK(ptrs != NULL); |
| 167 | |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 168 | // Don't need the lock to calculate the size of the freed pointers. |
| 169 | size_t bytes_freed = 0; |
| 170 | for (size_t i = 0; i < num_ptrs; i++) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 171 | mirror::Object* ptr = ptrs[i]; |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 172 | const size_t look_ahead = 8; |
| 173 | if (kPrefetchDuringDlMallocFreeList && i + look_ahead < num_ptrs) { |
| 174 | // The head of chunk for the allocation is sizeof(size_t) behind the allocation. |
| 175 | __builtin_prefetch(reinterpret_cast<char*>(ptrs[i + look_ahead]) - sizeof(size_t)); |
| 176 | } |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 177 | bytes_freed += AllocationSizeNonvirtual(ptr, nullptr); |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Mathieu Chartier | 0f72e41 | 2013-09-06 16:40:01 -0700 | [diff] [blame] | 180 | if (kRecentFreeCount > 0) { |
| 181 | MutexLock mu(self, lock_); |
| 182 | for (size_t i = 0; i < num_ptrs; i++) { |
| 183 | RegisterRecentFree(ptrs[i]); |
| 184 | } |
| 185 | } |
| 186 | |
Mathieu Chartier | 8e9a149 | 2012-10-04 12:25:40 -0700 | [diff] [blame] | 187 | if (kDebugSpaces) { |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 188 | size_t num_broken_ptrs = 0; |
| 189 | for (size_t i = 0; i < num_ptrs; i++) { |
| 190 | if (!Contains(ptrs[i])) { |
| 191 | num_broken_ptrs++; |
| 192 | LOG(ERROR) << "FreeList[" << i << "] (" << ptrs[i] << ") not in bounds of heap " << *this; |
| 193 | } else { |
| 194 | size_t size = mspace_usable_size(ptrs[i]); |
| 195 | memset(ptrs[i], 0xEF, size); |
| 196 | } |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 197 | } |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 198 | CHECK_EQ(num_broken_ptrs, 0u); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 199 | } |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 200 | |
| 201 | { |
| 202 | MutexLock mu(self, lock_); |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 203 | mspace_bulk_free(mspace_, reinterpret_cast<void**>(ptrs), num_ptrs); |
| 204 | return bytes_freed; |
| 205 | } |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | // Callback from dlmalloc when it needs to increase the footprint |
| 209 | extern "C" void* art_heap_morecore(void* mspace, intptr_t increment) { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 210 | Heap* heap = Runtime::Current()->GetHeap(); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 211 | DlMallocSpace* dlmalloc_space = heap->GetDlMallocSpace(); |
| 212 | // Support for multiple DlMalloc provided by a slow path. |
| 213 | if (UNLIKELY(dlmalloc_space == nullptr || dlmalloc_space->GetMspace() != mspace)) { |
| 214 | dlmalloc_space = nullptr; |
| 215 | for (space::ContinuousSpace* space : heap->GetContinuousSpaces()) { |
| 216 | if (space->IsDlMallocSpace()) { |
| 217 | DlMallocSpace* cur_dlmalloc_space = space->AsDlMallocSpace(); |
| 218 | if (cur_dlmalloc_space->GetMspace() == mspace) { |
| 219 | dlmalloc_space = cur_dlmalloc_space; |
| 220 | break; |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | CHECK(dlmalloc_space != nullptr) << "Couldn't find DlmMallocSpace with mspace=" << mspace; |
| 225 | } |
| 226 | return dlmalloc_space->MoreCore(increment); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 227 | } |
| 228 | |
Ian Rogers | 4893188 | 2013-01-22 14:35:16 -0800 | [diff] [blame] | 229 | size_t DlMallocSpace::Trim() { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 230 | MutexLock mu(Thread::Current(), lock_); |
Elliott Hughes | 9eebd3b | 2012-06-08 13:56:31 -0700 | [diff] [blame] | 231 | // Trim to release memory at the end of the space. |
| 232 | mspace_trim(mspace_, 0); |
| 233 | // Visit space looking for page-sized holes to advise the kernel we don't need. |
Ian Rogers | 4893188 | 2013-01-22 14:35:16 -0800 | [diff] [blame] | 234 | size_t reclaimed = 0; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 235 | mspace_inspect_all(mspace_, DlmallocMadviseCallback, &reclaimed); |
Ian Rogers | 4893188 | 2013-01-22 14:35:16 -0800 | [diff] [blame] | 236 | return reclaimed; |
Elliott Hughes | 9eebd3b | 2012-06-08 13:56:31 -0700 | [diff] [blame] | 237 | } |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 238 | |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 239 | void DlMallocSpace::Walk(void(*callback)(void *start, void *end, size_t num_bytes, void* callback_arg), |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 240 | void* arg) { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 241 | MutexLock mu(Thread::Current(), lock_); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 242 | mspace_inspect_all(mspace_, callback, arg); |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 243 | callback(NULL, NULL, 0, arg); // Indicate end of a space. |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 244 | } |
| 245 | |
Hiroshi Yamauchi | 09b07a9 | 2013-07-15 13:17:06 -0700 | [diff] [blame] | 246 | size_t DlMallocSpace::GetFootprint() { |
| 247 | MutexLock mu(Thread::Current(), lock_); |
| 248 | return mspace_footprint(mspace_); |
| 249 | } |
| 250 | |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 251 | size_t DlMallocSpace::GetFootprintLimit() { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 252 | MutexLock mu(Thread::Current(), lock_); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 253 | return mspace_footprint_limit(mspace_); |
| 254 | } |
| 255 | |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 256 | void DlMallocSpace::SetFootprintLimit(size_t new_size) { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 257 | MutexLock mu(Thread::Current(), lock_); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 258 | VLOG(heap) << "DlMallocSpace::SetFootprintLimit " << PrettySize(new_size); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 259 | // Compare against the actual footprint, rather than the Size(), because the heap may not have |
| 260 | // grown all the way to the allowed size yet. |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 261 | size_t current_space_size = mspace_footprint(mspace_); |
| 262 | if (new_size < current_space_size) { |
| 263 | // Don't let the space grow any more. |
| 264 | new_size = current_space_size; |
| 265 | } |
| 266 | mspace_set_footprint_limit(mspace_, new_size); |
| 267 | } |
| 268 | |
Hiroshi Yamauchi | be031ff | 2013-10-08 16:42:37 -0700 | [diff] [blame] | 269 | uint64_t DlMallocSpace::GetBytesAllocated() { |
Hiroshi Yamauchi | 4ce1f00 | 2013-11-18 14:49:09 -0800 | [diff] [blame] | 270 | MutexLock mu(Thread::Current(), lock_); |
| 271 | size_t bytes_allocated = 0; |
| 272 | mspace_inspect_all(mspace_, DlmallocBytesAllocatedCallback, &bytes_allocated); |
| 273 | return bytes_allocated; |
Hiroshi Yamauchi | be031ff | 2013-10-08 16:42:37 -0700 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | uint64_t DlMallocSpace::GetObjectsAllocated() { |
Hiroshi Yamauchi | 4ce1f00 | 2013-11-18 14:49:09 -0800 | [diff] [blame] | 277 | MutexLock mu(Thread::Current(), lock_); |
| 278 | size_t objects_allocated = 0; |
| 279 | mspace_inspect_all(mspace_, DlmallocObjectsAllocatedCallback, &objects_allocated); |
| 280 | return objects_allocated; |
Hiroshi Yamauchi | be031ff | 2013-10-08 16:42:37 -0700 | [diff] [blame] | 281 | } |
| 282 | |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 283 | void DlMallocSpace::Clear() { |
| 284 | madvise(GetMemMap()->Begin(), GetMemMap()->Size(), MADV_DONTNEED); |
| 285 | GetLiveBitmap()->Clear(); |
| 286 | GetMarkBitmap()->Clear(); |
| 287 | } |
| 288 | |
Mathieu Chartier | 15d3402 | 2014-02-26 17:16:38 -0800 | [diff] [blame^] | 289 | void DlMallocSpace::Reset() { |
| 290 | // TODO: Delete and create new mspace here. |
| 291 | } |
| 292 | |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 293 | #ifndef NDEBUG |
| 294 | void DlMallocSpace::CheckMoreCoreForPrecondition() { |
| 295 | lock_.AssertHeld(Thread::Current()); |
| 296 | } |
| 297 | #endif |
| 298 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 299 | } // namespace space |
| 300 | } // namespace gc |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 301 | } // namespace art |