| Hiroshi Yamauchi | 7cb7bbc | 2013-11-18 17:27:37 -0800 | [diff] [blame] | 1 | |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2013 The Android Open Source Project |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 18 | #include "rosalloc_space-inl.h" |
| Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 19 | |
| Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 20 | #include "base/time_utils.h" |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 21 | #include "gc/accounting/card_table.h" |
| Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 22 | #include "gc/accounting/space_bitmap-inl.h" |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 23 | #include "gc/heap.h" |
| Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame^] | 24 | #include "memory_tool_malloc_space-inl.h" |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 25 | #include "mirror/class-inl.h" |
| 26 | #include "mirror/object-inl.h" |
| 27 | #include "runtime.h" |
| Andreas Gampe | 508fdf3 | 2017-06-05 16:42:13 -0700 | [diff] [blame] | 28 | #include "scoped_thread_state_change-inl.h" |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 29 | #include "thread.h" |
| 30 | #include "thread_list.h" |
| 31 | #include "utils.h" |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 32 | |
| 33 | namespace art { |
| 34 | namespace gc { |
| 35 | namespace space { |
| 36 | |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 37 | static constexpr bool kPrefetchDuringRosAllocFreeList = false; |
| Mathieu Chartier | 8585bad | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 38 | static constexpr size_t kPrefetchLookAhead = 8; |
| 39 | // Use this only for verification, it is not safe to use since the class of the object may have |
| 40 | // been freed. |
| 41 | static constexpr bool kVerifyFreedBytes = false; |
| Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 42 | |
| Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 43 | // TODO: Fix |
| Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 44 | // template class MemoryToolMallocSpace<RosAllocSpace, allocator::RosAlloc*>; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 45 | |
| Andreas Gampe | d757632 | 2014-10-24 22:13:45 -0700 | [diff] [blame] | 46 | RosAllocSpace::RosAllocSpace(MemMap* mem_map, size_t initial_size, const std::string& name, |
| Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 47 | art::gc::allocator::RosAlloc* rosalloc, uint8_t* begin, uint8_t* end, |
| 48 | uint8_t* limit, size_t growth_limit, bool can_move_objects, |
| Andreas Gampe | d757632 | 2014-10-24 22:13:45 -0700 | [diff] [blame] | 49 | size_t starting_size, bool low_memory_mode) |
| Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 50 | : MallocSpace(name, mem_map, begin, end, limit, growth_limit, true, can_move_objects, |
| 51 | starting_size, initial_size), |
| 52 | rosalloc_(rosalloc), low_memory_mode_(low_memory_mode) { |
| 53 | CHECK(rosalloc != nullptr); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 56 | RosAllocSpace* RosAllocSpace::CreateFromMemMap(MemMap* mem_map, const std::string& name, |
| Ian Rogers | a55cf41 | 2014-02-27 00:31:26 -0800 | [diff] [blame] | 57 | size_t starting_size, size_t initial_size, |
| 58 | size_t growth_limit, size_t capacity, |
| Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 59 | bool low_memory_mode, bool can_move_objects) { |
| Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 60 | DCHECK(mem_map != nullptr); |
| Andreas Gampe | d757632 | 2014-10-24 22:13:45 -0700 | [diff] [blame] | 61 | |
| Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 62 | bool running_on_memory_tool = Runtime::Current()->IsRunningOnMemoryTool(); |
| Andreas Gampe | d757632 | 2014-10-24 22:13:45 -0700 | [diff] [blame] | 63 | |
| Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 64 | allocator::RosAlloc* rosalloc = CreateRosAlloc(mem_map->Begin(), starting_size, initial_size, |
| Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 65 | capacity, low_memory_mode, running_on_memory_tool); |
| Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 66 | if (rosalloc == nullptr) { |
| Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 67 | LOG(ERROR) << "Failed to initialize rosalloc for alloc space (" << name << ")"; |
| Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 68 | return nullptr; |
| Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 69 | } |
| 70 | |
| lzang1 | 385de73 | 2014-02-21 14:15:01 +0800 | [diff] [blame] | 71 | // Protect memory beyond the starting size. MoreCore will add r/w permissions when necessory |
| Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 72 | uint8_t* end = mem_map->Begin() + starting_size; |
| lzang1 | 385de73 | 2014-02-21 14:15:01 +0800 | [diff] [blame] | 73 | if (capacity - starting_size > 0) { |
| 74 | CHECK_MEMORY_CALL(mprotect, (end, capacity - starting_size, PROT_NONE), name); |
| Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | // Everything is set so record in immutable structure and leave |
| Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 78 | uint8_t* begin = mem_map->Begin(); |
| Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 79 | // TODO: Fix RosAllocSpace to support Valgrind/ASan. There is currently some issues with |
| Mathieu Chartier | 661974a | 2014-01-09 11:23:53 -0800 | [diff] [blame] | 80 | // AllocationSize caused by redzones. b/12944686 |
| Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 81 | if (running_on_memory_tool) { |
| 82 | return new MemoryToolMallocSpace<RosAllocSpace, kDefaultMemoryToolRedZoneBytes, false, true>( |
| Andreas Gampe | d757632 | 2014-10-24 22:13:45 -0700 | [diff] [blame] | 83 | mem_map, initial_size, name, rosalloc, begin, end, begin + capacity, growth_limit, |
| 84 | can_move_objects, starting_size, low_memory_mode); |
| Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 85 | } else { |
| Andreas Gampe | d757632 | 2014-10-24 22:13:45 -0700 | [diff] [blame] | 86 | return new RosAllocSpace(mem_map, initial_size, name, rosalloc, begin, end, begin + capacity, |
| 87 | growth_limit, can_move_objects, starting_size, low_memory_mode); |
| Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 88 | } |
| Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| Mathieu Chartier | 661974a | 2014-01-09 11:23:53 -0800 | [diff] [blame] | 91 | RosAllocSpace::~RosAllocSpace() { |
| 92 | delete rosalloc_; |
| 93 | } |
| 94 | |
| Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 95 | RosAllocSpace* RosAllocSpace::Create(const std::string& name, size_t initial_size, |
| Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 96 | size_t growth_limit, size_t capacity, uint8_t* requested_begin, |
| Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 97 | bool low_memory_mode, bool can_move_objects) { |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 98 | uint64_t start_time = 0; |
| 99 | if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) { |
| 100 | start_time = NanoTime(); |
| 101 | VLOG(startup) << "RosAllocSpace::Create entering " << name |
| 102 | << " initial_size=" << PrettySize(initial_size) |
| 103 | << " growth_limit=" << PrettySize(growth_limit) |
| 104 | << " capacity=" << PrettySize(capacity) |
| 105 | << " requested_begin=" << reinterpret_cast<void*>(requested_begin); |
| 106 | } |
| 107 | |
| 108 | // Memory we promise to rosalloc before it asks for morecore. |
| 109 | // Note: making this value large means that large allocations are unlikely to succeed as rosalloc |
| 110 | // will ask for this memory from sys_alloc which will fail as the footprint (this value plus the |
| 111 | // size of the large allocation) will be greater than the footprint limit. |
| Hiroshi Yamauchi | 5ccd498 | 2014-03-11 12:19:04 -0700 | [diff] [blame] | 112 | size_t starting_size = Heap::kDefaultStartingSize; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 113 | MemMap* mem_map = CreateMemMap(name, starting_size, &initial_size, &growth_limit, &capacity, |
| 114 | requested_begin); |
| Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 115 | if (mem_map == nullptr) { |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 116 | LOG(ERROR) << "Failed to create mem map for alloc space (" << name << ") of size " |
| 117 | << PrettySize(capacity); |
| Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 118 | return nullptr; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 119 | } |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 120 | |
| Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 121 | RosAllocSpace* space = CreateFromMemMap(mem_map, name, starting_size, initial_size, |
| Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 122 | growth_limit, capacity, low_memory_mode, |
| 123 | can_move_objects); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 124 | // We start out with only the initial size possibly containing objects. |
| 125 | if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) { |
| 126 | LOG(INFO) << "RosAllocSpace::Create exiting (" << PrettyDuration(NanoTime() - start_time) |
| 127 | << " ) " << *space; |
| 128 | } |
| 129 | return space; |
| 130 | } |
| 131 | |
| Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 132 | allocator::RosAlloc* RosAllocSpace::CreateRosAlloc(void* begin, size_t morecore_start, |
| 133 | size_t initial_size, |
| Andreas Gampe | d757632 | 2014-10-24 22:13:45 -0700 | [diff] [blame] | 134 | size_t maximum_size, bool low_memory_mode, |
| Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 135 | bool running_on_memory_tool) { |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 136 | // clear errno to allow PLOG on error |
| 137 | errno = 0; |
| 138 | // create rosalloc using our backing storage starting at begin and |
| 139 | // with a footprint of morecore_start. When morecore_start bytes of |
| 140 | // memory is exhaused morecore will be called. |
| Hiroshi Yamauchi | 573f7d2 | 2013-12-17 11:54:23 -0800 | [diff] [blame] | 141 | allocator::RosAlloc* rosalloc = new art::gc::allocator::RosAlloc( |
| Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 142 | begin, morecore_start, maximum_size, |
| Hiroshi Yamauchi | 573f7d2 | 2013-12-17 11:54:23 -0800 | [diff] [blame] | 143 | low_memory_mode ? |
| 144 | art::gc::allocator::RosAlloc::kPageReleaseModeAll : |
| Andreas Gampe | d757632 | 2014-10-24 22:13:45 -0700 | [diff] [blame] | 145 | art::gc::allocator::RosAlloc::kPageReleaseModeSizeAndEnd, |
| Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 146 | running_on_memory_tool); |
| Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 147 | if (rosalloc != nullptr) { |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 148 | rosalloc->SetFootprintLimit(initial_size); |
| 149 | } else { |
| 150 | PLOG(ERROR) << "RosAlloc::Create failed"; |
| Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 151 | } |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 152 | return rosalloc; |
| 153 | } |
| 154 | |
| Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 155 | mirror::Object* RosAllocSpace::AllocWithGrowth(Thread* self, size_t num_bytes, |
| Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 156 | size_t* bytes_allocated, size_t* usable_size, |
| 157 | size_t* bytes_tl_bulk_allocated) { |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 158 | mirror::Object* result; |
| 159 | { |
| 160 | MutexLock mu(self, lock_); |
| 161 | // Grow as much as possible within the space. |
| 162 | size_t max_allowed = Capacity(); |
| 163 | rosalloc_->SetFootprintLimit(max_allowed); |
| 164 | // Try the allocation. |
| Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 165 | result = AllocCommon(self, num_bytes, bytes_allocated, usable_size, |
| 166 | bytes_tl_bulk_allocated); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 167 | // Shrink back down as small as possible. |
| 168 | size_t footprint = rosalloc_->Footprint(); |
| 169 | rosalloc_->SetFootprintLimit(footprint); |
| 170 | } |
| 171 | // Note RosAlloc zeroes memory internally. |
| Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 172 | // Return the new allocation or null. |
| Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 173 | CHECK(!kDebugSpaces || result == nullptr || Contains(result)); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 174 | return result; |
| 175 | } |
| 176 | |
| Andreas Gampe | d757632 | 2014-10-24 22:13:45 -0700 | [diff] [blame] | 177 | MallocSpace* RosAllocSpace::CreateInstance(MemMap* mem_map, const std::string& name, |
| 178 | void* allocator, uint8_t* begin, uint8_t* end, |
| 179 | uint8_t* limit, size_t growth_limit, |
| Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 180 | bool can_move_objects) { |
| Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 181 | if (Runtime::Current()->IsRunningOnMemoryTool()) { |
| 182 | return new MemoryToolMallocSpace<RosAllocSpace, kDefaultMemoryToolRedZoneBytes, false, true>( |
| Andreas Gampe | d757632 | 2014-10-24 22:13:45 -0700 | [diff] [blame] | 183 | mem_map, initial_size_, name, reinterpret_cast<allocator::RosAlloc*>(allocator), begin, end, |
| 184 | limit, growth_limit, can_move_objects, starting_size_, low_memory_mode_); |
| 185 | } else { |
| 186 | return new RosAllocSpace(mem_map, initial_size_, name, |
| 187 | reinterpret_cast<allocator::RosAlloc*>(allocator), begin, end, limit, |
| 188 | growth_limit, can_move_objects, starting_size_, low_memory_mode_); |
| 189 | } |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | size_t RosAllocSpace::Free(Thread* self, mirror::Object* ptr) { |
| 193 | if (kDebugSpaces) { |
| Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 194 | CHECK(ptr != nullptr); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 195 | CHECK(Contains(ptr)) << "Free (" << ptr << ") not in bounds of heap " << *this; |
| 196 | } |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 197 | if (kRecentFreeCount > 0) { |
| 198 | MutexLock mu(self, lock_); |
| 199 | RegisterRecentFree(ptr); |
| 200 | } |
| Mathieu Chartier | 8585bad | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 201 | return rosalloc_->Free(self, ptr); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | size_t RosAllocSpace::FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs) { |
| Mathieu Chartier | 8585bad | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 205 | DCHECK(ptrs != nullptr); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 206 | |
| Mathieu Chartier | 8585bad | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 207 | size_t verify_bytes = 0; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 208 | for (size_t i = 0; i < num_ptrs; i++) { |
| Mathieu Chartier | 8585bad | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 209 | if (kPrefetchDuringRosAllocFreeList && i + kPrefetchLookAhead < num_ptrs) { |
| 210 | __builtin_prefetch(reinterpret_cast<char*>(ptrs[i + kPrefetchLookAhead])); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 211 | } |
| Mathieu Chartier | 8585bad | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 212 | if (kVerifyFreedBytes) { |
| Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 213 | verify_bytes += AllocationSizeNonvirtual<true>(ptrs[i], nullptr); |
| Mathieu Chartier | 8585bad | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 214 | } |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | if (kRecentFreeCount > 0) { |
| 218 | MutexLock mu(self, lock_); |
| 219 | for (size_t i = 0; i < num_ptrs; i++) { |
| 220 | RegisterRecentFree(ptrs[i]); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | if (kDebugSpaces) { |
| 225 | size_t num_broken_ptrs = 0; |
| 226 | for (size_t i = 0; i < num_ptrs; i++) { |
| 227 | if (!Contains(ptrs[i])) { |
| 228 | num_broken_ptrs++; |
| 229 | LOG(ERROR) << "FreeList[" << i << "] (" << ptrs[i] << ") not in bounds of heap " << *this; |
| 230 | } else { |
| 231 | size_t size = rosalloc_->UsableSize(ptrs[i]); |
| 232 | memset(ptrs[i], 0xEF, size); |
| 233 | } |
| 234 | } |
| 235 | CHECK_EQ(num_broken_ptrs, 0u); |
| 236 | } |
| 237 | |
| Mathieu Chartier | 8585bad | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 238 | const size_t bytes_freed = rosalloc_->BulkFree(self, reinterpret_cast<void**>(ptrs), num_ptrs); |
| 239 | if (kVerifyFreedBytes) { |
| 240 | CHECK_EQ(verify_bytes, bytes_freed); |
| 241 | } |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 242 | return bytes_freed; |
| 243 | } |
| 244 | |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 245 | size_t RosAllocSpace::Trim() { |
| Hiroshi Yamauchi | d9a88de | 2014-04-07 13:52:31 -0700 | [diff] [blame] | 246 | VLOG(heap) << "RosAllocSpace::Trim() "; |
| Hiroshi Yamauchi | 573f7d2 | 2013-12-17 11:54:23 -0800 | [diff] [blame] | 247 | { |
| Mathieu Chartier | a9d82fe | 2016-01-25 20:06:11 -0800 | [diff] [blame] | 248 | Thread* const self = Thread::Current(); |
| 249 | // SOA required for Rosalloc::Trim() -> ArtRosAllocMoreCore() -> Heap::GetRosAllocSpace. |
| 250 | ScopedObjectAccess soa(self); |
| 251 | MutexLock mu(self, lock_); |
| Hiroshi Yamauchi | 573f7d2 | 2013-12-17 11:54:23 -0800 | [diff] [blame] | 252 | // Trim to release memory at the end of the space. |
| 253 | rosalloc_->Trim(); |
| 254 | } |
| 255 | // Attempt to release pages if it does not release all empty pages. |
| 256 | if (!rosalloc_->DoesReleaseAllPages()) { |
| Hiroshi Yamauchi | d9a88de | 2014-04-07 13:52:31 -0700 | [diff] [blame] | 257 | return rosalloc_->ReleasePages(); |
| Hiroshi Yamauchi | 573f7d2 | 2013-12-17 11:54:23 -0800 | [diff] [blame] | 258 | } |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 259 | return 0; |
| 260 | } |
| 261 | |
| 262 | void RosAllocSpace::Walk(void(*callback)(void *start, void *end, size_t num_bytes, void* callback_arg), |
| 263 | void* arg) { |
| Hiroshi Yamauchi | 1cd53db | 2014-03-28 15:26:48 -0700 | [diff] [blame] | 264 | InspectAllRosAlloc(callback, arg, true); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | size_t RosAllocSpace::GetFootprint() { |
| 268 | MutexLock mu(Thread::Current(), lock_); |
| 269 | return rosalloc_->Footprint(); |
| 270 | } |
| 271 | |
| 272 | size_t RosAllocSpace::GetFootprintLimit() { |
| 273 | MutexLock mu(Thread::Current(), lock_); |
| 274 | return rosalloc_->FootprintLimit(); |
| 275 | } |
| 276 | |
| 277 | void RosAllocSpace::SetFootprintLimit(size_t new_size) { |
| 278 | MutexLock mu(Thread::Current(), lock_); |
| 279 | VLOG(heap) << "RosAllocSpace::SetFootprintLimit " << PrettySize(new_size); |
| 280 | // Compare against the actual footprint, rather than the Size(), because the heap may not have |
| 281 | // grown all the way to the allowed size yet. |
| 282 | size_t current_space_size = rosalloc_->Footprint(); |
| 283 | if (new_size < current_space_size) { |
| 284 | // Don't let the space grow any more. |
| 285 | new_size = current_space_size; |
| 286 | } |
| 287 | rosalloc_->SetFootprintLimit(new_size); |
| 288 | } |
| 289 | |
| 290 | uint64_t RosAllocSpace::GetBytesAllocated() { |
| Hiroshi Yamauchi | 4ce1f00 | 2013-11-18 14:49:09 -0800 | [diff] [blame] | 291 | size_t bytes_allocated = 0; |
| Hiroshi Yamauchi | 1cd53db | 2014-03-28 15:26:48 -0700 | [diff] [blame] | 292 | InspectAllRosAlloc(art::gc::allocator::RosAlloc::BytesAllocatedCallback, &bytes_allocated, false); |
| Hiroshi Yamauchi | 4ce1f00 | 2013-11-18 14:49:09 -0800 | [diff] [blame] | 293 | return bytes_allocated; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | uint64_t RosAllocSpace::GetObjectsAllocated() { |
| Hiroshi Yamauchi | 4ce1f00 | 2013-11-18 14:49:09 -0800 | [diff] [blame] | 297 | size_t objects_allocated = 0; |
| Hiroshi Yamauchi | 1cd53db | 2014-03-28 15:26:48 -0700 | [diff] [blame] | 298 | InspectAllRosAlloc(art::gc::allocator::RosAlloc::ObjectsAllocatedCallback, &objects_allocated, false); |
| Hiroshi Yamauchi | 4ce1f00 | 2013-11-18 14:49:09 -0800 | [diff] [blame] | 299 | return objects_allocated; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 300 | } |
| 301 | |
| Hiroshi Yamauchi | 1cd53db | 2014-03-28 15:26:48 -0700 | [diff] [blame] | 302 | void RosAllocSpace::InspectAllRosAllocWithSuspendAll( |
| 303 | void (*callback)(void *start, void *end, size_t num_bytes, void* callback_arg), |
| 304 | void* arg, bool do_null_callback_at_end) NO_THREAD_SAFETY_ANALYSIS { |
| 305 | // TODO: NO_THREAD_SAFETY_ANALYSIS. |
| 306 | Thread* self = Thread::Current(); |
| Mathieu Chartier | 4f55e22 | 2015-09-04 13:26:21 -0700 | [diff] [blame] | 307 | ScopedSuspendAll ssa(__FUNCTION__); |
| 308 | MutexLock mu(self, *Locks::runtime_shutdown_lock_); |
| 309 | MutexLock mu2(self, *Locks::thread_list_lock_); |
| 310 | rosalloc_->InspectAll(callback, arg); |
| 311 | if (do_null_callback_at_end) { |
| 312 | callback(nullptr, nullptr, 0, arg); // Indicate end of a space. |
| Hiroshi Yamauchi | 1cd53db | 2014-03-28 15:26:48 -0700 | [diff] [blame] | 313 | } |
| Hiroshi Yamauchi | 1cd53db | 2014-03-28 15:26:48 -0700 | [diff] [blame] | 314 | } |
| 315 | |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 316 | void RosAllocSpace::InspectAllRosAlloc(void (*callback)(void *start, void *end, size_t num_bytes, void* callback_arg), |
| Hiroshi Yamauchi | 1cd53db | 2014-03-28 15:26:48 -0700 | [diff] [blame] | 317 | void* arg, bool do_null_callback_at_end) NO_THREAD_SAFETY_ANALYSIS { |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 318 | // TODO: NO_THREAD_SAFETY_ANALYSIS. |
| 319 | Thread* self = Thread::Current(); |
| 320 | if (Locks::mutator_lock_->IsExclusiveHeld(self)) { |
| 321 | // The mutators are already suspended. For example, a call path |
| 322 | // from SignalCatcher::HandleSigQuit(). |
| 323 | rosalloc_->InspectAll(callback, arg); |
| Hiroshi Yamauchi | 1cd53db | 2014-03-28 15:26:48 -0700 | [diff] [blame] | 324 | if (do_null_callback_at_end) { |
| Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 325 | callback(nullptr, nullptr, 0, arg); // Indicate end of a space. |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 326 | } |
| Hiroshi Yamauchi | 1cd53db | 2014-03-28 15:26:48 -0700 | [diff] [blame] | 327 | } else if (Locks::mutator_lock_->IsSharedHeld(self)) { |
| 328 | // The mutators are not suspended yet and we have a shared access |
| 329 | // to the mutator lock. Temporarily release the shared access by |
| 330 | // transitioning to the suspend state, and suspend the mutators. |
| Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 331 | ScopedThreadSuspension sts(self, kSuspended); |
| Hiroshi Yamauchi | 1cd53db | 2014-03-28 15:26:48 -0700 | [diff] [blame] | 332 | InspectAllRosAllocWithSuspendAll(callback, arg, do_null_callback_at_end); |
| Hiroshi Yamauchi | 1cd53db | 2014-03-28 15:26:48 -0700 | [diff] [blame] | 333 | } else { |
| 334 | // The mutators are not suspended yet. Suspend the mutators. |
| 335 | InspectAllRosAllocWithSuspendAll(callback, arg, do_null_callback_at_end); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 336 | } |
| 337 | } |
| 338 | |
| Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 339 | size_t RosAllocSpace::RevokeThreadLocalBuffers(Thread* thread) { |
| 340 | return rosalloc_->RevokeThreadLocalRuns(thread); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 341 | } |
| 342 | |
| Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 343 | size_t RosAllocSpace::RevokeAllThreadLocalBuffers() { |
| 344 | return rosalloc_->RevokeAllThreadLocalRuns(); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 345 | } |
| 346 | |
| Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 347 | void RosAllocSpace::AssertThreadLocalBuffersAreRevoked(Thread* thread) { |
| 348 | if (kIsDebugBuild) { |
| 349 | rosalloc_->AssertThreadLocalRunsAreRevoked(thread); |
| 350 | } |
| 351 | } |
| 352 | |
| Hiroshi Yamauchi | c93c530 | 2014-03-20 16:15:37 -0700 | [diff] [blame] | 353 | void RosAllocSpace::AssertAllThreadLocalBuffersAreRevoked() { |
| 354 | if (kIsDebugBuild) { |
| 355 | rosalloc_->AssertAllThreadLocalRunsAreRevoked(); |
| 356 | } |
| 357 | } |
| 358 | |
| Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 359 | void RosAllocSpace::Clear() { |
| Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 360 | size_t footprint_limit = GetFootprintLimit(); |
| Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 361 | madvise(GetMemMap()->Begin(), GetMemMap()->Size(), MADV_DONTNEED); |
| Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 362 | live_bitmap_->Clear(); |
| 363 | mark_bitmap_->Clear(); |
| Ian Rogers | be2a1df | 2014-07-10 00:56:36 -0700 | [diff] [blame] | 364 | SetEnd(begin_ + starting_size_); |
| Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 365 | delete rosalloc_; |
| Hiroshi Yamauchi | 94c41df | 2014-12-18 21:19:24 -0800 | [diff] [blame] | 366 | rosalloc_ = CreateRosAlloc(mem_map_->Begin(), starting_size_, initial_size_, |
| 367 | NonGrowthLimitCapacity(), low_memory_mode_, |
| Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 368 | Runtime::Current()->IsRunningOnMemoryTool()); |
| Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 369 | SetFootprintLimit(footprint_limit); |
| Mathieu Chartier | 15d3402 | 2014-02-26 17:16:38 -0800 | [diff] [blame] | 370 | } |
| 371 | |
| Hiroshi Yamauchi | 565c2d9 | 2016-03-23 12:53:26 -0700 | [diff] [blame] | 372 | void RosAllocSpace::DumpStats(std::ostream& os) { |
| 373 | ScopedSuspendAll ssa(__FUNCTION__); |
| 374 | rosalloc_->DumpStats(os); |
| 375 | } |
| 376 | |
| Andreas Gampe | d490129 | 2017-05-30 18:41:34 -0700 | [diff] [blame] | 377 | template<bool kMaybeIsRunningOnMemoryTool> |
| 378 | size_t RosAllocSpace::AllocationSizeNonvirtual(mirror::Object* obj, size_t* usable_size) { |
| 379 | // obj is a valid object. Use its class in the header to get the size. |
| 380 | // Don't use verification since the object may be dead if we are sweeping. |
| 381 | size_t size = obj->SizeOf<kVerifyNone>(); |
| 382 | bool add_redzones = false; |
| 383 | if (kMaybeIsRunningOnMemoryTool) { |
| 384 | add_redzones = RUNNING_ON_MEMORY_TOOL ? kMemoryToolAddsRedzones : 0; |
| 385 | if (add_redzones) { |
| 386 | size += 2 * kDefaultMemoryToolRedZoneBytes; |
| 387 | } |
| 388 | } else { |
| 389 | DCHECK_EQ(RUNNING_ON_MEMORY_TOOL, 0U); |
| 390 | } |
| 391 | size_t size_by_size = rosalloc_->UsableSize(size); |
| 392 | if (kIsDebugBuild) { |
| 393 | // On memory tool, the red zone has an impact... |
| 394 | const uint8_t* obj_ptr = reinterpret_cast<const uint8_t*>(obj); |
| 395 | size_t size_by_ptr = rosalloc_->UsableSize( |
| 396 | obj_ptr - (add_redzones ? kDefaultMemoryToolRedZoneBytes : 0)); |
| 397 | if (size_by_size != size_by_ptr) { |
| 398 | LOG(INFO) << "Found a bad sized obj of size " << size |
| 399 | << " at " << std::hex << reinterpret_cast<intptr_t>(obj_ptr) << std::dec |
| 400 | << " size_by_size=" << size_by_size << " size_by_ptr=" << size_by_ptr; |
| 401 | } |
| 402 | DCHECK_EQ(size_by_size, size_by_ptr); |
| 403 | } |
| 404 | if (usable_size != nullptr) { |
| 405 | *usable_size = size_by_size; |
| 406 | } |
| 407 | return size_by_size; |
| 408 | } |
| 409 | |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 410 | } // namespace space |
| Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 411 | |
| 412 | namespace allocator { |
| 413 | |
| 414 | // Callback from rosalloc when it needs to increase the footprint. |
| Mathieu Chartier | a9d82fe | 2016-01-25 20:06:11 -0800 | [diff] [blame] | 415 | void* ArtRosAllocMoreCore(allocator::RosAlloc* rosalloc, intptr_t increment) |
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 416 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 417 | Heap* heap = Runtime::Current()->GetHeap(); |
| 418 | art::gc::space::RosAllocSpace* rosalloc_space = heap->GetRosAllocSpace(rosalloc); |
| 419 | DCHECK(rosalloc_space != nullptr); |
| 420 | DCHECK_EQ(rosalloc_space->GetRosAlloc(), rosalloc); |
| 421 | return rosalloc_space->MoreCore(increment); |
| 422 | } |
| 423 | |
| 424 | } // namespace allocator |
| 425 | |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 426 | } // namespace gc |
| 427 | } // namespace art |