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