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