blob: 237e070d1ac9d7049b5f9bac13d1ea739acba92d [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
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080020#include "barrier.h"
David Sehr67bf42e2018-02-26 16:43:04 -080021#include "base/safe_map.h"
Hiroshi Yamauchid5307ec2014-03-27 21:07:51 -070022#include "garbage_collector.h"
Mathieu Chartier763a31e2015-11-16 16:05:55 -080023#include "immune_spaces.h"
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080024#include "jni.h"
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080025#include "mirror/object_reference.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070026#include "offsets.h"
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080027
28#include <unordered_map>
29#include <vector>
Hiroshi Yamauchid5307ec2014-03-27 21:07:51 -070030
31namespace art {
Hiroshi Yamauchifebd0cf2016-09-14 19:31:25 -070032class Closure;
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080033class RootInfo;
34
Andreas Gampe5d08fcc2017-06-05 17:56:46 -070035namespace mirror {
36class 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_)
Hiroshi Yamauchid8db5a22016-06-28 14:07:41 -070082 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_, !immune_gray_stack_lock_);
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070083 void ReclaimPhase() REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!mark_stack_lock_);
Mathieu Chartier56fe2582016-07-14 13:30:03 -070084 void FinishPhase() REQUIRES(!mark_stack_lock_,
85 !rb_slow_path_histogram_lock_,
86 !skipped_blocks_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080087
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070088 void BindBitmaps() REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -070089 REQUIRES(!Locks::heap_bitmap_lock_);
Roland Levillainf73caca2018-08-24 17:19:07 +010090 GcType GetGcType() const override {
Mathieu Chartier8d1a9962016-08-17 16:39:45 -070091 return (kEnableGenerationalConcurrentCopyingCollection && young_gen_)
92 ? kGcTypeSticky
93 : kGcTypePartial;
Hiroshi Yamauchid5307ec2014-03-27 21:07:51 -070094 }
Roland Levillainf73caca2018-08-24 17:19:07 +010095 CollectorType GetCollectorType() const override {
Hiroshi Yamauchid5307ec2014-03-27 21:07:51 -070096 return kCollectorTypeCC;
97 }
Roland Levillainf73caca2018-08-24 17:19:07 +010098 void RevokeAllThreadLocalBuffers() override;
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080099 void SetRegionSpace(space::RegionSpace* region_space) {
100 DCHECK(region_space != nullptr);
101 region_space_ = region_space;
102 }
103 space::RegionSpace* RegionSpace() {
104 return region_space_;
105 }
Roland Levillain001eff92018-01-24 14:24:33 +0000106 // Assert the to-space invariant for a heap reference `ref` held in `obj` at offset `offset`.
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800107 void AssertToSpaceInvariant(mirror::Object* obj, MemberOffset offset, mirror::Object* ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700108 REQUIRES_SHARED(Locks::mutator_lock_);
Roland Levillain001eff92018-01-24 14:24:33 +0000109 // Assert the to-space invariant for a GC root reference `ref`.
Hiroshi Yamauchi3f64f252015-06-12 18:35:06 -0700110 void AssertToSpaceInvariant(GcRootSource* gc_root_source, mirror::Object* ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700111 REQUIRES_SHARED(Locks::mutator_lock_);
112 bool IsInToSpace(mirror::Object* ref) REQUIRES_SHARED(Locks::mutator_lock_) {
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800113 DCHECK(ref != nullptr);
114 return IsMarked(ref) == ref;
115 }
Roland Levillain8f7ea9a2018-01-26 17:27:59 +0000116 // Mark object `from_ref`, copying it to the to-space if needed.
Mathieu Chartier8d1a9962016-08-17 16:39:45 -0700117 template<bool kGrayImmuneObject = true, bool kNoUnEvac = false, bool kFromGCThread = false>
Hiroshi Yamauchi7a181542017-03-08 17:34:46 -0800118 ALWAYS_INLINE mirror::Object* Mark(Thread* const self,
119 mirror::Object* from_ref,
Mathieu Chartier1ca68902017-04-18 11:26:22 -0700120 mirror::Object* holder = nullptr,
121 MemberOffset offset = MemberOffset(0))
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700122 REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier56fe2582016-07-14 13:30:03 -0700123 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_, !immune_gray_stack_lock_);
124 ALWAYS_INLINE mirror::Object* MarkFromReadBarrier(mirror::Object* from_ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700125 REQUIRES_SHARED(Locks::mutator_lock_)
Hiroshi Yamauchid8db5a22016-06-28 14:07:41 -0700126 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_, !immune_gray_stack_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800127 bool IsMarking() const {
128 return is_marking_;
129 }
Mathieu Chartier3768ade2017-05-02 14:04:39 -0700130 // We may want to use read barrier entrypoints before is_marking_ is true since concurrent graying
131 // creates a small window where we might dispatch on these entrypoints.
132 bool IsUsingReadBarrierEntrypoints() const {
133 return is_using_read_barrier_entrypoints_;
134 }
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800135 bool IsActive() const {
136 return is_active_;
137 }
138 Barrier& GetBarrier() {
139 return *gc_barrier_;
140 }
Hiroshi Yamauchifebd0cf2016-09-14 19:31:25 -0700141 bool IsWeakRefAccessEnabled() REQUIRES(Locks::thread_list_lock_) {
142 return weak_ref_access_enabled_;
Hiroshi Yamauchi0b713572015-06-16 18:29:23 -0700143 }
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700144 void RevokeThreadLocalMarkStack(Thread* thread) REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -0700145 REQUIRES(!mark_stack_lock_);
Hiroshi Yamauchid5307ec2014-03-27 21:07:51 -0700146
Roland Levillainf73caca2018-08-24 17:19:07 +0100147 mirror::Object* IsMarked(mirror::Object* from_ref) override
Nicolas Geoffray13056a12017-05-11 11:48:28 +0000148 REQUIRES_SHARED(Locks::mutator_lock_);
149
Hiroshi Yamauchid5307ec2014-03-27 21:07:51 -0700150 private:
Hiroshi Yamauchi7a181542017-03-08 17:34:46 -0800151 void PushOntoMarkStack(Thread* const self, mirror::Object* obj)
152 REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -0700153 REQUIRES(!mark_stack_lock_);
Hiroshi Yamauchi7a181542017-03-08 17:34:46 -0800154 mirror::Object* Copy(Thread* const self,
155 mirror::Object* from_ref,
Mathieu Chartieref496d92017-04-28 18:58:59 -0700156 mirror::Object* holder,
157 MemberOffset offset)
158 REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartierd6636d32016-07-28 11:02:38 -0700159 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_, !immune_gray_stack_lock_);
Roland Levillain8f7ea9a2018-01-26 17:27:59 +0000160 // Scan the reference fields of object `to_ref`.
Mathieu Chartier8d1a9962016-08-17 16:39:45 -0700161 template <bool kNoUnEvac>
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700162 void Scan(mirror::Object* to_ref) REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -0700163 REQUIRES(!mark_stack_lock_);
Roland Levillain8f7ea9a2018-01-26 17:27:59 +0000164 // Process a field.
Mathieu Chartier8d1a9962016-08-17 16:39:45 -0700165 template <bool kNoUnEvac>
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800166 void Process(mirror::Object* obj, MemberOffset offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700167 REQUIRES_SHARED(Locks::mutator_lock_)
Hiroshi Yamauchid8db5a22016-06-28 14:07:41 -0700168 REQUIRES(!mark_stack_lock_ , !skipped_blocks_lock_, !immune_gray_stack_lock_);
Roland Levillainf73caca2018-08-24 17:19:07 +0100169 void VisitRoots(mirror::Object*** roots, size_t count, const RootInfo& info) override
170 REQUIRES_SHARED(Locks::mutator_lock_)
Hiroshi Yamauchid8db5a22016-06-28 14:07:41 -0700171 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_, !immune_gray_stack_lock_);
172 template<bool kGrayImmuneObject>
Hiroshi Yamauchi7a181542017-03-08 17:34:46 -0800173 void MarkRoot(Thread* const self, mirror::CompressedReference<mirror::Object>* root)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700174 REQUIRES_SHARED(Locks::mutator_lock_)
Hiroshi Yamauchid8db5a22016-06-28 14:07:41 -0700175 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_, !immune_gray_stack_lock_);
Roland Levillainf73caca2018-08-24 17:19:07 +0100176 void VisitRoots(mirror::CompressedReference<mirror::Object>** roots,
177 size_t count,
178 const RootInfo& info) override
179 REQUIRES_SHARED(Locks::mutator_lock_)
Hiroshi Yamauchid8db5a22016-06-28 14:07:41 -0700180 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_, !immune_gray_stack_lock_);
Mathieu Chartier90443472015-07-16 20:32:27 -0700181 void VerifyNoFromSpaceReferences() REQUIRES(Locks::mutator_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800182 accounting::ObjectStack* GetAllocationStack();
183 accounting::ObjectStack* GetLiveStack();
Roland Levillainf73caca2018-08-24 17:19:07 +0100184 void ProcessMarkStack() override REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -0700185 REQUIRES(!mark_stack_lock_);
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700186 bool ProcessMarkStackOnce() REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!mark_stack_lock_);
187 void ProcessMarkStackRef(mirror::Object* to_ref) REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -0700188 REQUIRES(!mark_stack_lock_);
Mathieu Chartier21328a12016-07-22 10:47:45 -0700189 void GrayAllDirtyImmuneObjects()
190 REQUIRES(Locks::mutator_lock_)
191 REQUIRES(!mark_stack_lock_);
Mathieu Chartier3768ade2017-05-02 14:04:39 -0700192 void GrayAllNewlyDirtyImmuneObjects()
193 REQUIRES(Locks::mutator_lock_)
194 REQUIRES(!mark_stack_lock_);
Mathieu Chartier21328a12016-07-22 10:47:45 -0700195 void VerifyGrayImmuneObjects()
196 REQUIRES(Locks::mutator_lock_)
197 REQUIRES(!mark_stack_lock_);
Mathieu Chartiera1467d02017-02-22 09:22:50 -0800198 void VerifyNoMissingCardMarks()
199 REQUIRES(Locks::mutator_lock_)
200 REQUIRES(!mark_stack_lock_);
Hiroshi Yamauchifebd0cf2016-09-14 19:31:25 -0700201 size_t ProcessThreadLocalMarkStacks(bool disable_weak_ref_access, Closure* checkpoint_callback)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700202 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!mark_stack_lock_);
Hiroshi Yamauchifebd0cf2016-09-14 19:31:25 -0700203 void RevokeThreadLocalMarkStacks(bool disable_weak_ref_access, Closure* checkpoint_callback)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700204 REQUIRES_SHARED(Locks::mutator_lock_);
205 void SwitchToSharedMarkStackMode() REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -0700206 REQUIRES(!mark_stack_lock_);
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700207 void SwitchToGcExclusiveMarkStackMode() REQUIRES_SHARED(Locks::mutator_lock_);
Roland Levillainf73caca2018-08-24 17:19:07 +0100208 void DelayReferenceReferent(ObjPtr<mirror::Class> klass,
209 ObjPtr<mirror::Reference> reference) override
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700210 REQUIRES_SHARED(Locks::mutator_lock_);
211 void ProcessReferences(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_);
Roland Levillainf73caca2018-08-24 17:19:07 +0100212 mirror::Object* MarkObject(mirror::Object* from_ref) override
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700213 REQUIRES_SHARED(Locks::mutator_lock_)
Hiroshi Yamauchid8db5a22016-06-28 14:07:41 -0700214 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_, !immune_gray_stack_lock_);
Roland Levillainf73caca2018-08-24 17:19:07 +0100215 void MarkHeapReference(mirror::HeapReference<mirror::Object>* from_ref,
216 bool do_atomic_update) override
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700217 REQUIRES_SHARED(Locks::mutator_lock_)
Hiroshi Yamauchid8db5a22016-06-28 14:07:41 -0700218 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_, !immune_gray_stack_lock_);
Mathieu Chartierc381c362016-08-23 13:27:53 -0700219 bool IsMarkedInUnevacFromSpace(mirror::Object* from_ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700220 REQUIRES_SHARED(Locks::mutator_lock_);
Lokesh Gidra519c1c72018-11-09 17:10:47 -0800221 bool IsMarkedInNonMovingSpace(mirror::Object* from_ref)
222 REQUIRES_SHARED(Locks::mutator_lock_);
Roland Levillainf73caca2018-08-24 17:19:07 +0100223 bool IsNullOrMarkedHeapReference(mirror::HeapReference<mirror::Object>* field,
224 bool do_atomic_update) override
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700225 REQUIRES_SHARED(Locks::mutator_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800226 void SweepSystemWeaks(Thread* self)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700227 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Locks::heap_bitmap_lock_);
Roland Levillainb1e1dc32018-07-10 19:19:31 +0100228 // Sweep unmarked objects to complete the garbage collection. Full GCs sweep
229 // all allocation spaces (except the region space). Sticky-bit GCs just sweep
230 // a subset of the heap.
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800231 void Sweep(bool swap_bitmaps)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700232 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::heap_bitmap_lock_, !mark_stack_lock_);
Roland Levillainb1e1dc32018-07-10 19:19:31 +0100233 // Sweep only pointers within an array.
234 void SweepArray(accounting::ObjectStack* allocation_stack_, bool swap_bitmaps)
235 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::heap_bitmap_lock_, !mark_stack_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800236 void SweepLargeObjects(bool swap_bitmaps)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700237 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::heap_bitmap_lock_);
Mathieu Chartier962cd7a2016-08-16 12:15:59 -0700238 void MarkZygoteLargeObjects()
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700239 REQUIRES_SHARED(Locks::mutator_lock_);
Hiroshi Yamauchi7a181542017-03-08 17:34:46 -0800240 void FillWithDummyObject(Thread* const self, mirror::Object* dummy_obj, size_t byte_size)
Mathieu Chartierd6636d32016-07-28 11:02:38 -0700241 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_, !immune_gray_stack_lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700242 REQUIRES_SHARED(Locks::mutator_lock_);
Hiroshi Yamauchi7a181542017-03-08 17:34:46 -0800243 mirror::Object* AllocateInSkippedBlock(Thread* const self, size_t alloc_size)
Mathieu Chartierd6636d32016-07-28 11:02:38 -0700244 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_, !immune_gray_stack_lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700245 REQUIRES_SHARED(Locks::mutator_lock_);
246 void CheckEmptyMarkStack() REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!mark_stack_lock_);
247 void IssueEmptyCheckpoint() REQUIRES_SHARED(Locks::mutator_lock_);
248 bool IsOnAllocStack(mirror::Object* ref) REQUIRES_SHARED(Locks::mutator_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800249 mirror::Object* GetFwdPtr(mirror::Object* from_ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700250 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier90443472015-07-16 20:32:27 -0700251 void FlipThreadRoots() REQUIRES(!Locks::mutator_lock_);
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700252 void SwapStacks() REQUIRES_SHARED(Locks::mutator_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800253 void RecordLiveStackFreezeSize(Thread* self);
254 void ComputeUnevacFromSpaceLiveRatio();
Hiroshi Yamauchi3f64f252015-06-12 18:35:06 -0700255 void LogFromSpaceRefHolder(mirror::Object* obj, MemberOffset offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700256 REQUIRES_SHARED(Locks::mutator_lock_);
Roland Levillain001eff92018-01-24 14:24:33 +0000257 // Dump information about reference `ref` and return it as a string.
258 // Use `ref_name` to name the reference in messages. Each message is prefixed with `indent`.
Andreas Gampebc802de2018-06-20 17:24:11 -0700259 std::string DumpReferenceInfo(mirror::Object* ref, const char* ref_name, const char* indent = "")
Roland Levillain001eff92018-01-24 14:24:33 +0000260 REQUIRES_SHARED(Locks::mutator_lock_);
261 // Dump information about heap reference `ref`, referenced from object `obj` at offset `offset`,
262 // and return it as a string.
263 std::string DumpHeapReference(mirror::Object* obj, MemberOffset offset, mirror::Object* ref)
264 REQUIRES_SHARED(Locks::mutator_lock_);
265 // Dump information about GC root `ref` and return it as a string.
266 std::string DumpGcRoot(mirror::Object* ref) REQUIRES_SHARED(Locks::mutator_lock_);
Hiroshi Yamauchi3f64f252015-06-12 18:35:06 -0700267 void AssertToSpaceInvariantInNonMovingSpace(mirror::Object* obj, mirror::Object* ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700268 REQUIRES_SHARED(Locks::mutator_lock_);
269 void ReenableWeakRefAccess(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_);
270 void DisableMarking() REQUIRES_SHARED(Locks::mutator_lock_);
271 void IssueDisableMarkingCheckpoint() REQUIRES_SHARED(Locks::mutator_lock_);
272 void ExpandGcMarkStack() REQUIRES_SHARED(Locks::mutator_lock_);
Hiroshi Yamauchi7a181542017-03-08 17:34:46 -0800273 mirror::Object* MarkNonMoving(Thread* const self,
274 mirror::Object* from_ref,
Mathieu Chartier1ca68902017-04-18 11:26:22 -0700275 mirror::Object* holder = nullptr,
276 MemberOffset offset = MemberOffset(0))
277 REQUIRES_SHARED(Locks::mutator_lock_)
Hiroshi Yamauchi723e6ce2015-10-28 20:59:47 -0700278 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_);
Hiroshi Yamauchi7a181542017-03-08 17:34:46 -0800279 ALWAYS_INLINE mirror::Object* MarkUnevacFromSpaceRegion(Thread* const self,
280 mirror::Object* from_ref,
Hiroshi Yamauchi8e674652015-12-22 11:09:18 -0800281 accounting::SpaceBitmap<kObjectAlignment>* bitmap)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700282 REQUIRES_SHARED(Locks::mutator_lock_)
Hiroshi Yamauchi8e674652015-12-22 11:09:18 -0800283 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_);
Hiroshi Yamauchid8db5a22016-06-28 14:07:41 -0700284 template<bool kGrayImmuneObject>
Hiroshi Yamauchi7a181542017-03-08 17:34:46 -0800285 ALWAYS_INLINE mirror::Object* MarkImmuneSpace(Thread* const self,
286 mirror::Object* from_ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700287 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!immune_gray_stack_lock_);
Hiroshi Yamauchid8db5a22016-06-28 14:07:41 -0700288 void ScanImmuneObject(mirror::Object* obj)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700289 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!mark_stack_lock_);
Hiroshi Yamauchi7a181542017-03-08 17:34:46 -0800290 mirror::Object* MarkFromReadBarrierWithMeasurements(Thread* const self,
291 mirror::Object* from_ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700292 REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier56fe2582016-07-14 13:30:03 -0700293 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_, !immune_gray_stack_lock_);
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100294 void DumpPerformanceInfo(std::ostream& os) override REQUIRES(!rb_slow_path_histogram_lock_);
Mathieu Chartier3768ade2017-05-02 14:04:39 -0700295 // Set the read barrier mark entrypoints to non-null.
296 void ActivateReadBarrierEntrypoints();
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800297
298 space::RegionSpace* region_space_; // The underlying region space.
299 std::unique_ptr<Barrier> gc_barrier_;
Hiroshi Yamauchi0b713572015-06-16 18:29:23 -0700300 std::unique_ptr<accounting::ObjectStack> gc_mark_stack_;
Roland Levillain8f7ea9a2018-01-26 17:27:59 +0000301
302 // The read-barrier mark-bit stack. Stores object references whose
303 // mark bit has been set by ConcurrentCopying::MarkFromReadBarrier,
304 // so that this bit can be reset at the end of the collection in
305 // ConcurrentCopying::FinishPhase. The mark bit of an object can be
306 // used by mutator read barrier code to quickly test whether that
307 // object has been already marked.
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700308 std::unique_ptr<accounting::ObjectStack> rb_mark_bit_stack_;
Roland Levillain8f7ea9a2018-01-26 17:27:59 +0000309 // Thread-unsafe Boolean value hinting that `rb_mark_bit_stack_` is
310 // full. A thread-safe test of whether the read-barrier mark-bit
311 // stack is full is implemented by `rb_mark_bit_stack_->AtomicPushBack(ref)`
312 // (see use case in ConcurrentCopying::MarkFromReadBarrier).
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700313 bool rb_mark_bit_stack_full_;
Roland Levillain8f7ea9a2018-01-26 17:27:59 +0000314
Hiroshi Yamauchi0b713572015-06-16 18:29:23 -0700315 Mutex mark_stack_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
316 std::vector<accounting::ObjectStack*> revoked_mark_stacks_
317 GUARDED_BY(mark_stack_lock_);
318 static constexpr size_t kMarkStackSize = kPageSize;
319 static constexpr size_t kMarkStackPoolSize = 256;
320 std::vector<accounting::ObjectStack*> pooled_mark_stacks_
321 GUARDED_BY(mark_stack_lock_);
322 Thread* thread_running_gc_;
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800323 bool is_marking_; // True while marking is ongoing.
Mathieu Chartier3768ade2017-05-02 14:04:39 -0700324 // True while we might dispatch on the read barrier entrypoints.
325 bool is_using_read_barrier_entrypoints_;
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800326 bool is_active_; // True while the collection is ongoing.
327 bool is_asserting_to_space_invariant_; // True while asserting the to-space invariant.
Mathieu Chartier763a31e2015-11-16 16:05:55 -0800328 ImmuneSpaces immune_spaces_;
Andreas Gamped4901292017-05-30 18:41:34 -0700329 accounting::ContinuousSpaceBitmap* region_space_bitmap_;
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800330 // A cache of Heap::GetMarkBitmap().
331 accounting::HeapBitmap* heap_mark_bitmap_;
332 size_t live_stack_freeze_size_;
333 size_t from_space_num_objects_at_first_pause_;
334 size_t from_space_num_bytes_at_first_pause_;
Hiroshi Yamauchi0b713572015-06-16 18:29:23 -0700335 Atomic<int> is_mark_stack_push_disallowed_;
336 enum MarkStackMode {
337 kMarkStackModeOff = 0, // Mark stack is off.
338 kMarkStackModeThreadLocal, // All threads except for the GC-running thread push refs onto
339 // thread-local mark stacks. The GC-running thread pushes onto and
340 // pops off the GC mark stack without a lock.
341 kMarkStackModeShared, // All threads share the GC mark stack with a lock.
342 kMarkStackModeGcExclusive // The GC-running thread pushes onto and pops from the GC mark stack
343 // without a lock. Other threads won't access the mark stack.
344 };
345 Atomic<MarkStackMode> mark_stack_mode_;
Hiroshi Yamauchifebd0cf2016-09-14 19:31:25 -0700346 bool weak_ref_access_enabled_ GUARDED_BY(Locks::thread_list_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800347
348 // How many objects and bytes we moved. Used for accounting.
Hiroshi Yamauchi7a181542017-03-08 17:34:46 -0800349 // GC thread moves many more objects than mutators.
350 // Therefore, we separate the two to avoid CAS.
351 Atomic<size_t> bytes_moved_; // Used by mutators
352 Atomic<size_t> objects_moved_; // Used by mutators
353 size_t bytes_moved_gc_thread_; // Used by GC
354 size_t objects_moved_gc_thread_; // Used by GC
Mathieu Chartiercca44a02016-08-17 10:07:29 -0700355 Atomic<uint64_t> cumulative_bytes_moved_;
356 Atomic<uint64_t> cumulative_objects_moved_;
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800357
Albert Mingkun Yangaf9cce12018-11-07 09:58:35 +0000358 // copied_live_bytes_ratio_sum_ is read and written by CC per GC, in
359 // ReclaimPhase, and is read by DumpPerformanceInfo (potentially from another
360 // thread). However, at present, DumpPerformanceInfo is only called when the
361 // runtime shuts down, so no concurrent access. The same reasoning goes for
362 // gc_count_ and reclaimed_bytes_ratio_sum_
363
Albert Mingkun Yange260e542018-11-05 13:45:59 +0000364 // The sum of of all copied live bytes ratio (to_bytes/from_bytes)
365 float copied_live_bytes_ratio_sum_;
366 // The number of GC counts, used to calculate the average above. (It doesn't
367 // include GC where from_bytes is zero, IOW, from-space is empty, which is
368 // possible for minor GC if all allocated objects are in non-moving
369 // space.)
370 size_t gc_count_;
371
Albert Mingkun Yangaf9cce12018-11-07 09:58:35 +0000372 // reclaimed_bytes_ratio = reclaimed_bytes/num_allocated_bytes per GC cycle
373 float reclaimed_bytes_ratio_sum_;
374
Mathieu Chartier8d1a9962016-08-17 16:39:45 -0700375 // Generational "sticky", only trace through dirty objects in region space.
376 const bool young_gen_;
Roland Levillain2d94e292018-08-15 16:46:30 +0100377 // If true, the GC thread is done scanning marked objects on dirty and aged
378 // card (see ConcurrentCopying::MarkingPhase).
Mathieu Chartier8d1a9962016-08-17 16:39:45 -0700379 Atomic<bool> done_scanning_;
380
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800381 // The skipped blocks are memory blocks/chucks that were copies of
382 // objects that were unused due to lost races (cas failures) at
383 // object copy/forward pointer install. They are reused.
384 Mutex skipped_blocks_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
385 std::multimap<size_t, uint8_t*> skipped_blocks_map_ GUARDED_BY(skipped_blocks_lock_);
386 Atomic<size_t> to_space_bytes_skipped_;
387 Atomic<size_t> to_space_objects_skipped_;
388
Mathieu Chartier56fe2582016-07-14 13:30:03 -0700389 // If measure_read_barrier_slow_path_ is true, we count how long is spent in MarkFromReadBarrier
390 // and also log.
391 bool measure_read_barrier_slow_path_;
392 // mark_from_read_barrier_measurements_ is true if systrace is enabled or
393 // measure_read_barrier_time_ is true.
394 bool mark_from_read_barrier_measurements_;
395 Atomic<uint64_t> rb_slow_path_ns_;
396 Atomic<uint64_t> rb_slow_path_count_;
397 Atomic<uint64_t> rb_slow_path_count_gc_;
398 mutable Mutex rb_slow_path_histogram_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
399 Histogram<uint64_t> rb_slow_path_time_histogram_ GUARDED_BY(rb_slow_path_histogram_lock_);
400 uint64_t rb_slow_path_count_total_ GUARDED_BY(rb_slow_path_histogram_lock_);
401 uint64_t rb_slow_path_count_gc_total_ GUARDED_BY(rb_slow_path_histogram_lock_);
402
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800403 accounting::ReadBarrierTable* rb_table_;
404 bool force_evacuate_all_; // True if all regions are evacuated.
Hiroshi Yamauchid8db5a22016-06-28 14:07:41 -0700405 Atomic<bool> updated_all_immune_objects_;
406 bool gc_grays_immune_objects_;
407 Mutex immune_gray_stack_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
408 std::vector<mirror::Object*> immune_gray_stack_ GUARDED_BY(immune_gray_stack_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800409
Mathieu Chartier3ed8ec12017-04-20 19:28:54 -0700410 // Class of java.lang.Object. Filled in from WellKnownClasses in FlipCallback. Must
411 // be filled in before flipping thread roots so that FillDummyObject can run. Not
412 // ObjPtr since the GC may transition to suspended and runnable between phases.
413 mirror::Class* java_lang_Object_;
414
Roland Levillainb1e1dc32018-07-10 19:19:31 +0100415 // Sweep array free buffer, used to sweep the spaces based on an array more
416 // efficiently, by recording dead objects to be freed in batches (see
417 // ConcurrentCopying::SweepArray).
418 MemMap sweep_array_free_buffer_mem_map_;
419
Albert Mingkun Yangaf9cce12018-11-07 09:58:35 +0000420 // Use signed because after_gc may be larger than before_gc.
421 int64_t num_bytes_allocated_before_gc_;
422
Mathieu Chartier3768ade2017-05-02 14:04:39 -0700423 class ActivateReadBarrierEntrypointsCallback;
424 class ActivateReadBarrierEntrypointsCheckpoint;
Mathieu Chartiera07f5592016-06-16 11:44:28 -0700425 class AssertToSpaceInvariantFieldVisitor;
Mathieu Chartiera07f5592016-06-16 11:44:28 -0700426 class AssertToSpaceInvariantRefsVisitor;
427 class ClearBlackPtrsVisitor;
428 class ComputeUnevacFromSpaceLiveRatioVisitor;
Hiroshi Yamauchifebd0cf2016-09-14 19:31:25 -0700429 class DisableMarkingCallback;
Mathieu Chartiera07f5592016-06-16 11:44:28 -0700430 class DisableMarkingCheckpoint;
Hiroshi Yamauchifebd0cf2016-09-14 19:31:25 -0700431 class DisableWeakRefAccessCallback;
Mathieu Chartiera07f5592016-06-16 11:44:28 -0700432 class FlipCallback;
Mathieu Chartier3768ade2017-05-02 14:04:39 -0700433 template <bool kConcurrent> class GrayImmuneObjectVisitor;
Hiroshi Yamauchid8db5a22016-06-28 14:07:41 -0700434 class ImmuneSpaceScanObjVisitor;
Mathieu Chartiera07f5592016-06-16 11:44:28 -0700435 class LostCopyVisitor;
Mathieu Chartier8d1a9962016-08-17 16:39:45 -0700436 template <bool kNoUnEvac> class RefFieldsVisitor;
Mathieu Chartiera07f5592016-06-16 11:44:28 -0700437 class RevokeThreadLocalMarkStackCheckpoint;
Hiroshi Yamauchid8db5a22016-06-28 14:07:41 -0700438 class ScopedGcGraysImmuneObjects;
439 class ThreadFlipVisitor;
Mathieu Chartier21328a12016-07-22 10:47:45 -0700440 class VerifyGrayImmuneObjectsVisitor;
Mathieu Chartiera07f5592016-06-16 11:44:28 -0700441 class VerifyNoFromSpaceRefsFieldVisitor;
Mathieu Chartiera07f5592016-06-16 11:44:28 -0700442 class VerifyNoFromSpaceRefsVisitor;
Mathieu Chartiera1467d02017-02-22 09:22:50 -0800443 class VerifyNoMissingCardMarkVisitor;
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800444
Mathieu Chartier3130cdf2015-05-03 15:20:23 -0700445 DISALLOW_IMPLICIT_CONSTRUCTORS(ConcurrentCopying);
Hiroshi Yamauchid5307ec2014-03-27 21:07:51 -0700446};
447
448} // namespace collector
449} // namespace gc
450} // namespace art
451
452#endif // ART_RUNTIME_GC_COLLECTOR_CONCURRENT_COPYING_H_