blob: 7d5d8d2e1945b68593b00be71d2d1b8b255d1a84 [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"
Mathieu Chartier83c8ee02014-01-28 14:50:23 -080022#include "object_callbacks.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070023#include "safe_map.h"
24
25#include <set>
26#include <vector>
27
28namespace art {
29namespace mirror {
30 class Object;
31} // namespace mirror
32
33namespace gc {
34
35namespace collector {
36 class MarkSweep;
37} // namespace collector
38namespace space {
39 class ContinuousSpace;
40 class Space;
41} // namespace space
42
43class Heap;
44
45namespace accounting {
46
47class SpaceBitmap;
48class HeapBitmap;
49
50// The mod-union table is the union of modified cards. It is used to allow the card table to be
51// cleared between GC phases, reducing the number of dirty cards that need to be scanned.
52class ModUnionTable {
53 public:
Ian Rogers8d31bbd2013-10-13 10:44:14 -070054 typedef std::set<byte*, std::less<byte*>, GcAllocator<byte*> > CardSet;
Mathieu Chartier0a9dc052013-07-25 11:01:28 -070055
Mathieu Chartier11409ae2013-09-23 11:49:36 -070056 explicit ModUnionTable(const std::string& name, Heap* heap, space::ContinuousSpace* space)
57 : name_(name),
58 heap_(heap),
59 space_(space) {
60 }
Ian Rogers1d54e732013-05-02 21:10:01 -070061
Brian Carlstrom93ba8932013-07-17 21:31:49 -070062 virtual ~ModUnionTable() {}
Ian Rogers1d54e732013-05-02 21:10:01 -070063
64 // Clear cards which map to a memory range of a space. This doesn't immediately update the
65 // mod-union table, as updating the mod-union table may have an associated cost, such as
66 // determining references to track.
Mathieu Chartier11409ae2013-09-23 11:49:36 -070067 virtual void ClearCards() = 0;
Ian Rogers1d54e732013-05-02 21:10:01 -070068
69 // Update the mod-union table using data stored by ClearCards. There may be multiple ClearCards
Mathieu Chartier11409ae2013-09-23 11:49:36 -070070 // before a call to update, for example, back-to-back sticky GCs. Also mark references to other
71 // spaces which are stored in the mod-union table.
Mathieu Chartier83c8ee02014-01-28 14:50:23 -080072 virtual void UpdateAndMarkReferences(RootCallback* callback, void* arg) = 0;
Ian Rogers1d54e732013-05-02 21:10:01 -070073
74 // Verification, sanity checks that we don't have clean cards which conflict with out cached data
75 // for said cards. Exclusive lock is required since verify sometimes uses
76 // SpaceBitmap::VisitMarkedRange and VisitMarkedRange can't know if the callback will modify the
77 // bitmap or not.
78 virtual void Verify() EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) = 0;
79
80 virtual void Dump(std::ostream& os) = 0;
Mathieu Chartier11409ae2013-09-23 11:49:36 -070081 space::ContinuousSpace* GetSpace() {
82 return space_;
83 }
Ian Rogers1d54e732013-05-02 21:10:01 -070084 Heap* GetHeap() const {
85 return heap_;
86 }
Mathieu Chartier11409ae2013-09-23 11:49:36 -070087 const std::string& GetName() const {
88 return name_;
89 }
Ian Rogers1d54e732013-05-02 21:10:01 -070090
91 protected:
Mathieu Chartier11409ae2013-09-23 11:49:36 -070092 const std::string name_;
Ian Rogers1d54e732013-05-02 21:10:01 -070093 Heap* const heap_;
Mathieu Chartier11409ae2013-09-23 11:49:36 -070094 space::ContinuousSpace* const space_;
Ian Rogers1d54e732013-05-02 21:10:01 -070095};
96
97// Reference caching implementation. Caches references pointing to alloc space(s) for each card.
98class ModUnionTableReferenceCache : public ModUnionTable {
99 public:
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700100 explicit ModUnionTableReferenceCache(const std::string& name, Heap* heap,
101 space::ContinuousSpace* space)
102 : ModUnionTable(name, heap, space) {}
Ian Rogers1d54e732013-05-02 21:10:01 -0700103 virtual ~ModUnionTableReferenceCache() {}
104
105 // Clear and store cards for a space.
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700106 void ClearCards();
Ian Rogers1d54e732013-05-02 21:10:01 -0700107
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700108 // Update table based on cleared cards and mark all references to the other spaces.
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800109 void UpdateAndMarkReferences(RootCallback* callback, void* arg)
Ian Rogers1d54e732013-05-02 21:10:01 -0700110 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
111 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
112
113 // Exclusive lock is required since verify uses SpaceBitmap::VisitMarkedRange and
114 // VisitMarkedRange can't know if the callback will modify the bitmap or not.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800115 void Verify()
116 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
117 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Ian Rogers1d54e732013-05-02 21:10:01 -0700118
119 // Function that tells whether or not to add a reference to the table.
120 virtual bool AddReference(const mirror::Object* obj, const mirror::Object* ref) = 0;
121
Ian Rogersef7d42f2014-01-06 12:55:46 -0800122 void Dump(std::ostream& os) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers1d54e732013-05-02 21:10:01 -0700123
124 protected:
125 // Cleared card array, used to update the mod-union table.
Mathieu Chartier0a9dc052013-07-25 11:01:28 -0700126 ModUnionTable::CardSet cleared_cards_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700127
128 // Maps from dirty cards to their corresponding alloc space references.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800129 SafeMap<const byte*, std::vector<mirror::HeapReference<mirror::Object>*>, std::less<const byte*>,
130 GcAllocator<std::pair<const byte*, std::vector<mirror::HeapReference<mirror::Object>*> > > >
131 references_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700132};
133
134// Card caching implementation. Keeps track of which cards we cleared and only this information.
135class ModUnionTableCardCache : public ModUnionTable {
136 public:
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700137 explicit ModUnionTableCardCache(const std::string& name, Heap* heap, space::ContinuousSpace* space)
138 : ModUnionTable(name, heap, space) {}
Ian Rogers1d54e732013-05-02 21:10:01 -0700139 virtual ~ModUnionTableCardCache() {}
140
141 // Clear and store cards for a space.
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700142 void ClearCards();
Ian Rogers1d54e732013-05-02 21:10:01 -0700143
144 // Mark all references to the alloc space(s).
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800145 void UpdateAndMarkReferences(RootCallback* callback, void* arg)
Ian Rogers1d54e732013-05-02 21:10:01 -0700146 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
147 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
148
149 // Nothing to verify.
150 void Verify() {}
151
152 void Dump(std::ostream& os);
153
154 protected:
155 // Cleared card array, used to update the mod-union table.
Mathieu Chartier0a9dc052013-07-25 11:01:28 -0700156 CardSet cleared_cards_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700157};
158
159} // namespace accounting
160} // namespace gc
161} // namespace art
162
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700163#endif // ART_RUNTIME_GC_ACCOUNTING_MOD_UNION_TABLE_H_