blob: 124713c17c2ae6417a8918c281a14cfa935ae798 [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_,
78 !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_)
Hiroshi Yamauchid8db5a22016-06-28 14:07:41 -070085 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_,
88 !rb_slow_path_histogram_lock_,
89 !skipped_blocks_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080090
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070091 void BindBitmaps() REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -070092 REQUIRES(!Locks::heap_bitmap_lock_);
Roland Levillainf73caca2018-08-24 17:19:07 +010093 GcType GetGcType() const override {
Albert Mingkun Yang0b4d1462018-11-29 13:25:35 +000094 return (use_generational_cc_ && young_gen_)
Mathieu Chartier8d1a9962016-08-17 16:39:45 -070095 ? kGcTypeSticky
96 : kGcTypePartial;
Hiroshi Yamauchid5307ec2014-03-27 21:07:51 -070097 }
Roland Levillainf73caca2018-08-24 17:19:07 +010098 CollectorType GetCollectorType() const override {
Hiroshi Yamauchid5307ec2014-03-27 21:07:51 -070099 return kCollectorTypeCC;
100 }
Roland Levillainf73caca2018-08-24 17:19:07 +0100101 void RevokeAllThreadLocalBuffers() override;
Lokesh Gidra1c34b712018-12-18 13:41:58 -0800102 // Creates inter-region ref bitmaps for region-space and non-moving-space.
103 // Gets called in Heap construction after the two spaces are created.
104 void CreateInterRegionRefBitmaps();
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800105 void SetRegionSpace(space::RegionSpace* region_space) {
106 DCHECK(region_space != nullptr);
107 region_space_ = region_space;
108 }
109 space::RegionSpace* RegionSpace() {
110 return region_space_;
111 }
Roland Levillain001eff92018-01-24 14:24:33 +0000112 // Assert the to-space invariant for a heap reference `ref` held in `obj` at offset `offset`.
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800113 void AssertToSpaceInvariant(mirror::Object* obj, MemberOffset offset, mirror::Object* ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700114 REQUIRES_SHARED(Locks::mutator_lock_);
Roland Levillain001eff92018-01-24 14:24:33 +0000115 // Assert the to-space invariant for a GC root reference `ref`.
Hiroshi Yamauchi3f64f252015-06-12 18:35:06 -0700116 void AssertToSpaceInvariant(GcRootSource* gc_root_source, mirror::Object* ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700117 REQUIRES_SHARED(Locks::mutator_lock_);
118 bool IsInToSpace(mirror::Object* ref) REQUIRES_SHARED(Locks::mutator_lock_) {
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800119 DCHECK(ref != nullptr);
120 return IsMarked(ref) == ref;
121 }
Roland Levillain8f7ea9a2018-01-26 17:27:59 +0000122 // Mark object `from_ref`, copying it to the to-space if needed.
Mathieu Chartier8d1a9962016-08-17 16:39:45 -0700123 template<bool kGrayImmuneObject = true, bool kNoUnEvac = false, bool kFromGCThread = false>
Hiroshi Yamauchi7a181542017-03-08 17:34:46 -0800124 ALWAYS_INLINE mirror::Object* Mark(Thread* const self,
125 mirror::Object* from_ref,
Mathieu Chartier1ca68902017-04-18 11:26:22 -0700126 mirror::Object* holder = nullptr,
127 MemberOffset offset = MemberOffset(0))
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700128 REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier56fe2582016-07-14 13:30:03 -0700129 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_, !immune_gray_stack_lock_);
130 ALWAYS_INLINE mirror::Object* MarkFromReadBarrier(mirror::Object* from_ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700131 REQUIRES_SHARED(Locks::mutator_lock_)
Hiroshi Yamauchid8db5a22016-06-28 14:07:41 -0700132 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_, !immune_gray_stack_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800133 bool IsMarking() const {
134 return is_marking_;
135 }
Mathieu Chartier3768ade2017-05-02 14:04:39 -0700136 // We may want to use read barrier entrypoints before is_marking_ is true since concurrent graying
137 // creates a small window where we might dispatch on these entrypoints.
138 bool IsUsingReadBarrierEntrypoints() const {
139 return is_using_read_barrier_entrypoints_;
140 }
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800141 bool IsActive() const {
142 return is_active_;
143 }
144 Barrier& GetBarrier() {
145 return *gc_barrier_;
146 }
Hiroshi Yamauchifebd0cf2016-09-14 19:31:25 -0700147 bool IsWeakRefAccessEnabled() REQUIRES(Locks::thread_list_lock_) {
148 return weak_ref_access_enabled_;
Hiroshi Yamauchi0b713572015-06-16 18:29:23 -0700149 }
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700150 void RevokeThreadLocalMarkStack(Thread* thread) REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -0700151 REQUIRES(!mark_stack_lock_);
Hiroshi Yamauchid5307ec2014-03-27 21:07:51 -0700152
Roland Levillainf73caca2018-08-24 17:19:07 +0100153 mirror::Object* IsMarked(mirror::Object* from_ref) override
Nicolas Geoffray13056a12017-05-11 11:48:28 +0000154 REQUIRES_SHARED(Locks::mutator_lock_);
155
Hiroshi Yamauchid5307ec2014-03-27 21:07:51 -0700156 private:
Hiroshi Yamauchi7a181542017-03-08 17:34:46 -0800157 void PushOntoMarkStack(Thread* const self, mirror::Object* obj)
158 REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -0700159 REQUIRES(!mark_stack_lock_);
Hiroshi Yamauchi7a181542017-03-08 17:34:46 -0800160 mirror::Object* Copy(Thread* const self,
161 mirror::Object* from_ref,
Mathieu Chartieref496d92017-04-28 18:58:59 -0700162 mirror::Object* holder,
163 MemberOffset offset)
164 REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartierd6636d32016-07-28 11:02:38 -0700165 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_, !immune_gray_stack_lock_);
Roland Levillain8f7ea9a2018-01-26 17:27:59 +0000166 // Scan the reference fields of object `to_ref`.
Mathieu Chartier8d1a9962016-08-17 16:39:45 -0700167 template <bool kNoUnEvac>
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700168 void Scan(mirror::Object* to_ref) REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -0700169 REQUIRES(!mark_stack_lock_);
Lokesh Gidra8f5aaad2018-12-11 15:05:56 -0800170 // Scan the reference fields of object 'obj' in the dirty cards during
171 // card-table scan. In addition to visiting the references, it also sets the
172 // read-barrier state to gray for Reference-type objects to ensure that
173 // GetReferent() called on these objects calls the read-barrier on the referent.
174 template <bool kNoUnEvac>
175 void ScanDirtyObject(mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_)
176 REQUIRES(!mark_stack_lock_);
Roland Levillain8f7ea9a2018-01-26 17:27:59 +0000177 // Process a field.
Mathieu Chartier8d1a9962016-08-17 16:39:45 -0700178 template <bool kNoUnEvac>
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800179 void Process(mirror::Object* obj, MemberOffset offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700180 REQUIRES_SHARED(Locks::mutator_lock_)
Hiroshi Yamauchid8db5a22016-06-28 14:07:41 -0700181 REQUIRES(!mark_stack_lock_ , !skipped_blocks_lock_, !immune_gray_stack_lock_);
Roland Levillainf73caca2018-08-24 17:19:07 +0100182 void VisitRoots(mirror::Object*** roots, size_t count, const RootInfo& info) override
183 REQUIRES_SHARED(Locks::mutator_lock_)
Hiroshi Yamauchid8db5a22016-06-28 14:07:41 -0700184 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_, !immune_gray_stack_lock_);
185 template<bool kGrayImmuneObject>
Hiroshi Yamauchi7a181542017-03-08 17:34:46 -0800186 void MarkRoot(Thread* const self, mirror::CompressedReference<mirror::Object>* root)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700187 REQUIRES_SHARED(Locks::mutator_lock_)
Hiroshi Yamauchid8db5a22016-06-28 14:07:41 -0700188 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_, !immune_gray_stack_lock_);
Roland Levillainf73caca2018-08-24 17:19:07 +0100189 void VisitRoots(mirror::CompressedReference<mirror::Object>** roots,
190 size_t count,
191 const RootInfo& info) override
192 REQUIRES_SHARED(Locks::mutator_lock_)
Hiroshi Yamauchid8db5a22016-06-28 14:07:41 -0700193 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_, !immune_gray_stack_lock_);
Mathieu Chartier90443472015-07-16 20:32:27 -0700194 void VerifyNoFromSpaceReferences() REQUIRES(Locks::mutator_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800195 accounting::ObjectStack* GetAllocationStack();
196 accounting::ObjectStack* GetLiveStack();
Roland Levillainf73caca2018-08-24 17:19:07 +0100197 void ProcessMarkStack() override REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -0700198 REQUIRES(!mark_stack_lock_);
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700199 bool ProcessMarkStackOnce() REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!mark_stack_lock_);
200 void ProcessMarkStackRef(mirror::Object* to_ref) REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -0700201 REQUIRES(!mark_stack_lock_);
Mathieu Chartier21328a12016-07-22 10:47:45 -0700202 void GrayAllDirtyImmuneObjects()
203 REQUIRES(Locks::mutator_lock_)
204 REQUIRES(!mark_stack_lock_);
Mathieu Chartier3768ade2017-05-02 14:04:39 -0700205 void GrayAllNewlyDirtyImmuneObjects()
206 REQUIRES(Locks::mutator_lock_)
207 REQUIRES(!mark_stack_lock_);
Mathieu Chartier21328a12016-07-22 10:47:45 -0700208 void VerifyGrayImmuneObjects()
209 REQUIRES(Locks::mutator_lock_)
210 REQUIRES(!mark_stack_lock_);
Mathieu Chartiera1467d02017-02-22 09:22:50 -0800211 void VerifyNoMissingCardMarks()
212 REQUIRES(Locks::mutator_lock_)
213 REQUIRES(!mark_stack_lock_);
Lokesh Gidra2a9824c2018-11-07 15:57:17 -0800214 template <typename Processor>
215 size_t ProcessThreadLocalMarkStacks(bool disable_weak_ref_access,
216 Closure* checkpoint_callback,
217 const Processor& processor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700218 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!mark_stack_lock_);
Hiroshi Yamauchifebd0cf2016-09-14 19:31:25 -0700219 void RevokeThreadLocalMarkStacks(bool disable_weak_ref_access, Closure* checkpoint_callback)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700220 REQUIRES_SHARED(Locks::mutator_lock_);
221 void SwitchToSharedMarkStackMode() REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -0700222 REQUIRES(!mark_stack_lock_);
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700223 void SwitchToGcExclusiveMarkStackMode() REQUIRES_SHARED(Locks::mutator_lock_);
Roland Levillainf73caca2018-08-24 17:19:07 +0100224 void DelayReferenceReferent(ObjPtr<mirror::Class> klass,
225 ObjPtr<mirror::Reference> reference) override
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700226 REQUIRES_SHARED(Locks::mutator_lock_);
227 void ProcessReferences(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_);
Roland Levillainf73caca2018-08-24 17:19:07 +0100228 mirror::Object* MarkObject(mirror::Object* from_ref) override
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700229 REQUIRES_SHARED(Locks::mutator_lock_)
Hiroshi Yamauchid8db5a22016-06-28 14:07:41 -0700230 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_, !immune_gray_stack_lock_);
Roland Levillainf73caca2018-08-24 17:19:07 +0100231 void MarkHeapReference(mirror::HeapReference<mirror::Object>* from_ref,
232 bool do_atomic_update) override
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700233 REQUIRES_SHARED(Locks::mutator_lock_)
Hiroshi Yamauchid8db5a22016-06-28 14:07:41 -0700234 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_, !immune_gray_stack_lock_);
Mathieu Chartierc381c362016-08-23 13:27:53 -0700235 bool IsMarkedInUnevacFromSpace(mirror::Object* from_ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700236 REQUIRES_SHARED(Locks::mutator_lock_);
Lokesh Gidra519c1c72018-11-09 17:10:47 -0800237 bool IsMarkedInNonMovingSpace(mirror::Object* from_ref)
238 REQUIRES_SHARED(Locks::mutator_lock_);
Roland Levillainf73caca2018-08-24 17:19:07 +0100239 bool IsNullOrMarkedHeapReference(mirror::HeapReference<mirror::Object>* field,
240 bool do_atomic_update) override
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700241 REQUIRES_SHARED(Locks::mutator_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800242 void SweepSystemWeaks(Thread* self)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700243 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Locks::heap_bitmap_lock_);
Roland Levillainb1e1dc32018-07-10 19:19:31 +0100244 // Sweep unmarked objects to complete the garbage collection. Full GCs sweep
245 // all allocation spaces (except the region space). Sticky-bit GCs just sweep
246 // a subset of the heap.
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800247 void Sweep(bool swap_bitmaps)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700248 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::heap_bitmap_lock_, !mark_stack_lock_);
Roland Levillainb1e1dc32018-07-10 19:19:31 +0100249 // Sweep only pointers within an array.
250 void SweepArray(accounting::ObjectStack* allocation_stack_, bool swap_bitmaps)
251 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::heap_bitmap_lock_, !mark_stack_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800252 void SweepLargeObjects(bool swap_bitmaps)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700253 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::heap_bitmap_lock_);
Mathieu Chartier962cd7a2016-08-16 12:15:59 -0700254 void MarkZygoteLargeObjects()
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700255 REQUIRES_SHARED(Locks::mutator_lock_);
Hiroshi Yamauchi7a181542017-03-08 17:34:46 -0800256 void FillWithDummyObject(Thread* const self, mirror::Object* dummy_obj, size_t byte_size)
Mathieu Chartierd6636d32016-07-28 11:02:38 -0700257 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_, !immune_gray_stack_lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700258 REQUIRES_SHARED(Locks::mutator_lock_);
Hiroshi Yamauchi7a181542017-03-08 17:34:46 -0800259 mirror::Object* AllocateInSkippedBlock(Thread* const self, size_t alloc_size)
Mathieu Chartierd6636d32016-07-28 11:02:38 -0700260 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_, !immune_gray_stack_lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700261 REQUIRES_SHARED(Locks::mutator_lock_);
262 void CheckEmptyMarkStack() REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!mark_stack_lock_);
263 void IssueEmptyCheckpoint() REQUIRES_SHARED(Locks::mutator_lock_);
264 bool IsOnAllocStack(mirror::Object* ref) REQUIRES_SHARED(Locks::mutator_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800265 mirror::Object* GetFwdPtr(mirror::Object* from_ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700266 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier90443472015-07-16 20:32:27 -0700267 void FlipThreadRoots() REQUIRES(!Locks::mutator_lock_);
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700268 void SwapStacks() REQUIRES_SHARED(Locks::mutator_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800269 void RecordLiveStackFreezeSize(Thread* self);
270 void ComputeUnevacFromSpaceLiveRatio();
Hiroshi Yamauchi3f64f252015-06-12 18:35:06 -0700271 void LogFromSpaceRefHolder(mirror::Object* obj, MemberOffset offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700272 REQUIRES_SHARED(Locks::mutator_lock_);
Roland Levillain001eff92018-01-24 14:24:33 +0000273 // Dump information about reference `ref` and return it as a string.
274 // Use `ref_name` to name the reference in messages. Each message is prefixed with `indent`.
Andreas Gampebc802de2018-06-20 17:24:11 -0700275 std::string DumpReferenceInfo(mirror::Object* ref, const char* ref_name, const char* indent = "")
Roland Levillain001eff92018-01-24 14:24:33 +0000276 REQUIRES_SHARED(Locks::mutator_lock_);
277 // Dump information about heap reference `ref`, referenced from object `obj` at offset `offset`,
278 // and return it as a string.
279 std::string DumpHeapReference(mirror::Object* obj, MemberOffset offset, mirror::Object* ref)
280 REQUIRES_SHARED(Locks::mutator_lock_);
281 // Dump information about GC root `ref` and return it as a string.
282 std::string DumpGcRoot(mirror::Object* ref) REQUIRES_SHARED(Locks::mutator_lock_);
Hiroshi Yamauchi3f64f252015-06-12 18:35:06 -0700283 void AssertToSpaceInvariantInNonMovingSpace(mirror::Object* obj, mirror::Object* ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700284 REQUIRES_SHARED(Locks::mutator_lock_);
285 void ReenableWeakRefAccess(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_);
286 void DisableMarking() REQUIRES_SHARED(Locks::mutator_lock_);
287 void IssueDisableMarkingCheckpoint() REQUIRES_SHARED(Locks::mutator_lock_);
288 void ExpandGcMarkStack() REQUIRES_SHARED(Locks::mutator_lock_);
Hiroshi Yamauchi7a181542017-03-08 17:34:46 -0800289 mirror::Object* MarkNonMoving(Thread* const self,
290 mirror::Object* from_ref,
Mathieu Chartier1ca68902017-04-18 11:26:22 -0700291 mirror::Object* holder = nullptr,
292 MemberOffset offset = MemberOffset(0))
293 REQUIRES_SHARED(Locks::mutator_lock_)
Hiroshi Yamauchi723e6ce2015-10-28 20:59:47 -0700294 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_);
Hiroshi Yamauchi7a181542017-03-08 17:34:46 -0800295 ALWAYS_INLINE mirror::Object* MarkUnevacFromSpaceRegion(Thread* const self,
296 mirror::Object* from_ref,
Hiroshi Yamauchi8e674652015-12-22 11:09:18 -0800297 accounting::SpaceBitmap<kObjectAlignment>* bitmap)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700298 REQUIRES_SHARED(Locks::mutator_lock_)
Hiroshi Yamauchi8e674652015-12-22 11:09:18 -0800299 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_);
Hiroshi Yamauchid8db5a22016-06-28 14:07:41 -0700300 template<bool kGrayImmuneObject>
Hiroshi Yamauchi7a181542017-03-08 17:34:46 -0800301 ALWAYS_INLINE mirror::Object* MarkImmuneSpace(Thread* const self,
302 mirror::Object* from_ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700303 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!immune_gray_stack_lock_);
Hiroshi Yamauchid8db5a22016-06-28 14:07:41 -0700304 void ScanImmuneObject(mirror::Object* obj)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700305 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!mark_stack_lock_);
Hiroshi Yamauchi7a181542017-03-08 17:34:46 -0800306 mirror::Object* MarkFromReadBarrierWithMeasurements(Thread* const self,
307 mirror::Object* from_ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700308 REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier56fe2582016-07-14 13:30:03 -0700309 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_, !immune_gray_stack_lock_);
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100310 void DumpPerformanceInfo(std::ostream& os) override REQUIRES(!rb_slow_path_histogram_lock_);
Mathieu Chartier3768ade2017-05-02 14:04:39 -0700311 // Set the read barrier mark entrypoints to non-null.
312 void ActivateReadBarrierEntrypoints();
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800313
Lokesh Gidra2a9824c2018-11-07 15:57:17 -0800314 void CaptureThreadRootsForMarking() REQUIRES_SHARED(Locks::mutator_lock_);
315 void AddLiveBytesAndScanRef(mirror::Object* ref) REQUIRES_SHARED(Locks::mutator_lock_);
316 bool TestMarkBitmapForRef(mirror::Object* ref) REQUIRES_SHARED(Locks::mutator_lock_);
317 template <bool kAtomic = false>
318 bool TestAndSetMarkBitForRef(mirror::Object* ref) REQUIRES_SHARED(Locks::mutator_lock_);
319 void PushOntoLocalMarkStack(mirror::Object* ref) REQUIRES_SHARED(Locks::mutator_lock_);
320 void ProcessMarkStackForMarkingAndComputeLiveBytes() REQUIRES_SHARED(Locks::mutator_lock_)
321 REQUIRES(!mark_stack_lock_);
322
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800323 space::RegionSpace* region_space_; // The underlying region space.
324 std::unique_ptr<Barrier> gc_barrier_;
Hiroshi Yamauchi0b713572015-06-16 18:29:23 -0700325 std::unique_ptr<accounting::ObjectStack> gc_mark_stack_;
Roland Levillain8f7ea9a2018-01-26 17:27:59 +0000326
Albert Mingkun Yang0b4d1462018-11-29 13:25:35 +0000327 // If true, enable generational collection when using the Concurrent Copying
328 // (CC) collector, i.e. use sticky-bit CC for minor collections and (full) CC
329 // for major collections. Generational CC collection is currently only
330 // compatible with Baker read barriers. Set in Heap constructor.
331 const bool use_generational_cc_;
332
333 // Generational "sticky", only trace through dirty objects in region space.
334 const bool young_gen_;
335
336 // If true, the GC thread is done scanning marked objects on dirty and aged
337 // card (see ConcurrentCopying::CopyingPhase).
338 Atomic<bool> done_scanning_;
339
Roland Levillain8f7ea9a2018-01-26 17:27:59 +0000340 // The read-barrier mark-bit stack. Stores object references whose
341 // mark bit has been set by ConcurrentCopying::MarkFromReadBarrier,
342 // so that this bit can be reset at the end of the collection in
343 // ConcurrentCopying::FinishPhase. The mark bit of an object can be
344 // used by mutator read barrier code to quickly test whether that
345 // object has been already marked.
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700346 std::unique_ptr<accounting::ObjectStack> rb_mark_bit_stack_;
Roland Levillain8f7ea9a2018-01-26 17:27:59 +0000347 // Thread-unsafe Boolean value hinting that `rb_mark_bit_stack_` is
348 // full. A thread-safe test of whether the read-barrier mark-bit
349 // stack is full is implemented by `rb_mark_bit_stack_->AtomicPushBack(ref)`
350 // (see use case in ConcurrentCopying::MarkFromReadBarrier).
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700351 bool rb_mark_bit_stack_full_;
Roland Levillain8f7ea9a2018-01-26 17:27:59 +0000352
Hiroshi Yamauchi0b713572015-06-16 18:29:23 -0700353 Mutex mark_stack_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
354 std::vector<accounting::ObjectStack*> revoked_mark_stacks_
355 GUARDED_BY(mark_stack_lock_);
356 static constexpr size_t kMarkStackSize = kPageSize;
357 static constexpr size_t kMarkStackPoolSize = 256;
358 std::vector<accounting::ObjectStack*> pooled_mark_stacks_
359 GUARDED_BY(mark_stack_lock_);
360 Thread* thread_running_gc_;
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800361 bool is_marking_; // True while marking is ongoing.
Mathieu Chartier3768ade2017-05-02 14:04:39 -0700362 // True while we might dispatch on the read barrier entrypoints.
363 bool is_using_read_barrier_entrypoints_;
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800364 bool is_active_; // True while the collection is ongoing.
365 bool is_asserting_to_space_invariant_; // True while asserting the to-space invariant.
Mathieu Chartier763a31e2015-11-16 16:05:55 -0800366 ImmuneSpaces immune_spaces_;
Andreas Gamped4901292017-05-30 18:41:34 -0700367 accounting::ContinuousSpaceBitmap* region_space_bitmap_;
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800368 // A cache of Heap::GetMarkBitmap().
369 accounting::HeapBitmap* heap_mark_bitmap_;
370 size_t live_stack_freeze_size_;
371 size_t from_space_num_objects_at_first_pause_;
372 size_t from_space_num_bytes_at_first_pause_;
Hiroshi Yamauchi0b713572015-06-16 18:29:23 -0700373 Atomic<int> is_mark_stack_push_disallowed_;
374 enum MarkStackMode {
375 kMarkStackModeOff = 0, // Mark stack is off.
376 kMarkStackModeThreadLocal, // All threads except for the GC-running thread push refs onto
377 // thread-local mark stacks. The GC-running thread pushes onto and
378 // pops off the GC mark stack without a lock.
379 kMarkStackModeShared, // All threads share the GC mark stack with a lock.
380 kMarkStackModeGcExclusive // The GC-running thread pushes onto and pops from the GC mark stack
381 // without a lock. Other threads won't access the mark stack.
382 };
383 Atomic<MarkStackMode> mark_stack_mode_;
Hiroshi Yamauchifebd0cf2016-09-14 19:31:25 -0700384 bool weak_ref_access_enabled_ GUARDED_BY(Locks::thread_list_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800385
386 // How many objects and bytes we moved. Used for accounting.
Hiroshi Yamauchi7a181542017-03-08 17:34:46 -0800387 // GC thread moves many more objects than mutators.
388 // Therefore, we separate the two to avoid CAS.
389 Atomic<size_t> bytes_moved_; // Used by mutators
390 Atomic<size_t> objects_moved_; // Used by mutators
391 size_t bytes_moved_gc_thread_; // Used by GC
392 size_t objects_moved_gc_thread_; // Used by GC
Mathieu Chartiercca44a02016-08-17 10:07:29 -0700393 Atomic<uint64_t> cumulative_bytes_moved_;
394 Atomic<uint64_t> cumulative_objects_moved_;
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800395
Albert Mingkun Yangaf9cce12018-11-07 09:58:35 +0000396 // copied_live_bytes_ratio_sum_ is read and written by CC per GC, in
397 // ReclaimPhase, and is read by DumpPerformanceInfo (potentially from another
398 // thread). However, at present, DumpPerformanceInfo is only called when the
399 // runtime shuts down, so no concurrent access. The same reasoning goes for
400 // gc_count_ and reclaimed_bytes_ratio_sum_
401
Albert Mingkun Yange260e542018-11-05 13:45:59 +0000402 // The sum of of all copied live bytes ratio (to_bytes/from_bytes)
403 float copied_live_bytes_ratio_sum_;
404 // The number of GC counts, used to calculate the average above. (It doesn't
405 // include GC where from_bytes is zero, IOW, from-space is empty, which is
406 // possible for minor GC if all allocated objects are in non-moving
407 // space.)
408 size_t gc_count_;
Lokesh Gidrad0c5b252018-12-05 01:10:40 -0800409 // Bit is set if the corresponding object has inter-region references that
410 // were found during the marking phase of two-phase full-heap GC cycle.
Lokesh Gidra32650ab2018-12-18 16:19:05 -0800411 std::unique_ptr<accounting::ContinuousSpaceBitmap> region_space_inter_region_bitmap_;
412 std::unique_ptr<accounting::ContinuousSpaceBitmap> non_moving_space_inter_region_bitmap_;
Albert Mingkun Yange260e542018-11-05 13:45:59 +0000413
Albert Mingkun Yangaf9cce12018-11-07 09:58:35 +0000414 // reclaimed_bytes_ratio = reclaimed_bytes/num_allocated_bytes per GC cycle
415 float reclaimed_bytes_ratio_sum_;
416
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800417 // The skipped blocks are memory blocks/chucks that were copies of
418 // objects that were unused due to lost races (cas failures) at
419 // object copy/forward pointer install. They are reused.
420 Mutex skipped_blocks_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
421 std::multimap<size_t, uint8_t*> skipped_blocks_map_ GUARDED_BY(skipped_blocks_lock_);
422 Atomic<size_t> to_space_bytes_skipped_;
423 Atomic<size_t> to_space_objects_skipped_;
424
Mathieu Chartier56fe2582016-07-14 13:30:03 -0700425 // If measure_read_barrier_slow_path_ is true, we count how long is spent in MarkFromReadBarrier
426 // and also log.
427 bool measure_read_barrier_slow_path_;
428 // mark_from_read_barrier_measurements_ is true if systrace is enabled or
429 // measure_read_barrier_time_ is true.
430 bool mark_from_read_barrier_measurements_;
431 Atomic<uint64_t> rb_slow_path_ns_;
432 Atomic<uint64_t> rb_slow_path_count_;
433 Atomic<uint64_t> rb_slow_path_count_gc_;
434 mutable Mutex rb_slow_path_histogram_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
435 Histogram<uint64_t> rb_slow_path_time_histogram_ GUARDED_BY(rb_slow_path_histogram_lock_);
436 uint64_t rb_slow_path_count_total_ GUARDED_BY(rb_slow_path_histogram_lock_);
437 uint64_t rb_slow_path_count_gc_total_ GUARDED_BY(rb_slow_path_histogram_lock_);
438
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800439 accounting::ReadBarrierTable* rb_table_;
440 bool force_evacuate_all_; // True if all regions are evacuated.
Hiroshi Yamauchid8db5a22016-06-28 14:07:41 -0700441 Atomic<bool> updated_all_immune_objects_;
442 bool gc_grays_immune_objects_;
443 Mutex immune_gray_stack_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
444 std::vector<mirror::Object*> immune_gray_stack_ GUARDED_BY(immune_gray_stack_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800445
Mathieu Chartier3ed8ec12017-04-20 19:28:54 -0700446 // Class of java.lang.Object. Filled in from WellKnownClasses in FlipCallback. Must
447 // be filled in before flipping thread roots so that FillDummyObject can run. Not
448 // ObjPtr since the GC may transition to suspended and runnable between phases.
449 mirror::Class* java_lang_Object_;
450
Roland Levillainb1e1dc32018-07-10 19:19:31 +0100451 // Sweep array free buffer, used to sweep the spaces based on an array more
452 // efficiently, by recording dead objects to be freed in batches (see
453 // ConcurrentCopying::SweepArray).
454 MemMap sweep_array_free_buffer_mem_map_;
455
Albert Mingkun Yangaf9cce12018-11-07 09:58:35 +0000456 // Use signed because after_gc may be larger than before_gc.
457 int64_t num_bytes_allocated_before_gc_;
458
Mathieu Chartier3768ade2017-05-02 14:04:39 -0700459 class ActivateReadBarrierEntrypointsCallback;
460 class ActivateReadBarrierEntrypointsCheckpoint;
Mathieu Chartiera07f5592016-06-16 11:44:28 -0700461 class AssertToSpaceInvariantFieldVisitor;
Mathieu Chartiera07f5592016-06-16 11:44:28 -0700462 class AssertToSpaceInvariantRefsVisitor;
463 class ClearBlackPtrsVisitor;
464 class ComputeUnevacFromSpaceLiveRatioVisitor;
Hiroshi Yamauchifebd0cf2016-09-14 19:31:25 -0700465 class DisableMarkingCallback;
Mathieu Chartiera07f5592016-06-16 11:44:28 -0700466 class DisableMarkingCheckpoint;
Hiroshi Yamauchifebd0cf2016-09-14 19:31:25 -0700467 class DisableWeakRefAccessCallback;
Mathieu Chartiera07f5592016-06-16 11:44:28 -0700468 class FlipCallback;
Mathieu Chartier3768ade2017-05-02 14:04:39 -0700469 template <bool kConcurrent> class GrayImmuneObjectVisitor;
Hiroshi Yamauchid8db5a22016-06-28 14:07:41 -0700470 class ImmuneSpaceScanObjVisitor;
Mathieu Chartiera07f5592016-06-16 11:44:28 -0700471 class LostCopyVisitor;
Mathieu Chartier8d1a9962016-08-17 16:39:45 -0700472 template <bool kNoUnEvac> class RefFieldsVisitor;
Mathieu Chartiera07f5592016-06-16 11:44:28 -0700473 class RevokeThreadLocalMarkStackCheckpoint;
Hiroshi Yamauchid8db5a22016-06-28 14:07:41 -0700474 class ScopedGcGraysImmuneObjects;
475 class ThreadFlipVisitor;
Mathieu Chartier21328a12016-07-22 10:47:45 -0700476 class VerifyGrayImmuneObjectsVisitor;
Mathieu Chartiera07f5592016-06-16 11:44:28 -0700477 class VerifyNoFromSpaceRefsFieldVisitor;
Mathieu Chartiera07f5592016-06-16 11:44:28 -0700478 class VerifyNoFromSpaceRefsVisitor;
Mathieu Chartiera1467d02017-02-22 09:22:50 -0800479 class VerifyNoMissingCardMarkVisitor;
Lokesh Gidra2a9824c2018-11-07 15:57:17 -0800480 class ImmuneSpaceCaptureRefsVisitor;
481 template <bool kAtomicTestAndSet = false> class CaptureRootsForMarkingVisitor;
482 class CaptureThreadRootsForMarkingAndCheckpoint;
483 template <bool kHandleInterRegionRefs> class ComputeLiveBytesAndMarkRefFieldsVisitor;
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800484
Mathieu Chartier3130cdf2015-05-03 15:20:23 -0700485 DISALLOW_IMPLICIT_CONSTRUCTORS(ConcurrentCopying);
Hiroshi Yamauchid5307ec2014-03-27 21:07:51 -0700486};
487
488} // namespace collector
489} // namespace gc
490} // namespace art
491
492#endif // ART_RUNTIME_GC_COLLECTOR_CONCURRENT_COPYING_H_