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 | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 20 | #include <memory> |
| 21 | |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 22 | #include "base/atomic.h" |
Sameer Abu Asal | 4aeb567 | 2013-02-19 15:30:35 -0800 | [diff] [blame] | 23 | #include "barrier.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 24 | #include "base/macros.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 25 | #include "base/mutex.h" |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 26 | #include "garbage_collector.h" |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 27 | #include "gc/accounting/heap_bitmap.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 28 | #include "gc_root.h" |
Mathieu Chartier | 763a31e | 2015-11-16 16:05:55 -0800 | [diff] [blame] | 29 | #include "immune_spaces.h" |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 30 | #include "offsets.h" |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 31 | |
| 32 | namespace art { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 33 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 34 | namespace mirror { |
Mathieu Chartier | 1ac1c2b | 2015-09-22 14:53:32 -0700 | [diff] [blame] | 35 | class Class; |
| 36 | class Object; |
| 37 | class Reference; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 38 | } // namespace mirror |
| 39 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 40 | class Thread; |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 41 | enum VisitRootFlags : uint8_t; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 42 | |
| 43 | namespace gc { |
| 44 | |
Mathieu Chartier | 4aeec17 | 2014-03-27 16:09:46 -0700 | [diff] [blame] | 45 | class Heap; |
| 46 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 47 | namespace accounting { |
Mathieu Chartier | 1ac1c2b | 2015-09-22 14:53:32 -0700 | [diff] [blame] | 48 | template<typename T> class AtomicStack; |
| 49 | typedef AtomicStack<mirror::Object> ObjectStack; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 50 | } // namespace accounting |
| 51 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 52 | namespace collector { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 53 | |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 54 | class MarkSweep : public GarbageCollector { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 55 | public: |
Roland Levillain | 3887c46 | 2015-08-12 18:15:42 +0100 | [diff] [blame] | 56 | MarkSweep(Heap* heap, bool is_concurrent, const std::string& name_prefix = ""); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 57 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 58 | ~MarkSweep() {} |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 59 | |
Roland Levillain | f73caca | 2018-08-24 17:19:07 +0100 | [diff] [blame] | 60 | void RunPhases() override REQUIRES(!mark_stack_lock_); |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 61 | void InitializePhase(); |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 62 | void MarkingPhase() REQUIRES(!mark_stack_lock_) REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | 1ac1c2b | 2015-09-22 14:53:32 -0700 | [diff] [blame] | 63 | void PausePhase() REQUIRES(Locks::mutator_lock_) REQUIRES(!mark_stack_lock_); |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 64 | void ReclaimPhase() REQUIRES(!mark_stack_lock_) REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 65 | void FinishPhase(); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 66 | virtual void MarkReachableObjects() |
Mathieu Chartier | 1ac1c2b | 2015-09-22 14:53:32 -0700 | [diff] [blame] | 67 | REQUIRES(Locks::heap_bitmap_lock_) |
| 68 | REQUIRES(!mark_stack_lock_) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 69 | REQUIRES_SHARED(Locks::mutator_lock_); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 70 | |
Hiroshi Yamauchi | 3e41780 | 2014-03-20 12:03:02 -0700 | [diff] [blame] | 71 | bool IsConcurrent() const { |
| 72 | return is_concurrent_; |
| 73 | } |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 74 | |
Roland Levillain | f73caca | 2018-08-24 17:19:07 +0100 | [diff] [blame] | 75 | GcType GetGcType() const override { |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 76 | return kGcTypeFull; |
| 77 | } |
| 78 | |
Roland Levillain | f73caca | 2018-08-24 17:19:07 +0100 | [diff] [blame] | 79 | CollectorType GetCollectorType() const override { |
Hiroshi Yamauchi | 3e41780 | 2014-03-20 12:03:02 -0700 | [diff] [blame] | 80 | return is_concurrent_ ? kCollectorTypeCMS : kCollectorTypeMS; |
| 81 | } |
| 82 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 83 | // Initializes internal structures. |
Jesse Wilson | 078f9b0 | 2011-11-18 17:51:47 -0500 | [diff] [blame] | 84 | void Init(); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 85 | |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 86 | // Find the default mark bitmap. |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 87 | void FindDefaultSpaceBitmap() REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 88 | |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 89 | // Marks all objects in the root set at the start of a garbage collection. |
| 90 | void MarkRoots(Thread* self) |
Mathieu Chartier | 1ac1c2b | 2015-09-22 14:53:32 -0700 | [diff] [blame] | 91 | REQUIRES(Locks::heap_bitmap_lock_) |
| 92 | REQUIRES(!mark_stack_lock_) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 93 | REQUIRES_SHARED(Locks::mutator_lock_); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 94 | |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 95 | void MarkNonThreadRoots() |
Mathieu Chartier | 1ac1c2b | 2015-09-22 14:53:32 -0700 | [diff] [blame] | 96 | REQUIRES(Locks::heap_bitmap_lock_) |
| 97 | REQUIRES(!mark_stack_lock_) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 98 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 99 | |
neo.chae | a2d1b28 | 2016-11-08 08:40:46 +0900 | [diff] [blame] | 100 | virtual void MarkConcurrentRoots(VisitRootFlags flags) |
Mathieu Chartier | 1ac1c2b | 2015-09-22 14:53:32 -0700 | [diff] [blame] | 101 | REQUIRES(Locks::heap_bitmap_lock_) |
| 102 | REQUIRES(!mark_stack_lock_) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 103 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | 9ebae1f | 2012-10-15 17:38:16 -0700 | [diff] [blame] | 104 | |
Mathieu Chartier | 0f7bf6a | 2014-03-28 10:05:39 -0700 | [diff] [blame] | 105 | void MarkRootsCheckpoint(Thread* self, bool revoke_ros_alloc_thread_local_buffers_at_checkpoint) |
Mathieu Chartier | 1ac1c2b | 2015-09-22 14:53:32 -0700 | [diff] [blame] | 106 | REQUIRES(Locks::heap_bitmap_lock_) |
| 107 | REQUIRES(!mark_stack_lock_) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 108 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 109 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 110 | // Builds a mark stack and recursively mark until it empties. |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 111 | void RecursiveMark() |
Mathieu Chartier | 1ac1c2b | 2015-09-22 14:53:32 -0700 | [diff] [blame] | 112 | REQUIRES(Locks::heap_bitmap_lock_) |
| 113 | REQUIRES(!mark_stack_lock_) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 114 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 115 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 116 | // Bind the live bits to the mark bits of bitmaps for spaces that are never collected, ie |
| 117 | // the image. Mark that portion of the heap as immune. |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 118 | virtual void BindBitmaps() REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 119 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 120 | // Builds a mark stack with objects on dirty cards and recursively mark until it empties. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 121 | void RecursiveMarkDirtyObjects(bool paused, uint8_t minimum_age) |
Mathieu Chartier | 1ac1c2b | 2015-09-22 14:53:32 -0700 | [diff] [blame] | 122 | REQUIRES(Locks::heap_bitmap_lock_) |
| 123 | REQUIRES(!mark_stack_lock_) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 124 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 125 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 126 | // Remarks the root set after completing the concurrent mark. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 127 | void ReMarkRoots() |
Mathieu Chartier | 1ac1c2b | 2015-09-22 14:53:32 -0700 | [diff] [blame] | 128 | REQUIRES(Locks::heap_bitmap_lock_) |
| 129 | REQUIRES(!mark_stack_lock_) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 130 | REQUIRES_SHARED(Locks::mutator_lock_); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 131 | |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 132 | void ProcessReferences(Thread* self) |
Mathieu Chartier | 1ac1c2b | 2015-09-22 14:53:32 -0700 | [diff] [blame] | 133 | REQUIRES(!mark_stack_lock_) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 134 | REQUIRES_SHARED(Locks::mutator_lock_); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 135 | |
Mathieu Chartier | 4aeec17 | 2014-03-27 16:09:46 -0700 | [diff] [blame] | 136 | // Update and mark references from immune spaces. |
| 137 | void UpdateAndMarkModUnion() |
Mathieu Chartier | 1ac1c2b | 2015-09-22 14:53:32 -0700 | [diff] [blame] | 138 | REQUIRES(!mark_stack_lock_) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 139 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | 11409ae | 2013-09-23 11:49:36 -0700 | [diff] [blame] | 140 | |
Mathieu Chartier | dda54f5 | 2014-02-24 09:58:40 -0800 | [diff] [blame] | 141 | // Pre clean cards to reduce how much work is needed in the pause. |
| 142 | void PreCleanCards() |
Mathieu Chartier | 1ac1c2b | 2015-09-22 14:53:32 -0700 | [diff] [blame] | 143 | REQUIRES(Locks::heap_bitmap_lock_) |
| 144 | REQUIRES(!mark_stack_lock_) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 145 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | dda54f5 | 2014-02-24 09:58:40 -0800 | [diff] [blame] | 146 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 147 | // Sweeps unmarked objects to complete the garbage collection. Virtual as by default it sweeps |
| 148 | // all allocation spaces. Partial and sticky GCs want to just sweep a subset of the heap. |
Mathieu Chartier | 1ac1c2b | 2015-09-22 14:53:32 -0700 | [diff] [blame] | 149 | virtual void Sweep(bool swap_bitmaps) |
| 150 | REQUIRES(Locks::heap_bitmap_lock_) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 151 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 152 | |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 153 | // Sweeps unmarked objects to complete the garbage collection. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 154 | void SweepLargeObjects(bool swap_bitmaps) REQUIRES(Locks::heap_bitmap_lock_); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 155 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 156 | // Sweep only pointers within an array. WARNING: Trashes objects. |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 157 | void SweepArray(accounting::ObjectStack* allocation_stack_, bool swap_bitmaps) |
Mathieu Chartier | 1ac1c2b | 2015-09-22 14:53:32 -0700 | [diff] [blame] | 158 | REQUIRES(Locks::heap_bitmap_lock_) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 159 | REQUIRES_SHARED(Locks::mutator_lock_); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 160 | |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 161 | // Blackens an object. |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 162 | void ScanObject(mirror::Object* obj) |
Mathieu Chartier | 1ac1c2b | 2015-09-22 14:53:32 -0700 | [diff] [blame] | 163 | REQUIRES(Locks::heap_bitmap_lock_) |
| 164 | REQUIRES(!mark_stack_lock_) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 165 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 166 | |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 167 | // No thread safety analysis due to lambdas. |
| 168 | template<typename MarkVisitor, typename ReferenceVisitor> |
Mathieu Chartier | 1ac1c2b | 2015-09-22 14:53:32 -0700 | [diff] [blame] | 169 | void ScanObjectVisit(mirror::Object* obj, |
| 170 | const MarkVisitor& visitor, |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 171 | const ReferenceVisitor& ref_visitor) |
Mathieu Chartier | 1ac1c2b | 2015-09-22 14:53:32 -0700 | [diff] [blame] | 172 | REQUIRES(Locks::heap_bitmap_lock_) |
| 173 | REQUIRES(!mark_stack_lock_) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 174 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 175 | |
Mathieu Chartier | 0f7bf6a | 2014-03-28 10:05:39 -0700 | [diff] [blame] | 176 | void SweepSystemWeaks(Thread* self) |
Mathieu Chartier | 1ac1c2b | 2015-09-22 14:53:32 -0700 | [diff] [blame] | 177 | REQUIRES(!Locks::heap_bitmap_lock_) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 178 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 179 | |
Mathieu Chartier | 6aa3df9 | 2013-09-17 15:17:28 -0700 | [diff] [blame] | 180 | static mirror::Object* VerifySystemWeakIsLiveCallback(mirror::Object* obj, void* arg) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 181 | REQUIRES_SHARED(Locks::heap_bitmap_lock_, Locks::mutator_lock_); |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 182 | |
| 183 | void VerifySystemWeaks() |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 184 | REQUIRES_SHARED(Locks::mutator_lock_, Locks::heap_bitmap_lock_); |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 185 | |
| 186 | // 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] | 187 | void VerifyIsLive(const mirror::Object* obj) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 188 | REQUIRES_SHARED(Locks::mutator_lock_, Locks::heap_bitmap_lock_); |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 189 | |
Roland Levillain | f73caca | 2018-08-24 17:19:07 +0100 | [diff] [blame] | 190 | bool IsNullOrMarkedHeapReference(mirror::HeapReference<mirror::Object>* ref, |
| 191 | bool do_atomic_update) override |
Mathieu Chartier | 1ac1c2b | 2015-09-22 14:53:32 -0700 | [diff] [blame] | 192 | REQUIRES(Locks::heap_bitmap_lock_) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 193 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | 308351a | 2014-06-15 12:39:02 -0700 | [diff] [blame] | 194 | |
Roland Levillain | f73caca | 2018-08-24 17:19:07 +0100 | [diff] [blame] | 195 | void VisitRoots(mirror::Object*** roots, size_t count, const RootInfo& info) override |
Mathieu Chartier | 1ac1c2b | 2015-09-22 14:53:32 -0700 | [diff] [blame] | 196 | REQUIRES(Locks::heap_bitmap_lock_) |
| 197 | REQUIRES(!mark_stack_lock_) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 198 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 199 | |
Roland Levillain | f73caca | 2018-08-24 17:19:07 +0100 | [diff] [blame] | 200 | void VisitRoots(mirror::CompressedReference<mirror::Object>** roots, |
| 201 | size_t count, |
| 202 | const RootInfo& info) override |
Mathieu Chartier | 1ac1c2b | 2015-09-22 14:53:32 -0700 | [diff] [blame] | 203 | REQUIRES(Locks::heap_bitmap_lock_) |
| 204 | REQUIRES(!mark_stack_lock_) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 205 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 206 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 207 | // Marks an object. |
Roland Levillain | f73caca | 2018-08-24 17:19:07 +0100 | [diff] [blame] | 208 | mirror::Object* MarkObject(mirror::Object* obj) override |
Mathieu Chartier | 1ac1c2b | 2015-09-22 14:53:32 -0700 | [diff] [blame] | 209 | REQUIRES(Locks::heap_bitmap_lock_) |
| 210 | REQUIRES(!mark_stack_lock_) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 211 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | 1ac1c2b | 2015-09-22 14:53:32 -0700 | [diff] [blame] | 212 | |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 213 | void MarkObject(mirror::Object* obj, mirror::Object* holder, MemberOffset offset) |
Mathieu Chartier | 1ac1c2b | 2015-09-22 14:53:32 -0700 | [diff] [blame] | 214 | REQUIRES(Locks::heap_bitmap_lock_) |
| 215 | REQUIRES(!mark_stack_lock_) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 216 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | 1ac1c2b | 2015-09-22 14:53:32 -0700 | [diff] [blame] | 217 | |
Roland Levillain | f73caca | 2018-08-24 17:19:07 +0100 | [diff] [blame] | 218 | void MarkHeapReference(mirror::HeapReference<mirror::Object>* ref, |
| 219 | bool do_atomic_update) override |
Mathieu Chartier | 1ac1c2b | 2015-09-22 14:53:32 -0700 | [diff] [blame] | 220 | REQUIRES(Locks::heap_bitmap_lock_) |
| 221 | REQUIRES(!mark_stack_lock_) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 222 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 223 | |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 224 | Barrier& GetBarrier() { |
| 225 | return *gc_barrier_; |
| 226 | } |
| 227 | |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 228 | // Schedules an unmarked object for reference processing. |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 229 | void DelayReferenceReferent(ObjPtr<mirror::Class> klass, ObjPtr<mirror::Reference> reference) |
Yi Kong | 3940254 | 2019-03-24 02:47:16 -0700 | [diff] [blame^] | 230 | override REQUIRES_SHARED(Locks::heap_bitmap_lock_, Locks::mutator_lock_); |
Mathieu Chartier | 407f702 | 2014-02-18 14:37:05 -0800 | [diff] [blame] | 231 | |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 232 | protected: |
Mathieu Chartier | 8118781 | 2015-07-15 14:24:07 -0700 | [diff] [blame] | 233 | // Returns object if the object is marked in the heap bitmap, otherwise null. |
Roland Levillain | f73caca | 2018-08-24 17:19:07 +0100 | [diff] [blame] | 234 | mirror::Object* IsMarked(mirror::Object* object) override |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 235 | REQUIRES_SHARED(Locks::heap_bitmap_lock_, Locks::mutator_lock_); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 236 | |
Mathieu Chartier | 1ac1c2b | 2015-09-22 14:53:32 -0700 | [diff] [blame] | 237 | void MarkObjectNonNull(mirror::Object* obj, |
| 238 | mirror::Object* holder = nullptr, |
Hiroshi Yamauchi | eb2baaf | 2015-05-13 21:14:22 -0700 | [diff] [blame] | 239 | MemberOffset offset = MemberOffset(0)) |
Mathieu Chartier | 1ac1c2b | 2015-09-22 14:53:32 -0700 | [diff] [blame] | 240 | REQUIRES(Locks::heap_bitmap_lock_) |
| 241 | REQUIRES(!mark_stack_lock_) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 242 | REQUIRES_SHARED(Locks::mutator_lock_); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 243 | |
Mathieu Chartier | 9642c96 | 2013-08-05 17:40:36 -0700 | [diff] [blame] | 244 | // Marks an object atomically, safe to use from multiple threads. |
Mathieu Chartier | cb535da | 2015-01-23 13:50:03 -0800 | [diff] [blame] | 245 | void MarkObjectNonNullParallel(mirror::Object* obj) |
Mathieu Chartier | 1ac1c2b | 2015-09-22 14:53:32 -0700 | [diff] [blame] | 246 | REQUIRES(!mark_stack_lock_) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 247 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | 9642c96 | 2013-08-05 17:40:36 -0700 | [diff] [blame] | 248 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 249 | // Returns true if we need to add obj to a mark stack. |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 250 | bool MarkObjectParallel(mirror::Object* obj) NO_THREAD_SAFETY_ANALYSIS; |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 251 | |
Mathieu Chartier | 6f1c949 | 2012-10-15 12:08:41 -0700 | [diff] [blame] | 252 | // Verify the roots of the heap and print out information related to any invalid roots. |
| 253 | // Called in MarkObject, so may we may not hold the mutator lock. |
Mathieu Chartier | fc80ff7 | 2016-11-28 13:13:28 -0800 | [diff] [blame] | 254 | void VerifySuspendedThreadRoots(std::ostream& os) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 255 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | 6f1c949 | 2012-10-15 12:08:41 -0700 | [diff] [blame] | 256 | |
Mathieu Chartier | ba311b4 | 2013-08-27 13:02:30 -0700 | [diff] [blame] | 257 | // Expand mark stack to 2x its current size. |
Mathieu Chartier | 1ac1c2b | 2015-09-22 14:53:32 -0700 | [diff] [blame] | 258 | void ExpandMarkStack() |
| 259 | REQUIRES(mark_stack_lock_) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 260 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | 1ac1c2b | 2015-09-22 14:53:32 -0700 | [diff] [blame] | 261 | |
| 262 | void ResizeMarkStack(size_t new_size) |
| 263 | REQUIRES(mark_stack_lock_) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 264 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | ac86a7c | 2012-11-12 15:03:16 -0800 | [diff] [blame] | 265 | |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 266 | // Returns how many threads we should use for the current GC phase based on if we are paused, |
| 267 | // whether or not we care about pauses. |
| 268 | size_t GetThreadCount(bool paused) const; |
| 269 | |
Mathieu Chartier | 0e54cd0 | 2014-03-20 12:41:23 -0700 | [diff] [blame] | 270 | // Push a single reference on a mark stack. |
Mathieu Chartier | 1ac1c2b | 2015-09-22 14:53:32 -0700 | [diff] [blame] | 271 | void PushOnMarkStack(mirror::Object* obj) |
| 272 | REQUIRES(!mark_stack_lock_) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 273 | REQUIRES_SHARED(Locks::mutator_lock_); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 274 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 275 | // Blackens objects grayed during a garbage collection. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 276 | void ScanGrayObjects(bool paused, uint8_t minimum_age) |
Mathieu Chartier | 1ac1c2b | 2015-09-22 14:53:32 -0700 | [diff] [blame] | 277 | REQUIRES(Locks::heap_bitmap_lock_) |
| 278 | REQUIRES(!mark_stack_lock_) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 279 | REQUIRES_SHARED(Locks::mutator_lock_); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 280 | |
Roland Levillain | f73caca | 2018-08-24 17:19:07 +0100 | [diff] [blame] | 281 | void ProcessMarkStack() override |
Mathieu Chartier | 1ac1c2b | 2015-09-22 14:53:32 -0700 | [diff] [blame] | 282 | REQUIRES(Locks::heap_bitmap_lock_) |
| 283 | REQUIRES(!mark_stack_lock_) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 284 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 285 | ProcessMarkStack(false); |
| 286 | } |
| 287 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 288 | // Recursively blackens objects on the mark stack. |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 289 | void ProcessMarkStack(bool paused) |
Mathieu Chartier | 1ac1c2b | 2015-09-22 14:53:32 -0700 | [diff] [blame] | 290 | REQUIRES(Locks::heap_bitmap_lock_) |
| 291 | REQUIRES(!mark_stack_lock_) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 292 | REQUIRES_SHARED(Locks::mutator_lock_); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 293 | |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 294 | void ProcessMarkStackParallel(size_t thread_count) |
Mathieu Chartier | 1ac1c2b | 2015-09-22 14:53:32 -0700 | [diff] [blame] | 295 | REQUIRES(Locks::heap_bitmap_lock_) |
| 296 | REQUIRES(!mark_stack_lock_) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 297 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 298 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 299 | // Used to Get around thread safety annotations. The call is from MarkingPhase and is guarded by |
Mathieu Chartier | c22c59e | 2014-02-24 15:16:06 -0800 | [diff] [blame] | 300 | // IsExclusiveHeld. |
| 301 | void RevokeAllThreadLocalAllocationStacks(Thread* self) NO_THREAD_SAFETY_ANALYSIS; |
| 302 | |
Hiroshi Yamauchi | c93c530 | 2014-03-20 16:15:37 -0700 | [diff] [blame] | 303 | // Revoke all the thread-local buffers. |
Yi Kong | 3940254 | 2019-03-24 02:47:16 -0700 | [diff] [blame^] | 304 | void RevokeAllThreadLocalBuffers() override; |
Hiroshi Yamauchi | c93c530 | 2014-03-20 16:15:37 -0700 | [diff] [blame] | 305 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 306 | // Whether or not we count how many of each type of object were scanned. |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 307 | static constexpr bool kCountScannedTypes = false; |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 308 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 309 | // Current space, we check this space first to avoid searching for the appropriate space for an |
| 310 | // object. |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 311 | accounting::ContinuousSpaceBitmap* current_space_bitmap_; |
Mathieu Chartier | 0e54cd0 | 2014-03-20 12:41:23 -0700 | [diff] [blame] | 312 | // Cache the heap's mark bitmap to prevent having to do 2 loads during slow path marking. |
| 313 | accounting::HeapBitmap* mark_bitmap_; |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 314 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 315 | accounting::ObjectStack* mark_stack_; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 316 | |
Mathieu Chartier | 763a31e | 2015-11-16 16:05:55 -0800 | [diff] [blame] | 317 | // Every object inside the immune spaces is assumed to be marked. Immune spaces that aren't in the |
| 318 | // immune region are handled by the normal marking logic. |
| 319 | ImmuneSpaces immune_spaces_; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 320 | |
Mathieu Chartier | 94c32c5 | 2013-08-09 11:14:04 -0700 | [diff] [blame] | 321 | // Parallel finger. |
| 322 | AtomicInteger atomic_finger_; |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 323 | |
| 324 | AtomicInteger no_reference_class_count_; |
| 325 | AtomicInteger normal_count_; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 326 | // Number of classes scanned, if kCountScannedTypes. |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 327 | AtomicInteger class_count_; |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 328 | // Number of object arrays scanned, if kCountScannedTypes. |
| 329 | AtomicInteger object_array_count_; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 330 | // Number of non-class/arrays scanned, if kCountScannedTypes. |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 331 | AtomicInteger other_count_; |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 332 | // Number of java.lang.ref.Reference instances. |
| 333 | AtomicInteger reference_count_; |
| 334 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 335 | AtomicInteger large_object_test_; |
| 336 | AtomicInteger large_object_mark_; |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 337 | AtomicInteger overhead_time_; |
| 338 | AtomicInteger work_chunks_created_; |
| 339 | AtomicInteger work_chunks_deleted_; |
Mathieu Chartier | 0e54cd0 | 2014-03-20 12:41:23 -0700 | [diff] [blame] | 340 | AtomicInteger mark_null_count_; |
| 341 | AtomicInteger mark_immune_count_; |
| 342 | AtomicInteger mark_fastpath_count_; |
| 343 | AtomicInteger mark_slowpath_count_; |
Mathieu Chartier | 0f72e41 | 2013-09-06 16:40:01 -0700 | [diff] [blame] | 344 | |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 345 | std::unique_ptr<Barrier> gc_barrier_; |
Mathieu Chartier | 958291c | 2013-08-27 18:14:55 -0700 | [diff] [blame] | 346 | Mutex mark_stack_lock_ ACQUIRED_AFTER(Locks::classlinker_classes_lock_); |
Ian Rogers | 1bd4b4c | 2013-04-18 17:47:42 -0700 | [diff] [blame] | 347 | |
| 348 | const bool is_concurrent_; |
| 349 | |
Ian Rogers | 3e5cf30 | 2014-05-20 16:40:37 -0700 | [diff] [blame] | 350 | // Verification. |
| 351 | size_t live_stack_freeze_size_; |
| 352 | |
Roland Levillain | b1e1dc3 | 2018-07-10 19:19:31 +0100 | [diff] [blame] | 353 | // Sweep array free buffer, used to sweep the spaces based on an array more |
| 354 | // efficiently, by recording dead objects to be freed in batches (see |
| 355 | // MarkSweep::SweepArray). |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 356 | MemMap sweep_array_free_buffer_mem_map_; |
Hiroshi Yamauchi | bbdc5bc | 2014-05-28 14:04:59 -0700 | [diff] [blame] | 357 | |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 358 | private: |
Mathieu Chartier | a07f559 | 2016-06-16 11:44:28 -0700 | [diff] [blame] | 359 | class CardScanTask; |
| 360 | class CheckpointMarkThreadRoots; |
| 361 | class DelayReferenceReferentVisitor; |
| 362 | template<bool kUseFinger> class MarkStackTask; |
| 363 | class MarkObjectSlowPath; |
| 364 | class RecursiveMarkTask; |
| 365 | class ScanObjectParallelVisitor; |
| 366 | class ScanObjectVisitor; |
| 367 | class VerifyRootMarkedVisitor; |
| 368 | class VerifyRootVisitor; |
| 369 | class VerifySystemWeakVisitor; |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 370 | |
Mathieu Chartier | 3130cdf | 2015-05-03 15:20:23 -0700 | [diff] [blame] | 371 | DISALLOW_IMPLICIT_CONSTRUCTORS(MarkSweep); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 372 | }; |
| 373 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 374 | } // namespace collector |
| 375 | } // namespace gc |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 376 | } // namespace art |
| 377 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 378 | #endif // ART_RUNTIME_GC_COLLECTOR_MARK_SWEEP_H_ |