• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 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_MOD_UNION_TABLE_H_
18 #define ART_RUNTIME_GC_ACCOUNTING_MOD_UNION_TABLE_H_
19 
20 #include "bitmap.h"
21 #include "base/allocator.h"
22 #include "card_table.h"
23 #include "globals.h"
24 #include "object_callbacks.h"
25 #include "safe_map.h"
26 
27 #include <set>
28 #include <vector>
29 
30 namespace art {
31 namespace mirror {
32 class Object;
33 }  // namespace mirror
34 
35 namespace gc {
36 namespace space {
37   class ContinuousSpace;
38 }  // namespace space
39 class Heap;
40 
41 namespace accounting {
42 
43 // The mod-union table is the union of modified cards. It is used to allow the card table to be
44 // cleared between GC phases, reducing the number of dirty cards that need to be scanned.
45 class ModUnionTable {
46  public:
47   typedef std::set<uint8_t*, std::less<uint8_t*>,
48                    TrackingAllocator<uint8_t*, kAllocatorTagModUnionCardSet>> CardSet;
49   typedef MemoryRangeBitmap<CardTable::kCardSize> CardBitmap;
50 
ModUnionTable(const std::string & name,Heap * heap,space::ContinuousSpace * space)51   explicit ModUnionTable(const std::string& name, Heap* heap, space::ContinuousSpace* space)
52       : name_(name),
53         heap_(heap),
54         space_(space) {}
55 
~ModUnionTable()56   virtual ~ModUnionTable() {}
57 
58   // Process cards for a memory range of a space. This doesn't immediately update the mod-union
59   // table, as updating the mod-union table may have an associated cost, such as determining
60   // references to track.
61   virtual void ProcessCards() = 0;
62 
63   // Set all the cards.
64   virtual void SetCards() = 0;
65 
66   // Clear all of the table.
67   virtual void ClearTable() = 0;
68 
69   // Update the mod-union table using data stored by ProcessCards. There may be multiple
70   // ProcessCards before a call to update, for example, back-to-back sticky GCs. Also mark
71   // references to other spaces which are stored in the mod-union table.
72   virtual void UpdateAndMarkReferences(MarkObjectVisitor* visitor) = 0;
73 
74   // Visit all of the objects that may contain references to other spaces.
75   virtual void VisitObjects(ObjectCallback* callback, void* arg) = 0;
76 
77   // Verification, sanity checks that we don't have clean cards which conflict with out cached data
78   // for said cards. Exclusive lock is required since verify sometimes uses
79   // SpaceBitmap::VisitMarkedRange and VisitMarkedRange can't know if the callback will modify the
80   // bitmap or not.
81   virtual void Verify() REQUIRES(Locks::heap_bitmap_lock_) = 0;
82 
83   // Returns true if a card is marked inside the mod union table. Used for testing. The address
84   // doesn't need to be aligned.
85   virtual bool ContainsCardFor(uintptr_t addr) = 0;
86 
87   // Filter out cards that don't need to be marked. Automatically done with UpdateAndMarkReferences.
88   void FilterCards();
89 
90   virtual void Dump(std::ostream& os) = 0;
91 
GetSpace()92   space::ContinuousSpace* GetSpace() {
93     return space_;
94   }
95 
GetHeap()96   Heap* GetHeap() const {
97     return heap_;
98   }
99 
GetName()100   const std::string& GetName() const {
101     return name_;
102   }
103 
104  protected:
105   const std::string name_;
106   Heap* const heap_;
107   space::ContinuousSpace* const space_;
108 };
109 
110 // Reference caching implementation. Caches references pointing to alloc space(s) for each card.
111 class ModUnionTableReferenceCache : public ModUnionTable {
112  public:
ModUnionTableReferenceCache(const std::string & name,Heap * heap,space::ContinuousSpace * space)113   explicit ModUnionTableReferenceCache(const std::string& name, Heap* heap,
114                                        space::ContinuousSpace* space)
115       : ModUnionTable(name, heap, space) {}
116 
~ModUnionTableReferenceCache()117   virtual ~ModUnionTableReferenceCache() {}
118 
119   // Clear and store cards for a space.
120   void ProcessCards() OVERRIDE;
121 
122   // Update table based on cleared cards and mark all references to the other spaces.
123   void UpdateAndMarkReferences(MarkObjectVisitor* visitor) OVERRIDE
124       REQUIRES_SHARED(Locks::mutator_lock_)
125       REQUIRES(Locks::heap_bitmap_lock_);
126 
127   virtual void VisitObjects(ObjectCallback* callback, void* arg) OVERRIDE
128       REQUIRES(Locks::heap_bitmap_lock_)
129       REQUIRES_SHARED(Locks::mutator_lock_);
130 
131   // Exclusive lock is required since verify uses SpaceBitmap::VisitMarkedRange and
132   // VisitMarkedRange can't know if the callback will modify the bitmap or not.
133   void Verify() OVERRIDE
134       REQUIRES_SHARED(Locks::mutator_lock_)
135       REQUIRES(Locks::heap_bitmap_lock_);
136 
137   // Function that tells whether or not to add a reference to the table.
138   virtual bool ShouldAddReference(const mirror::Object* ref) const = 0;
139 
140   virtual bool ContainsCardFor(uintptr_t addr) OVERRIDE;
141 
142   virtual void Dump(std::ostream& os) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_);
143 
144   virtual void SetCards() OVERRIDE;
145 
146   virtual void ClearTable() OVERRIDE;
147 
148  protected:
149   // Cleared card array, used to update the mod-union table.
150   ModUnionTable::CardSet cleared_cards_;
151 
152   // Maps from dirty cards to their corresponding alloc space references.
153   AllocationTrackingSafeMap<const uint8_t*, std::vector<mirror::HeapReference<mirror::Object>*>,
154                             kAllocatorTagModUnionReferenceArray> references_;
155 };
156 
157 // Card caching implementation. Keeps track of which cards we cleared and only this information.
158 class ModUnionTableCardCache : public ModUnionTable {
159  public:
160   // Note: There is assumption that the space End() doesn't change.
161   explicit ModUnionTableCardCache(const std::string& name, Heap* heap,
162                                   space::ContinuousSpace* space);
163 
~ModUnionTableCardCache()164   virtual ~ModUnionTableCardCache() {}
165 
166   // Clear and store cards for a space.
167   virtual void ProcessCards() OVERRIDE;
168 
169   // Mark all references to the alloc space(s).
170   virtual void UpdateAndMarkReferences(MarkObjectVisitor* visitor) OVERRIDE
171       REQUIRES(Locks::heap_bitmap_lock_)
172       REQUIRES_SHARED(Locks::mutator_lock_);
173 
174   virtual void VisitObjects(ObjectCallback* callback, void* arg) OVERRIDE
175       REQUIRES(Locks::heap_bitmap_lock_)
176       REQUIRES_SHARED(Locks::mutator_lock_);
177 
178   // Nothing to verify.
Verify()179   virtual void Verify() OVERRIDE {}
180 
181   virtual void Dump(std::ostream& os) OVERRIDE;
182 
183   virtual bool ContainsCardFor(uintptr_t addr) OVERRIDE;
184 
185   virtual void SetCards() OVERRIDE;
186 
187   virtual void ClearTable() OVERRIDE;
188 
189  protected:
190   // Cleared card bitmap, used to update the mod-union table.
191   std::unique_ptr<CardBitmap> card_bitmap_;
192 };
193 
194 }  // namespace accounting
195 }  // namespace gc
196 }  // namespace art
197 
198 #endif  // ART_RUNTIME_GC_ACCOUNTING_MOD_UNION_TABLE_H_
199