blob: 0cb5a3e8d7bc99c75002e88265bdfc77e13e21cd [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 Chartier6f382012019-07-30 09:47:35 -070021#include "gc/accounting/space_bitmap.h"
Mathieu Chartier763a31e2015-11-16 16:05:55 -080022#include "immune_spaces.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070023#include "offsets.h"
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080024
Andreas Gampe8f1c8e52019-01-08 10:34:16 -080025#include <map>
Lokesh Gidrad0c5b252018-12-05 01:10:40 -080026#include <memory>
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080027#include <vector>
Hiroshi Yamauchid5307ec2014-03-27 21:07:51 -070028
29namespace art {
Andreas Gampe8f1c8e52019-01-08 10:34:16 -080030class Barrier;
Hiroshi Yamauchifebd0cf2016-09-14 19:31:25 -070031class Closure;
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080032class RootInfo;
33
Andreas Gampe5d08fcc2017-06-05 17:56:46 -070034namespace mirror {
Andreas Gampe8f1c8e52019-01-08 10:34:16 -080035template<class MirrorType> class CompressedReference;
36template<class MirrorType> class HeapReference;
Andreas Gampe5d08fcc2017-06-05 17:56:46 -070037class Object;
38} // namespace mirror
39
Hiroshi Yamauchid5307ec2014-03-27 21:07:51 -070040namespace gc {
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080041
42namespace accounting {
Igor Murashkin2ffb7032017-11-08 13:35:21 -080043template<typename T> class AtomicStack;
44typedef AtomicStack<mirror::Object> ObjectStack;
45template <size_t kAlignment> class SpaceBitmap;
46typedef SpaceBitmap<kObjectAlignment> ContinuousSpaceBitmap;
47class HeapBitmap;
48class ReadBarrierTable;
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080049} // namespace accounting
50
51namespace space {
Igor Murashkin2ffb7032017-11-08 13:35:21 -080052class RegionSpace;
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080053} // namespace space
54
Hiroshi Yamauchid5307ec2014-03-27 21:07:51 -070055namespace collector {
56
57class ConcurrentCopying : public GarbageCollector {
58 public:
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080059 // Enable the no-from-space-refs verification at the pause.
Hiroshi Yamauchidaf61a12016-06-10 14:27:38 -070060 static constexpr bool kEnableNoFromSpaceRefsVerification = kIsDebugBuild;
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080061 // Enable the from-space bytes/objects check.
Hiroshi Yamauchidaf61a12016-06-10 14:27:38 -070062 static constexpr bool kEnableFromSpaceAccountingCheck = kIsDebugBuild;
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080063 // Enable verbose mode.
Hiroshi Yamauchi3c448932016-01-22 16:26:50 -080064 static constexpr bool kVerboseMode = false;
Mathieu Chartier36a270a2016-07-28 18:08:51 -070065 // If kGrayDirtyImmuneObjects is true then we gray dirty objects in the GC pause to prevent dirty
66 // pages.
67 static constexpr bool kGrayDirtyImmuneObjects = true;
Hiroshi Yamauchid5307ec2014-03-27 21:07:51 -070068
Albert Mingkun Yang0b4d1462018-11-29 13:25:35 +000069 ConcurrentCopying(Heap* heap,
70 bool young_gen,
71 bool use_generational_cc,
72 const std::string& name_prefix = "",
73 bool measure_read_barrier_slow_path = false);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080074 ~ConcurrentCopying();
Hiroshi Yamauchid5307ec2014-03-27 21:07:51 -070075
Roland Levillainf73caca2018-08-24 17:19:07 +010076 void RunPhases() override
Mathieu Chartier56fe2582016-07-14 13:30:03 -070077 REQUIRES(!immune_gray_stack_lock_,
78 !mark_stack_lock_,
Hans Boehme7d7e9d2019-06-03 18:36:06 +000079 !rb_slow_path_histogram_lock_,
80 !skipped_blocks_lock_);
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070081 void InitializePhase() REQUIRES_SHARED(Locks::mutator_lock_)
Hiroshi Yamauchid8db5a22016-06-28 14:07:41 -070082 REQUIRES(!mark_stack_lock_, !immune_gray_stack_lock_);
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070083 void MarkingPhase() REQUIRES_SHARED(Locks::mutator_lock_)
Lokesh Gidra2a9824c2018-11-07 15:57:17 -080084 REQUIRES(!mark_stack_lock_);
85 void CopyingPhase() REQUIRES_SHARED(Locks::mutator_lock_)
Hans Boehme7d7e9d2019-06-03 18:36:06 +000086 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_, !immune_gray_stack_lock_);
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070087 void ReclaimPhase() REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!mark_stack_lock_);
Mathieu Chartier56fe2582016-07-14 13:30:03 -070088 void FinishPhase() REQUIRES(!mark_stack_lock_,
Hans Boehme7d7e9d2019-06-03 18:36:06 +000089 !rb_slow_path_histogram_lock_,
90 !skipped_blocks_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080091
Lokesh Gidra10d0c962019-03-07 22:40:36 +000092 void CaptureRssAtPeak() REQUIRES(!mark_stack_lock_);
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070093 void BindBitmaps() REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -070094 REQUIRES(!Locks::heap_bitmap_lock_);
Roland Levillainf73caca2018-08-24 17:19:07 +010095 GcType GetGcType() const override {
Albert Mingkun Yang0b4d1462018-11-29 13:25:35 +000096 return (use_generational_cc_ && young_gen_)
Mathieu Chartier8d1a9962016-08-17 16:39:45 -070097 ? kGcTypeSticky
98 : kGcTypePartial;
Hiroshi Yamauchid5307ec2014-03-27 21:07:51 -070099 }
Roland Levillainf73caca2018-08-24 17:19:07 +0100100 CollectorType GetCollectorType() const override {
Hiroshi Yamauchid5307ec2014-03-27 21:07:51 -0700101 return kCollectorTypeCC;
102 }
Roland Levillainf73caca2018-08-24 17:19:07 +0100103 void RevokeAllThreadLocalBuffers() override;
Lokesh Gidra1c34b712018-12-18 13:41:58 -0800104 // Creates inter-region ref bitmaps for region-space and non-moving-space.
105 // Gets called in Heap construction after the two spaces are created.
106 void CreateInterRegionRefBitmaps();
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800107 void SetRegionSpace(space::RegionSpace* region_space) {
108 DCHECK(region_space != nullptr);
109 region_space_ = region_space;
110 }
111 space::RegionSpace* RegionSpace() {
112 return region_space_;
113 }
Roland Levillain001eff92018-01-24 14:24:33 +0000114 // Assert the to-space invariant for a heap reference `ref` held in `obj` at offset `offset`.
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800115 void AssertToSpaceInvariant(mirror::Object* obj, MemberOffset offset, mirror::Object* ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700116 REQUIRES_SHARED(Locks::mutator_lock_);
Roland Levillain001eff92018-01-24 14:24:33 +0000117 // Assert the to-space invariant for a GC root reference `ref`.
Hiroshi Yamauchi3f64f252015-06-12 18:35:06 -0700118 void AssertToSpaceInvariant(GcRootSource* gc_root_source, mirror::Object* ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700119 REQUIRES_SHARED(Locks::mutator_lock_);
120 bool IsInToSpace(mirror::Object* ref) REQUIRES_SHARED(Locks::mutator_lock_) {
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800121 DCHECK(ref != nullptr);
122 return IsMarked(ref) == ref;
123 }
Roland Levillain8f7ea9a2018-01-26 17:27:59 +0000124 // Mark object `from_ref`, copying it to the to-space if needed.
Mathieu Chartier8d1a9962016-08-17 16:39:45 -0700125 template<bool kGrayImmuneObject = true, bool kNoUnEvac = false, bool kFromGCThread = false>
Hiroshi Yamauchi7a181542017-03-08 17:34:46 -0800126 ALWAYS_INLINE mirror::Object* Mark(Thread* const self,
127 mirror::Object* from_ref,
Mathieu Chartier1ca68902017-04-18 11:26:22 -0700128 mirror::Object* holder = nullptr,
129 MemberOffset offset = MemberOffset(0))
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700130 REQUIRES_SHARED(Locks::mutator_lock_)
Hans Boehme7d7e9d2019-06-03 18:36:06 +0000131 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_, !immune_gray_stack_lock_);
Mathieu Chartier56fe2582016-07-14 13:30:03 -0700132 ALWAYS_INLINE mirror::Object* MarkFromReadBarrier(mirror::Object* from_ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700133 REQUIRES_SHARED(Locks::mutator_lock_)
Hans Boehme7d7e9d2019-06-03 18:36:06 +0000134 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_, !immune_gray_stack_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800135 bool IsMarking() const {
136 return is_marking_;
137 }
Mathieu Chartier3768ade2017-05-02 14:04:39 -0700138 // We may want to use read barrier entrypoints before is_marking_ is true since concurrent graying
139 // creates a small window where we might dispatch on these entrypoints.
140 bool IsUsingReadBarrierEntrypoints() const {
141 return is_using_read_barrier_entrypoints_;
142 }
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800143 bool IsActive() const {
144 return is_active_;
145 }
146 Barrier& GetBarrier() {
147 return *gc_barrier_;
148 }
Hiroshi Yamauchifebd0cf2016-09-14 19:31:25 -0700149 bool IsWeakRefAccessEnabled() REQUIRES(Locks::thread_list_lock_) {
150 return weak_ref_access_enabled_;
Hiroshi Yamauchi0b713572015-06-16 18:29:23 -0700151 }
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700152 void RevokeThreadLocalMarkStack(Thread* thread) REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -0700153 REQUIRES(!mark_stack_lock_);
Hiroshi Yamauchid5307ec2014-03-27 21:07:51 -0700154
Roland Levillainf73caca2018-08-24 17:19:07 +0100155 mirror::Object* IsMarked(mirror::Object* from_ref) override
Nicolas Geoffray13056a12017-05-11 11:48:28 +0000156 REQUIRES_SHARED(Locks::mutator_lock_);
157
Hiroshi Yamauchid5307ec2014-03-27 21:07:51 -0700158 private:
Hiroshi Yamauchi7a181542017-03-08 17:34:46 -0800159 void PushOntoMarkStack(Thread* const self, mirror::Object* obj)
160 REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -0700161 REQUIRES(!mark_stack_lock_);
Hiroshi Yamauchi7a181542017-03-08 17:34:46 -0800162 mirror::Object* Copy(Thread* const self,
163 mirror::Object* from_ref,
Mathieu Chartieref496d92017-04-28 18:58:59 -0700164 mirror::Object* holder,
165 MemberOffset offset)
166 REQUIRES_SHARED(Locks::mutator_lock_)
Hans Boehme7d7e9d2019-06-03 18:36:06 +0000167 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_, !immune_gray_stack_lock_);
Roland Levillain8f7ea9a2018-01-26 17:27:59 +0000168 // Scan the reference fields of object `to_ref`.
Mathieu Chartier8d1a9962016-08-17 16:39:45 -0700169 template <bool kNoUnEvac>
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700170 void Scan(mirror::Object* to_ref) REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -0700171 REQUIRES(!mark_stack_lock_);
Lokesh Gidra8f5aaad2018-12-11 15:05:56 -0800172 // Scan the reference fields of object 'obj' in the dirty cards during
173 // card-table scan. In addition to visiting the references, it also sets the
174 // read-barrier state to gray for Reference-type objects to ensure that
175 // GetReferent() called on these objects calls the read-barrier on the referent.
176 template <bool kNoUnEvac>
177 void ScanDirtyObject(mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_)
178 REQUIRES(!mark_stack_lock_);
Roland Levillain8f7ea9a2018-01-26 17:27:59 +0000179 // Process a field.
Mathieu Chartier8d1a9962016-08-17 16:39:45 -0700180 template <bool kNoUnEvac>
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800181 void Process(mirror::Object* obj, MemberOffset offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700182 REQUIRES_SHARED(Locks::mutator_lock_)
Hans Boehme7d7e9d2019-06-03 18:36:06 +0000183 REQUIRES(!mark_stack_lock_ , !skipped_blocks_lock_, !immune_gray_stack_lock_);
Roland Levillainf73caca2018-08-24 17:19:07 +0100184 void VisitRoots(mirror::Object*** roots, size_t count, const RootInfo& info) override
185 REQUIRES_SHARED(Locks::mutator_lock_)
Hans Boehme7d7e9d2019-06-03 18:36:06 +0000186 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_, !immune_gray_stack_lock_);
Hiroshi Yamauchid8db5a22016-06-28 14:07:41 -0700187 template<bool kGrayImmuneObject>
Hiroshi Yamauchi7a181542017-03-08 17:34:46 -0800188 void MarkRoot(Thread* const self, mirror::CompressedReference<mirror::Object>* root)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700189 REQUIRES_SHARED(Locks::mutator_lock_)
Hans Boehme7d7e9d2019-06-03 18:36:06 +0000190 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_, !immune_gray_stack_lock_);
Roland Levillainf73caca2018-08-24 17:19:07 +0100191 void VisitRoots(mirror::CompressedReference<mirror::Object>** roots,
192 size_t count,
193 const RootInfo& info) override
194 REQUIRES_SHARED(Locks::mutator_lock_)
Hans Boehme7d7e9d2019-06-03 18:36:06 +0000195 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_, !immune_gray_stack_lock_);
Mathieu Chartier90443472015-07-16 20:32:27 -0700196 void VerifyNoFromSpaceReferences() REQUIRES(Locks::mutator_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800197 accounting::ObjectStack* GetAllocationStack();
198 accounting::ObjectStack* GetLiveStack();
Roland Levillainf73caca2018-08-24 17:19:07 +0100199 void ProcessMarkStack() override REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -0700200 REQUIRES(!mark_stack_lock_);
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700201 bool ProcessMarkStackOnce() REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!mark_stack_lock_);
202 void ProcessMarkStackRef(mirror::Object* to_ref) REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -0700203 REQUIRES(!mark_stack_lock_);
Mathieu Chartier21328a12016-07-22 10:47:45 -0700204 void GrayAllDirtyImmuneObjects()
205 REQUIRES(Locks::mutator_lock_)
206 REQUIRES(!mark_stack_lock_);
Mathieu Chartier3768ade2017-05-02 14:04:39 -0700207 void GrayAllNewlyDirtyImmuneObjects()
208 REQUIRES(Locks::mutator_lock_)
209 REQUIRES(!mark_stack_lock_);
Mathieu Chartier21328a12016-07-22 10:47:45 -0700210 void VerifyGrayImmuneObjects()
211 REQUIRES(Locks::mutator_lock_)
212 REQUIRES(!mark_stack_lock_);
Mathieu Chartiera1467d02017-02-22 09:22:50 -0800213 void VerifyNoMissingCardMarks()
214 REQUIRES(Locks::mutator_lock_)
215 REQUIRES(!mark_stack_lock_);
Lokesh Gidra2a9824c2018-11-07 15:57:17 -0800216 template <typename Processor>
217 size_t ProcessThreadLocalMarkStacks(bool disable_weak_ref_access,
218 Closure* checkpoint_callback,
219 const Processor& processor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700220 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!mark_stack_lock_);
Hiroshi Yamauchifebd0cf2016-09-14 19:31:25 -0700221 void RevokeThreadLocalMarkStacks(bool disable_weak_ref_access, Closure* checkpoint_callback)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700222 REQUIRES_SHARED(Locks::mutator_lock_);
223 void SwitchToSharedMarkStackMode() REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -0700224 REQUIRES(!mark_stack_lock_);
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700225 void SwitchToGcExclusiveMarkStackMode() REQUIRES_SHARED(Locks::mutator_lock_);
Roland Levillainf73caca2018-08-24 17:19:07 +0100226 void DelayReferenceReferent(ObjPtr<mirror::Class> klass,
227 ObjPtr<mirror::Reference> reference) override
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700228 REQUIRES_SHARED(Locks::mutator_lock_);
229 void ProcessReferences(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_);
Roland Levillainf73caca2018-08-24 17:19:07 +0100230 mirror::Object* MarkObject(mirror::Object* from_ref) override
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700231 REQUIRES_SHARED(Locks::mutator_lock_)
Hans Boehme7d7e9d2019-06-03 18:36:06 +0000232 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_, !immune_gray_stack_lock_);
Roland Levillainf73caca2018-08-24 17:19:07 +0100233 void MarkHeapReference(mirror::HeapReference<mirror::Object>* from_ref,
234 bool do_atomic_update) override
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700235 REQUIRES_SHARED(Locks::mutator_lock_)
Hans Boehme7d7e9d2019-06-03 18:36:06 +0000236 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_, !immune_gray_stack_lock_);
Mathieu Chartierc381c362016-08-23 13:27:53 -0700237 bool IsMarkedInUnevacFromSpace(mirror::Object* from_ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700238 REQUIRES_SHARED(Locks::mutator_lock_);
Lokesh Gidra519c1c72018-11-09 17:10:47 -0800239 bool IsMarkedInNonMovingSpace(mirror::Object* from_ref)
240 REQUIRES_SHARED(Locks::mutator_lock_);
Roland Levillainf73caca2018-08-24 17:19:07 +0100241 bool IsNullOrMarkedHeapReference(mirror::HeapReference<mirror::Object>* field,
242 bool do_atomic_update) override
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700243 REQUIRES_SHARED(Locks::mutator_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800244 void SweepSystemWeaks(Thread* self)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700245 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Locks::heap_bitmap_lock_);
Roland Levillainb1e1dc32018-07-10 19:19:31 +0100246 // Sweep unmarked objects to complete the garbage collection. Full GCs sweep
247 // all allocation spaces (except the region space). Sticky-bit GCs just sweep
248 // a subset of the heap.
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800249 void Sweep(bool swap_bitmaps)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700250 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::heap_bitmap_lock_, !mark_stack_lock_);
Roland Levillainb1e1dc32018-07-10 19:19:31 +0100251 // Sweep only pointers within an array.
252 void SweepArray(accounting::ObjectStack* allocation_stack_, bool swap_bitmaps)
253 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::heap_bitmap_lock_, !mark_stack_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800254 void SweepLargeObjects(bool swap_bitmaps)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700255 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::heap_bitmap_lock_);
Mathieu Chartier962cd7a2016-08-16 12:15:59 -0700256 void MarkZygoteLargeObjects()
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700257 REQUIRES_SHARED(Locks::mutator_lock_);
Hiroshi Yamauchi7a181542017-03-08 17:34:46 -0800258 void FillWithDummyObject(Thread* const self, mirror::Object* dummy_obj, size_t byte_size)
Hans Boehme7d7e9d2019-06-03 18:36:06 +0000259 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_, !immune_gray_stack_lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700260 REQUIRES_SHARED(Locks::mutator_lock_);
Hiroshi Yamauchi7a181542017-03-08 17:34:46 -0800261 mirror::Object* AllocateInSkippedBlock(Thread* const self, size_t alloc_size)
Hans Boehme7d7e9d2019-06-03 18:36:06 +0000262 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_, !immune_gray_stack_lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700263 REQUIRES_SHARED(Locks::mutator_lock_);
264 void CheckEmptyMarkStack() REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!mark_stack_lock_);
265 void IssueEmptyCheckpoint() REQUIRES_SHARED(Locks::mutator_lock_);
266 bool IsOnAllocStack(mirror::Object* ref) REQUIRES_SHARED(Locks::mutator_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800267 mirror::Object* GetFwdPtr(mirror::Object* from_ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700268 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier90443472015-07-16 20:32:27 -0700269 void FlipThreadRoots() REQUIRES(!Locks::mutator_lock_);
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700270 void SwapStacks() REQUIRES_SHARED(Locks::mutator_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800271 void RecordLiveStackFreezeSize(Thread* self);
272 void ComputeUnevacFromSpaceLiveRatio();
Hiroshi Yamauchi3f64f252015-06-12 18:35:06 -0700273 void LogFromSpaceRefHolder(mirror::Object* obj, MemberOffset offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700274 REQUIRES_SHARED(Locks::mutator_lock_);
Roland Levillain001eff92018-01-24 14:24:33 +0000275 // Dump information about reference `ref` and return it as a string.
276 // Use `ref_name` to name the reference in messages. Each message is prefixed with `indent`.
Andreas Gampebc802de2018-06-20 17:24:11 -0700277 std::string DumpReferenceInfo(mirror::Object* ref, const char* ref_name, const char* indent = "")
Roland Levillain001eff92018-01-24 14:24:33 +0000278 REQUIRES_SHARED(Locks::mutator_lock_);
279 // Dump information about heap reference `ref`, referenced from object `obj` at offset `offset`,
280 // and return it as a string.
281 std::string DumpHeapReference(mirror::Object* obj, MemberOffset offset, mirror::Object* ref)
282 REQUIRES_SHARED(Locks::mutator_lock_);
283 // Dump information about GC root `ref` and return it as a string.
284 std::string DumpGcRoot(mirror::Object* ref) REQUIRES_SHARED(Locks::mutator_lock_);
Hiroshi Yamauchi3f64f252015-06-12 18:35:06 -0700285 void AssertToSpaceInvariantInNonMovingSpace(mirror::Object* obj, mirror::Object* ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700286 REQUIRES_SHARED(Locks::mutator_lock_);
287 void ReenableWeakRefAccess(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_);
288 void DisableMarking() REQUIRES_SHARED(Locks::mutator_lock_);
289 void IssueDisableMarkingCheckpoint() REQUIRES_SHARED(Locks::mutator_lock_);
290 void ExpandGcMarkStack() REQUIRES_SHARED(Locks::mutator_lock_);
Hiroshi Yamauchi7a181542017-03-08 17:34:46 -0800291 mirror::Object* MarkNonMoving(Thread* const self,
292 mirror::Object* from_ref,
Mathieu Chartier1ca68902017-04-18 11:26:22 -0700293 mirror::Object* holder = nullptr,
294 MemberOffset offset = MemberOffset(0))
295 REQUIRES_SHARED(Locks::mutator_lock_)
Hans Boehme7d7e9d2019-06-03 18:36:06 +0000296 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_);
Hiroshi Yamauchi7a181542017-03-08 17:34:46 -0800297 ALWAYS_INLINE mirror::Object* MarkUnevacFromSpaceRegion(Thread* const self,
298 mirror::Object* from_ref,
Hiroshi Yamauchi8e674652015-12-22 11:09:18 -0800299 accounting::SpaceBitmap<kObjectAlignment>* bitmap)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700300 REQUIRES_SHARED(Locks::mutator_lock_)
Hans Boehme7d7e9d2019-06-03 18:36:06 +0000301 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_);
Hiroshi Yamauchid8db5a22016-06-28 14:07:41 -0700302 template<bool kGrayImmuneObject>
Hiroshi Yamauchi7a181542017-03-08 17:34:46 -0800303 ALWAYS_INLINE mirror::Object* MarkImmuneSpace(Thread* const self,
304 mirror::Object* from_ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700305 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!immune_gray_stack_lock_);
Hiroshi Yamauchid8db5a22016-06-28 14:07:41 -0700306 void ScanImmuneObject(mirror::Object* obj)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700307 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!mark_stack_lock_);
Hiroshi Yamauchi7a181542017-03-08 17:34:46 -0800308 mirror::Object* MarkFromReadBarrierWithMeasurements(Thread* const self,
309 mirror::Object* from_ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700310 REQUIRES_SHARED(Locks::mutator_lock_)
Hans Boehme7d7e9d2019-06-03 18:36:06 +0000311 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_, !immune_gray_stack_lock_);
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100312 void DumpPerformanceInfo(std::ostream& os) override REQUIRES(!rb_slow_path_histogram_lock_);
Mathieu Chartier3768ade2017-05-02 14:04:39 -0700313 // Set the read barrier mark entrypoints to non-null.
314 void ActivateReadBarrierEntrypoints();
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800315
Lokesh Gidra2a9824c2018-11-07 15:57:17 -0800316 void CaptureThreadRootsForMarking() REQUIRES_SHARED(Locks::mutator_lock_);
317 void AddLiveBytesAndScanRef(mirror::Object* ref) REQUIRES_SHARED(Locks::mutator_lock_);
318 bool TestMarkBitmapForRef(mirror::Object* ref) REQUIRES_SHARED(Locks::mutator_lock_);
319 template <bool kAtomic = false>
320 bool TestAndSetMarkBitForRef(mirror::Object* ref) REQUIRES_SHARED(Locks::mutator_lock_);
321 void PushOntoLocalMarkStack(mirror::Object* ref) REQUIRES_SHARED(Locks::mutator_lock_);
322 void ProcessMarkStackForMarkingAndComputeLiveBytes() REQUIRES_SHARED(Locks::mutator_lock_)
323 REQUIRES(!mark_stack_lock_);
324
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800325 space::RegionSpace* region_space_; // The underlying region space.
326 std::unique_ptr<Barrier> gc_barrier_;
Hiroshi Yamauchi0b713572015-06-16 18:29:23 -0700327 std::unique_ptr<accounting::ObjectStack> gc_mark_stack_;
Roland Levillain8f7ea9a2018-01-26 17:27:59 +0000328
Albert Mingkun Yang0b4d1462018-11-29 13:25:35 +0000329 // If true, enable generational collection when using the Concurrent Copying
330 // (CC) collector, i.e. use sticky-bit CC for minor collections and (full) CC
331 // for major collections. Generational CC collection is currently only
332 // compatible with Baker read barriers. Set in Heap constructor.
333 const bool use_generational_cc_;
334
335 // Generational "sticky", only trace through dirty objects in region space.
336 const bool young_gen_;
337
338 // If true, the GC thread is done scanning marked objects on dirty and aged
339 // card (see ConcurrentCopying::CopyingPhase).
340 Atomic<bool> done_scanning_;
341
Roland Levillain8f7ea9a2018-01-26 17:27:59 +0000342 // The read-barrier mark-bit stack. Stores object references whose
343 // mark bit has been set by ConcurrentCopying::MarkFromReadBarrier,
344 // so that this bit can be reset at the end of the collection in
345 // ConcurrentCopying::FinishPhase. The mark bit of an object can be
346 // used by mutator read barrier code to quickly test whether that
347 // object has been already marked.
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700348 std::unique_ptr<accounting::ObjectStack> rb_mark_bit_stack_;
Roland Levillain8f7ea9a2018-01-26 17:27:59 +0000349 // Thread-unsafe Boolean value hinting that `rb_mark_bit_stack_` is
350 // full. A thread-safe test of whether the read-barrier mark-bit
351 // stack is full is implemented by `rb_mark_bit_stack_->AtomicPushBack(ref)`
352 // (see use case in ConcurrentCopying::MarkFromReadBarrier).
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700353 bool rb_mark_bit_stack_full_;
Roland Levillain8f7ea9a2018-01-26 17:27:59 +0000354
Hiroshi Yamauchi0b713572015-06-16 18:29:23 -0700355 Mutex mark_stack_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
356 std::vector<accounting::ObjectStack*> revoked_mark_stacks_
357 GUARDED_BY(mark_stack_lock_);
358 static constexpr size_t kMarkStackSize = kPageSize;
359 static constexpr size_t kMarkStackPoolSize = 256;
360 std::vector<accounting::ObjectStack*> pooled_mark_stacks_
361 GUARDED_BY(mark_stack_lock_);
362 Thread* thread_running_gc_;
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800363 bool is_marking_; // True while marking is ongoing.
Mathieu Chartier3768ade2017-05-02 14:04:39 -0700364 // True while we might dispatch on the read barrier entrypoints.
365 bool is_using_read_barrier_entrypoints_;
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800366 bool is_active_; // True while the collection is ongoing.
367 bool is_asserting_to_space_invariant_; // True while asserting the to-space invariant.
Mathieu Chartier763a31e2015-11-16 16:05:55 -0800368 ImmuneSpaces immune_spaces_;
Andreas Gamped4901292017-05-30 18:41:34 -0700369 accounting::ContinuousSpaceBitmap* region_space_bitmap_;
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800370 // A cache of Heap::GetMarkBitmap().
371 accounting::HeapBitmap* heap_mark_bitmap_;
372 size_t live_stack_freeze_size_;
Hans Boehma253c2d2019-05-13 12:38:54 -0700373 size_t from_space_num_objects_at_first_pause_; // Computed if kEnableFromSpaceAccountingCheck
374 size_t from_space_num_bytes_at_first_pause_; // Computed if kEnableFromSpaceAccountingCheck
Hiroshi Yamauchi0b713572015-06-16 18:29:23 -0700375 Atomic<int> is_mark_stack_push_disallowed_;
376 enum MarkStackMode {
377 kMarkStackModeOff = 0, // Mark stack is off.
378 kMarkStackModeThreadLocal, // All threads except for the GC-running thread push refs onto
379 // thread-local mark stacks. The GC-running thread pushes onto and
380 // pops off the GC mark stack without a lock.
381 kMarkStackModeShared, // All threads share the GC mark stack with a lock.
382 kMarkStackModeGcExclusive // The GC-running thread pushes onto and pops from the GC mark stack
383 // without a lock. Other threads won't access the mark stack.
384 };
385 Atomic<MarkStackMode> mark_stack_mode_;
Hiroshi Yamauchifebd0cf2016-09-14 19:31:25 -0700386 bool weak_ref_access_enabled_ GUARDED_BY(Locks::thread_list_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800387
Hans Boehma253c2d2019-05-13 12:38:54 -0700388 // How many objects and bytes we moved. The GC thread moves many more objects
389 // than mutators. Therefore, we separate the two to avoid CAS. Bytes_moved_ and
390 // bytes_moved_gc_thread_ are critical for GC triggering; the others are just informative.
Hiroshi Yamauchi7a181542017-03-08 17:34:46 -0800391 Atomic<size_t> bytes_moved_; // Used by mutators
392 Atomic<size_t> objects_moved_; // Used by mutators
393 size_t bytes_moved_gc_thread_; // Used by GC
394 size_t objects_moved_gc_thread_; // Used by GC
Mathieu Chartiercca44a02016-08-17 10:07:29 -0700395 Atomic<uint64_t> cumulative_bytes_moved_;
396 Atomic<uint64_t> cumulative_objects_moved_;
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800397
Albert Mingkun Yangaf9cce12018-11-07 09:58:35 +0000398 // copied_live_bytes_ratio_sum_ is read and written by CC per GC, in
399 // ReclaimPhase, and is read by DumpPerformanceInfo (potentially from another
400 // thread). However, at present, DumpPerformanceInfo is only called when the
401 // runtime shuts down, so no concurrent access. The same reasoning goes for
402 // gc_count_ and reclaimed_bytes_ratio_sum_
403
Albert Mingkun Yange260e542018-11-05 13:45:59 +0000404 // The sum of of all copied live bytes ratio (to_bytes/from_bytes)
405 float copied_live_bytes_ratio_sum_;
406 // The number of GC counts, used to calculate the average above. (It doesn't
407 // include GC where from_bytes is zero, IOW, from-space is empty, which is
408 // possible for minor GC if all allocated objects are in non-moving
409 // space.)
410 size_t gc_count_;
Lokesh Gidrad0c5b252018-12-05 01:10:40 -0800411 // Bit is set if the corresponding object has inter-region references that
412 // were found during the marking phase of two-phase full-heap GC cycle.
Mathieu Chartier6f382012019-07-30 09:47:35 -0700413 accounting::ContinuousSpaceBitmap region_space_inter_region_bitmap_;
414 accounting::ContinuousSpaceBitmap non_moving_space_inter_region_bitmap_;
Albert Mingkun Yange260e542018-11-05 13:45:59 +0000415
Albert Mingkun Yangaf9cce12018-11-07 09:58:35 +0000416 // reclaimed_bytes_ratio = reclaimed_bytes/num_allocated_bytes per GC cycle
417 float reclaimed_bytes_ratio_sum_;
418
Hans Boehme7d7e9d2019-06-03 18:36:06 +0000419 // The skipped blocks are memory blocks/chucks that were copies of
420 // objects that were unused due to lost races (cas failures) at
421 // object copy/forward pointer install. They may be reused.
422 // Skipped blocks are always in region space. Their size is included directly
423 // in num_bytes_allocated_, i.e. they are treated as allocated, but may be directly
424 // used without going through a GC cycle like other objects. They are reused only
425 // if we run out of region space. TODO: Revisit this design.
426 Mutex skipped_blocks_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
427 std::multimap<size_t, uint8_t*> skipped_blocks_map_ GUARDED_BY(skipped_blocks_lock_);
428 Atomic<size_t> to_space_bytes_skipped_;
429 Atomic<size_t> to_space_objects_skipped_;
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800430
Mathieu Chartier56fe2582016-07-14 13:30:03 -0700431 // If measure_read_barrier_slow_path_ is true, we count how long is spent in MarkFromReadBarrier
432 // and also log.
433 bool measure_read_barrier_slow_path_;
434 // mark_from_read_barrier_measurements_ is true if systrace is enabled or
435 // measure_read_barrier_time_ is true.
436 bool mark_from_read_barrier_measurements_;
437 Atomic<uint64_t> rb_slow_path_ns_;
438 Atomic<uint64_t> rb_slow_path_count_;
439 Atomic<uint64_t> rb_slow_path_count_gc_;
440 mutable Mutex rb_slow_path_histogram_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
441 Histogram<uint64_t> rb_slow_path_time_histogram_ GUARDED_BY(rb_slow_path_histogram_lock_);
442 uint64_t rb_slow_path_count_total_ GUARDED_BY(rb_slow_path_histogram_lock_);
443 uint64_t rb_slow_path_count_gc_total_ GUARDED_BY(rb_slow_path_histogram_lock_);
444
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800445 accounting::ReadBarrierTable* rb_table_;
446 bool force_evacuate_all_; // True if all regions are evacuated.
Hiroshi Yamauchid8db5a22016-06-28 14:07:41 -0700447 Atomic<bool> updated_all_immune_objects_;
448 bool gc_grays_immune_objects_;
449 Mutex immune_gray_stack_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
450 std::vector<mirror::Object*> immune_gray_stack_ GUARDED_BY(immune_gray_stack_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800451
Mathieu Chartier3ed8ec12017-04-20 19:28:54 -0700452 // Class of java.lang.Object. Filled in from WellKnownClasses in FlipCallback. Must
453 // be filled in before flipping thread roots so that FillDummyObject can run. Not
454 // ObjPtr since the GC may transition to suspended and runnable between phases.
455 mirror::Class* java_lang_Object_;
456
Roland Levillainb1e1dc32018-07-10 19:19:31 +0100457 // Sweep array free buffer, used to sweep the spaces based on an array more
458 // efficiently, by recording dead objects to be freed in batches (see
459 // ConcurrentCopying::SweepArray).
460 MemMap sweep_array_free_buffer_mem_map_;
461
Albert Mingkun Yangaf9cce12018-11-07 09:58:35 +0000462 // Use signed because after_gc may be larger than before_gc.
463 int64_t num_bytes_allocated_before_gc_;
464
Mathieu Chartier3768ade2017-05-02 14:04:39 -0700465 class ActivateReadBarrierEntrypointsCallback;
466 class ActivateReadBarrierEntrypointsCheckpoint;
Mathieu Chartiera07f5592016-06-16 11:44:28 -0700467 class AssertToSpaceInvariantFieldVisitor;
Mathieu Chartiera07f5592016-06-16 11:44:28 -0700468 class AssertToSpaceInvariantRefsVisitor;
469 class ClearBlackPtrsVisitor;
470 class ComputeUnevacFromSpaceLiveRatioVisitor;
Hiroshi Yamauchifebd0cf2016-09-14 19:31:25 -0700471 class DisableMarkingCallback;
Mathieu Chartiera07f5592016-06-16 11:44:28 -0700472 class DisableMarkingCheckpoint;
Hiroshi Yamauchifebd0cf2016-09-14 19:31:25 -0700473 class DisableWeakRefAccessCallback;
Mathieu Chartiera07f5592016-06-16 11:44:28 -0700474 class FlipCallback;
Mathieu Chartier3768ade2017-05-02 14:04:39 -0700475 template <bool kConcurrent> class GrayImmuneObjectVisitor;
Hiroshi Yamauchid8db5a22016-06-28 14:07:41 -0700476 class ImmuneSpaceScanObjVisitor;
Mathieu Chartiera07f5592016-06-16 11:44:28 -0700477 class LostCopyVisitor;
Mathieu Chartier8d1a9962016-08-17 16:39:45 -0700478 template <bool kNoUnEvac> class RefFieldsVisitor;
Mathieu Chartiera07f5592016-06-16 11:44:28 -0700479 class RevokeThreadLocalMarkStackCheckpoint;
Hiroshi Yamauchid8db5a22016-06-28 14:07:41 -0700480 class ScopedGcGraysImmuneObjects;
481 class ThreadFlipVisitor;
Mathieu Chartier21328a12016-07-22 10:47:45 -0700482 class VerifyGrayImmuneObjectsVisitor;
Mathieu Chartiera07f5592016-06-16 11:44:28 -0700483 class VerifyNoFromSpaceRefsFieldVisitor;
Mathieu Chartiera07f5592016-06-16 11:44:28 -0700484 class VerifyNoFromSpaceRefsVisitor;
Mathieu Chartiera1467d02017-02-22 09:22:50 -0800485 class VerifyNoMissingCardMarkVisitor;
Lokesh Gidra2a9824c2018-11-07 15:57:17 -0800486 class ImmuneSpaceCaptureRefsVisitor;
487 template <bool kAtomicTestAndSet = false> class CaptureRootsForMarkingVisitor;
488 class CaptureThreadRootsForMarkingAndCheckpoint;
489 template <bool kHandleInterRegionRefs> class ComputeLiveBytesAndMarkRefFieldsVisitor;
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800490
Mathieu Chartier3130cdf2015-05-03 15:20:23 -0700491 DISALLOW_IMPLICIT_CONSTRUCTORS(ConcurrentCopying);
Hiroshi Yamauchid5307ec2014-03-27 21:07:51 -0700492};
493
494} // namespace collector
495} // namespace gc
496} // namespace art
497
498#endif // ART_RUNTIME_GC_COLLECTOR_CONCURRENT_COPYING_H_