blob: 2e5752b91ea036f8fa74bc03a54ce158b26429a4 [file] [log] [blame]
Hiroshi Yamauchid5307ec2014-03-27 21:07:51 -07001/*
2 * Copyright (C) 2014 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
17#ifndef ART_RUNTIME_GC_COLLECTOR_CONCURRENT_COPYING_H_
18#define ART_RUNTIME_GC_COLLECTOR_CONCURRENT_COPYING_H_
19
20#include "garbage_collector.h"
Mathieu Chartier763a31e2015-11-16 16:05:55 -080021#include "immune_spaces.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070022#include "offsets.h"
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080023
Andreas Gampe8f1c8e52019-01-08 10:34:16 -080024#include <map>
Lokesh Gidrad0c5b252018-12-05 01:10:40 -080025#include <memory>
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080026#include <vector>
Hiroshi Yamauchid5307ec2014-03-27 21:07:51 -070027
28namespace art {
Andreas Gampe8f1c8e52019-01-08 10:34:16 -080029class Barrier;
Hiroshi Yamauchifebd0cf2016-09-14 19:31:25 -070030class Closure;
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080031class RootInfo;
32
Andreas Gampe5d08fcc2017-06-05 17:56:46 -070033namespace mirror {
Andreas Gampe8f1c8e52019-01-08 10:34:16 -080034template<class MirrorType> class CompressedReference;
35template<class MirrorType> class HeapReference;
Andreas Gampe5d08fcc2017-06-05 17:56:46 -070036class Object;
37} // namespace mirror
38
Hiroshi Yamauchid5307ec2014-03-27 21:07:51 -070039namespace gc {
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080040
41namespace accounting {
Igor Murashkin2ffb7032017-11-08 13:35:21 -080042template<typename T> class AtomicStack;
43typedef AtomicStack<mirror::Object> ObjectStack;
44template <size_t kAlignment> class SpaceBitmap;
45typedef SpaceBitmap<kObjectAlignment> ContinuousSpaceBitmap;
46class HeapBitmap;
47class ReadBarrierTable;
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080048} // namespace accounting
49
50namespace space {
Igor Murashkin2ffb7032017-11-08 13:35:21 -080051class RegionSpace;
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080052} // namespace space
53
Hiroshi Yamauchid5307ec2014-03-27 21:07:51 -070054namespace collector {
55
56class ConcurrentCopying : public GarbageCollector {
57 public:
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080058 // Enable the no-from-space-refs verification at the pause.
Hiroshi Yamauchidaf61a12016-06-10 14:27:38 -070059 static constexpr bool kEnableNoFromSpaceRefsVerification = kIsDebugBuild;
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080060 // Enable the from-space bytes/objects check.
Hiroshi Yamauchidaf61a12016-06-10 14:27:38 -070061 static constexpr bool kEnableFromSpaceAccountingCheck = kIsDebugBuild;
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080062 // Enable verbose mode.
Hiroshi Yamauchi3c448932016-01-22 16:26:50 -080063 static constexpr bool kVerboseMode = false;
Mathieu Chartier36a270a2016-07-28 18:08:51 -070064 // If kGrayDirtyImmuneObjects is true then we gray dirty objects in the GC pause to prevent dirty
65 // pages.
66 static constexpr bool kGrayDirtyImmuneObjects = true;
Hiroshi Yamauchid5307ec2014-03-27 21:07:51 -070067
Albert Mingkun Yang0b4d1462018-11-29 13:25:35 +000068 ConcurrentCopying(Heap* heap,
69 bool young_gen,
70 bool use_generational_cc,
71 const std::string& name_prefix = "",
72 bool measure_read_barrier_slow_path = false);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080073 ~ConcurrentCopying();
Hiroshi Yamauchid5307ec2014-03-27 21:07:51 -070074
Roland Levillainf73caca2018-08-24 17:19:07 +010075 void RunPhases() override
Mathieu Chartier56fe2582016-07-14 13:30:03 -070076 REQUIRES(!immune_gray_stack_lock_,
77 !mark_stack_lock_,
Hans Boehme7d7e9d2019-06-03 18:36:06 +000078 !rb_slow_path_histogram_lock_,
79 !skipped_blocks_lock_);
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070080 void InitializePhase() REQUIRES_SHARED(Locks::mutator_lock_)
Hiroshi Yamauchid8db5a22016-06-28 14:07:41 -070081 REQUIRES(!mark_stack_lock_, !immune_gray_stack_lock_);
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070082 void MarkingPhase() REQUIRES_SHARED(Locks::mutator_lock_)
Lokesh Gidra2a9824c2018-11-07 15:57:17 -080083 REQUIRES(!mark_stack_lock_);
84 void CopyingPhase() REQUIRES_SHARED(Locks::mutator_lock_)
Hans Boehme7d7e9d2019-06-03 18:36:06 +000085 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_, !immune_gray_stack_lock_);
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070086 void ReclaimPhase() REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!mark_stack_lock_);
Mathieu Chartier56fe2582016-07-14 13:30:03 -070087 void FinishPhase() REQUIRES(!mark_stack_lock_,
Hans Boehme7d7e9d2019-06-03 18:36:06 +000088 !rb_slow_path_histogram_lock_,
89 !skipped_blocks_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080090
Lokesh Gidra10d0c962019-03-07 22:40:36 +000091 void CaptureRssAtPeak() REQUIRES(!mark_stack_lock_);
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070092 void BindBitmaps() REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -070093 REQUIRES(!Locks::heap_bitmap_lock_);
Roland Levillainf73caca2018-08-24 17:19:07 +010094 GcType GetGcType() const override {
Albert Mingkun Yang0b4d1462018-11-29 13:25:35 +000095 return (use_generational_cc_ && young_gen_)
Mathieu Chartier8d1a9962016-08-17 16:39:45 -070096 ? kGcTypeSticky
97 : kGcTypePartial;
Hiroshi Yamauchid5307ec2014-03-27 21:07:51 -070098 }
Roland Levillainf73caca2018-08-24 17:19:07 +010099 CollectorType GetCollectorType() const override {
Hiroshi Yamauchid5307ec2014-03-27 21:07:51 -0700100 return kCollectorTypeCC;
101 }
Roland Levillainf73caca2018-08-24 17:19:07 +0100102 void RevokeAllThreadLocalBuffers() override;
Lokesh Gidra1c34b712018-12-18 13:41:58 -0800103 // Creates inter-region ref bitmaps for region-space and non-moving-space.
104 // Gets called in Heap construction after the two spaces are created.
105 void CreateInterRegionRefBitmaps();
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800106 void SetRegionSpace(space::RegionSpace* region_space) {
107 DCHECK(region_space != nullptr);
108 region_space_ = region_space;
109 }
110 space::RegionSpace* RegionSpace() {
111 return region_space_;
112 }
Roland Levillain001eff92018-01-24 14:24:33 +0000113 // Assert the to-space invariant for a heap reference `ref` held in `obj` at offset `offset`.
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800114 void AssertToSpaceInvariant(mirror::Object* obj, MemberOffset offset, mirror::Object* ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700115 REQUIRES_SHARED(Locks::mutator_lock_);
Roland Levillain001eff92018-01-24 14:24:33 +0000116 // Assert the to-space invariant for a GC root reference `ref`.
Hiroshi Yamauchi3f64f252015-06-12 18:35:06 -0700117 void AssertToSpaceInvariant(GcRootSource* gc_root_source, mirror::Object* ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700118 REQUIRES_SHARED(Locks::mutator_lock_);
119 bool IsInToSpace(mirror::Object* ref) REQUIRES_SHARED(Locks::mutator_lock_) {
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800120 DCHECK(ref != nullptr);
121 return IsMarked(ref) == ref;
122 }
Roland Levillain8f7ea9a2018-01-26 17:27:59 +0000123 // Mark object `from_ref`, copying it to the to-space if needed.
Mathieu Chartier8d1a9962016-08-17 16:39:45 -0700124 template<bool kGrayImmuneObject = true, bool kNoUnEvac = false, bool kFromGCThread = false>
Hiroshi Yamauchi7a181542017-03-08 17:34:46 -0800125 ALWAYS_INLINE mirror::Object* Mark(Thread* const self,
126 mirror::Object* from_ref,
Mathieu Chartier1ca68902017-04-18 11:26:22 -0700127 mirror::Object* holder = nullptr,
128 MemberOffset offset = MemberOffset(0))
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700129 REQUIRES_SHARED(Locks::mutator_lock_)
Hans Boehme7d7e9d2019-06-03 18:36:06 +0000130 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_, !immune_gray_stack_lock_);
Mathieu Chartier56fe2582016-07-14 13:30:03 -0700131 ALWAYS_INLINE mirror::Object* MarkFromReadBarrier(mirror::Object* from_ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700132 REQUIRES_SHARED(Locks::mutator_lock_)
Hans Boehme7d7e9d2019-06-03 18:36:06 +0000133 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_, !immune_gray_stack_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800134 bool IsMarking() const {
135 return is_marking_;
136 }
Mathieu Chartier3768ade2017-05-02 14:04:39 -0700137 // We may want to use read barrier entrypoints before is_marking_ is true since concurrent graying
138 // creates a small window where we might dispatch on these entrypoints.
139 bool IsUsingReadBarrierEntrypoints() const {
140 return is_using_read_barrier_entrypoints_;
141 }
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800142 bool IsActive() const {
143 return is_active_;
144 }
145 Barrier& GetBarrier() {
146 return *gc_barrier_;
147 }
Hiroshi Yamauchifebd0cf2016-09-14 19:31:25 -0700148 bool IsWeakRefAccessEnabled() REQUIRES(Locks::thread_list_lock_) {
149 return weak_ref_access_enabled_;
Hiroshi Yamauchi0b713572015-06-16 18:29:23 -0700150 }
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700151 void RevokeThreadLocalMarkStack(Thread* thread) REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -0700152 REQUIRES(!mark_stack_lock_);
Hiroshi Yamauchid5307ec2014-03-27 21:07:51 -0700153
Roland Levillainf73caca2018-08-24 17:19:07 +0100154 mirror::Object* IsMarked(mirror::Object* from_ref) override
Nicolas Geoffray13056a12017-05-11 11:48:28 +0000155 REQUIRES_SHARED(Locks::mutator_lock_);
156
Hiroshi Yamauchid5307ec2014-03-27 21:07:51 -0700157 private:
Hiroshi Yamauchi7a181542017-03-08 17:34:46 -0800158 void PushOntoMarkStack(Thread* const self, mirror::Object* obj)
159 REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -0700160 REQUIRES(!mark_stack_lock_);
Hiroshi Yamauchi7a181542017-03-08 17:34:46 -0800161 mirror::Object* Copy(Thread* const self,
162 mirror::Object* from_ref,
Mathieu Chartieref496d92017-04-28 18:58:59 -0700163 mirror::Object* holder,
164 MemberOffset offset)
165 REQUIRES_SHARED(Locks::mutator_lock_)
Hans Boehme7d7e9d2019-06-03 18:36:06 +0000166 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_, !immune_gray_stack_lock_);
Roland Levillain8f7ea9a2018-01-26 17:27:59 +0000167 // Scan the reference fields of object `to_ref`.
Mathieu Chartier8d1a9962016-08-17 16:39:45 -0700168 template <bool kNoUnEvac>
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700169 void Scan(mirror::Object* to_ref) REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -0700170 REQUIRES(!mark_stack_lock_);
Lokesh Gidra8f5aaad2018-12-11 15:05:56 -0800171 // Scan the reference fields of object 'obj' in the dirty cards during
172 // card-table scan. In addition to visiting the references, it also sets the
173 // read-barrier state to gray for Reference-type objects to ensure that
174 // GetReferent() called on these objects calls the read-barrier on the referent.
175 template <bool kNoUnEvac>
176 void ScanDirtyObject(mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_)
177 REQUIRES(!mark_stack_lock_);
Roland Levillain8f7ea9a2018-01-26 17:27:59 +0000178 // Process a field.
Mathieu Chartier8d1a9962016-08-17 16:39:45 -0700179 template <bool kNoUnEvac>
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800180 void Process(mirror::Object* obj, MemberOffset offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700181 REQUIRES_SHARED(Locks::mutator_lock_)
Hans Boehme7d7e9d2019-06-03 18:36:06 +0000182 REQUIRES(!mark_stack_lock_ , !skipped_blocks_lock_, !immune_gray_stack_lock_);
Roland Levillainf73caca2018-08-24 17:19:07 +0100183 void VisitRoots(mirror::Object*** roots, size_t count, const RootInfo& info) override
184 REQUIRES_SHARED(Locks::mutator_lock_)
Hans Boehme7d7e9d2019-06-03 18:36:06 +0000185 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_, !immune_gray_stack_lock_);
Hiroshi Yamauchid8db5a22016-06-28 14:07:41 -0700186 template<bool kGrayImmuneObject>
Hiroshi Yamauchi7a181542017-03-08 17:34:46 -0800187 void MarkRoot(Thread* const self, mirror::CompressedReference<mirror::Object>* root)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700188 REQUIRES_SHARED(Locks::mutator_lock_)
Hans Boehme7d7e9d2019-06-03 18:36:06 +0000189 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_, !immune_gray_stack_lock_);
Roland Levillainf73caca2018-08-24 17:19:07 +0100190 void VisitRoots(mirror::CompressedReference<mirror::Object>** roots,
191 size_t count,
192 const RootInfo& info) override
193 REQUIRES_SHARED(Locks::mutator_lock_)
Hans Boehme7d7e9d2019-06-03 18:36:06 +0000194 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_, !immune_gray_stack_lock_);
Mathieu Chartier90443472015-07-16 20:32:27 -0700195 void VerifyNoFromSpaceReferences() REQUIRES(Locks::mutator_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800196 accounting::ObjectStack* GetAllocationStack();
197 accounting::ObjectStack* GetLiveStack();
Roland Levillainf73caca2018-08-24 17:19:07 +0100198 void ProcessMarkStack() override REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -0700199 REQUIRES(!mark_stack_lock_);
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700200 bool ProcessMarkStackOnce() REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!mark_stack_lock_);
201 void ProcessMarkStackRef(mirror::Object* to_ref) REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -0700202 REQUIRES(!mark_stack_lock_);
Mathieu Chartier21328a12016-07-22 10:47:45 -0700203 void GrayAllDirtyImmuneObjects()
204 REQUIRES(Locks::mutator_lock_)
205 REQUIRES(!mark_stack_lock_);
Mathieu Chartier3768ade2017-05-02 14:04:39 -0700206 void GrayAllNewlyDirtyImmuneObjects()
207 REQUIRES(Locks::mutator_lock_)
208 REQUIRES(!mark_stack_lock_);
Mathieu Chartier21328a12016-07-22 10:47:45 -0700209 void VerifyGrayImmuneObjects()
210 REQUIRES(Locks::mutator_lock_)
211 REQUIRES(!mark_stack_lock_);
Mathieu Chartiera1467d02017-02-22 09:22:50 -0800212 void VerifyNoMissingCardMarks()
213 REQUIRES(Locks::mutator_lock_)
214 REQUIRES(!mark_stack_lock_);
Lokesh Gidra2a9824c2018-11-07 15:57:17 -0800215 template <typename Processor>
216 size_t ProcessThreadLocalMarkStacks(bool disable_weak_ref_access,
217 Closure* checkpoint_callback,
218 const Processor& processor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700219 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!mark_stack_lock_);
Hiroshi Yamauchifebd0cf2016-09-14 19:31:25 -0700220 void RevokeThreadLocalMarkStacks(bool disable_weak_ref_access, Closure* checkpoint_callback)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700221 REQUIRES_SHARED(Locks::mutator_lock_);
222 void SwitchToSharedMarkStackMode() REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -0700223 REQUIRES(!mark_stack_lock_);
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700224 void SwitchToGcExclusiveMarkStackMode() REQUIRES_SHARED(Locks::mutator_lock_);
Roland Levillainf73caca2018-08-24 17:19:07 +0100225 void DelayReferenceReferent(ObjPtr<mirror::Class> klass,
226 ObjPtr<mirror::Reference> reference) override
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700227 REQUIRES_SHARED(Locks::mutator_lock_);
228 void ProcessReferences(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_);
Roland Levillainf73caca2018-08-24 17:19:07 +0100229 mirror::Object* MarkObject(mirror::Object* from_ref) override
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700230 REQUIRES_SHARED(Locks::mutator_lock_)
Hans Boehme7d7e9d2019-06-03 18:36:06 +0000231 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_, !immune_gray_stack_lock_);
Roland Levillainf73caca2018-08-24 17:19:07 +0100232 void MarkHeapReference(mirror::HeapReference<mirror::Object>* from_ref,
233 bool do_atomic_update) override
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700234 REQUIRES_SHARED(Locks::mutator_lock_)
Hans Boehme7d7e9d2019-06-03 18:36:06 +0000235 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_, !immune_gray_stack_lock_);
Mathieu Chartierc381c362016-08-23 13:27:53 -0700236 bool IsMarkedInUnevacFromSpace(mirror::Object* from_ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700237 REQUIRES_SHARED(Locks::mutator_lock_);
Lokesh Gidra519c1c72018-11-09 17:10:47 -0800238 bool IsMarkedInNonMovingSpace(mirror::Object* from_ref)
239 REQUIRES_SHARED(Locks::mutator_lock_);
Roland Levillainf73caca2018-08-24 17:19:07 +0100240 bool IsNullOrMarkedHeapReference(mirror::HeapReference<mirror::Object>* field,
241 bool do_atomic_update) override
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700242 REQUIRES_SHARED(Locks::mutator_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800243 void SweepSystemWeaks(Thread* self)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700244 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Locks::heap_bitmap_lock_);
Roland Levillainb1e1dc32018-07-10 19:19:31 +0100245 // Sweep unmarked objects to complete the garbage collection. Full GCs sweep
246 // all allocation spaces (except the region space). Sticky-bit GCs just sweep
247 // a subset of the heap.
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800248 void Sweep(bool swap_bitmaps)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700249 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::heap_bitmap_lock_, !mark_stack_lock_);
Roland Levillainb1e1dc32018-07-10 19:19:31 +0100250 // Sweep only pointers within an array.
251 void SweepArray(accounting::ObjectStack* allocation_stack_, bool swap_bitmaps)
252 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::heap_bitmap_lock_, !mark_stack_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800253 void SweepLargeObjects(bool swap_bitmaps)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700254 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::heap_bitmap_lock_);
Mathieu Chartier962cd7a2016-08-16 12:15:59 -0700255 void MarkZygoteLargeObjects()
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700256 REQUIRES_SHARED(Locks::mutator_lock_);
Hiroshi Yamauchi7a181542017-03-08 17:34:46 -0800257 void FillWithDummyObject(Thread* const self, mirror::Object* dummy_obj, size_t byte_size)
Hans Boehme7d7e9d2019-06-03 18:36:06 +0000258 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_, !immune_gray_stack_lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700259 REQUIRES_SHARED(Locks::mutator_lock_);
Hiroshi Yamauchi7a181542017-03-08 17:34:46 -0800260 mirror::Object* AllocateInSkippedBlock(Thread* const self, size_t alloc_size)
Hans Boehme7d7e9d2019-06-03 18:36:06 +0000261 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_, !immune_gray_stack_lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700262 REQUIRES_SHARED(Locks::mutator_lock_);
263 void CheckEmptyMarkStack() REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!mark_stack_lock_);
264 void IssueEmptyCheckpoint() REQUIRES_SHARED(Locks::mutator_lock_);
265 bool IsOnAllocStack(mirror::Object* ref) REQUIRES_SHARED(Locks::mutator_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800266 mirror::Object* GetFwdPtr(mirror::Object* from_ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700267 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier90443472015-07-16 20:32:27 -0700268 void FlipThreadRoots() REQUIRES(!Locks::mutator_lock_);
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700269 void SwapStacks() REQUIRES_SHARED(Locks::mutator_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800270 void RecordLiveStackFreezeSize(Thread* self);
271 void ComputeUnevacFromSpaceLiveRatio();
Hiroshi Yamauchi3f64f252015-06-12 18:35:06 -0700272 void LogFromSpaceRefHolder(mirror::Object* obj, MemberOffset offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700273 REQUIRES_SHARED(Locks::mutator_lock_);
Roland Levillain001eff92018-01-24 14:24:33 +0000274 // Dump information about reference `ref` and return it as a string.
275 // Use `ref_name` to name the reference in messages. Each message is prefixed with `indent`.
Andreas Gampebc802de2018-06-20 17:24:11 -0700276 std::string DumpReferenceInfo(mirror::Object* ref, const char* ref_name, const char* indent = "")
Roland Levillain001eff92018-01-24 14:24:33 +0000277 REQUIRES_SHARED(Locks::mutator_lock_);
278 // Dump information about heap reference `ref`, referenced from object `obj` at offset `offset`,
279 // and return it as a string.
280 std::string DumpHeapReference(mirror::Object* obj, MemberOffset offset, mirror::Object* ref)
281 REQUIRES_SHARED(Locks::mutator_lock_);
282 // Dump information about GC root `ref` and return it as a string.
283 std::string DumpGcRoot(mirror::Object* ref) REQUIRES_SHARED(Locks::mutator_lock_);
Hiroshi Yamauchi3f64f252015-06-12 18:35:06 -0700284 void AssertToSpaceInvariantInNonMovingSpace(mirror::Object* obj, mirror::Object* ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700285 REQUIRES_SHARED(Locks::mutator_lock_);
286 void ReenableWeakRefAccess(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_);
287 void DisableMarking() REQUIRES_SHARED(Locks::mutator_lock_);
288 void IssueDisableMarkingCheckpoint() REQUIRES_SHARED(Locks::mutator_lock_);
289 void ExpandGcMarkStack() REQUIRES_SHARED(Locks::mutator_lock_);
Hiroshi Yamauchi7a181542017-03-08 17:34:46 -0800290 mirror::Object* MarkNonMoving(Thread* const self,
291 mirror::Object* from_ref,
Mathieu Chartier1ca68902017-04-18 11:26:22 -0700292 mirror::Object* holder = nullptr,
293 MemberOffset offset = MemberOffset(0))
294 REQUIRES_SHARED(Locks::mutator_lock_)
Hans Boehme7d7e9d2019-06-03 18:36:06 +0000295 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_);
Hiroshi Yamauchi7a181542017-03-08 17:34:46 -0800296 ALWAYS_INLINE mirror::Object* MarkUnevacFromSpaceRegion(Thread* const self,
297 mirror::Object* from_ref,
Hiroshi Yamauchi8e674652015-12-22 11:09:18 -0800298 accounting::SpaceBitmap<kObjectAlignment>* bitmap)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700299 REQUIRES_SHARED(Locks::mutator_lock_)
Hans Boehme7d7e9d2019-06-03 18:36:06 +0000300 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_);
Hiroshi Yamauchid8db5a22016-06-28 14:07:41 -0700301 template<bool kGrayImmuneObject>
Hiroshi Yamauchi7a181542017-03-08 17:34:46 -0800302 ALWAYS_INLINE mirror::Object* MarkImmuneSpace(Thread* const self,
303 mirror::Object* from_ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700304 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!immune_gray_stack_lock_);
Hiroshi Yamauchid8db5a22016-06-28 14:07:41 -0700305 void ScanImmuneObject(mirror::Object* obj)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700306 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!mark_stack_lock_);
Hiroshi Yamauchi7a181542017-03-08 17:34:46 -0800307 mirror::Object* MarkFromReadBarrierWithMeasurements(Thread* const self,
308 mirror::Object* from_ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700309 REQUIRES_SHARED(Locks::mutator_lock_)
Hans Boehme7d7e9d2019-06-03 18:36:06 +0000310 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_, !immune_gray_stack_lock_);
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100311 void DumpPerformanceInfo(std::ostream& os) override REQUIRES(!rb_slow_path_histogram_lock_);
Mathieu Chartier3768ade2017-05-02 14:04:39 -0700312 // Set the read barrier mark entrypoints to non-null.
313 void ActivateReadBarrierEntrypoints();
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800314
Lokesh Gidra2a9824c2018-11-07 15:57:17 -0800315 void CaptureThreadRootsForMarking() REQUIRES_SHARED(Locks::mutator_lock_);
316 void AddLiveBytesAndScanRef(mirror::Object* ref) REQUIRES_SHARED(Locks::mutator_lock_);
317 bool TestMarkBitmapForRef(mirror::Object* ref) REQUIRES_SHARED(Locks::mutator_lock_);
318 template <bool kAtomic = false>
319 bool TestAndSetMarkBitForRef(mirror::Object* ref) REQUIRES_SHARED(Locks::mutator_lock_);
320 void PushOntoLocalMarkStack(mirror::Object* ref) REQUIRES_SHARED(Locks::mutator_lock_);
321 void ProcessMarkStackForMarkingAndComputeLiveBytes() REQUIRES_SHARED(Locks::mutator_lock_)
322 REQUIRES(!mark_stack_lock_);
323
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800324 space::RegionSpace* region_space_; // The underlying region space.
325 std::unique_ptr<Barrier> gc_barrier_;
Hiroshi Yamauchi0b713572015-06-16 18:29:23 -0700326 std::unique_ptr<accounting::ObjectStack> gc_mark_stack_;
Roland Levillain8f7ea9a2018-01-26 17:27:59 +0000327
Albert Mingkun Yang0b4d1462018-11-29 13:25:35 +0000328 // If true, enable generational collection when using the Concurrent Copying
329 // (CC) collector, i.e. use sticky-bit CC for minor collections and (full) CC
330 // for major collections. Generational CC collection is currently only
331 // compatible with Baker read barriers. Set in Heap constructor.
332 const bool use_generational_cc_;
333
334 // Generational "sticky", only trace through dirty objects in region space.
335 const bool young_gen_;
336
337 // If true, the GC thread is done scanning marked objects on dirty and aged
338 // card (see ConcurrentCopying::CopyingPhase).
339 Atomic<bool> done_scanning_;
340
Roland Levillain8f7ea9a2018-01-26 17:27:59 +0000341 // The read-barrier mark-bit stack. Stores object references whose
342 // mark bit has been set by ConcurrentCopying::MarkFromReadBarrier,
343 // so that this bit can be reset at the end of the collection in
344 // ConcurrentCopying::FinishPhase. The mark bit of an object can be
345 // used by mutator read barrier code to quickly test whether that
346 // object has been already marked.
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700347 std::unique_ptr<accounting::ObjectStack> rb_mark_bit_stack_;
Roland Levillain8f7ea9a2018-01-26 17:27:59 +0000348 // Thread-unsafe Boolean value hinting that `rb_mark_bit_stack_` is
349 // full. A thread-safe test of whether the read-barrier mark-bit
350 // stack is full is implemented by `rb_mark_bit_stack_->AtomicPushBack(ref)`
351 // (see use case in ConcurrentCopying::MarkFromReadBarrier).
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700352 bool rb_mark_bit_stack_full_;
Roland Levillain8f7ea9a2018-01-26 17:27:59 +0000353
Hiroshi Yamauchi0b713572015-06-16 18:29:23 -0700354 Mutex mark_stack_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
355 std::vector<accounting::ObjectStack*> revoked_mark_stacks_
356 GUARDED_BY(mark_stack_lock_);
357 static constexpr size_t kMarkStackSize = kPageSize;
358 static constexpr size_t kMarkStackPoolSize = 256;
359 std::vector<accounting::ObjectStack*> pooled_mark_stacks_
360 GUARDED_BY(mark_stack_lock_);
361 Thread* thread_running_gc_;
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800362 bool is_marking_; // True while marking is ongoing.
Mathieu Chartier3768ade2017-05-02 14:04:39 -0700363 // True while we might dispatch on the read barrier entrypoints.
364 bool is_using_read_barrier_entrypoints_;
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800365 bool is_active_; // True while the collection is ongoing.
366 bool is_asserting_to_space_invariant_; // True while asserting the to-space invariant.
Mathieu Chartier763a31e2015-11-16 16:05:55 -0800367 ImmuneSpaces immune_spaces_;
Andreas Gamped4901292017-05-30 18:41:34 -0700368 accounting::ContinuousSpaceBitmap* region_space_bitmap_;
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800369 // A cache of Heap::GetMarkBitmap().
370 accounting::HeapBitmap* heap_mark_bitmap_;
371 size_t live_stack_freeze_size_;
Hans Boehma253c2d2019-05-13 12:38:54 -0700372 size_t from_space_num_objects_at_first_pause_; // Computed if kEnableFromSpaceAccountingCheck
373 size_t from_space_num_bytes_at_first_pause_; // Computed if kEnableFromSpaceAccountingCheck
Hiroshi Yamauchi0b713572015-06-16 18:29:23 -0700374 Atomic<int> is_mark_stack_push_disallowed_;
375 enum MarkStackMode {
376 kMarkStackModeOff = 0, // Mark stack is off.
377 kMarkStackModeThreadLocal, // All threads except for the GC-running thread push refs onto
378 // thread-local mark stacks. The GC-running thread pushes onto and
379 // pops off the GC mark stack without a lock.
380 kMarkStackModeShared, // All threads share the GC mark stack with a lock.
381 kMarkStackModeGcExclusive // The GC-running thread pushes onto and pops from the GC mark stack
382 // without a lock. Other threads won't access the mark stack.
383 };
384 Atomic<MarkStackMode> mark_stack_mode_;
Hiroshi Yamauchifebd0cf2016-09-14 19:31:25 -0700385 bool weak_ref_access_enabled_ GUARDED_BY(Locks::thread_list_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800386
Hans Boehma253c2d2019-05-13 12:38:54 -0700387 // How many objects and bytes we moved. The GC thread moves many more objects
388 // than mutators. Therefore, we separate the two to avoid CAS. Bytes_moved_ and
389 // bytes_moved_gc_thread_ are critical for GC triggering; the others are just informative.
Hiroshi Yamauchi7a181542017-03-08 17:34:46 -0800390 Atomic<size_t> bytes_moved_; // Used by mutators
391 Atomic<size_t> objects_moved_; // Used by mutators
392 size_t bytes_moved_gc_thread_; // Used by GC
393 size_t objects_moved_gc_thread_; // Used by GC
Mathieu Chartiercca44a02016-08-17 10:07:29 -0700394 Atomic<uint64_t> cumulative_bytes_moved_;
395 Atomic<uint64_t> cumulative_objects_moved_;
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800396
Albert Mingkun Yangaf9cce12018-11-07 09:58:35 +0000397 // copied_live_bytes_ratio_sum_ is read and written by CC per GC, in
398 // ReclaimPhase, and is read by DumpPerformanceInfo (potentially from another
399 // thread). However, at present, DumpPerformanceInfo is only called when the
400 // runtime shuts down, so no concurrent access. The same reasoning goes for
401 // gc_count_ and reclaimed_bytes_ratio_sum_
402
Albert Mingkun Yange260e542018-11-05 13:45:59 +0000403 // The sum of of all copied live bytes ratio (to_bytes/from_bytes)
404 float copied_live_bytes_ratio_sum_;
405 // The number of GC counts, used to calculate the average above. (It doesn't
406 // include GC where from_bytes is zero, IOW, from-space is empty, which is
407 // possible for minor GC if all allocated objects are in non-moving
408 // space.)
409 size_t gc_count_;
Lokesh Gidrad0c5b252018-12-05 01:10:40 -0800410 // Bit is set if the corresponding object has inter-region references that
411 // were found during the marking phase of two-phase full-heap GC cycle.
Lokesh Gidra32650ab2018-12-18 16:19:05 -0800412 std::unique_ptr<accounting::ContinuousSpaceBitmap> region_space_inter_region_bitmap_;
413 std::unique_ptr<accounting::ContinuousSpaceBitmap> non_moving_space_inter_region_bitmap_;
Albert Mingkun Yange260e542018-11-05 13:45:59 +0000414
Albert Mingkun Yangaf9cce12018-11-07 09:58:35 +0000415 // reclaimed_bytes_ratio = reclaimed_bytes/num_allocated_bytes per GC cycle
416 float reclaimed_bytes_ratio_sum_;
417
Hans Boehme7d7e9d2019-06-03 18:36:06 +0000418 // The skipped blocks are memory blocks/chucks that were copies of
419 // objects that were unused due to lost races (cas failures) at
420 // object copy/forward pointer install. They may be reused.
421 // Skipped blocks are always in region space. Their size is included directly
422 // in num_bytes_allocated_, i.e. they are treated as allocated, but may be directly
423 // used without going through a GC cycle like other objects. They are reused only
424 // if we run out of region space. TODO: Revisit this design.
425 Mutex skipped_blocks_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
426 std::multimap<size_t, uint8_t*> skipped_blocks_map_ GUARDED_BY(skipped_blocks_lock_);
427 Atomic<size_t> to_space_bytes_skipped_;
428 Atomic<size_t> to_space_objects_skipped_;
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800429
Mathieu Chartier56fe2582016-07-14 13:30:03 -0700430 // If measure_read_barrier_slow_path_ is true, we count how long is spent in MarkFromReadBarrier
431 // and also log.
432 bool measure_read_barrier_slow_path_;
433 // mark_from_read_barrier_measurements_ is true if systrace is enabled or
434 // measure_read_barrier_time_ is true.
435 bool mark_from_read_barrier_measurements_;
436 Atomic<uint64_t> rb_slow_path_ns_;
437 Atomic<uint64_t> rb_slow_path_count_;
438 Atomic<uint64_t> rb_slow_path_count_gc_;
439 mutable Mutex rb_slow_path_histogram_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
440 Histogram<uint64_t> rb_slow_path_time_histogram_ GUARDED_BY(rb_slow_path_histogram_lock_);
441 uint64_t rb_slow_path_count_total_ GUARDED_BY(rb_slow_path_histogram_lock_);
442 uint64_t rb_slow_path_count_gc_total_ GUARDED_BY(rb_slow_path_histogram_lock_);
443
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800444 accounting::ReadBarrierTable* rb_table_;
445 bool force_evacuate_all_; // True if all regions are evacuated.
Hiroshi Yamauchid8db5a22016-06-28 14:07:41 -0700446 Atomic<bool> updated_all_immune_objects_;
447 bool gc_grays_immune_objects_;
448 Mutex immune_gray_stack_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
449 std::vector<mirror::Object*> immune_gray_stack_ GUARDED_BY(immune_gray_stack_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800450
Mathieu Chartier3ed8ec12017-04-20 19:28:54 -0700451 // Class of java.lang.Object. Filled in from WellKnownClasses in FlipCallback. Must
452 // be filled in before flipping thread roots so that FillDummyObject can run. Not
453 // ObjPtr since the GC may transition to suspended and runnable between phases.
454 mirror::Class* java_lang_Object_;
455
Roland Levillainb1e1dc32018-07-10 19:19:31 +0100456 // Sweep array free buffer, used to sweep the spaces based on an array more
457 // efficiently, by recording dead objects to be freed in batches (see
458 // ConcurrentCopying::SweepArray).
459 MemMap sweep_array_free_buffer_mem_map_;
460
Albert Mingkun Yangaf9cce12018-11-07 09:58:35 +0000461 // Use signed because after_gc may be larger than before_gc.
462 int64_t num_bytes_allocated_before_gc_;
463
Mathieu Chartier3768ade2017-05-02 14:04:39 -0700464 class ActivateReadBarrierEntrypointsCallback;
465 class ActivateReadBarrierEntrypointsCheckpoint;
Mathieu Chartiera07f5592016-06-16 11:44:28 -0700466 class AssertToSpaceInvariantFieldVisitor;
Mathieu Chartiera07f5592016-06-16 11:44:28 -0700467 class AssertToSpaceInvariantRefsVisitor;
468 class ClearBlackPtrsVisitor;
469 class ComputeUnevacFromSpaceLiveRatioVisitor;
Hiroshi Yamauchifebd0cf2016-09-14 19:31:25 -0700470 class DisableMarkingCallback;
Mathieu Chartiera07f5592016-06-16 11:44:28 -0700471 class DisableMarkingCheckpoint;
Hiroshi Yamauchifebd0cf2016-09-14 19:31:25 -0700472 class DisableWeakRefAccessCallback;
Mathieu Chartiera07f5592016-06-16 11:44:28 -0700473 class FlipCallback;
Mathieu Chartier3768ade2017-05-02 14:04:39 -0700474 template <bool kConcurrent> class GrayImmuneObjectVisitor;
Hiroshi Yamauchid8db5a22016-06-28 14:07:41 -0700475 class ImmuneSpaceScanObjVisitor;
Mathieu Chartiera07f5592016-06-16 11:44:28 -0700476 class LostCopyVisitor;
Mathieu Chartier8d1a9962016-08-17 16:39:45 -0700477 template <bool kNoUnEvac> class RefFieldsVisitor;
Mathieu Chartiera07f5592016-06-16 11:44:28 -0700478 class RevokeThreadLocalMarkStackCheckpoint;
Hiroshi Yamauchid8db5a22016-06-28 14:07:41 -0700479 class ScopedGcGraysImmuneObjects;
480 class ThreadFlipVisitor;
Mathieu Chartier21328a12016-07-22 10:47:45 -0700481 class VerifyGrayImmuneObjectsVisitor;
Mathieu Chartiera07f5592016-06-16 11:44:28 -0700482 class VerifyNoFromSpaceRefsFieldVisitor;
Mathieu Chartiera07f5592016-06-16 11:44:28 -0700483 class VerifyNoFromSpaceRefsVisitor;
Mathieu Chartiera1467d02017-02-22 09:22:50 -0800484 class VerifyNoMissingCardMarkVisitor;
Lokesh Gidra2a9824c2018-11-07 15:57:17 -0800485 class ImmuneSpaceCaptureRefsVisitor;
486 template <bool kAtomicTestAndSet = false> class CaptureRootsForMarkingVisitor;
487 class CaptureThreadRootsForMarkingAndCheckpoint;
488 template <bool kHandleInterRegionRefs> class ComputeLiveBytesAndMarkRefFieldsVisitor;
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800489
Mathieu Chartier3130cdf2015-05-03 15:20:23 -0700490 DISALLOW_IMPLICIT_CONSTRUCTORS(ConcurrentCopying);
Hiroshi Yamauchid5307ec2014-03-27 21:07:51 -0700491};
492
493} // namespace collector
494} // namespace gc
495} // namespace art
496
497#endif // ART_RUNTIME_GC_COLLECTOR_CONCURRENT_COPYING_H_