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