Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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 "malloc_space.h" |
| 18 | |
| 19 | #include "gc/accounting/card_table.h" |
| 20 | #include "gc/heap.h" |
| 21 | #include "mirror/class-inl.h" |
| 22 | #include "mirror/object-inl.h" |
| 23 | #include "runtime.h" |
| 24 | #include "thread.h" |
| 25 | #include "thread_list.h" |
| 26 | #include "utils.h" |
| 27 | |
| 28 | namespace art { |
| 29 | namespace gc { |
| 30 | namespace space { |
| 31 | |
| 32 | size_t MallocSpace::bitmap_index_ = 0; |
| 33 | |
| 34 | MallocSpace::MallocSpace(const std::string& name, MemMap* mem_map, |
| 35 | byte* begin, byte* end, byte* limit, size_t growth_limit) |
| 36 | : ContinuousMemMapAllocSpace(name, mem_map, begin, end, limit, kGcRetentionPolicyAlwaysCollect), |
| 37 | recent_free_pos_(0), lock_("allocation space lock", kAllocSpaceLock), |
| 38 | growth_limit_(growth_limit) { |
| 39 | size_t bitmap_index = bitmap_index_++; |
| 40 | static const uintptr_t kGcCardSize = static_cast<uintptr_t>(accounting::CardTable::kCardSize); |
| 41 | CHECK(IsAligned<kGcCardSize>(reinterpret_cast<uintptr_t>(mem_map->Begin()))); |
| 42 | CHECK(IsAligned<kGcCardSize>(reinterpret_cast<uintptr_t>(mem_map->End()))); |
| 43 | live_bitmap_.reset(accounting::SpaceBitmap::Create( |
| 44 | StringPrintf("allocspace %s live-bitmap %d", name.c_str(), static_cast<int>(bitmap_index)), |
| 45 | Begin(), Capacity())); |
| 46 | DCHECK(live_bitmap_.get() != NULL) << "could not create allocspace live bitmap #" << bitmap_index; |
| 47 | mark_bitmap_.reset(accounting::SpaceBitmap::Create( |
| 48 | StringPrintf("allocspace %s mark-bitmap %d", name.c_str(), static_cast<int>(bitmap_index)), |
| 49 | Begin(), Capacity())); |
| 50 | DCHECK(live_bitmap_.get() != NULL) << "could not create allocspace mark bitmap #" << bitmap_index; |
| 51 | for (auto& freed : recent_freed_objects_) { |
| 52 | freed.first = nullptr; |
| 53 | freed.second = nullptr; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | MemMap* MallocSpace::CreateMemMap(const std::string& name, size_t starting_size, size_t* initial_size, |
| 58 | size_t* growth_limit, size_t* capacity, byte* requested_begin) { |
| 59 | // Sanity check arguments |
| 60 | if (starting_size > *initial_size) { |
| 61 | *initial_size = starting_size; |
| 62 | } |
| 63 | if (*initial_size > *growth_limit) { |
| 64 | LOG(ERROR) << "Failed to create alloc space (" << name << ") where the initial size (" |
| 65 | << PrettySize(*initial_size) << ") is larger than its capacity (" |
| 66 | << PrettySize(*growth_limit) << ")"; |
| 67 | return NULL; |
| 68 | } |
| 69 | if (*growth_limit > *capacity) { |
| 70 | LOG(ERROR) << "Failed to create alloc space (" << name << ") where the growth limit capacity (" |
| 71 | << PrettySize(*growth_limit) << ") is larger than the capacity (" |
| 72 | << PrettySize(*capacity) << ")"; |
| 73 | return NULL; |
| 74 | } |
| 75 | |
| 76 | // Page align growth limit and capacity which will be used to manage mmapped storage |
| 77 | *growth_limit = RoundUp(*growth_limit, kPageSize); |
| 78 | *capacity = RoundUp(*capacity, kPageSize); |
| 79 | |
| 80 | std::string error_msg; |
| 81 | MemMap* mem_map = MemMap::MapAnonymous(name.c_str(), requested_begin, *capacity, |
| 82 | PROT_READ | PROT_WRITE, &error_msg); |
| 83 | if (mem_map == NULL) { |
| 84 | LOG(ERROR) << "Failed to allocate pages for alloc space (" << name << ") of size " |
| 85 | << PrettySize(*capacity) << ": " << error_msg; |
| 86 | return NULL; |
| 87 | } |
| 88 | return mem_map; |
| 89 | } |
| 90 | |
| 91 | void MallocSpace::SwapBitmaps() { |
| 92 | live_bitmap_.swap(mark_bitmap_); |
| 93 | // Swap names to get more descriptive diagnostics. |
| 94 | std::string temp_name(live_bitmap_->GetName()); |
| 95 | live_bitmap_->SetName(mark_bitmap_->GetName()); |
| 96 | mark_bitmap_->SetName(temp_name); |
| 97 | } |
| 98 | |
| 99 | mirror::Class* MallocSpace::FindRecentFreedObject(const mirror::Object* obj) { |
| 100 | size_t pos = recent_free_pos_; |
| 101 | // Start at the most recently freed object and work our way back since there may be duplicates |
| 102 | // caused by dlmalloc reusing memory. |
| 103 | if (kRecentFreeCount > 0) { |
| 104 | for (size_t i = 0; i + 1 < kRecentFreeCount + 1; ++i) { |
| 105 | pos = pos != 0 ? pos - 1 : kRecentFreeMask; |
| 106 | if (recent_freed_objects_[pos].first == obj) { |
| 107 | return recent_freed_objects_[pos].second; |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | return nullptr; |
| 112 | } |
| 113 | |
| 114 | void MallocSpace::RegisterRecentFree(mirror::Object* ptr) { |
| 115 | recent_freed_objects_[recent_free_pos_] = std::make_pair(ptr, ptr->GetClass()); |
| 116 | recent_free_pos_ = (recent_free_pos_ + 1) & kRecentFreeMask; |
| 117 | } |
| 118 | |
| 119 | void MallocSpace::SetGrowthLimit(size_t growth_limit) { |
| 120 | growth_limit = RoundUp(growth_limit, kPageSize); |
| 121 | growth_limit_ = growth_limit; |
| 122 | if (Size() > growth_limit_) { |
| 123 | end_ = begin_ + growth_limit; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | void* MallocSpace::MoreCore(intptr_t increment) { |
| 128 | CheckMoreCoreForPrecondition(); |
| 129 | byte* original_end = end_; |
| 130 | if (increment != 0) { |
| 131 | VLOG(heap) << "MallocSpace::MoreCore " << PrettySize(increment); |
| 132 | byte* new_end = original_end + increment; |
| 133 | if (increment > 0) { |
| 134 | // Should never be asked to increase the allocation beyond the capacity of the space. Enforced |
| 135 | // by mspace_set_footprint_limit. |
| 136 | CHECK_LE(new_end, Begin() + Capacity()); |
| 137 | CHECK_MEMORY_CALL(mprotect, (original_end, increment, PROT_READ | PROT_WRITE), GetName()); |
| 138 | } else { |
| 139 | // Should never be asked for negative footprint (ie before begin). Zero footprint is ok. |
| 140 | CHECK_GE(original_end + increment, Begin()); |
| 141 | // Advise we don't need the pages and protect them |
| 142 | // TODO: by removing permissions to the pages we may be causing TLB shoot-down which can be |
| 143 | // expensive (note the same isn't true for giving permissions to a page as the protected |
| 144 | // page shouldn't be in a TLB). We should investigate performance impact of just |
| 145 | // removing ignoring the memory protection change here and in Space::CreateAllocSpace. It's |
| 146 | // likely just a useful debug feature. |
| 147 | size_t size = -increment; |
| 148 | CHECK_MEMORY_CALL(madvise, (new_end, size, MADV_DONTNEED), GetName()); |
| 149 | CHECK_MEMORY_CALL(mprotect, (new_end, size, PROT_NONE), GetName()); |
| 150 | } |
| 151 | // Update end_ |
| 152 | end_ = new_end; |
| 153 | } |
| 154 | return original_end; |
| 155 | } |
| 156 | |
| 157 | // Returns the old mark bitmap. |
| 158 | accounting::SpaceBitmap* MallocSpace::BindLiveToMarkBitmap() { |
| 159 | accounting::SpaceBitmap* live_bitmap = GetLiveBitmap(); |
| 160 | accounting::SpaceBitmap* mark_bitmap = mark_bitmap_.release(); |
| 161 | temp_bitmap_.reset(mark_bitmap); |
| 162 | mark_bitmap_.reset(live_bitmap); |
| 163 | return mark_bitmap; |
| 164 | } |
| 165 | |
| 166 | bool MallocSpace::HasBoundBitmaps() const { |
| 167 | return temp_bitmap_.get() != nullptr; |
| 168 | } |
| 169 | |
| 170 | void MallocSpace::UnBindBitmaps() { |
| 171 | CHECK(HasBoundBitmaps()); |
| 172 | // At this point, the temp_bitmap holds our old mark bitmap. |
| 173 | accounting::SpaceBitmap* new_bitmap = temp_bitmap_.release(); |
| 174 | CHECK_EQ(mark_bitmap_.release(), live_bitmap_.get()); |
| 175 | mark_bitmap_.reset(new_bitmap); |
| 176 | DCHECK(temp_bitmap_.get() == NULL); |
| 177 | } |
| 178 | |
Hiroshi Yamauchi | 573f7d2 | 2013-12-17 11:54:23 -0800 | [diff] [blame^] | 179 | MallocSpace* MallocSpace::CreateZygoteSpace(const char* alloc_space_name, bool low_memory_mode) { |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 180 | // For RosAlloc, revoke thread local runs before creating a new |
| 181 | // alloc space so that we won't mix thread local runs from different |
| 182 | // alloc spaces. |
| 183 | RevokeAllThreadLocalBuffers(); |
| 184 | end_ = reinterpret_cast<byte*>(RoundUp(reinterpret_cast<uintptr_t>(end_), kPageSize)); |
| 185 | DCHECK(IsAligned<accounting::CardTable::kCardSize>(begin_)); |
| 186 | DCHECK(IsAligned<accounting::CardTable::kCardSize>(end_)); |
| 187 | DCHECK(IsAligned<kPageSize>(begin_)); |
| 188 | DCHECK(IsAligned<kPageSize>(end_)); |
| 189 | size_t size = RoundUp(Size(), kPageSize); |
| 190 | // Trim the heap so that we minimize the size of the Zygote space. |
| 191 | Trim(); |
| 192 | // TODO: Not hardcode these in? |
| 193 | const size_t starting_size = kPageSize; |
| 194 | const size_t initial_size = 2 * MB; |
| 195 | // Remaining size is for the new alloc space. |
| 196 | const size_t growth_limit = growth_limit_ - size; |
| 197 | const size_t capacity = Capacity() - size; |
| 198 | VLOG(heap) << "Begin " << reinterpret_cast<const void*>(begin_) << "\n" |
| 199 | << "End " << reinterpret_cast<const void*>(end_) << "\n" |
| 200 | << "Size " << size << "\n" |
| 201 | << "GrowthLimit " << growth_limit_ << "\n" |
| 202 | << "Capacity " << Capacity(); |
| 203 | SetGrowthLimit(RoundUp(size, kPageSize)); |
| 204 | SetFootprintLimit(RoundUp(size, kPageSize)); |
| 205 | // FIXME: Do we need reference counted pointers here? |
| 206 | // Make the two spaces share the same mark bitmaps since the bitmaps span both of the spaces. |
| 207 | VLOG(heap) << "Creating new AllocSpace: "; |
| 208 | VLOG(heap) << "Size " << GetMemMap()->Size(); |
| 209 | VLOG(heap) << "GrowthLimit " << PrettySize(growth_limit); |
| 210 | VLOG(heap) << "Capacity " << PrettySize(capacity); |
| 211 | // Remap the tail. |
| 212 | std::string error_msg; |
| 213 | UniquePtr<MemMap> mem_map(GetMemMap()->RemapAtEnd(end_, alloc_space_name, |
| 214 | PROT_READ | PROT_WRITE, &error_msg)); |
| 215 | CHECK(mem_map.get() != nullptr) << error_msg; |
Hiroshi Yamauchi | 573f7d2 | 2013-12-17 11:54:23 -0800 | [diff] [blame^] | 216 | void* allocator = CreateAllocator(end_, starting_size, initial_size, low_memory_mode); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 217 | // Protect memory beyond the initial size. |
| 218 | byte* end = mem_map->Begin() + starting_size; |
| 219 | if (capacity - initial_size > 0) { |
| 220 | CHECK_MEMORY_CALL(mprotect, (end, capacity - initial_size, PROT_NONE), alloc_space_name); |
| 221 | } |
| 222 | MallocSpace* alloc_space = CreateInstance(alloc_space_name, mem_map.release(), allocator, |
| 223 | end_, end, limit_, growth_limit); |
| 224 | SetLimit(End()); |
| 225 | live_bitmap_->SetHeapLimit(reinterpret_cast<uintptr_t>(End())); |
| 226 | CHECK_EQ(live_bitmap_->HeapLimit(), reinterpret_cast<uintptr_t>(End())); |
| 227 | mark_bitmap_->SetHeapLimit(reinterpret_cast<uintptr_t>(End())); |
| 228 | CHECK_EQ(mark_bitmap_->HeapLimit(), reinterpret_cast<uintptr_t>(End())); |
| 229 | VLOG(heap) << "zygote space creation done"; |
| 230 | return alloc_space; |
| 231 | } |
| 232 | |
| 233 | void MallocSpace::Dump(std::ostream& os) const { |
| 234 | os << GetType() |
| 235 | << " begin=" << reinterpret_cast<void*>(Begin()) |
| 236 | << ",end=" << reinterpret_cast<void*>(End()) |
| 237 | << ",size=" << PrettySize(Size()) << ",capacity=" << PrettySize(Capacity()) |
| 238 | << ",name=\"" << GetName() << "\"]"; |
| 239 | } |
| 240 | |
| 241 | } // namespace space |
| 242 | } // namespace gc |
| 243 | } // namespace art |