blob: a41c17a74862f65fc0c5750e42b44b5f1f8c7c52 [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
Chih-Hung Hsieha5931182016-09-01 15:08:13 -070068 explicit ConcurrentCopying(Heap* heap,
Mathieu Chartier8d1a9962016-08-17 16:39:45 -070069 bool young_gen,
Chih-Hung Hsieha5931182016-09-01 15:08:13 -070070 const std::string& name_prefix = "",
71 bool measure_read_barrier_slow_path = false);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080072 ~ConcurrentCopying();
Hiroshi Yamauchid5307ec2014-03-27 21:07:51 -070073
Roland Levillainf73caca2018-08-24 17:19:07 +010074 void RunPhases() override
Mathieu Chartier56fe2582016-07-14 13:30:03 -070075 REQUIRES(!immune_gray_stack_lock_,
76 !mark_stack_lock_,
77 !rb_slow_path_histogram_lock_,
78 !skipped_blocks_lock_);
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070079 void InitializePhase() REQUIRES_SHARED(Locks::mutator_lock_)
Hiroshi Yamauchid8db5a22016-06-28 14:07:41 -070080 REQUIRES(!mark_stack_lock_, !immune_gray_stack_lock_);
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070081 void MarkingPhase() REQUIRES_SHARED(Locks::mutator_lock_)
Lokesh Gidra2a9824c2018-11-07 15:57:17 -080082 REQUIRES(!mark_stack_lock_);
83 void CopyingPhase() REQUIRES_SHARED(Locks::mutator_lock_)
Hiroshi Yamauchid8db5a22016-06-28 14:07:41 -070084 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_, !immune_gray_stack_lock_);
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070085 void ReclaimPhase() REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!mark_stack_lock_);
Mathieu Chartier56fe2582016-07-14 13:30:03 -070086 void FinishPhase() REQUIRES(!mark_stack_lock_,
87 !rb_slow_path_histogram_lock_,
88 !skipped_blocks_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080089
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070090 void BindBitmaps() REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -070091 REQUIRES(!Locks::heap_bitmap_lock_);
Roland Levillainf73caca2018-08-24 17:19:07 +010092 GcType GetGcType() const override {
Mathieu Chartier8d1a9962016-08-17 16:39:45 -070093 return (kEnableGenerationalConcurrentCopyingCollection && young_gen_)
94 ? kGcTypeSticky
95 : kGcTypePartial;
Hiroshi Yamauchid5307ec2014-03-27 21:07:51 -070096 }
Roland Levillainf73caca2018-08-24 17:19:07 +010097 CollectorType GetCollectorType() const override {
Hiroshi Yamauchid5307ec2014-03-27 21:07:51 -070098 return kCollectorTypeCC;
99 }
Roland Levillainf73caca2018-08-24 17:19:07 +0100100 void RevokeAllThreadLocalBuffers() override;
Lokesh Gidra1c34b712018-12-18 13:41:58 -0800101 // Creates inter-region ref bitmaps for region-space and non-moving-space.
102 // Gets called in Heap construction after the two spaces are created.
103 void CreateInterRegionRefBitmaps();
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800104 void SetRegionSpace(space::RegionSpace* region_space) {
105 DCHECK(region_space != nullptr);
106 region_space_ = region_space;
107 }
108 space::RegionSpace* RegionSpace() {
109 return region_space_;
110 }
Roland Levillain001eff92018-01-24 14:24:33 +0000111 // Assert the to-space invariant for a heap reference `ref` held in `obj` at offset `offset`.
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800112 void AssertToSpaceInvariant(mirror::Object* obj, MemberOffset offset, mirror::Object* ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700113 REQUIRES_SHARED(Locks::mutator_lock_);
Roland Levillain001eff92018-01-24 14:24:33 +0000114 // Assert the to-space invariant for a GC root reference `ref`.
Hiroshi Yamauchi3f64f252015-06-12 18:35:06 -0700115 void AssertToSpaceInvariant(GcRootSource* gc_root_source, mirror::Object* ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700116 REQUIRES_SHARED(Locks::mutator_lock_);
117 bool IsInToSpace(mirror::Object* ref) REQUIRES_SHARED(Locks::mutator_lock_) {
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800118 DCHECK(ref != nullptr);
119 return IsMarked(ref) == ref;
120 }
Roland Levillain8f7ea9a2018-01-26 17:27:59 +0000121 // Mark object `from_ref`, copying it to the to-space if needed.
Mathieu Chartier8d1a9962016-08-17 16:39:45 -0700122 template<bool kGrayImmuneObject = true, bool kNoUnEvac = false, bool kFromGCThread = false>
Hiroshi Yamauchi7a181542017-03-08 17:34:46 -0800123 ALWAYS_INLINE mirror::Object* Mark(Thread* const self,
124 mirror::Object* from_ref,
Mathieu Chartier1ca68902017-04-18 11:26:22 -0700125 mirror::Object* holder = nullptr,
126 MemberOffset offset = MemberOffset(0))
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700127 REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier56fe2582016-07-14 13:30:03 -0700128 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_, !immune_gray_stack_lock_);
129 ALWAYS_INLINE mirror::Object* MarkFromReadBarrier(mirror::Object* from_ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700130 REQUIRES_SHARED(Locks::mutator_lock_)
Hiroshi Yamauchid8db5a22016-06-28 14:07:41 -0700131 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_, !immune_gray_stack_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800132 bool IsMarking() const {
133 return is_marking_;
134 }
Mathieu Chartier3768ade2017-05-02 14:04:39 -0700135 // We may want to use read barrier entrypoints before is_marking_ is true since concurrent graying
136 // creates a small window where we might dispatch on these entrypoints.
137 bool IsUsingReadBarrierEntrypoints() const {
138 return is_using_read_barrier_entrypoints_;
139 }
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800140 bool IsActive() const {
141 return is_active_;
142 }
143 Barrier& GetBarrier() {
144 return *gc_barrier_;
145 }
Hiroshi Yamauchifebd0cf2016-09-14 19:31:25 -0700146 bool IsWeakRefAccessEnabled() REQUIRES(Locks::thread_list_lock_) {
147 return weak_ref_access_enabled_;
Hiroshi Yamauchi0b713572015-06-16 18:29:23 -0700148 }
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700149 void RevokeThreadLocalMarkStack(Thread* thread) REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -0700150 REQUIRES(!mark_stack_lock_);
Hiroshi Yamauchid5307ec2014-03-27 21:07:51 -0700151
Roland Levillainf73caca2018-08-24 17:19:07 +0100152 mirror::Object* IsMarked(mirror::Object* from_ref) override
Nicolas Geoffray13056a12017-05-11 11:48:28 +0000153 REQUIRES_SHARED(Locks::mutator_lock_);
154
Hiroshi Yamauchid5307ec2014-03-27 21:07:51 -0700155 private:
Hiroshi Yamauchi7a181542017-03-08 17:34:46 -0800156 void PushOntoMarkStack(Thread* const self, mirror::Object* obj)
157 REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -0700158 REQUIRES(!mark_stack_lock_);
Hiroshi Yamauchi7a181542017-03-08 17:34:46 -0800159 mirror::Object* Copy(Thread* const self,
160 mirror::Object* from_ref,
Mathieu Chartieref496d92017-04-28 18:58:59 -0700161 mirror::Object* holder,
162 MemberOffset offset)
163 REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartierd6636d32016-07-28 11:02:38 -0700164 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_, !immune_gray_stack_lock_);
Roland Levillain8f7ea9a2018-01-26 17:27:59 +0000165 // Scan the reference fields of object `to_ref`.
Mathieu Chartier8d1a9962016-08-17 16:39:45 -0700166 template <bool kNoUnEvac>
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700167 void Scan(mirror::Object* to_ref) REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -0700168 REQUIRES(!mark_stack_lock_);
Lokesh Gidra8f5aaad2018-12-11 15:05:56 -0800169 // Scan the reference fields of object 'obj' in the dirty cards during
170 // card-table scan. In addition to visiting the references, it also sets the
171 // read-barrier state to gray for Reference-type objects to ensure that
172 // GetReferent() called on these objects calls the read-barrier on the referent.
173 template <bool kNoUnEvac>
174 void ScanDirtyObject(mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_)
175 REQUIRES(!mark_stack_lock_);
Roland Levillain8f7ea9a2018-01-26 17:27:59 +0000176 // Process a field.
Mathieu Chartier8d1a9962016-08-17 16:39:45 -0700177 template <bool kNoUnEvac>
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800178 void Process(mirror::Object* obj, MemberOffset offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700179 REQUIRES_SHARED(Locks::mutator_lock_)
Hiroshi Yamauchid8db5a22016-06-28 14:07:41 -0700180 REQUIRES(!mark_stack_lock_ , !skipped_blocks_lock_, !immune_gray_stack_lock_);
Roland Levillainf73caca2018-08-24 17:19:07 +0100181 void VisitRoots(mirror::Object*** roots, size_t count, const RootInfo& info) override
182 REQUIRES_SHARED(Locks::mutator_lock_)
Hiroshi Yamauchid8db5a22016-06-28 14:07:41 -0700183 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_, !immune_gray_stack_lock_);
184 template<bool kGrayImmuneObject>
Hiroshi Yamauchi7a181542017-03-08 17:34:46 -0800185 void MarkRoot(Thread* const self, mirror::CompressedReference<mirror::Object>* root)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700186 REQUIRES_SHARED(Locks::mutator_lock_)
Hiroshi Yamauchid8db5a22016-06-28 14:07:41 -0700187 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_, !immune_gray_stack_lock_);
Roland Levillainf73caca2018-08-24 17:19:07 +0100188 void VisitRoots(mirror::CompressedReference<mirror::Object>** roots,
189 size_t count,
190 const RootInfo& info) override
191 REQUIRES_SHARED(Locks::mutator_lock_)
Hiroshi Yamauchid8db5a22016-06-28 14:07:41 -0700192 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_, !immune_gray_stack_lock_);
Mathieu Chartier90443472015-07-16 20:32:27 -0700193 void VerifyNoFromSpaceReferences() REQUIRES(Locks::mutator_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800194 accounting::ObjectStack* GetAllocationStack();
195 accounting::ObjectStack* GetLiveStack();
Roland Levillainf73caca2018-08-24 17:19:07 +0100196 void ProcessMarkStack() override REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -0700197 REQUIRES(!mark_stack_lock_);
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700198 bool ProcessMarkStackOnce() REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!mark_stack_lock_);
199 void ProcessMarkStackRef(mirror::Object* to_ref) REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -0700200 REQUIRES(!mark_stack_lock_);
Mathieu Chartier21328a12016-07-22 10:47:45 -0700201 void GrayAllDirtyImmuneObjects()
202 REQUIRES(Locks::mutator_lock_)
203 REQUIRES(!mark_stack_lock_);
Mathieu Chartier3768ade2017-05-02 14:04:39 -0700204 void GrayAllNewlyDirtyImmuneObjects()
205 REQUIRES(Locks::mutator_lock_)
206 REQUIRES(!mark_stack_lock_);
Mathieu Chartier21328a12016-07-22 10:47:45 -0700207 void VerifyGrayImmuneObjects()
208 REQUIRES(Locks::mutator_lock_)
209 REQUIRES(!mark_stack_lock_);
Mathieu Chartiera1467d02017-02-22 09:22:50 -0800210 void VerifyNoMissingCardMarks()
211 REQUIRES(Locks::mutator_lock_)
212 REQUIRES(!mark_stack_lock_);
Lokesh Gidra2a9824c2018-11-07 15:57:17 -0800213 template <typename Processor>
214 size_t ProcessThreadLocalMarkStacks(bool disable_weak_ref_access,
215 Closure* checkpoint_callback,
216 const Processor& processor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700217 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!mark_stack_lock_);
Hiroshi Yamauchifebd0cf2016-09-14 19:31:25 -0700218 void RevokeThreadLocalMarkStacks(bool disable_weak_ref_access, Closure* checkpoint_callback)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700219 REQUIRES_SHARED(Locks::mutator_lock_);
220 void SwitchToSharedMarkStackMode() REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -0700221 REQUIRES(!mark_stack_lock_);
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700222 void SwitchToGcExclusiveMarkStackMode() REQUIRES_SHARED(Locks::mutator_lock_);
Roland Levillainf73caca2018-08-24 17:19:07 +0100223 void DelayReferenceReferent(ObjPtr<mirror::Class> klass,
224 ObjPtr<mirror::Reference> reference) override
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700225 REQUIRES_SHARED(Locks::mutator_lock_);
226 void ProcessReferences(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_);
Roland Levillainf73caca2018-08-24 17:19:07 +0100227 mirror::Object* MarkObject(mirror::Object* from_ref) override
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700228 REQUIRES_SHARED(Locks::mutator_lock_)
Hiroshi Yamauchid8db5a22016-06-28 14:07:41 -0700229 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_, !immune_gray_stack_lock_);
Roland Levillainf73caca2018-08-24 17:19:07 +0100230 void MarkHeapReference(mirror::HeapReference<mirror::Object>* from_ref,
231 bool do_atomic_update) override
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700232 REQUIRES_SHARED(Locks::mutator_lock_)
Hiroshi Yamauchid8db5a22016-06-28 14:07:41 -0700233 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_, !immune_gray_stack_lock_);
Mathieu Chartierc381c362016-08-23 13:27:53 -0700234 bool IsMarkedInUnevacFromSpace(mirror::Object* from_ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700235 REQUIRES_SHARED(Locks::mutator_lock_);
Lokesh Gidra519c1c72018-11-09 17:10:47 -0800236 bool IsMarkedInNonMovingSpace(mirror::Object* from_ref)
237 REQUIRES_SHARED(Locks::mutator_lock_);
Roland Levillainf73caca2018-08-24 17:19:07 +0100238 bool IsNullOrMarkedHeapReference(mirror::HeapReference<mirror::Object>* field,
239 bool do_atomic_update) override
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700240 REQUIRES_SHARED(Locks::mutator_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800241 void SweepSystemWeaks(Thread* self)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700242 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Locks::heap_bitmap_lock_);
Roland Levillainb1e1dc32018-07-10 19:19:31 +0100243 // Sweep unmarked objects to complete the garbage collection. Full GCs sweep
244 // all allocation spaces (except the region space). Sticky-bit GCs just sweep
245 // a subset of the heap.
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800246 void Sweep(bool swap_bitmaps)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700247 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::heap_bitmap_lock_, !mark_stack_lock_);
Roland Levillainb1e1dc32018-07-10 19:19:31 +0100248 // Sweep only pointers within an array.
249 void SweepArray(accounting::ObjectStack* allocation_stack_, bool swap_bitmaps)
250 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::heap_bitmap_lock_, !mark_stack_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800251 void SweepLargeObjects(bool swap_bitmaps)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700252 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::heap_bitmap_lock_);
Mathieu Chartier962cd7a2016-08-16 12:15:59 -0700253 void MarkZygoteLargeObjects()
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700254 REQUIRES_SHARED(Locks::mutator_lock_);
Hiroshi Yamauchi7a181542017-03-08 17:34:46 -0800255 void FillWithDummyObject(Thread* const self, mirror::Object* dummy_obj, size_t byte_size)
Mathieu Chartierd6636d32016-07-28 11:02:38 -0700256 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_, !immune_gray_stack_lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700257 REQUIRES_SHARED(Locks::mutator_lock_);
Hiroshi Yamauchi7a181542017-03-08 17:34:46 -0800258 mirror::Object* AllocateInSkippedBlock(Thread* const self, size_t alloc_size)
Mathieu Chartierd6636d32016-07-28 11:02:38 -0700259 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_, !immune_gray_stack_lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700260 REQUIRES_SHARED(Locks::mutator_lock_);
261 void CheckEmptyMarkStack() REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!mark_stack_lock_);
262 void IssueEmptyCheckpoint() REQUIRES_SHARED(Locks::mutator_lock_);
263 bool IsOnAllocStack(mirror::Object* ref) REQUIRES_SHARED(Locks::mutator_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800264 mirror::Object* GetFwdPtr(mirror::Object* from_ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700265 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier90443472015-07-16 20:32:27 -0700266 void FlipThreadRoots() REQUIRES(!Locks::mutator_lock_);
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700267 void SwapStacks() REQUIRES_SHARED(Locks::mutator_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800268 void RecordLiveStackFreezeSize(Thread* self);
269 void ComputeUnevacFromSpaceLiveRatio();
Hiroshi Yamauchi3f64f252015-06-12 18:35:06 -0700270 void LogFromSpaceRefHolder(mirror::Object* obj, MemberOffset offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700271 REQUIRES_SHARED(Locks::mutator_lock_);
Roland Levillain001eff92018-01-24 14:24:33 +0000272 // Dump information about reference `ref` and return it as a string.
273 // Use `ref_name` to name the reference in messages. Each message is prefixed with `indent`.
Andreas Gampebc802de2018-06-20 17:24:11 -0700274 std::string DumpReferenceInfo(mirror::Object* ref, const char* ref_name, const char* indent = "")
Roland Levillain001eff92018-01-24 14:24:33 +0000275 REQUIRES_SHARED(Locks::mutator_lock_);
276 // Dump information about heap reference `ref`, referenced from object `obj` at offset `offset`,
277 // and return it as a string.
278 std::string DumpHeapReference(mirror::Object* obj, MemberOffset offset, mirror::Object* ref)
279 REQUIRES_SHARED(Locks::mutator_lock_);
280 // Dump information about GC root `ref` and return it as a string.
281 std::string DumpGcRoot(mirror::Object* ref) REQUIRES_SHARED(Locks::mutator_lock_);
Hiroshi Yamauchi3f64f252015-06-12 18:35:06 -0700282 void AssertToSpaceInvariantInNonMovingSpace(mirror::Object* obj, mirror::Object* ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700283 REQUIRES_SHARED(Locks::mutator_lock_);
284 void ReenableWeakRefAccess(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_);
285 void DisableMarking() REQUIRES_SHARED(Locks::mutator_lock_);
286 void IssueDisableMarkingCheckpoint() REQUIRES_SHARED(Locks::mutator_lock_);
287 void ExpandGcMarkStack() REQUIRES_SHARED(Locks::mutator_lock_);
Hiroshi Yamauchi7a181542017-03-08 17:34:46 -0800288 mirror::Object* MarkNonMoving(Thread* const self,
289 mirror::Object* from_ref,
Mathieu Chartier1ca68902017-04-18 11:26:22 -0700290 mirror::Object* holder = nullptr,
291 MemberOffset offset = MemberOffset(0))
292 REQUIRES_SHARED(Locks::mutator_lock_)
Hiroshi Yamauchi723e6ce2015-10-28 20:59:47 -0700293 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_);
Hiroshi Yamauchi7a181542017-03-08 17:34:46 -0800294 ALWAYS_INLINE mirror::Object* MarkUnevacFromSpaceRegion(Thread* const self,
295 mirror::Object* from_ref,
Hiroshi Yamauchi8e674652015-12-22 11:09:18 -0800296 accounting::SpaceBitmap<kObjectAlignment>* bitmap)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700297 REQUIRES_SHARED(Locks::mutator_lock_)
Hiroshi Yamauchi8e674652015-12-22 11:09:18 -0800298 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_);
Hiroshi Yamauchid8db5a22016-06-28 14:07:41 -0700299 template<bool kGrayImmuneObject>
Hiroshi Yamauchi7a181542017-03-08 17:34:46 -0800300 ALWAYS_INLINE mirror::Object* MarkImmuneSpace(Thread* const self,
301 mirror::Object* from_ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700302 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!immune_gray_stack_lock_);
Hiroshi Yamauchid8db5a22016-06-28 14:07:41 -0700303 void ScanImmuneObject(mirror::Object* obj)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700304 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!mark_stack_lock_);
Hiroshi Yamauchi7a181542017-03-08 17:34:46 -0800305 mirror::Object* MarkFromReadBarrierWithMeasurements(Thread* const self,
306 mirror::Object* from_ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700307 REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier56fe2582016-07-14 13:30:03 -0700308 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_, !immune_gray_stack_lock_);
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100309 void DumpPerformanceInfo(std::ostream& os) override REQUIRES(!rb_slow_path_histogram_lock_);
Mathieu Chartier3768ade2017-05-02 14:04:39 -0700310 // Set the read barrier mark entrypoints to non-null.
311 void ActivateReadBarrierEntrypoints();
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800312
Lokesh Gidra2a9824c2018-11-07 15:57:17 -0800313 void CaptureThreadRootsForMarking() REQUIRES_SHARED(Locks::mutator_lock_);
314 void AddLiveBytesAndScanRef(mirror::Object* ref) REQUIRES_SHARED(Locks::mutator_lock_);
315 bool TestMarkBitmapForRef(mirror::Object* ref) REQUIRES_SHARED(Locks::mutator_lock_);
316 template <bool kAtomic = false>
317 bool TestAndSetMarkBitForRef(mirror::Object* ref) REQUIRES_SHARED(Locks::mutator_lock_);
318 void PushOntoLocalMarkStack(mirror::Object* ref) REQUIRES_SHARED(Locks::mutator_lock_);
319 void ProcessMarkStackForMarkingAndComputeLiveBytes() REQUIRES_SHARED(Locks::mutator_lock_)
320 REQUIRES(!mark_stack_lock_);
321
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800322 space::RegionSpace* region_space_; // The underlying region space.
323 std::unique_ptr<Barrier> gc_barrier_;
Hiroshi Yamauchi0b713572015-06-16 18:29:23 -0700324 std::unique_ptr<accounting::ObjectStack> gc_mark_stack_;
Roland Levillain8f7ea9a2018-01-26 17:27:59 +0000325
326 // The read-barrier mark-bit stack. Stores object references whose
327 // mark bit has been set by ConcurrentCopying::MarkFromReadBarrier,
328 // so that this bit can be reset at the end of the collection in
329 // ConcurrentCopying::FinishPhase. The mark bit of an object can be
330 // used by mutator read barrier code to quickly test whether that
331 // object has been already marked.
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700332 std::unique_ptr<accounting::ObjectStack> rb_mark_bit_stack_;
Roland Levillain8f7ea9a2018-01-26 17:27:59 +0000333 // Thread-unsafe Boolean value hinting that `rb_mark_bit_stack_` is
334 // full. A thread-safe test of whether the read-barrier mark-bit
335 // stack is full is implemented by `rb_mark_bit_stack_->AtomicPushBack(ref)`
336 // (see use case in ConcurrentCopying::MarkFromReadBarrier).
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700337 bool rb_mark_bit_stack_full_;
Roland Levillain8f7ea9a2018-01-26 17:27:59 +0000338
Hiroshi Yamauchi0b713572015-06-16 18:29:23 -0700339 Mutex mark_stack_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
340 std::vector<accounting::ObjectStack*> revoked_mark_stacks_
341 GUARDED_BY(mark_stack_lock_);
342 static constexpr size_t kMarkStackSize = kPageSize;
343 static constexpr size_t kMarkStackPoolSize = 256;
344 std::vector<accounting::ObjectStack*> pooled_mark_stacks_
345 GUARDED_BY(mark_stack_lock_);
346 Thread* thread_running_gc_;
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800347 bool is_marking_; // True while marking is ongoing.
Mathieu Chartier3768ade2017-05-02 14:04:39 -0700348 // True while we might dispatch on the read barrier entrypoints.
349 bool is_using_read_barrier_entrypoints_;
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800350 bool is_active_; // True while the collection is ongoing.
351 bool is_asserting_to_space_invariant_; // True while asserting the to-space invariant.
Mathieu Chartier763a31e2015-11-16 16:05:55 -0800352 ImmuneSpaces immune_spaces_;
Andreas Gamped4901292017-05-30 18:41:34 -0700353 accounting::ContinuousSpaceBitmap* region_space_bitmap_;
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800354 // A cache of Heap::GetMarkBitmap().
355 accounting::HeapBitmap* heap_mark_bitmap_;
356 size_t live_stack_freeze_size_;
357 size_t from_space_num_objects_at_first_pause_;
358 size_t from_space_num_bytes_at_first_pause_;
Hiroshi Yamauchi0b713572015-06-16 18:29:23 -0700359 Atomic<int> is_mark_stack_push_disallowed_;
360 enum MarkStackMode {
361 kMarkStackModeOff = 0, // Mark stack is off.
362 kMarkStackModeThreadLocal, // All threads except for the GC-running thread push refs onto
363 // thread-local mark stacks. The GC-running thread pushes onto and
364 // pops off the GC mark stack without a lock.
365 kMarkStackModeShared, // All threads share the GC mark stack with a lock.
366 kMarkStackModeGcExclusive // The GC-running thread pushes onto and pops from the GC mark stack
367 // without a lock. Other threads won't access the mark stack.
368 };
369 Atomic<MarkStackMode> mark_stack_mode_;
Hiroshi Yamauchifebd0cf2016-09-14 19:31:25 -0700370 bool weak_ref_access_enabled_ GUARDED_BY(Locks::thread_list_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800371
372 // How many objects and bytes we moved. Used for accounting.
Hiroshi Yamauchi7a181542017-03-08 17:34:46 -0800373 // GC thread moves many more objects than mutators.
374 // Therefore, we separate the two to avoid CAS.
375 Atomic<size_t> bytes_moved_; // Used by mutators
376 Atomic<size_t> objects_moved_; // Used by mutators
377 size_t bytes_moved_gc_thread_; // Used by GC
378 size_t objects_moved_gc_thread_; // Used by GC
Mathieu Chartiercca44a02016-08-17 10:07:29 -0700379 Atomic<uint64_t> cumulative_bytes_moved_;
380 Atomic<uint64_t> cumulative_objects_moved_;
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800381
Albert Mingkun Yangaf9cce12018-11-07 09:58:35 +0000382 // copied_live_bytes_ratio_sum_ is read and written by CC per GC, in
383 // ReclaimPhase, and is read by DumpPerformanceInfo (potentially from another
384 // thread). However, at present, DumpPerformanceInfo is only called when the
385 // runtime shuts down, so no concurrent access. The same reasoning goes for
386 // gc_count_ and reclaimed_bytes_ratio_sum_
387
Albert Mingkun Yange260e542018-11-05 13:45:59 +0000388 // The sum of of all copied live bytes ratio (to_bytes/from_bytes)
389 float copied_live_bytes_ratio_sum_;
390 // The number of GC counts, used to calculate the average above. (It doesn't
391 // include GC where from_bytes is zero, IOW, from-space is empty, which is
392 // possible for minor GC if all allocated objects are in non-moving
393 // space.)
394 size_t gc_count_;
Lokesh Gidrad0c5b252018-12-05 01:10:40 -0800395 // Bit is set if the corresponding object has inter-region references that
396 // were found during the marking phase of two-phase full-heap GC cycle.
Lokesh Gidra32650ab2018-12-18 16:19:05 -0800397 std::unique_ptr<accounting::ContinuousSpaceBitmap> region_space_inter_region_bitmap_;
398 std::unique_ptr<accounting::ContinuousSpaceBitmap> non_moving_space_inter_region_bitmap_;
Albert Mingkun Yange260e542018-11-05 13:45:59 +0000399
Albert Mingkun Yangaf9cce12018-11-07 09:58:35 +0000400 // reclaimed_bytes_ratio = reclaimed_bytes/num_allocated_bytes per GC cycle
401 float reclaimed_bytes_ratio_sum_;
402
Mathieu Chartier8d1a9962016-08-17 16:39:45 -0700403 // Generational "sticky", only trace through dirty objects in region space.
404 const bool young_gen_;
Roland Levillain2d94e292018-08-15 16:46:30 +0100405 // If true, the GC thread is done scanning marked objects on dirty and aged
Lokesh Gidra2a9824c2018-11-07 15:57:17 -0800406 // card (see ConcurrentCopying::CopyingPhase).
Mathieu Chartier8d1a9962016-08-17 16:39:45 -0700407 Atomic<bool> done_scanning_;
408
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800409 // The skipped blocks are memory blocks/chucks that were copies of
410 // objects that were unused due to lost races (cas failures) at
411 // object copy/forward pointer install. They are reused.
412 Mutex skipped_blocks_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
413 std::multimap<size_t, uint8_t*> skipped_blocks_map_ GUARDED_BY(skipped_blocks_lock_);
414 Atomic<size_t> to_space_bytes_skipped_;
415 Atomic<size_t> to_space_objects_skipped_;
416
Mathieu Chartier56fe2582016-07-14 13:30:03 -0700417 // If measure_read_barrier_slow_path_ is true, we count how long is spent in MarkFromReadBarrier
418 // and also log.
419 bool measure_read_barrier_slow_path_;
420 // mark_from_read_barrier_measurements_ is true if systrace is enabled or
421 // measure_read_barrier_time_ is true.
422 bool mark_from_read_barrier_measurements_;
423 Atomic<uint64_t> rb_slow_path_ns_;
424 Atomic<uint64_t> rb_slow_path_count_;
425 Atomic<uint64_t> rb_slow_path_count_gc_;
426 mutable Mutex rb_slow_path_histogram_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
427 Histogram<uint64_t> rb_slow_path_time_histogram_ GUARDED_BY(rb_slow_path_histogram_lock_);
428 uint64_t rb_slow_path_count_total_ GUARDED_BY(rb_slow_path_histogram_lock_);
429 uint64_t rb_slow_path_count_gc_total_ GUARDED_BY(rb_slow_path_histogram_lock_);
430
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800431 accounting::ReadBarrierTable* rb_table_;
432 bool force_evacuate_all_; // True if all regions are evacuated.
Hiroshi Yamauchid8db5a22016-06-28 14:07:41 -0700433 Atomic<bool> updated_all_immune_objects_;
434 bool gc_grays_immune_objects_;
435 Mutex immune_gray_stack_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
436 std::vector<mirror::Object*> immune_gray_stack_ GUARDED_BY(immune_gray_stack_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800437
Mathieu Chartier3ed8ec12017-04-20 19:28:54 -0700438 // Class of java.lang.Object. Filled in from WellKnownClasses in FlipCallback. Must
439 // be filled in before flipping thread roots so that FillDummyObject can run. Not
440 // ObjPtr since the GC may transition to suspended and runnable between phases.
441 mirror::Class* java_lang_Object_;
442
Roland Levillainb1e1dc32018-07-10 19:19:31 +0100443 // Sweep array free buffer, used to sweep the spaces based on an array more
444 // efficiently, by recording dead objects to be freed in batches (see
445 // ConcurrentCopying::SweepArray).
446 MemMap sweep_array_free_buffer_mem_map_;
447
Albert Mingkun Yangaf9cce12018-11-07 09:58:35 +0000448 // Use signed because after_gc may be larger than before_gc.
449 int64_t num_bytes_allocated_before_gc_;
450
Mathieu Chartier3768ade2017-05-02 14:04:39 -0700451 class ActivateReadBarrierEntrypointsCallback;
452 class ActivateReadBarrierEntrypointsCheckpoint;
Mathieu Chartiera07f5592016-06-16 11:44:28 -0700453 class AssertToSpaceInvariantFieldVisitor;
Mathieu Chartiera07f5592016-06-16 11:44:28 -0700454 class AssertToSpaceInvariantRefsVisitor;
455 class ClearBlackPtrsVisitor;
456 class ComputeUnevacFromSpaceLiveRatioVisitor;
Hiroshi Yamauchifebd0cf2016-09-14 19:31:25 -0700457 class DisableMarkingCallback;
Mathieu Chartiera07f5592016-06-16 11:44:28 -0700458 class DisableMarkingCheckpoint;
Hiroshi Yamauchifebd0cf2016-09-14 19:31:25 -0700459 class DisableWeakRefAccessCallback;
Mathieu Chartiera07f5592016-06-16 11:44:28 -0700460 class FlipCallback;
Mathieu Chartier3768ade2017-05-02 14:04:39 -0700461 template <bool kConcurrent> class GrayImmuneObjectVisitor;
Hiroshi Yamauchid8db5a22016-06-28 14:07:41 -0700462 class ImmuneSpaceScanObjVisitor;
Mathieu Chartiera07f5592016-06-16 11:44:28 -0700463 class LostCopyVisitor;
Mathieu Chartier8d1a9962016-08-17 16:39:45 -0700464 template <bool kNoUnEvac> class RefFieldsVisitor;
Mathieu Chartiera07f5592016-06-16 11:44:28 -0700465 class RevokeThreadLocalMarkStackCheckpoint;
Hiroshi Yamauchid8db5a22016-06-28 14:07:41 -0700466 class ScopedGcGraysImmuneObjects;
467 class ThreadFlipVisitor;
Mathieu Chartier21328a12016-07-22 10:47:45 -0700468 class VerifyGrayImmuneObjectsVisitor;
Mathieu Chartiera07f5592016-06-16 11:44:28 -0700469 class VerifyNoFromSpaceRefsFieldVisitor;
Mathieu Chartiera07f5592016-06-16 11:44:28 -0700470 class VerifyNoFromSpaceRefsVisitor;
Mathieu Chartiera1467d02017-02-22 09:22:50 -0800471 class VerifyNoMissingCardMarkVisitor;
Lokesh Gidra2a9824c2018-11-07 15:57:17 -0800472 class ImmuneSpaceCaptureRefsVisitor;
473 template <bool kAtomicTestAndSet = false> class CaptureRootsForMarkingVisitor;
474 class CaptureThreadRootsForMarkingAndCheckpoint;
475 template <bool kHandleInterRegionRefs> class ComputeLiveBytesAndMarkRefFieldsVisitor;
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800476
Mathieu Chartier3130cdf2015-05-03 15:20:23 -0700477 DISALLOW_IMPLICIT_CONSTRUCTORS(ConcurrentCopying);
Hiroshi Yamauchid5307ec2014-03-27 21:07:51 -0700478};
479
480} // namespace collector
481} // namespace gc
482} // namespace art
483
484#endif // ART_RUNTIME_GC_COLLECTOR_CONCURRENT_COPYING_H_