Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | */ |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 16 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_GC_COLLECTOR_MARK_SWEEP_H_ |
| 18 | #define ART_RUNTIME_GC_COLLECTOR_MARK_SWEEP_H_ |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 19 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 20 | #include "atomic.h" |
Sameer Abu Asal | 4aeb567 | 2013-02-19 15:30:35 -0800 | [diff] [blame] | 21 | #include "barrier.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 22 | #include "base/macros.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 23 | #include "base/mutex.h" |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 24 | #include "garbage_collector.h" |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 25 | #include "gc/accounting/heap_bitmap.h" |
Mathieu Chartier | 8d56210 | 2014-03-12 17:42:10 -0700 | [diff] [blame] | 26 | #include "immune_region.h" |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 27 | #include "object_callbacks.h" |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 28 | #include "offsets.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 29 | #include "UniquePtr.h" |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 30 | |
| 31 | namespace art { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 32 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 33 | namespace mirror { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 34 | class Class; |
| 35 | class Object; |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 36 | class Reference; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 37 | } // namespace mirror |
| 38 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 39 | class Thread; |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 40 | enum VisitRootFlags : uint8_t; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 41 | |
| 42 | namespace gc { |
| 43 | |
Mathieu Chartier | 4aeec17 | 2014-03-27 16:09:46 -0700 | [diff] [blame] | 44 | class Heap; |
| 45 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 46 | namespace accounting { |
Mathieu Chartier | 4aeec17 | 2014-03-27 16:09:46 -0700 | [diff] [blame] | 47 | template<typename T> class AtomicStack; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 48 | typedef AtomicStack<mirror::Object*> ObjectStack; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 49 | } // namespace accounting |
| 50 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 51 | namespace collector { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 52 | |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 53 | class MarkSweep : public GarbageCollector { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 54 | public: |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 55 | explicit MarkSweep(Heap* heap, bool is_concurrent, const std::string& name_prefix = ""); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 56 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 57 | ~MarkSweep() {} |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 58 | |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 59 | virtual void RunPhases() OVERRIDE NO_THREAD_SAFETY_ANALYSIS; |
| 60 | void InitializePhase(); |
| 61 | void MarkingPhase() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 62 | void PausePhase() EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 63 | void ReclaimPhase() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 64 | void FinishPhase() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 65 | virtual void MarkReachableObjects() |
| 66 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 67 | EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 68 | |
Hiroshi Yamauchi | 3e41780 | 2014-03-20 12:03:02 -0700 | [diff] [blame] | 69 | bool IsConcurrent() const { |
| 70 | return is_concurrent_; |
| 71 | } |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 72 | |
| 73 | virtual GcType GetGcType() const OVERRIDE { |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 74 | return kGcTypeFull; |
| 75 | } |
| 76 | |
Hiroshi Yamauchi | 3e41780 | 2014-03-20 12:03:02 -0700 | [diff] [blame] | 77 | virtual CollectorType GetCollectorType() const OVERRIDE { |
| 78 | return is_concurrent_ ? kCollectorTypeCMS : kCollectorTypeMS; |
| 79 | } |
| 80 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 81 | // Initializes internal structures. |
Jesse Wilson | 078f9b0 | 2011-11-18 17:51:47 -0500 | [diff] [blame] | 82 | void Init(); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 83 | |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 84 | // Find the default mark bitmap. |
Mathieu Chartier | 0e54cd0 | 2014-03-20 12:41:23 -0700 | [diff] [blame] | 85 | void FindDefaultSpaceBitmap(); |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 86 | |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 87 | // Marks all objects in the root set at the start of a garbage collection. |
| 88 | void MarkRoots(Thread* self) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 89 | EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) |
| 90 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 91 | |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 92 | void MarkNonThreadRoots() |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 93 | EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) |
| 94 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 95 | |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 96 | void MarkConcurrentRoots(VisitRootFlags flags) |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 97 | EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) |
| 98 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Mathieu Chartier | 9ebae1f | 2012-10-15 17:38:16 -0700 | [diff] [blame] | 99 | |
Mathieu Chartier | 0f7bf6a | 2014-03-28 10:05:39 -0700 | [diff] [blame] | 100 | void MarkRootsCheckpoint(Thread* self, bool revoke_ros_alloc_thread_local_buffers_at_checkpoint) |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 101 | EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) |
| 102 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 103 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 104 | // Builds a mark stack and recursively mark until it empties. |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 105 | void RecursiveMark() |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 106 | EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) |
| 107 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 108 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 109 | // Bind the live bits to the mark bits of bitmaps for spaces that are never collected, ie |
| 110 | // the image. Mark that portion of the heap as immune. |
| 111 | virtual void BindBitmaps() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 112 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 113 | // Builds a mark stack with objects on dirty cards and recursively mark until it empties. |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 114 | void RecursiveMarkDirtyObjects(bool paused, byte minimum_age) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 115 | EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) |
| 116 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 117 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 118 | // Remarks the root set after completing the concurrent mark. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 119 | void ReMarkRoots() |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 120 | EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) |
| 121 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 122 | |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 123 | void ProcessReferences(Thread* self) |
| 124 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 125 | |
Mathieu Chartier | 601276a | 2014-03-20 15:12:30 -0700 | [diff] [blame] | 126 | void PreProcessReferences() |
Mathieu Chartier | 1ad2784 | 2014-03-19 17:08:17 -0700 | [diff] [blame] | 127 | EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) |
| 128 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 129 | |
Mathieu Chartier | 4aeec17 | 2014-03-27 16:09:46 -0700 | [diff] [blame] | 130 | // Update and mark references from immune spaces. |
| 131 | void UpdateAndMarkModUnion() |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 132 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 133 | |
Mathieu Chartier | dda54f5 | 2014-02-24 09:58:40 -0800 | [diff] [blame] | 134 | // Pre clean cards to reduce how much work is needed in the pause. |
| 135 | void PreCleanCards() |
| 136 | EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) |
| 137 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 138 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 139 | // Sweeps unmarked objects to complete the garbage collection. Virtual as by default it sweeps |
| 140 | // all allocation spaces. Partial and sticky GCs want to just sweep a subset of the heap. |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 141 | virtual void Sweep(bool swap_bitmaps) EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 142 | |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 143 | // Sweeps unmarked objects to complete the garbage collection. |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 144 | void SweepLargeObjects(bool swap_bitmaps) EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 145 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 146 | // Sweep only pointers within an array. WARNING: Trashes objects. |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 147 | void SweepArray(accounting::ObjectStack* allocation_stack_, bool swap_bitmaps) |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 148 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 149 | EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 150 | |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 151 | // Blackens an object. |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 152 | void ScanObject(mirror::Object* obj) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 153 | EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) |
| 154 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 155 | |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 156 | // No thread safety analysis due to lambdas. |
| 157 | template<typename MarkVisitor, typename ReferenceVisitor> |
| 158 | void ScanObjectVisit(mirror::Object* obj, const MarkVisitor& visitor, |
| 159 | const ReferenceVisitor& ref_visitor) |
| 160 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 161 | EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 162 | |
Mathieu Chartier | 0f7bf6a | 2014-03-28 10:05:39 -0700 | [diff] [blame] | 163 | void SweepSystemWeaks(Thread* self) |
| 164 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) LOCKS_EXCLUDED(Locks::heap_bitmap_lock_); |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 165 | |
Mathieu Chartier | 6aa3df9 | 2013-09-17 15:17:28 -0700 | [diff] [blame] | 166 | static mirror::Object* VerifySystemWeakIsLiveCallback(mirror::Object* obj, void* arg) |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 167 | SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_); |
| 168 | |
| 169 | void VerifySystemWeaks() |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 170 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_); |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 171 | |
| 172 | // Verify that an object is live, either in a live bitmap or in the allocation stack. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 173 | void VerifyIsLive(const mirror::Object* obj) |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 174 | SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_); |
| 175 | |
Mathieu Chartier | 3bb57c7 | 2014-02-18 11:38:45 -0800 | [diff] [blame] | 176 | static mirror::Object* MarkObjectCallback(mirror::Object* obj, void* arg) |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 177 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 178 | EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_); |
| 179 | |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 180 | static void MarkHeapReferenceCallback(mirror::HeapReference<mirror::Object>* ref, void* arg) |
| 181 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 182 | EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_); |
| 183 | |
Mathieu Chartier | 815873e | 2014-02-13 18:02:13 -0800 | [diff] [blame] | 184 | static void MarkRootCallback(mirror::Object** root, void* arg, uint32_t thread_id, |
| 185 | RootType root_type) |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 186 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 187 | EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_); |
| 188 | |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 189 | static void VerifyRootMarked(mirror::Object** root, void* arg, uint32_t /*thread_id*/, |
| 190 | RootType /*root_type*/) |
| 191 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 192 | EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_); |
| 193 | |
Mathieu Chartier | 3bb57c7 | 2014-02-18 11:38:45 -0800 | [diff] [blame] | 194 | static void ProcessMarkStackPausedCallback(void* arg) |
| 195 | EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_); |
Mathieu Chartier | 815873e | 2014-02-13 18:02:13 -0800 | [diff] [blame] | 196 | |
| 197 | static void MarkRootParallelCallback(mirror::Object** root, void* arg, uint32_t thread_id, |
| 198 | RootType root_type) |
| 199 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Mathieu Chartier | ac86a7c | 2012-11-12 15:03:16 -0800 | [diff] [blame] | 200 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 201 | // Marks an object. |
Mathieu Chartier | 0e54cd0 | 2014-03-20 12:41:23 -0700 | [diff] [blame] | 202 | void MarkObject(mirror::Object* obj) |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 203 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 204 | EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_); |
| 205 | |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 206 | Barrier& GetBarrier() { |
| 207 | return *gc_barrier_; |
| 208 | } |
| 209 | |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 210 | // Schedules an unmarked object for reference processing. |
| 211 | void DelayReferenceReferent(mirror::Class* klass, mirror::Reference* reference) |
| 212 | SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_); |
| 213 | |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 214 | protected: |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 215 | // Returns true if the object has its bit set in the mark bitmap. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 216 | bool IsMarked(const mirror::Object* object) const; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 217 | |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 218 | static mirror::Object* IsMarkedCallback(mirror::Object* object, void* arg) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 219 | SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_); |
Mathieu Chartier | 46a2363 | 2012-08-07 18:44:40 -0700 | [diff] [blame] | 220 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 221 | static void VerifyImageRootVisitor(mirror::Object* root, void* arg) |
Mathieu Chartier | 0e54cd0 | 2014-03-20 12:41:23 -0700 | [diff] [blame] | 222 | SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_); |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 223 | |
Mathieu Chartier | 0e54cd0 | 2014-03-20 12:41:23 -0700 | [diff] [blame] | 224 | void MarkObjectNonNull(mirror::Object* obj) |
Mathieu Chartier | 9642c96 | 2013-08-05 17:40:36 -0700 | [diff] [blame] | 225 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 226 | EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 227 | |
Mathieu Chartier | 9642c96 | 2013-08-05 17:40:36 -0700 | [diff] [blame] | 228 | // Marks an object atomically, safe to use from multiple threads. |
Mathieu Chartier | 0e54cd0 | 2014-03-20 12:41:23 -0700 | [diff] [blame] | 229 | void MarkObjectNonNullParallel(mirror::Object* obj); |
Mathieu Chartier | 9642c96 | 2013-08-05 17:40:36 -0700 | [diff] [blame] | 230 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 231 | // Returns true if we need to add obj to a mark stack. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 232 | bool MarkObjectParallel(const mirror::Object* obj) NO_THREAD_SAFETY_ANALYSIS; |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 233 | |
Mathieu Chartier | 6f1c949 | 2012-10-15 12:08:41 -0700 | [diff] [blame] | 234 | // Verify the roots of the heap and print out information related to any invalid roots. |
| 235 | // Called in MarkObject, so may we may not hold the mutator lock. |
| 236 | void VerifyRoots() |
| 237 | NO_THREAD_SAFETY_ANALYSIS; |
| 238 | |
Mathieu Chartier | ba311b4 | 2013-08-27 13:02:30 -0700 | [diff] [blame] | 239 | // Expand mark stack to 2x its current size. |
| 240 | void ExpandMarkStack() EXCLUSIVE_LOCKS_REQUIRED(mark_stack_lock_); |
| 241 | void ResizeMarkStack(size_t new_size) EXCLUSIVE_LOCKS_REQUIRED(mark_stack_lock_); |
Mathieu Chartier | ac86a7c | 2012-11-12 15:03:16 -0800 | [diff] [blame] | 242 | |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 243 | // Returns how many threads we should use for the current GC phase based on if we are paused, |
| 244 | // whether or not we care about pauses. |
| 245 | size_t GetThreadCount(bool paused) const; |
| 246 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 247 | static void VerifyRootCallback(const mirror::Object* root, void* arg, size_t vreg, |
Mathieu Chartier | 7bf9f19 | 2014-04-04 11:09:41 -0700 | [diff] [blame] | 248 | const StackVisitor *visitor, RootType root_type); |
Mathieu Chartier | 6f1c949 | 2012-10-15 12:08:41 -0700 | [diff] [blame] | 249 | |
Mathieu Chartier | 7bf9f19 | 2014-04-04 11:09:41 -0700 | [diff] [blame] | 250 | void VerifyRoot(const mirror::Object* root, size_t vreg, const StackVisitor* visitor, |
| 251 | RootType root_type) NO_THREAD_SAFETY_ANALYSIS; |
Mathieu Chartier | 6f1c949 | 2012-10-15 12:08:41 -0700 | [diff] [blame] | 252 | |
Mathieu Chartier | 0e54cd0 | 2014-03-20 12:41:23 -0700 | [diff] [blame] | 253 | // Push a single reference on a mark stack. |
| 254 | void PushOnMarkStack(mirror::Object* obj); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 255 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 256 | // Blackens objects grayed during a garbage collection. |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 257 | void ScanGrayObjects(bool paused, byte minimum_age) |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 258 | EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) |
| 259 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 260 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 261 | // Recursively blackens objects on the mark stack. |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 262 | void ProcessMarkStack(bool paused) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 263 | EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) |
| 264 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 265 | |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 266 | void ProcessMarkStackParallel(size_t thread_count) |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 267 | EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) |
| 268 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 269 | |
Mathieu Chartier | c22c59e | 2014-02-24 15:16:06 -0800 | [diff] [blame] | 270 | // Used to get around thread safety annotations. The call is from MarkingPhase and is guarded by |
| 271 | // IsExclusiveHeld. |
| 272 | void RevokeAllThreadLocalAllocationStacks(Thread* self) NO_THREAD_SAFETY_ANALYSIS; |
| 273 | |
Hiroshi Yamauchi | c93c530 | 2014-03-20 16:15:37 -0700 | [diff] [blame] | 274 | // Revoke all the thread-local buffers. |
| 275 | void RevokeAllThreadLocalBuffers(); |
| 276 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 277 | // Whether or not we count how many of each type of object were scanned. |
| 278 | static const bool kCountScannedTypes = false; |
| 279 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 280 | // Current space, we check this space first to avoid searching for the appropriate space for an |
| 281 | // object. |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 282 | accounting::ContinuousSpaceBitmap* current_space_bitmap_; |
Mathieu Chartier | 0e54cd0 | 2014-03-20 12:41:23 -0700 | [diff] [blame] | 283 | // Cache the heap's mark bitmap to prevent having to do 2 loads during slow path marking. |
| 284 | accounting::HeapBitmap* mark_bitmap_; |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 285 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 286 | accounting::ObjectStack* mark_stack_; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 287 | |
Mathieu Chartier | 4aeec17 | 2014-03-27 16:09:46 -0700 | [diff] [blame] | 288 | // Immune region, every object inside the immune range is assumed to be marked. |
Mathieu Chartier | 8d56210 | 2014-03-12 17:42:10 -0700 | [diff] [blame] | 289 | ImmuneRegion immune_region_; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 290 | |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 291 | // Parallel finger. |
| 292 | AtomicInteger atomic_finger_; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 293 | // Number of classes scanned, if kCountScannedTypes. |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 294 | AtomicInteger class_count_; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 295 | // Number of arrays scanned, if kCountScannedTypes. |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 296 | AtomicInteger array_count_; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 297 | // Number of non-class/arrays scanned, if kCountScannedTypes. |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 298 | AtomicInteger other_count_; |
| 299 | AtomicInteger large_object_test_; |
| 300 | AtomicInteger large_object_mark_; |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 301 | AtomicInteger overhead_time_; |
| 302 | AtomicInteger work_chunks_created_; |
| 303 | AtomicInteger work_chunks_deleted_; |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 304 | AtomicInteger reference_count_; |
Mathieu Chartier | 0e54cd0 | 2014-03-20 12:41:23 -0700 | [diff] [blame] | 305 | AtomicInteger mark_null_count_; |
| 306 | AtomicInteger mark_immune_count_; |
| 307 | AtomicInteger mark_fastpath_count_; |
| 308 | AtomicInteger mark_slowpath_count_; |
Mathieu Chartier | 0f72e41 | 2013-09-06 16:40:01 -0700 | [diff] [blame] | 309 | |
| 310 | // Verification. |
| 311 | size_t live_stack_freeze_size_; |
Elliott Hughes | 352a424 | 2011-10-31 15:15:21 -0700 | [diff] [blame] | 312 | |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 313 | UniquePtr<Barrier> gc_barrier_; |
Mathieu Chartier | 958291c | 2013-08-27 18:14:55 -0700 | [diff] [blame] | 314 | Mutex mark_stack_lock_ ACQUIRED_AFTER(Locks::classlinker_classes_lock_); |
Ian Rogers | 1bd4b4c | 2013-04-18 17:47:42 -0700 | [diff] [blame] | 315 | |
| 316 | const bool is_concurrent_; |
| 317 | |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 318 | private: |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 319 | friend class AddIfReachesAllocSpaceVisitor; // Used by mod-union table. |
Mathieu Chartier | 0f72e41 | 2013-09-06 16:40:01 -0700 | [diff] [blame] | 320 | friend class CardScanTask; |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 321 | friend class CheckBitmapVisitor; |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 322 | friend class CheckReferenceVisitor; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 323 | friend class art::gc::Heap; |
Mathieu Chartier | 0e54cd0 | 2014-03-20 12:41:23 -0700 | [diff] [blame] | 324 | friend class MarkObjectVisitor; |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 325 | friend class ModUnionCheckReferences; |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 326 | friend class ModUnionClearCardVisitor; |
Mathieu Chartier | e6e0651 | 2012-06-26 15:00:26 -0700 | [diff] [blame] | 327 | friend class ModUnionReferenceVisitor; |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 328 | friend class ModUnionVisitor; |
| 329 | friend class ModUnionTableBitmap; |
Mathieu Chartier | e6e0651 | 2012-06-26 15:00:26 -0700 | [diff] [blame] | 330 | friend class ModUnionTableReferenceCache; |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 331 | friend class ModUnionScanImageRootVisitor; |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 332 | template<bool kUseFinger> friend class MarkStackTask; |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 333 | friend class FifoMarkStackChunk; |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 334 | friend class MarkSweepMarkObjectSlowPath; |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 335 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 336 | DISALLOW_COPY_AND_ASSIGN(MarkSweep); |
| 337 | }; |
| 338 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 339 | } // namespace collector |
| 340 | } // namespace gc |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 341 | } // namespace art |
| 342 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 343 | #endif // ART_RUNTIME_GC_COLLECTOR_MARK_SWEEP_H_ |