Home | History | Annotate | Download | only in accounting
      1 /*
      2  * Copyright (C) 2011 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 #ifndef ART_RUNTIME_GC_ACCOUNTING_CARD_TABLE_H_
     18 #define ART_RUNTIME_GC_ACCOUNTING_CARD_TABLE_H_
     19 
     20 #include <memory>
     21 
     22 #include "base/mutex.h"
     23 #include "globals.h"
     24 #include "mem_map.h"
     25 
     26 namespace art {
     27 
     28 namespace mirror {
     29   class Object;
     30 }  // namespace mirror
     31 
     32 namespace gc {
     33 
     34 namespace space {
     35   class ContinuousSpace;
     36 }  // namespace space
     37 
     38 class Heap;
     39 
     40 namespace accounting {
     41 
     42 template<size_t kAlignment> class SpaceBitmap;
     43 
     44 // Maintain a card table from the the write barrier. All writes of
     45 // non-NULL values to heap addresses should go through an entry in
     46 // WriteBarrier, and from there to here.
     47 class CardTable {
     48  public:
     49   static constexpr size_t kCardShift = 7;
     50   static constexpr size_t kCardSize = 1 << kCardShift;
     51   static constexpr uint8_t kCardClean = 0x0;
     52   static constexpr uint8_t kCardDirty = 0x70;
     53 
     54   static CardTable* Create(const byte* heap_begin, size_t heap_capacity);
     55 
     56   // Set the card associated with the given address to GC_CARD_DIRTY.
     57   ALWAYS_INLINE void MarkCard(const void *addr) {
     58     *CardFromAddr(addr) = kCardDirty;
     59   }
     60 
     61   // Is the object on a dirty card?
     62   bool IsDirty(const mirror::Object* obj) const {
     63     return GetCard(obj) == kCardDirty;
     64   }
     65 
     66   // Return the state of the card at an address.
     67   byte GetCard(const mirror::Object* obj) const {
     68     return *CardFromAddr(obj);
     69   }
     70 
     71   // Visit and clear cards within memory range, only visits dirty cards.
     72   template <typename Visitor>
     73   void VisitClear(const void* start, const void* end, const Visitor& visitor) {
     74     byte* card_start = CardFromAddr(start);
     75     byte* card_end = CardFromAddr(end);
     76     for (byte* it = card_start; it != card_end; ++it) {
     77       if (*it == kCardDirty) {
     78         *it = kCardClean;
     79         visitor(it);
     80       }
     81     }
     82   }
     83 
     84   // Returns a value that when added to a heap address >> GC_CARD_SHIFT will address the appropriate
     85   // card table byte. For convenience this value is cached in every Thread
     86   byte* GetBiasedBegin() const {
     87     return biased_begin_;
     88   }
     89 
     90   /*
     91    * Visitor is expected to take in a card and return the new value. When a value is modified, the
     92    * modify visitor is called.
     93    * visitor: The visitor which modifies the cards. Returns the new value for a card given an old
     94    * value.
     95    * modified: Whenever the visitor modifies a card, this visitor is called on the card. Enables
     96    * us to know which cards got cleared.
     97    */
     98   template <typename Visitor, typename ModifiedVisitor>
     99   void ModifyCardsAtomic(byte* scan_begin, byte* scan_end, const Visitor& visitor,
    100                          const ModifiedVisitor& modified);
    101 
    102   // For every dirty at least minumum age between begin and end invoke the visitor with the
    103   // specified argument. Returns how many cards the visitor was run on.
    104   template <typename Visitor>
    105   size_t Scan(SpaceBitmap<kObjectAlignment>* bitmap, byte* scan_begin, byte* scan_end,
    106               const Visitor& visitor,
    107               const byte minimum_age = kCardDirty) const
    108       EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
    109       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
    110 
    111   // Assertion used to check the given address is covered by the card table
    112   void CheckAddrIsInCardTable(const byte* addr) const;
    113 
    114   // Resets all of the bytes in the card table to clean.
    115   void ClearCardTable();
    116 
    117   // Resets all of the bytes in the card table which do not map to the image space.
    118   void ClearSpaceCards(space::ContinuousSpace* space);
    119 
    120   // Returns the first address in the heap which maps to this card.
    121   void* AddrFromCard(const byte *card_addr) const ALWAYS_INLINE;
    122 
    123   // Returns the address of the relevant byte in the card table, given an address on the heap.
    124   byte* CardFromAddr(const void *addr) const ALWAYS_INLINE;
    125 
    126   bool AddrIsInCardTable(const void* addr) const;
    127 
    128  private:
    129   CardTable(MemMap* begin, byte* biased_begin, size_t offset);
    130 
    131   // Returns true iff the card table address is within the bounds of the card table.
    132   bool IsValidCard(const byte* card_addr) const {
    133     byte* begin = mem_map_->Begin() + offset_;
    134     byte* end = mem_map_->End();
    135     return card_addr >= begin && card_addr < end;
    136   }
    137 
    138   void CheckCardValid(byte* card) const ALWAYS_INLINE;
    139 
    140   // Verifies that all gray objects are on a dirty card.
    141   void VerifyCardTable();
    142 
    143   // Mmapped pages for the card table
    144   std::unique_ptr<MemMap> mem_map_;
    145   // Value used to compute card table addresses from object addresses, see GetBiasedBegin
    146   byte* const biased_begin_;
    147   // Card table doesn't begin at the beginning of the mem_map_, instead it is displaced by offset
    148   // to allow the byte value of biased_begin_ to equal GC_CARD_DIRTY
    149   const size_t offset_;
    150 };
    151 
    152 }  // namespace accounting
    153 }  // namespace gc
    154 }  // namespace art
    155 
    156 #endif  // ART_RUNTIME_GC_ACCOUNTING_CARD_TABLE_H_
    157