blob: bf665c50dcb41adbaa081dca09d7f9f50a48a9d5 [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
Mathieu Chartier21328a12016-07-22 10:47:45 -070071 // Visit all of the objects that may contain references to other spaces.
72 virtual void VisitObjects(ObjectCallback* callback, void* arg) = 0;
73
Ian Rogers1d54e732013-05-02 21:10:01 -070074 // 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.
Mathieu Chartier90443472015-07-16 20:32:27 -070078 virtual void Verify() REQUIRES(Locks::heap_bitmap_lock_) = 0;
Ian Rogers1d54e732013-05-02 21:10:01 -070079
Mathieu Chartier4858a932015-01-23 13:18:53 -080080 // Returns true if a card is marked inside the mod union table. Used for testing. The address
81 // doesn't need to be aligned.
82 virtual bool ContainsCardFor(uintptr_t addr) = 0;
83
Mathieu Chartier21328a12016-07-22 10:47:45 -070084 // Filter out cards that don't need to be marked. Automatically done with UpdateAndMarkReferences.
85 void FilterCards();
86
Ian Rogers1d54e732013-05-02 21:10:01 -070087 virtual void Dump(std::ostream& os) = 0;
Mathieu Chartierc8950822015-09-15 14:07:10 -070088
Mathieu Chartier11409ae2013-09-23 11:49:36 -070089 space::ContinuousSpace* GetSpace() {
90 return space_;
91 }
Mathieu Chartierc8950822015-09-15 14:07:10 -070092
Ian Rogers1d54e732013-05-02 21:10:01 -070093 Heap* GetHeap() const {
94 return heap_;
95 }
Mathieu Chartierc8950822015-09-15 14:07:10 -070096
Mathieu Chartier11409ae2013-09-23 11:49:36 -070097 const std::string& GetName() const {
98 return name_;
99 }
Ian Rogers1d54e732013-05-02 21:10:01 -0700100
101 protected:
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700102 const std::string name_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700103 Heap* const heap_;
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700104 space::ContinuousSpace* const space_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700105};
106
107// Reference caching implementation. Caches references pointing to alloc space(s) for each card.
108class ModUnionTableReferenceCache : public ModUnionTable {
109 public:
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700110 explicit ModUnionTableReferenceCache(const std::string& name, Heap* heap,
111 space::ContinuousSpace* space)
112 : ModUnionTable(name, heap, space) {}
Mathieu Chartierc8950822015-09-15 14:07:10 -0700113
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 Chartier97509952015-07-13 14:35:43 -0700120 void UpdateAndMarkReferences(MarkObjectVisitor* visitor) 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
Mathieu Chartier21328a12016-07-22 10:47:45 -0700124 virtual void VisitObjects(ObjectCallback* callback, void* arg) OVERRIDE
125 REQUIRES(Locks::heap_bitmap_lock_)
126 SHARED_REQUIRES(Locks::mutator_lock_);
127
Ian Rogers1d54e732013-05-02 21:10:01 -0700128 // Exclusive lock is required since verify uses SpaceBitmap::VisitMarkedRange and
129 // VisitMarkedRange can't know if the callback will modify the bitmap or not.
Mathieu Chartier4858a932015-01-23 13:18:53 -0800130 void Verify() OVERRIDE
Mathieu Chartier90443472015-07-16 20:32:27 -0700131 SHARED_REQUIRES(Locks::mutator_lock_)
132 REQUIRES(Locks::heap_bitmap_lock_);
Ian Rogers1d54e732013-05-02 21:10:01 -0700133
134 // Function that tells whether or not to add a reference to the table.
Mathieu Chartier4aeec172014-03-27 16:09:46 -0700135 virtual bool ShouldAddReference(const mirror::Object* ref) const = 0;
Ian Rogers1d54e732013-05-02 21:10:01 -0700136
Mathieu Chartier4858a932015-01-23 13:18:53 -0800137 virtual bool ContainsCardFor(uintptr_t addr) OVERRIDE;
Ian Rogers1d54e732013-05-02 21:10:01 -0700138
Mathieu Chartier90443472015-07-16 20:32:27 -0700139 virtual void Dump(std::ostream& os) OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartier4858a932015-01-23 13:18:53 -0800140
141 virtual void SetCards() OVERRIDE;
Mathieu Chartiere4cab172014-08-19 18:24:04 -0700142
Ian Rogers1d54e732013-05-02 21:10:01 -0700143 protected:
144 // Cleared card array, used to update the mod-union table.
Mathieu Chartier0a9dc052013-07-25 11:01:28 -0700145 ModUnionTable::CardSet cleared_cards_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700146
147 // Maps from dirty cards to their corresponding alloc space references.
Ian Rogers13735952014-10-08 12:43:28 -0700148 AllocationTrackingSafeMap<const uint8_t*, std::vector<mirror::HeapReference<mirror::Object>*>,
Mathieu Chartierbad02672014-08-25 13:08:22 -0700149 kAllocatorTagModUnionReferenceArray> references_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700150};
151
152// Card caching implementation. Keeps track of which cards we cleared and only this information.
153class ModUnionTableCardCache : public ModUnionTable {
154 public:
Mathieu Chartier4858a932015-01-23 13:18:53 -0800155 // Note: There is assumption that the space End() doesn't change.
156 explicit ModUnionTableCardCache(const std::string& name, Heap* heap,
157 space::ContinuousSpace* space);
Mathieu Chartierc8950822015-09-15 14:07:10 -0700158
Ian Rogers1d54e732013-05-02 21:10:01 -0700159 virtual ~ModUnionTableCardCache() {}
160
161 // Clear and store cards for a space.
Mathieu Chartier4858a932015-01-23 13:18:53 -0800162 virtual void ClearCards() OVERRIDE;
Ian Rogers1d54e732013-05-02 21:10:01 -0700163
164 // Mark all references to the alloc space(s).
Mathieu Chartier97509952015-07-13 14:35:43 -0700165 virtual void UpdateAndMarkReferences(MarkObjectVisitor* visitor) OVERRIDE
Mathieu Chartier90443472015-07-16 20:32:27 -0700166 REQUIRES(Locks::heap_bitmap_lock_)
167 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers1d54e732013-05-02 21:10:01 -0700168
Mathieu Chartier21328a12016-07-22 10:47:45 -0700169 virtual void VisitObjects(ObjectCallback* callback, void* arg) OVERRIDE
170 REQUIRES(Locks::heap_bitmap_lock_)
171 SHARED_REQUIRES(Locks::mutator_lock_);
172
Ian Rogers1d54e732013-05-02 21:10:01 -0700173 // Nothing to verify.
Mathieu Chartier4858a932015-01-23 13:18:53 -0800174 virtual void Verify() OVERRIDE {}
Ian Rogers1d54e732013-05-02 21:10:01 -0700175
Mathieu Chartier4858a932015-01-23 13:18:53 -0800176 virtual void Dump(std::ostream& os) OVERRIDE;
Ian Rogers1d54e732013-05-02 21:10:01 -0700177
Mathieu Chartier4858a932015-01-23 13:18:53 -0800178 virtual bool ContainsCardFor(uintptr_t addr) OVERRIDE;
179
180 // Sets all the cards in the mod union table to be marked.
181 virtual void SetCards() OVERRIDE;
Mathieu Chartiere4cab172014-08-19 18:24:04 -0700182
Ian Rogers1d54e732013-05-02 21:10:01 -0700183 protected:
Mathieu Chartier4858a932015-01-23 13:18:53 -0800184 // Cleared card bitmap, used to update the mod-union table.
185 std::unique_ptr<CardBitmap> card_bitmap_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700186};
187
188} // namespace accounting
189} // namespace gc
190} // namespace art
191
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700192#endif // ART_RUNTIME_GC_ACCOUNTING_MOD_UNION_TABLE_H_