blob: 2e232ca6a89cb893dbd172afde28704cc12cb9de [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 {
32 class Object;
33} // namespace mirror
34
35namespace gc {
36
37namespace collector {
38 class MarkSweep;
39} // namespace collector
40namespace space {
41 class ContinuousSpace;
42 class Space;
43} // namespace space
44
45class Heap;
46
47namespace accounting {
48
Mathieu Chartier4858a932015-01-23 13:18:53 -080049class Bitmap;
Ian Rogers1d54e732013-05-02 21:10:01 -070050class HeapBitmap;
51
52// The mod-union table is the union of modified cards. It is used to allow the card table to be
53// cleared between GC phases, reducing the number of dirty cards that need to be scanned.
54class ModUnionTable {
55 public:
Ian Rogers13735952014-10-08 12:43:28 -070056 typedef std::set<uint8_t*, std::less<uint8_t*>,
57 TrackingAllocator<uint8_t*, kAllocatorTagModUnionCardSet>> CardSet;
Mathieu Chartier4858a932015-01-23 13:18:53 -080058 typedef MemoryRangeBitmap<CardTable::kCardSize> CardBitmap;
Mathieu Chartier0a9dc052013-07-25 11:01:28 -070059
Mathieu Chartier11409ae2013-09-23 11:49:36 -070060 explicit ModUnionTable(const std::string& name, Heap* heap, space::ContinuousSpace* space)
61 : name_(name),
62 heap_(heap),
63 space_(space) {
64 }
Ian Rogers1d54e732013-05-02 21:10:01 -070065
Brian Carlstrom93ba8932013-07-17 21:31:49 -070066 virtual ~ModUnionTable() {}
Ian Rogers1d54e732013-05-02 21:10:01 -070067
68 // Clear cards which map to a memory range of a space. This doesn't immediately update the
69 // mod-union table, as updating the mod-union table may have an associated cost, such as
70 // determining references to track.
Mathieu Chartier11409ae2013-09-23 11:49:36 -070071 virtual void ClearCards() = 0;
Ian Rogers1d54e732013-05-02 21:10:01 -070072
Mathieu Chartiere4cab172014-08-19 18:24:04 -070073 // Set all the cards.
74 virtual void SetCards() = 0;
75
Ian Rogers1d54e732013-05-02 21:10:01 -070076 // Update the mod-union table using data stored by ClearCards. There may be multiple ClearCards
Mathieu Chartier11409ae2013-09-23 11:49:36 -070077 // before a call to update, for example, back-to-back sticky GCs. Also mark references to other
78 // spaces which are stored in the mod-union table.
Mathieu Chartier407f7022014-02-18 14:37:05 -080079 virtual void UpdateAndMarkReferences(MarkHeapReferenceCallback* callback, void* arg) = 0;
Ian Rogers1d54e732013-05-02 21:10:01 -070080
81 // Verification, sanity checks that we don't have clean cards which conflict with out cached data
82 // for said cards. Exclusive lock is required since verify sometimes uses
83 // SpaceBitmap::VisitMarkedRange and VisitMarkedRange can't know if the callback will modify the
84 // bitmap or not.
85 virtual void Verify() EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) = 0;
86
Mathieu Chartier4858a932015-01-23 13:18:53 -080087 // Returns true if a card is marked inside the mod union table. Used for testing. The address
88 // doesn't need to be aligned.
89 virtual bool ContainsCardFor(uintptr_t addr) = 0;
90
Ian Rogers1d54e732013-05-02 21:10:01 -070091 virtual void Dump(std::ostream& os) = 0;
Mathieu Chartier11409ae2013-09-23 11:49:36 -070092 space::ContinuousSpace* GetSpace() {
93 return space_;
94 }
Ian Rogers1d54e732013-05-02 21:10:01 -070095 Heap* GetHeap() const {
96 return heap_;
97 }
Mathieu Chartier11409ae2013-09-23 11:49:36 -070098 const std::string& GetName() const {
99 return name_;
100 }
Ian Rogers1d54e732013-05-02 21:10:01 -0700101
102 protected:
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700103 const std::string name_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700104 Heap* const heap_;
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700105 space::ContinuousSpace* const space_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700106};
107
108// Reference caching implementation. Caches references pointing to alloc space(s) for each card.
109class ModUnionTableReferenceCache : public ModUnionTable {
110 public:
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700111 explicit ModUnionTableReferenceCache(const std::string& name, Heap* heap,
112 space::ContinuousSpace* space)
113 : ModUnionTable(name, heap, space) {}
Ian Rogers1d54e732013-05-02 21:10:01 -0700114 virtual ~ModUnionTableReferenceCache() {}
115
116 // Clear and store cards for a space.
Mathieu Chartier4858a932015-01-23 13:18:53 -0800117 void ClearCards() OVERRIDE;
Ian Rogers1d54e732013-05-02 21:10:01 -0700118
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700119 // Update table based on cleared cards and mark all references to the other spaces.
Mathieu Chartier4858a932015-01-23 13:18:53 -0800120 void UpdateAndMarkReferences(MarkHeapReferenceCallback* callback, void* arg) OVERRIDE
Ian Rogers1d54e732013-05-02 21:10:01 -0700121 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
122 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
123
124 // Exclusive lock is required since verify uses SpaceBitmap::VisitMarkedRange and
125 // VisitMarkedRange can't know if the callback will modify the bitmap or not.
Mathieu Chartier4858a932015-01-23 13:18:53 -0800126 void Verify() OVERRIDE
Ian Rogersef7d42f2014-01-06 12:55:46 -0800127 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
128 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Ian Rogers1d54e732013-05-02 21:10:01 -0700129
130 // Function that tells whether or not to add a reference to the table.
Mathieu Chartier4aeec172014-03-27 16:09:46 -0700131 virtual bool ShouldAddReference(const mirror::Object* ref) const = 0;
Ian Rogers1d54e732013-05-02 21:10:01 -0700132
Mathieu Chartier4858a932015-01-23 13:18:53 -0800133 virtual bool ContainsCardFor(uintptr_t addr) OVERRIDE;
Ian Rogers1d54e732013-05-02 21:10:01 -0700134
Mathieu Chartier4858a932015-01-23 13:18:53 -0800135 virtual void Dump(std::ostream& os) OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
136
137 virtual void SetCards() OVERRIDE;
Mathieu Chartiere4cab172014-08-19 18:24:04 -0700138
Ian Rogers1d54e732013-05-02 21:10:01 -0700139 protected:
140 // Cleared card array, used to update the mod-union table.
Mathieu Chartier0a9dc052013-07-25 11:01:28 -0700141 ModUnionTable::CardSet cleared_cards_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700142
143 // Maps from dirty cards to their corresponding alloc space references.
Ian Rogers13735952014-10-08 12:43:28 -0700144 AllocationTrackingSafeMap<const uint8_t*, std::vector<mirror::HeapReference<mirror::Object>*>,
Mathieu Chartierbad02672014-08-25 13:08:22 -0700145 kAllocatorTagModUnionReferenceArray> references_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700146};
147
148// Card caching implementation. Keeps track of which cards we cleared and only this information.
149class ModUnionTableCardCache : public ModUnionTable {
150 public:
Mathieu Chartier4858a932015-01-23 13:18:53 -0800151 // Note: There is assumption that the space End() doesn't change.
152 explicit ModUnionTableCardCache(const std::string& name, Heap* heap,
153 space::ContinuousSpace* space);
Ian Rogers1d54e732013-05-02 21:10:01 -0700154 virtual ~ModUnionTableCardCache() {}
155
156 // Clear and store cards for a space.
Mathieu Chartier4858a932015-01-23 13:18:53 -0800157 virtual void ClearCards() OVERRIDE;
Ian Rogers1d54e732013-05-02 21:10:01 -0700158
159 // Mark all references to the alloc space(s).
Mathieu Chartier4858a932015-01-23 13:18:53 -0800160 virtual void UpdateAndMarkReferences(MarkHeapReferenceCallback* callback, void* arg) OVERRIDE
Ian Rogers1d54e732013-05-02 21:10:01 -0700161 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
162 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
163
164 // Nothing to verify.
Mathieu Chartier4858a932015-01-23 13:18:53 -0800165 virtual void Verify() OVERRIDE {}
Ian Rogers1d54e732013-05-02 21:10:01 -0700166
Mathieu Chartier4858a932015-01-23 13:18:53 -0800167 virtual void Dump(std::ostream& os) OVERRIDE;
Ian Rogers1d54e732013-05-02 21:10:01 -0700168
Mathieu Chartier4858a932015-01-23 13:18:53 -0800169 virtual bool ContainsCardFor(uintptr_t addr) OVERRIDE;
170
171 // Sets all the cards in the mod union table to be marked.
172 virtual void SetCards() OVERRIDE;
Mathieu Chartiere4cab172014-08-19 18:24:04 -0700173
Ian Rogers1d54e732013-05-02 21:10:01 -0700174 protected:
Mathieu Chartier4858a932015-01-23 13:18:53 -0800175 // Cleared card bitmap, used to update the mod-union table.
176 std::unique_ptr<CardBitmap> card_bitmap_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700177};
178
179} // namespace accounting
180} // namespace gc
181} // namespace art
182
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700183#endif // ART_RUNTIME_GC_ACCOUNTING_MOD_UNION_TABLE_H_