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