Home | History | Annotate | Download | only in accounting
      1 /*
      2  * Copyright (C) 2010 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 "card_table.h"
     18 
     19 #include "base/logging.h"
     20 #include "base/systrace.h"
     21 #include "card_table-inl.h"
     22 #include "gc/heap.h"
     23 #include "gc/space/space.h"
     24 #include "heap_bitmap.h"
     25 #include "mem_map.h"
     26 #include "runtime.h"
     27 #include "utils.h"
     28 
     29 namespace art {
     30 namespace gc {
     31 namespace accounting {
     32 
     33 constexpr size_t CardTable::kCardShift;
     34 constexpr size_t CardTable::kCardSize;
     35 constexpr uint8_t CardTable::kCardClean;
     36 constexpr uint8_t CardTable::kCardDirty;
     37 
     38 /*
     39  * Maintain a card table from the write barrier. All writes of
     40  * non-null values to heap addresses should go through an entry in
     41  * WriteBarrier, and from there to here.
     42  *
     43  * The heap is divided into "cards" of GC_CARD_SIZE bytes, as
     44  * determined by GC_CARD_SHIFT. The card table contains one byte of
     45  * data per card, to be used by the GC. The value of the byte will be
     46  * one of GC_CARD_CLEAN or GC_CARD_DIRTY.
     47  *
     48  * After any store of a non-null object pointer into a heap object,
     49  * code is obliged to mark the card dirty. The setters in
     50  * object.h [such as SetFieldObject] do this for you. The
     51  * compiler also contains code to mark cards as dirty.
     52  *
     53  * The card table's base [the "biased card table"] gets set to a
     54  * rather strange value.  In order to keep the JIT from having to
     55  * fabricate or load GC_DIRTY_CARD to store into the card table,
     56  * biased base is within the mmap allocation at a point where its low
     57  * byte is equal to GC_DIRTY_CARD. See CardTable::Create for details.
     58  */
     59 
     60 CardTable* CardTable::Create(const uint8_t* heap_begin, size_t heap_capacity) {
     61   ScopedTrace trace(__PRETTY_FUNCTION__);
     62   /* Set up the card table */
     63   size_t capacity = heap_capacity / kCardSize;
     64   /* Allocate an extra 256 bytes to allow fixed low-byte of base */
     65   std::string error_msg;
     66   std::unique_ptr<MemMap> mem_map(
     67       MemMap::MapAnonymous("card table", nullptr, capacity + 256, PROT_READ | PROT_WRITE,
     68                            false, false, &error_msg));
     69   CHECK(mem_map.get() != nullptr) << "couldn't allocate card table: " << error_msg;
     70   // All zeros is the correct initial value; all clean. Anonymous mmaps are initialized to zero, we
     71   // don't clear the card table to avoid unnecessary pages being allocated
     72   static_assert(kCardClean == 0, "kCardClean must be 0");
     73 
     74   uint8_t* cardtable_begin = mem_map->Begin();
     75   CHECK(cardtable_begin != nullptr);
     76 
     77   // We allocated up to a bytes worth of extra space to allow biased_begin's byte value to equal
     78   // kCardDirty, compute a offset value to make this the case
     79   size_t offset = 0;
     80   uint8_t* biased_begin = reinterpret_cast<uint8_t*>(reinterpret_cast<uintptr_t>(cardtable_begin) -
     81       (reinterpret_cast<uintptr_t>(heap_begin) >> kCardShift));
     82   uintptr_t biased_byte = reinterpret_cast<uintptr_t>(biased_begin) & 0xff;
     83   if (biased_byte != kCardDirty) {
     84     int delta = kCardDirty - biased_byte;
     85     offset = delta + (delta < 0 ? 0x100 : 0);
     86     biased_begin += offset;
     87   }
     88   CHECK_EQ(reinterpret_cast<uintptr_t>(biased_begin) & 0xff, kCardDirty);
     89   return new CardTable(mem_map.release(), biased_begin, offset);
     90 }
     91 
     92 CardTable::CardTable(MemMap* mem_map, uint8_t* biased_begin, size_t offset)
     93     : mem_map_(mem_map), biased_begin_(biased_begin), offset_(offset) {
     94 }
     95 
     96 CardTable::~CardTable() {
     97   // Destroys MemMap via std::unique_ptr<>.
     98 }
     99 
    100 void CardTable::ClearSpaceCards(space::ContinuousSpace* space) {
    101   // TODO: clear just the range of the table that has been modified
    102   uint8_t* card_start = CardFromAddr(space->Begin());
    103   uint8_t* card_end = CardFromAddr(space->End());  // Make sure to round up.
    104   memset(reinterpret_cast<void*>(card_start), kCardClean, card_end - card_start);
    105 }
    106 
    107 void CardTable::ClearCardTable() {
    108   static_assert(kCardClean == 0, "kCardClean must be 0");
    109   mem_map_->MadviseDontNeedAndZero();
    110 }
    111 
    112 void CardTable::ClearCardRange(uint8_t* start, uint8_t* end) {
    113   if (!kMadviseZeroes) {
    114     memset(start, 0, end - start);
    115     return;
    116   }
    117   CHECK_ALIGNED(reinterpret_cast<uintptr_t>(start), kCardSize);
    118   CHECK_ALIGNED(reinterpret_cast<uintptr_t>(end), kCardSize);
    119   static_assert(kCardClean == 0, "kCardClean must be 0");
    120   uint8_t* start_card = CardFromAddr(start);
    121   uint8_t* end_card = CardFromAddr(end);
    122   uint8_t* round_start = AlignUp(start_card, kPageSize);
    123   uint8_t* round_end = AlignDown(end_card, kPageSize);
    124   if (round_start < round_end) {
    125     madvise(round_start, round_end - round_start, MADV_DONTNEED);
    126   }
    127   // Handle unaligned regions at start / end.
    128   memset(start_card, 0, std::min(round_start, end_card) - start_card);
    129   memset(std::max(round_end, start_card), 0, end_card - std::max(round_end, start_card));
    130 }
    131 
    132 bool CardTable::AddrIsInCardTable(const void* addr) const {
    133   return IsValidCard(biased_begin_ + ((uintptr_t)addr >> kCardShift));
    134 }
    135 
    136 void CardTable::CheckAddrIsInCardTable(const uint8_t* addr) const {
    137   uint8_t* card_addr = biased_begin_ + ((uintptr_t)addr >> kCardShift);
    138   uint8_t* begin = mem_map_->Begin() + offset_;
    139   uint8_t* end = mem_map_->End();
    140   CHECK(AddrIsInCardTable(addr))
    141       << "Card table " << this
    142       << " begin: " << reinterpret_cast<void*>(begin)
    143       << " end: " << reinterpret_cast<void*>(end)
    144       << " card_addr: " << reinterpret_cast<void*>(card_addr)
    145       << " heap begin: " << AddrFromCard(begin)
    146       << " heap end: " << AddrFromCard(end)
    147       << " addr: " << reinterpret_cast<const void*>(addr);
    148 }
    149 
    150 void CardTable::VerifyCardTable() {
    151   UNIMPLEMENTED(WARNING) << "Card table verification";
    152 }
    153 
    154 }  // namespace accounting
    155 }  // namespace gc
    156 }  // namespace art
    157