blob: 591365f33a75eebdc07f706e86a72d02716577c3 [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 Chartier4858a932015-01-23 13:18:53 -080020#include "bitmap.h"
Mathieu Chartierbad02672014-08-25 13:08:22 -070021#include "base/allocator.h"
Mathieu Chartier4858a932015-01-23 13:18:53 -080022#include "card_table.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070023#include "globals.h"
Mathieu Chartier83c8ee02014-01-28 14:50:23 -080024#include "object_callbacks.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070025#include "safe_map.h"
26
27#include <set>
28#include <vector>
29
30namespace art {
31namespace mirror {
Mathieu Chartierc8950822015-09-15 14:07:10 -070032class Object;
Ian Rogers1d54e732013-05-02 21:10:01 -070033} // namespace mirror
34
35namespace gc {
Ian Rogers1d54e732013-05-02 21:10:01 -070036namespace space {
37 class ContinuousSpace;
Ian Rogers1d54e732013-05-02 21:10:01 -070038} // namespace space
Ian Rogers1d54e732013-05-02 21:10:01 -070039class Heap;
40
41namespace accounting {
42
Ian Rogers1d54e732013-05-02 21:10:01 -070043// The mod-union table is the union of modified cards. It is used to allow the card table to be
44// cleared between GC phases, reducing the number of dirty cards that need to be scanned.
45class ModUnionTable {
46 public:
Ian Rogers13735952014-10-08 12:43:28 -070047 typedef std::set<uint8_t*, std::less<uint8_t*>,
48 TrackingAllocator<uint8_t*, kAllocatorTagModUnionCardSet>> CardSet;
Mathieu Chartier4858a932015-01-23 13:18:53 -080049 typedef MemoryRangeBitmap<CardTable::kCardSize> CardBitmap;
Mathieu Chartier0a9dc052013-07-25 11:01:28 -070050
Mathieu Chartier11409ae2013-09-23 11:49:36 -070051 explicit ModUnionTable(const std::string& name, Heap* heap, space::ContinuousSpace* space)
52 : name_(name),
53 heap_(heap),
Mathieu Chartierc8950822015-09-15 14:07:10 -070054 space_(space) {}
Ian Rogers1d54e732013-05-02 21:10:01 -070055
Brian Carlstrom93ba8932013-07-17 21:31:49 -070056 virtual ~ModUnionTable() {}
Ian Rogers1d54e732013-05-02 21:10:01 -070057
Mathieu Chartier6e6078a2016-10-24 15:45:41 -070058 // Process cards for a memory range of a space. This doesn't immediately update the mod-union
59 // table, as updating the mod-union table may have an associated cost, such as determining
60 // references to track.
61 virtual void ProcessCards() = 0;
Ian Rogers1d54e732013-05-02 21:10:01 -070062
Mathieu Chartiere4cab172014-08-19 18:24:04 -070063 // Set all the cards.
64 virtual void SetCards() = 0;
65
Mathieu Chartier962cd7a2016-08-16 12:15:59 -070066 // Clear all of the table.
67 virtual void ClearTable() = 0;
68
Mathieu Chartier6e6078a2016-10-24 15:45:41 -070069 // Update the mod-union table using data stored by ProcessCards. There may be multiple
70 // ProcessCards before a call to update, for example, back-to-back sticky GCs. Also mark
71 // references to other spaces which are stored in the mod-union table.
Mathieu Chartier97509952015-07-13 14:35:43 -070072 virtual void UpdateAndMarkReferences(MarkObjectVisitor* visitor) = 0;
Ian Rogers1d54e732013-05-02 21:10:01 -070073
Mathieu Chartier21328a12016-07-22 10:47:45 -070074 // Visit all of the objects that may contain references to other spaces.
75 virtual void VisitObjects(ObjectCallback* callback, void* arg) = 0;
76
Ian Rogers1d54e732013-05-02 21:10:01 -070077 // Verification, sanity checks that we don't have clean cards which conflict with out cached data
78 // for said cards. Exclusive lock is required since verify sometimes uses
79 // SpaceBitmap::VisitMarkedRange and VisitMarkedRange can't know if the callback will modify the
80 // bitmap or not.
Mathieu Chartier90443472015-07-16 20:32:27 -070081 virtual void Verify() REQUIRES(Locks::heap_bitmap_lock_) = 0;
Ian Rogers1d54e732013-05-02 21:10:01 -070082
Mathieu Chartier4858a932015-01-23 13:18:53 -080083 // Returns true if a card is marked inside the mod union table. Used for testing. The address
84 // doesn't need to be aligned.
85 virtual bool ContainsCardFor(uintptr_t addr) = 0;
86
Mathieu Chartier21328a12016-07-22 10:47:45 -070087 // Filter out cards that don't need to be marked. Automatically done with UpdateAndMarkReferences.
88 void FilterCards();
89
Ian Rogers1d54e732013-05-02 21:10:01 -070090 virtual void Dump(std::ostream& os) = 0;
Mathieu Chartierc8950822015-09-15 14:07:10 -070091
Mathieu Chartier11409ae2013-09-23 11:49:36 -070092 space::ContinuousSpace* GetSpace() {
93 return space_;
94 }
Mathieu Chartierc8950822015-09-15 14:07:10 -070095
Ian Rogers1d54e732013-05-02 21:10:01 -070096 Heap* GetHeap() const {
97 return heap_;
98 }
Mathieu Chartierc8950822015-09-15 14:07:10 -070099
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700100 const std::string& GetName() const {
101 return name_;
102 }
Ian Rogers1d54e732013-05-02 21:10:01 -0700103
104 protected:
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700105 const std::string name_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700106 Heap* const heap_;
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700107 space::ContinuousSpace* const space_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700108};
109
110// Reference caching implementation. Caches references pointing to alloc space(s) for each card.
111class ModUnionTableReferenceCache : public ModUnionTable {
112 public:
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700113 explicit ModUnionTableReferenceCache(const std::string& name, Heap* heap,
114 space::ContinuousSpace* space)
115 : ModUnionTable(name, heap, space) {}
Mathieu Chartierc8950822015-09-15 14:07:10 -0700116
Ian Rogers1d54e732013-05-02 21:10:01 -0700117 virtual ~ModUnionTableReferenceCache() {}
118
119 // Clear and store cards for a space.
Mathieu Chartier6e6078a2016-10-24 15:45:41 -0700120 void ProcessCards() OVERRIDE;
Ian Rogers1d54e732013-05-02 21:10:01 -0700121
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700122 // Update table based on cleared cards and mark all references to the other spaces.
Mathieu Chartier97509952015-07-13 14:35:43 -0700123 void UpdateAndMarkReferences(MarkObjectVisitor* visitor) OVERRIDE
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700124 REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -0700125 REQUIRES(Locks::heap_bitmap_lock_);
Ian Rogers1d54e732013-05-02 21:10:01 -0700126
Mathieu Chartier21328a12016-07-22 10:47:45 -0700127 virtual void VisitObjects(ObjectCallback* callback, void* arg) OVERRIDE
128 REQUIRES(Locks::heap_bitmap_lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700129 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier21328a12016-07-22 10:47:45 -0700130
Ian Rogers1d54e732013-05-02 21:10:01 -0700131 // Exclusive lock is required since verify uses SpaceBitmap::VisitMarkedRange and
132 // VisitMarkedRange can't know if the callback will modify the bitmap or not.
Mathieu Chartier4858a932015-01-23 13:18:53 -0800133 void Verify() OVERRIDE
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700134 REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -0700135 REQUIRES(Locks::heap_bitmap_lock_);
Ian Rogers1d54e732013-05-02 21:10:01 -0700136
137 // Function that tells whether or not to add a reference to the table.
Mathieu Chartier4aeec172014-03-27 16:09:46 -0700138 virtual bool ShouldAddReference(const mirror::Object* ref) const = 0;
Ian Rogers1d54e732013-05-02 21:10:01 -0700139
Mathieu Chartier4858a932015-01-23 13:18:53 -0800140 virtual bool ContainsCardFor(uintptr_t addr) OVERRIDE;
Ian Rogers1d54e732013-05-02 21:10:01 -0700141
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700142 virtual void Dump(std::ostream& os) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier4858a932015-01-23 13:18:53 -0800143
144 virtual void SetCards() OVERRIDE;
Mathieu Chartiere4cab172014-08-19 18:24:04 -0700145
Mathieu Chartier962cd7a2016-08-16 12:15:59 -0700146 virtual void ClearTable() OVERRIDE;
147
Ian Rogers1d54e732013-05-02 21:10:01 -0700148 protected:
149 // Cleared card array, used to update the mod-union table.
Mathieu Chartier0a9dc052013-07-25 11:01:28 -0700150 ModUnionTable::CardSet cleared_cards_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700151
152 // Maps from dirty cards to their corresponding alloc space references.
Ian Rogers13735952014-10-08 12:43:28 -0700153 AllocationTrackingSafeMap<const uint8_t*, std::vector<mirror::HeapReference<mirror::Object>*>,
Mathieu Chartierbad02672014-08-25 13:08:22 -0700154 kAllocatorTagModUnionReferenceArray> references_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700155};
156
157// Card caching implementation. Keeps track of which cards we cleared and only this information.
158class ModUnionTableCardCache : public ModUnionTable {
159 public:
Mathieu Chartier4858a932015-01-23 13:18:53 -0800160 // Note: There is assumption that the space End() doesn't change.
161 explicit ModUnionTableCardCache(const std::string& name, Heap* heap,
162 space::ContinuousSpace* space);
Mathieu Chartierc8950822015-09-15 14:07:10 -0700163
Ian Rogers1d54e732013-05-02 21:10:01 -0700164 virtual ~ModUnionTableCardCache() {}
165
166 // Clear and store cards for a space.
Mathieu Chartier6e6078a2016-10-24 15:45:41 -0700167 virtual void ProcessCards() OVERRIDE;
Ian Rogers1d54e732013-05-02 21:10:01 -0700168
169 // Mark all references to the alloc space(s).
Mathieu Chartier97509952015-07-13 14:35:43 -0700170 virtual void UpdateAndMarkReferences(MarkObjectVisitor* visitor) OVERRIDE
Mathieu Chartier90443472015-07-16 20:32:27 -0700171 REQUIRES(Locks::heap_bitmap_lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700172 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers1d54e732013-05-02 21:10:01 -0700173
Mathieu Chartier21328a12016-07-22 10:47:45 -0700174 virtual void VisitObjects(ObjectCallback* callback, void* arg) OVERRIDE
175 REQUIRES(Locks::heap_bitmap_lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700176 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier21328a12016-07-22 10:47:45 -0700177
Ian Rogers1d54e732013-05-02 21:10:01 -0700178 // Nothing to verify.
Mathieu Chartier4858a932015-01-23 13:18:53 -0800179 virtual void Verify() OVERRIDE {}
Ian Rogers1d54e732013-05-02 21:10:01 -0700180
Mathieu Chartier4858a932015-01-23 13:18:53 -0800181 virtual void Dump(std::ostream& os) OVERRIDE;
Ian Rogers1d54e732013-05-02 21:10:01 -0700182
Mathieu Chartier4858a932015-01-23 13:18:53 -0800183 virtual bool ContainsCardFor(uintptr_t addr) OVERRIDE;
184
Mathieu Chartier4858a932015-01-23 13:18:53 -0800185 virtual void SetCards() OVERRIDE;
Mathieu Chartiere4cab172014-08-19 18:24:04 -0700186
Mathieu Chartier962cd7a2016-08-16 12:15:59 -0700187 virtual void ClearTable() OVERRIDE;
188
Ian Rogers1d54e732013-05-02 21:10:01 -0700189 protected:
Mathieu Chartier4858a932015-01-23 13:18:53 -0800190 // Cleared card bitmap, used to update the mod-union table.
191 std::unique_ptr<CardBitmap> card_bitmap_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700192};
193
194} // namespace accounting
195} // namespace gc
196} // namespace art
197
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700198#endif // ART_RUNTIME_GC_ACCOUNTING_MOD_UNION_TABLE_H_