Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 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 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_GC_ACCOUNTING_MOD_UNION_TABLE_H_ |
| 18 | #define ART_RUNTIME_GC_ACCOUNTING_MOD_UNION_TABLE_H_ |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 19 | |
| 20 | #include "globals.h" |
| 21 | #include "safe_map.h" |
| 22 | |
| 23 | #include <set> |
| 24 | #include <vector> |
| 25 | |
| 26 | namespace art { |
| 27 | namespace mirror { |
| 28 | class Object; |
| 29 | } // namespace mirror |
| 30 | |
| 31 | namespace gc { |
| 32 | |
| 33 | namespace collector { |
| 34 | class MarkSweep; |
| 35 | } // namespace collector |
| 36 | namespace space { |
| 37 | class ContinuousSpace; |
| 38 | class Space; |
| 39 | } // namespace space |
| 40 | |
| 41 | class Heap; |
| 42 | |
| 43 | namespace accounting { |
| 44 | |
| 45 | class SpaceBitmap; |
| 46 | class HeapBitmap; |
| 47 | |
| 48 | // The mod-union table is the union of modified cards. It is used to allow the card table to be |
| 49 | // cleared between GC phases, reducing the number of dirty cards that need to be scanned. |
| 50 | class ModUnionTable { |
| 51 | public: |
| 52 | ModUnionTable(Heap* heap) : heap_(heap) { |
| 53 | } |
| 54 | |
| 55 | virtual ~ModUnionTable() { |
| 56 | } |
| 57 | |
| 58 | // Clear cards which map to a memory range of a space. This doesn't immediately update the |
| 59 | // mod-union table, as updating the mod-union table may have an associated cost, such as |
| 60 | // determining references to track. |
| 61 | virtual void ClearCards(space::ContinuousSpace* space) = 0; |
| 62 | |
| 63 | // Update the mod-union table using data stored by ClearCards. There may be multiple ClearCards |
| 64 | // before a call to update, for example, back-to-back sticky GCs. |
| 65 | virtual void Update() = 0; |
| 66 | |
| 67 | // Mark the bitmaps for all references which are stored in the mod-union table. |
| 68 | virtual void MarkReferences(collector::MarkSweep* mark_sweep) = 0; |
| 69 | |
| 70 | // Verification, sanity checks that we don't have clean cards which conflict with out cached data |
| 71 | // for said cards. Exclusive lock is required since verify sometimes uses |
| 72 | // SpaceBitmap::VisitMarkedRange and VisitMarkedRange can't know if the callback will modify the |
| 73 | // bitmap or not. |
| 74 | virtual void Verify() EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) = 0; |
| 75 | |
| 76 | virtual void Dump(std::ostream& os) = 0; |
| 77 | |
| 78 | Heap* GetHeap() const { |
| 79 | return heap_; |
| 80 | } |
| 81 | |
| 82 | protected: |
| 83 | Heap* const heap_; |
| 84 | }; |
| 85 | |
| 86 | // Reference caching implementation. Caches references pointing to alloc space(s) for each card. |
| 87 | class ModUnionTableReferenceCache : public ModUnionTable { |
| 88 | public: |
| 89 | ModUnionTableReferenceCache(Heap* heap) : ModUnionTable(heap) {} |
| 90 | virtual ~ModUnionTableReferenceCache() {} |
| 91 | |
| 92 | // Clear and store cards for a space. |
| 93 | void ClearCards(space::ContinuousSpace* space); |
| 94 | |
| 95 | // Update table based on cleared cards. |
| 96 | void Update() |
| 97 | EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) |
| 98 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 99 | |
| 100 | // Mark all references to the alloc space(s). |
| 101 | void MarkReferences(collector::MarkSweep* mark_sweep) |
| 102 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 103 | EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_); |
| 104 | |
| 105 | // Exclusive lock is required since verify uses SpaceBitmap::VisitMarkedRange and |
| 106 | // VisitMarkedRange can't know if the callback will modify the bitmap or not. |
| 107 | void Verify() EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_); |
| 108 | |
| 109 | // Function that tells whether or not to add a reference to the table. |
| 110 | virtual bool AddReference(const mirror::Object* obj, const mirror::Object* ref) = 0; |
| 111 | |
| 112 | void Dump(std::ostream& os); |
| 113 | |
| 114 | protected: |
| 115 | // Cleared card array, used to update the mod-union table. |
| 116 | std::set<byte*> cleared_cards_; |
| 117 | |
| 118 | // Maps from dirty cards to their corresponding alloc space references. |
| 119 | SafeMap<const byte*, std::vector<const mirror::Object*> > references_; |
| 120 | }; |
| 121 | |
| 122 | // Card caching implementation. Keeps track of which cards we cleared and only this information. |
| 123 | class ModUnionTableCardCache : public ModUnionTable { |
| 124 | public: |
| 125 | ModUnionTableCardCache(Heap* heap) : ModUnionTable(heap) {} |
| 126 | virtual ~ModUnionTableCardCache() {} |
| 127 | |
| 128 | // Clear and store cards for a space. |
| 129 | void ClearCards(space::ContinuousSpace* space); |
| 130 | |
| 131 | // Nothing to update as all dirty cards were placed into cleared cards during clearing. |
| 132 | void Update() {} |
| 133 | |
| 134 | // Mark all references to the alloc space(s). |
| 135 | void MarkReferences(collector::MarkSweep* mark_sweep) |
| 136 | EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) |
| 137 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 138 | |
| 139 | // Nothing to verify. |
| 140 | void Verify() {} |
| 141 | |
| 142 | void Dump(std::ostream& os); |
| 143 | |
| 144 | protected: |
| 145 | // Cleared card array, used to update the mod-union table. |
| 146 | std::set<byte*> cleared_cards_; |
| 147 | }; |
| 148 | |
| 149 | } // namespace accounting |
| 150 | } // namespace gc |
| 151 | } // namespace art |
| 152 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 153 | #endif // ART_RUNTIME_GC_ACCOUNTING_MOD_UNION_TABLE_H_ |