blob: 5ae7c77c191326037eb825d12903565fb90e5326 [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
Ian Rogers1d54e732013-05-02 21:10:01 -070047class HeapBitmap;
48
49// The mod-union table is the union of modified cards. It is used to allow the card table to be
50// cleared between GC phases, reducing the number of dirty cards that need to be scanned.
51class ModUnionTable {
52 public:
Ian Rogers8d31bbd2013-10-13 10:44:14 -070053 typedef std::set<byte*, std::less<byte*>, GcAllocator<byte*> > CardSet;
Mathieu Chartier0a9dc052013-07-25 11:01:28 -070054
Mathieu Chartier11409ae2013-09-23 11:49:36 -070055 explicit ModUnionTable(const std::string& name, Heap* heap, space::ContinuousSpace* space)
56 : name_(name),
57 heap_(heap),
58 space_(space) {
59 }
Ian Rogers1d54e732013-05-02 21:10:01 -070060
Brian Carlstrom93ba8932013-07-17 21:31:49 -070061 virtual ~ModUnionTable() {}
Ian Rogers1d54e732013-05-02 21:10:01 -070062
63 // Clear cards which map to a memory range of a space. This doesn't immediately update the
64 // mod-union table, as updating the mod-union table may have an associated cost, such as
65 // determining references to track.
Mathieu Chartier11409ae2013-09-23 11:49:36 -070066 virtual void ClearCards() = 0;
Ian Rogers1d54e732013-05-02 21:10:01 -070067
68 // Update the mod-union table using data stored by ClearCards. There may be multiple ClearCards
Mathieu Chartier11409ae2013-09-23 11:49:36 -070069 // before a call to update, for example, back-to-back sticky GCs. Also mark references to other
70 // spaces which are stored in the mod-union table.
Mathieu Chartier407f7022014-02-18 14:37:05 -080071 virtual void UpdateAndMarkReferences(MarkHeapReferenceCallback* callback, void* arg) = 0;
Ian Rogers1d54e732013-05-02 21:10:01 -070072
73 // Verification, sanity checks that we don't have clean cards which conflict with out cached data
74 // for said cards. Exclusive lock is required since verify sometimes uses
75 // SpaceBitmap::VisitMarkedRange and VisitMarkedRange can't know if the callback will modify the
76 // bitmap or not.
77 virtual void Verify() EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) = 0;
78
79 virtual void Dump(std::ostream& os) = 0;
Mathieu Chartier11409ae2013-09-23 11:49:36 -070080 space::ContinuousSpace* GetSpace() {
81 return space_;
82 }
Ian Rogers1d54e732013-05-02 21:10:01 -070083 Heap* GetHeap() const {
84 return heap_;
85 }
Mathieu Chartier11409ae2013-09-23 11:49:36 -070086 const std::string& GetName() const {
87 return name_;
88 }
Ian Rogers1d54e732013-05-02 21:10:01 -070089
90 protected:
Mathieu Chartier11409ae2013-09-23 11:49:36 -070091 const std::string name_;
Ian Rogers1d54e732013-05-02 21:10:01 -070092 Heap* const heap_;
Mathieu Chartier11409ae2013-09-23 11:49:36 -070093 space::ContinuousSpace* const space_;
Ian Rogers1d54e732013-05-02 21:10:01 -070094};
95
96// Reference caching implementation. Caches references pointing to alloc space(s) for each card.
97class ModUnionTableReferenceCache : public ModUnionTable {
98 public:
Mathieu Chartier11409ae2013-09-23 11:49:36 -070099 explicit ModUnionTableReferenceCache(const std::string& name, Heap* heap,
100 space::ContinuousSpace* space)
101 : ModUnionTable(name, heap, space) {}
Ian Rogers1d54e732013-05-02 21:10:01 -0700102 virtual ~ModUnionTableReferenceCache() {}
103
104 // Clear and store cards for a space.
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700105 void ClearCards();
Ian Rogers1d54e732013-05-02 21:10:01 -0700106
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700107 // Update table based on cleared cards and mark all references to the other spaces.
Mathieu Chartier407f7022014-02-18 14:37:05 -0800108 void UpdateAndMarkReferences(MarkHeapReferenceCallback* callback, void* arg)
Ian Rogers1d54e732013-05-02 21:10:01 -0700109 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
110 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
111
112 // Exclusive lock is required since verify uses SpaceBitmap::VisitMarkedRange and
113 // VisitMarkedRange can't know if the callback will modify the bitmap or not.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800114 void Verify()
115 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
116 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Ian Rogers1d54e732013-05-02 21:10:01 -0700117
118 // Function that tells whether or not to add a reference to the table.
Mathieu Chartier4aeec172014-03-27 16:09:46 -0700119 virtual bool ShouldAddReference(const mirror::Object* ref) const = 0;
Ian Rogers1d54e732013-05-02 21:10:01 -0700120
Ian Rogersef7d42f2014-01-06 12:55:46 -0800121 void Dump(std::ostream& os) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers1d54e732013-05-02 21:10:01 -0700122
123 protected:
124 // Cleared card array, used to update the mod-union table.
Mathieu Chartier0a9dc052013-07-25 11:01:28 -0700125 ModUnionTable::CardSet cleared_cards_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700126
127 // Maps from dirty cards to their corresponding alloc space references.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800128 SafeMap<const byte*, std::vector<mirror::HeapReference<mirror::Object>*>, std::less<const byte*>,
129 GcAllocator<std::pair<const byte*, std::vector<mirror::HeapReference<mirror::Object>*> > > >
130 references_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700131};
132
133// Card caching implementation. Keeps track of which cards we cleared and only this information.
134class ModUnionTableCardCache : public ModUnionTable {
135 public:
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700136 explicit ModUnionTableCardCache(const std::string& name, Heap* heap, space::ContinuousSpace* space)
137 : ModUnionTable(name, heap, space) {}
Ian Rogers1d54e732013-05-02 21:10:01 -0700138 virtual ~ModUnionTableCardCache() {}
139
140 // Clear and store cards for a space.
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700141 void ClearCards();
Ian Rogers1d54e732013-05-02 21:10:01 -0700142
143 // Mark all references to the alloc space(s).
Mathieu Chartier407f7022014-02-18 14:37:05 -0800144 void UpdateAndMarkReferences(MarkHeapReferenceCallback* callback, void* arg)
Ian Rogers1d54e732013-05-02 21:10:01 -0700145 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
146 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
147
148 // Nothing to verify.
149 void Verify() {}
150
151 void Dump(std::ostream& os);
152
153 protected:
154 // Cleared card array, used to update the mod-union table.
Mathieu Chartier0a9dc052013-07-25 11:01:28 -0700155 CardSet cleared_cards_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700156};
157
158} // namespace accounting
159} // namespace gc
160} // namespace art
161
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700162#endif // ART_RUNTIME_GC_ACCOUNTING_MOD_UNION_TABLE_H_