blob: a7a4246fcf6b9063ca2b32505f63073f5e16c3f3 [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
58 // Clear cards which map to a memory range of a space. This doesn't immediately update the
59 // mod-union table, as updating the mod-union table may have an associated cost, such as
60 // determining references to track.
Mathieu Chartier11409ae2013-09-23 11:49:36 -070061 virtual void ClearCards() = 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
Ian Rogers1d54e732013-05-02 21:10:01 -070066 // Update the mod-union table using data stored by ClearCards. There may be multiple ClearCards
Mathieu Chartier11409ae2013-09-23 11:49:36 -070067 // before a call to update, for example, back-to-back sticky GCs. Also mark references to other
68 // spaces which are stored in the mod-union table.
Mathieu Chartier97509952015-07-13 14:35:43 -070069 virtual void UpdateAndMarkReferences(MarkObjectVisitor* visitor) = 0;
Ian Rogers1d54e732013-05-02 21:10:01 -070070
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.
Mathieu Chartier90443472015-07-16 20:32:27 -070075 virtual void Verify() REQUIRES(Locks::heap_bitmap_lock_) = 0;
Ian Rogers1d54e732013-05-02 21:10:01 -070076
Mathieu Chartier4858a932015-01-23 13:18:53 -080077 // Returns true if a card is marked inside the mod union table. Used for testing. The address
78 // doesn't need to be aligned.
79 virtual bool ContainsCardFor(uintptr_t addr) = 0;
80
Ian Rogers1d54e732013-05-02 21:10:01 -070081 virtual void Dump(std::ostream& os) = 0;
Mathieu Chartierc8950822015-09-15 14:07:10 -070082
Mathieu Chartier11409ae2013-09-23 11:49:36 -070083 space::ContinuousSpace* GetSpace() {
84 return space_;
85 }
Mathieu Chartierc8950822015-09-15 14:07:10 -070086
Ian Rogers1d54e732013-05-02 21:10:01 -070087 Heap* GetHeap() const {
88 return heap_;
89 }
Mathieu Chartierc8950822015-09-15 14:07:10 -070090
Mathieu Chartier11409ae2013-09-23 11:49:36 -070091 const std::string& GetName() const {
92 return name_;
93 }
Ian Rogers1d54e732013-05-02 21:10:01 -070094
95 protected:
Mathieu Chartier11409ae2013-09-23 11:49:36 -070096 const std::string name_;
Ian Rogers1d54e732013-05-02 21:10:01 -070097 Heap* const heap_;
Mathieu Chartier11409ae2013-09-23 11:49:36 -070098 space::ContinuousSpace* const space_;
Ian Rogers1d54e732013-05-02 21:10:01 -070099};
100
101// Reference caching implementation. Caches references pointing to alloc space(s) for each card.
102class ModUnionTableReferenceCache : public ModUnionTable {
103 public:
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700104 explicit ModUnionTableReferenceCache(const std::string& name, Heap* heap,
105 space::ContinuousSpace* space)
106 : ModUnionTable(name, heap, space) {}
Mathieu Chartierc8950822015-09-15 14:07:10 -0700107
Ian Rogers1d54e732013-05-02 21:10:01 -0700108 virtual ~ModUnionTableReferenceCache() {}
109
110 // Clear and store cards for a space.
Mathieu Chartier4858a932015-01-23 13:18:53 -0800111 void ClearCards() OVERRIDE;
Ian Rogers1d54e732013-05-02 21:10:01 -0700112
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700113 // Update table based on cleared cards and mark all references to the other spaces.
Mathieu Chartier97509952015-07-13 14:35:43 -0700114 void UpdateAndMarkReferences(MarkObjectVisitor* visitor) OVERRIDE
Mathieu Chartier90443472015-07-16 20:32:27 -0700115 SHARED_REQUIRES(Locks::mutator_lock_)
116 REQUIRES(Locks::heap_bitmap_lock_);
Ian Rogers1d54e732013-05-02 21:10:01 -0700117
118 // Exclusive lock is required since verify uses SpaceBitmap::VisitMarkedRange and
119 // VisitMarkedRange can't know if the callback will modify the bitmap or not.
Mathieu Chartier4858a932015-01-23 13:18:53 -0800120 void Verify() OVERRIDE
Mathieu Chartier90443472015-07-16 20:32:27 -0700121 SHARED_REQUIRES(Locks::mutator_lock_)
122 REQUIRES(Locks::heap_bitmap_lock_);
Ian Rogers1d54e732013-05-02 21:10:01 -0700123
124 // Function that tells whether or not to add a reference to the table.
Mathieu Chartier4aeec172014-03-27 16:09:46 -0700125 virtual bool ShouldAddReference(const mirror::Object* ref) const = 0;
Ian Rogers1d54e732013-05-02 21:10:01 -0700126
Mathieu Chartier4858a932015-01-23 13:18:53 -0800127 virtual bool ContainsCardFor(uintptr_t addr) OVERRIDE;
Ian Rogers1d54e732013-05-02 21:10:01 -0700128
Mathieu Chartier90443472015-07-16 20:32:27 -0700129 virtual void Dump(std::ostream& os) OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartier4858a932015-01-23 13:18:53 -0800130
131 virtual void SetCards() OVERRIDE;
Mathieu Chartiere4cab172014-08-19 18:24:04 -0700132
Ian Rogers1d54e732013-05-02 21:10:01 -0700133 protected:
134 // Cleared card array, used to update the mod-union table.
Mathieu Chartier0a9dc052013-07-25 11:01:28 -0700135 ModUnionTable::CardSet cleared_cards_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700136
137 // Maps from dirty cards to their corresponding alloc space references.
Ian Rogers13735952014-10-08 12:43:28 -0700138 AllocationTrackingSafeMap<const uint8_t*, std::vector<mirror::HeapReference<mirror::Object>*>,
Mathieu Chartierbad02672014-08-25 13:08:22 -0700139 kAllocatorTagModUnionReferenceArray> references_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700140};
141
142// Card caching implementation. Keeps track of which cards we cleared and only this information.
143class ModUnionTableCardCache : public ModUnionTable {
144 public:
Mathieu Chartier4858a932015-01-23 13:18:53 -0800145 // Note: There is assumption that the space End() doesn't change.
146 explicit ModUnionTableCardCache(const std::string& name, Heap* heap,
147 space::ContinuousSpace* space);
Mathieu Chartierc8950822015-09-15 14:07:10 -0700148
Ian Rogers1d54e732013-05-02 21:10:01 -0700149 virtual ~ModUnionTableCardCache() {}
150
151 // Clear and store cards for a space.
Mathieu Chartier4858a932015-01-23 13:18:53 -0800152 virtual void ClearCards() OVERRIDE;
Ian Rogers1d54e732013-05-02 21:10:01 -0700153
154 // Mark all references to the alloc space(s).
Mathieu Chartier97509952015-07-13 14:35:43 -0700155 virtual void UpdateAndMarkReferences(MarkObjectVisitor* visitor) OVERRIDE
Mathieu Chartier90443472015-07-16 20:32:27 -0700156 REQUIRES(Locks::heap_bitmap_lock_)
157 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers1d54e732013-05-02 21:10:01 -0700158
159 // Nothing to verify.
Mathieu Chartier4858a932015-01-23 13:18:53 -0800160 virtual void Verify() OVERRIDE {}
Ian Rogers1d54e732013-05-02 21:10:01 -0700161
Mathieu Chartier4858a932015-01-23 13:18:53 -0800162 virtual void Dump(std::ostream& os) OVERRIDE;
Ian Rogers1d54e732013-05-02 21:10:01 -0700163
Mathieu Chartier4858a932015-01-23 13:18:53 -0800164 virtual bool ContainsCardFor(uintptr_t addr) OVERRIDE;
165
166 // Sets all the cards in the mod union table to be marked.
167 virtual void SetCards() OVERRIDE;
Mathieu Chartiere4cab172014-08-19 18:24:04 -0700168
Ian Rogers1d54e732013-05-02 21:10:01 -0700169 protected:
Mathieu Chartier4858a932015-01-23 13:18:53 -0800170 // Cleared card bitmap, used to update the mod-union table.
171 std::unique_ptr<CardBitmap> card_bitmap_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700172};
173
174} // namespace accounting
175} // namespace gc
176} // namespace art
177
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700178#endif // ART_RUNTIME_GC_ACCOUNTING_MOD_UNION_TABLE_H_