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