blob: e1ff97a9414c54c31ef9c2e82dfd5f3c9c75f3c3 [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
Hiroshi Yamauchid8db5a22016-06-28 14:07:41 -070074 virtual 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_);
Hiroshi Yamauchid5307ec2014-03-27 21:07:51 -070090 virtual 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 }
95 virtual CollectorType GetCollectorType() const OVERRIDE {
96 return kCollectorTypeCC;
97 }
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080098 virtual void RevokeAllThreadLocalBuffers() OVERRIDE;
99 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
Nicolas Geoffray13056a12017-05-11 11:48:28 +0000147 virtual mirror::Object* IsMarked(mirror::Object* from_ref) OVERRIDE
148 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_);
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700169 virtual void VisitRoots(mirror::Object*** roots, size_t count, const RootInfo& info)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700170 OVERRIDE 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_);
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700176 virtual void VisitRoots(mirror::CompressedReference<mirror::Object>** roots, size_t count,
177 const RootInfo& info)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700178 OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_)
Hiroshi Yamauchid8db5a22016-06-28 14:07:41 -0700179 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_, !immune_gray_stack_lock_);
Mathieu Chartier90443472015-07-16 20:32:27 -0700180 void VerifyNoFromSpaceReferences() REQUIRES(Locks::mutator_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800181 accounting::ObjectStack* GetAllocationStack();
182 accounting::ObjectStack* GetLiveStack();
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700183 virtual void ProcessMarkStack() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -0700184 REQUIRES(!mark_stack_lock_);
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700185 bool ProcessMarkStackOnce() REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!mark_stack_lock_);
186 void ProcessMarkStackRef(mirror::Object* to_ref) REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -0700187 REQUIRES(!mark_stack_lock_);
Mathieu Chartier21328a12016-07-22 10:47:45 -0700188 void GrayAllDirtyImmuneObjects()
189 REQUIRES(Locks::mutator_lock_)
190 REQUIRES(!mark_stack_lock_);
Mathieu Chartier3768ade2017-05-02 14:04:39 -0700191 void GrayAllNewlyDirtyImmuneObjects()
192 REQUIRES(Locks::mutator_lock_)
193 REQUIRES(!mark_stack_lock_);
Mathieu Chartier21328a12016-07-22 10:47:45 -0700194 void VerifyGrayImmuneObjects()
195 REQUIRES(Locks::mutator_lock_)
196 REQUIRES(!mark_stack_lock_);
Mathieu Chartiera1467d02017-02-22 09:22:50 -0800197 void VerifyNoMissingCardMarks()
198 REQUIRES(Locks::mutator_lock_)
199 REQUIRES(!mark_stack_lock_);
Hiroshi Yamauchifebd0cf2016-09-14 19:31:25 -0700200 size_t ProcessThreadLocalMarkStacks(bool disable_weak_ref_access, Closure* checkpoint_callback)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700201 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!mark_stack_lock_);
Hiroshi Yamauchifebd0cf2016-09-14 19:31:25 -0700202 void RevokeThreadLocalMarkStacks(bool disable_weak_ref_access, Closure* checkpoint_callback)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700203 REQUIRES_SHARED(Locks::mutator_lock_);
204 void SwitchToSharedMarkStackMode() REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -0700205 REQUIRES(!mark_stack_lock_);
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700206 void SwitchToGcExclusiveMarkStackMode() REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier31e88222016-10-14 18:43:19 -0700207 virtual void DelayReferenceReferent(ObjPtr<mirror::Class> klass,
208 ObjPtr<mirror::Reference> reference) OVERRIDE
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700209 REQUIRES_SHARED(Locks::mutator_lock_);
210 void ProcessReferences(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier97509952015-07-13 14:35:43 -0700211 virtual mirror::Object* MarkObject(mirror::Object* from_ref) OVERRIDE
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700212 REQUIRES_SHARED(Locks::mutator_lock_)
Hiroshi Yamauchid8db5a22016-06-28 14:07:41 -0700213 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_, !immune_gray_stack_lock_);
Hiroshi Yamauchi057d9772017-02-17 15:33:23 -0800214 virtual void MarkHeapReference(mirror::HeapReference<mirror::Object>* from_ref,
215 bool do_atomic_update) OVERRIDE
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700216 REQUIRES_SHARED(Locks::mutator_lock_)
Hiroshi Yamauchid8db5a22016-06-28 14:07:41 -0700217 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_, !immune_gray_stack_lock_);
Mathieu Chartierc381c362016-08-23 13:27:53 -0700218 bool IsMarkedInUnevacFromSpace(mirror::Object* from_ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700219 REQUIRES_SHARED(Locks::mutator_lock_);
Hiroshi Yamauchi65f5f242016-12-19 11:44:47 -0800220 virtual bool IsNullOrMarkedHeapReference(mirror::HeapReference<mirror::Object>* field,
221 bool do_atomic_update) OVERRIDE
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700222 REQUIRES_SHARED(Locks::mutator_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800223 void SweepSystemWeaks(Thread* self)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700224 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Locks::heap_bitmap_lock_);
Roland Levillainb1e1dc32018-07-10 19:19:31 +0100225 // Sweep unmarked objects to complete the garbage collection. Full GCs sweep
226 // all allocation spaces (except the region space). Sticky-bit GCs just sweep
227 // a subset of the heap.
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800228 void Sweep(bool swap_bitmaps)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700229 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::heap_bitmap_lock_, !mark_stack_lock_);
Roland Levillainb1e1dc32018-07-10 19:19:31 +0100230 // Sweep only pointers within an array.
231 void SweepArray(accounting::ObjectStack* allocation_stack_, bool swap_bitmaps)
232 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::heap_bitmap_lock_, !mark_stack_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800233 void SweepLargeObjects(bool swap_bitmaps)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700234 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::heap_bitmap_lock_);
Mathieu Chartier962cd7a2016-08-16 12:15:59 -0700235 void MarkZygoteLargeObjects()
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700236 REQUIRES_SHARED(Locks::mutator_lock_);
Hiroshi Yamauchi7a181542017-03-08 17:34:46 -0800237 void FillWithDummyObject(Thread* const self, mirror::Object* dummy_obj, size_t byte_size)
Mathieu Chartierd6636d32016-07-28 11:02:38 -0700238 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_, !immune_gray_stack_lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700239 REQUIRES_SHARED(Locks::mutator_lock_);
Hiroshi Yamauchi7a181542017-03-08 17:34:46 -0800240 mirror::Object* AllocateInSkippedBlock(Thread* const self, size_t alloc_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_);
243 void CheckEmptyMarkStack() REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!mark_stack_lock_);
244 void IssueEmptyCheckpoint() REQUIRES_SHARED(Locks::mutator_lock_);
245 bool IsOnAllocStack(mirror::Object* ref) REQUIRES_SHARED(Locks::mutator_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800246 mirror::Object* GetFwdPtr(mirror::Object* from_ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700247 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier90443472015-07-16 20:32:27 -0700248 void FlipThreadRoots() REQUIRES(!Locks::mutator_lock_);
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700249 void SwapStacks() REQUIRES_SHARED(Locks::mutator_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800250 void RecordLiveStackFreezeSize(Thread* self);
251 void ComputeUnevacFromSpaceLiveRatio();
Hiroshi Yamauchi3f64f252015-06-12 18:35:06 -0700252 void LogFromSpaceRefHolder(mirror::Object* obj, MemberOffset offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700253 REQUIRES_SHARED(Locks::mutator_lock_);
Roland Levillain001eff92018-01-24 14:24:33 +0000254 // Dump information about reference `ref` and return it as a string.
255 // Use `ref_name` to name the reference in messages. Each message is prefixed with `indent`.
Andreas Gampebc802de2018-06-20 17:24:11 -0700256 std::string DumpReferenceInfo(mirror::Object* ref, const char* ref_name, const char* indent = "")
Roland Levillain001eff92018-01-24 14:24:33 +0000257 REQUIRES_SHARED(Locks::mutator_lock_);
258 // Dump information about heap reference `ref`, referenced from object `obj` at offset `offset`,
259 // and return it as a string.
260 std::string DumpHeapReference(mirror::Object* obj, MemberOffset offset, mirror::Object* ref)
261 REQUIRES_SHARED(Locks::mutator_lock_);
262 // Dump information about GC root `ref` and return it as a string.
263 std::string DumpGcRoot(mirror::Object* ref) REQUIRES_SHARED(Locks::mutator_lock_);
Hiroshi Yamauchi3f64f252015-06-12 18:35:06 -0700264 void AssertToSpaceInvariantInNonMovingSpace(mirror::Object* obj, mirror::Object* ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700265 REQUIRES_SHARED(Locks::mutator_lock_);
266 void ReenableWeakRefAccess(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_);
267 void DisableMarking() REQUIRES_SHARED(Locks::mutator_lock_);
268 void IssueDisableMarkingCheckpoint() REQUIRES_SHARED(Locks::mutator_lock_);
269 void ExpandGcMarkStack() REQUIRES_SHARED(Locks::mutator_lock_);
Hiroshi Yamauchi7a181542017-03-08 17:34:46 -0800270 mirror::Object* MarkNonMoving(Thread* const self,
271 mirror::Object* from_ref,
Mathieu Chartier1ca68902017-04-18 11:26:22 -0700272 mirror::Object* holder = nullptr,
273 MemberOffset offset = MemberOffset(0))
274 REQUIRES_SHARED(Locks::mutator_lock_)
Hiroshi Yamauchi723e6ce2015-10-28 20:59:47 -0700275 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_);
Hiroshi Yamauchi7a181542017-03-08 17:34:46 -0800276 ALWAYS_INLINE mirror::Object* MarkUnevacFromSpaceRegion(Thread* const self,
277 mirror::Object* from_ref,
Hiroshi Yamauchi8e674652015-12-22 11:09:18 -0800278 accounting::SpaceBitmap<kObjectAlignment>* bitmap)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700279 REQUIRES_SHARED(Locks::mutator_lock_)
Hiroshi Yamauchi8e674652015-12-22 11:09:18 -0800280 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_);
Hiroshi Yamauchid8db5a22016-06-28 14:07:41 -0700281 template<bool kGrayImmuneObject>
Hiroshi Yamauchi7a181542017-03-08 17:34:46 -0800282 ALWAYS_INLINE mirror::Object* MarkImmuneSpace(Thread* const self,
283 mirror::Object* from_ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700284 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!immune_gray_stack_lock_);
Hiroshi Yamauchi7a181542017-03-08 17:34:46 -0800285 void PushOntoFalseGrayStack(Thread* const self, mirror::Object* obj)
286 REQUIRES_SHARED(Locks::mutator_lock_)
Hiroshi Yamauchi8e674652015-12-22 11:09:18 -0800287 REQUIRES(!mark_stack_lock_);
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700288 void ProcessFalseGrayStack() REQUIRES_SHARED(Locks::mutator_lock_)
Hiroshi Yamauchi8e674652015-12-22 11:09:18 -0800289 REQUIRES(!mark_stack_lock_);
Hiroshi Yamauchid8db5a22016-06-28 14:07:41 -0700290 void ScanImmuneObject(mirror::Object* obj)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700291 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!mark_stack_lock_);
Hiroshi Yamauchi7a181542017-03-08 17:34:46 -0800292 mirror::Object* MarkFromReadBarrierWithMeasurements(Thread* const self,
293 mirror::Object* from_ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700294 REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier56fe2582016-07-14 13:30:03 -0700295 REQUIRES(!mark_stack_lock_, !skipped_blocks_lock_, !immune_gray_stack_lock_);
296 void DumpPerformanceInfo(std::ostream& os) OVERRIDE REQUIRES(!rb_slow_path_histogram_lock_);
Mathieu Chartier3768ade2017-05-02 14:04:39 -0700297 // Set the read barrier mark entrypoints to non-null.
298 void ActivateReadBarrierEntrypoints();
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800299
300 space::RegionSpace* region_space_; // The underlying region space.
301 std::unique_ptr<Barrier> gc_barrier_;
Hiroshi Yamauchi0b713572015-06-16 18:29:23 -0700302 std::unique_ptr<accounting::ObjectStack> gc_mark_stack_;
Roland Levillain8f7ea9a2018-01-26 17:27:59 +0000303
304 // The read-barrier mark-bit stack. Stores object references whose
305 // mark bit has been set by ConcurrentCopying::MarkFromReadBarrier,
306 // so that this bit can be reset at the end of the collection in
307 // ConcurrentCopying::FinishPhase. The mark bit of an object can be
308 // used by mutator read barrier code to quickly test whether that
309 // object has been already marked.
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700310 std::unique_ptr<accounting::ObjectStack> rb_mark_bit_stack_;
Roland Levillain8f7ea9a2018-01-26 17:27:59 +0000311 // Thread-unsafe Boolean value hinting that `rb_mark_bit_stack_` is
312 // full. A thread-safe test of whether the read-barrier mark-bit
313 // stack is full is implemented by `rb_mark_bit_stack_->AtomicPushBack(ref)`
314 // (see use case in ConcurrentCopying::MarkFromReadBarrier).
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700315 bool rb_mark_bit_stack_full_;
Roland Levillain8f7ea9a2018-01-26 17:27:59 +0000316
Hiroshi Yamauchi8e674652015-12-22 11:09:18 -0800317 std::vector<mirror::Object*> false_gray_stack_ GUARDED_BY(mark_stack_lock_);
Hiroshi Yamauchi0b713572015-06-16 18:29:23 -0700318 Mutex mark_stack_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
319 std::vector<accounting::ObjectStack*> revoked_mark_stacks_
320 GUARDED_BY(mark_stack_lock_);
321 static constexpr size_t kMarkStackSize = kPageSize;
322 static constexpr size_t kMarkStackPoolSize = 256;
323 std::vector<accounting::ObjectStack*> pooled_mark_stacks_
324 GUARDED_BY(mark_stack_lock_);
325 Thread* thread_running_gc_;
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800326 bool is_marking_; // True while marking is ongoing.
Mathieu Chartier3768ade2017-05-02 14:04:39 -0700327 // True while we might dispatch on the read barrier entrypoints.
328 bool is_using_read_barrier_entrypoints_;
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800329 bool is_active_; // True while the collection is ongoing.
330 bool is_asserting_to_space_invariant_; // True while asserting the to-space invariant.
Mathieu Chartier763a31e2015-11-16 16:05:55 -0800331 ImmuneSpaces immune_spaces_;
Andreas Gamped4901292017-05-30 18:41:34 -0700332 accounting::ContinuousSpaceBitmap* region_space_bitmap_;
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800333 // A cache of Heap::GetMarkBitmap().
334 accounting::HeapBitmap* heap_mark_bitmap_;
335 size_t live_stack_freeze_size_;
336 size_t from_space_num_objects_at_first_pause_;
337 size_t from_space_num_bytes_at_first_pause_;
Hiroshi Yamauchi0b713572015-06-16 18:29:23 -0700338 Atomic<int> is_mark_stack_push_disallowed_;
339 enum MarkStackMode {
340 kMarkStackModeOff = 0, // Mark stack is off.
341 kMarkStackModeThreadLocal, // All threads except for the GC-running thread push refs onto
342 // thread-local mark stacks. The GC-running thread pushes onto and
343 // pops off the GC mark stack without a lock.
344 kMarkStackModeShared, // All threads share the GC mark stack with a lock.
345 kMarkStackModeGcExclusive // The GC-running thread pushes onto and pops from the GC mark stack
346 // without a lock. Other threads won't access the mark stack.
347 };
348 Atomic<MarkStackMode> mark_stack_mode_;
Hiroshi Yamauchifebd0cf2016-09-14 19:31:25 -0700349 bool weak_ref_access_enabled_ GUARDED_BY(Locks::thread_list_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800350
351 // How many objects and bytes we moved. Used for accounting.
Hiroshi Yamauchi7a181542017-03-08 17:34:46 -0800352 // GC thread moves many more objects than mutators.
353 // Therefore, we separate the two to avoid CAS.
354 Atomic<size_t> bytes_moved_; // Used by mutators
355 Atomic<size_t> objects_moved_; // Used by mutators
356 size_t bytes_moved_gc_thread_; // Used by GC
357 size_t objects_moved_gc_thread_; // Used by GC
Mathieu Chartiercca44a02016-08-17 10:07:29 -0700358 Atomic<uint64_t> cumulative_bytes_moved_;
359 Atomic<uint64_t> cumulative_objects_moved_;
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800360
Mathieu Chartier8d1a9962016-08-17 16:39:45 -0700361 // Generational "sticky", only trace through dirty objects in region space.
362 const bool young_gen_;
363 Atomic<bool> done_scanning_;
364
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800365 // The skipped blocks are memory blocks/chucks that were copies of
366 // objects that were unused due to lost races (cas failures) at
367 // object copy/forward pointer install. They are reused.
368 Mutex skipped_blocks_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
369 std::multimap<size_t, uint8_t*> skipped_blocks_map_ GUARDED_BY(skipped_blocks_lock_);
370 Atomic<size_t> to_space_bytes_skipped_;
371 Atomic<size_t> to_space_objects_skipped_;
372
Mathieu Chartier56fe2582016-07-14 13:30:03 -0700373 // If measure_read_barrier_slow_path_ is true, we count how long is spent in MarkFromReadBarrier
374 // and also log.
375 bool measure_read_barrier_slow_path_;
376 // mark_from_read_barrier_measurements_ is true if systrace is enabled or
377 // measure_read_barrier_time_ is true.
378 bool mark_from_read_barrier_measurements_;
379 Atomic<uint64_t> rb_slow_path_ns_;
380 Atomic<uint64_t> rb_slow_path_count_;
381 Atomic<uint64_t> rb_slow_path_count_gc_;
382 mutable Mutex rb_slow_path_histogram_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
383 Histogram<uint64_t> rb_slow_path_time_histogram_ GUARDED_BY(rb_slow_path_histogram_lock_);
384 uint64_t rb_slow_path_count_total_ GUARDED_BY(rb_slow_path_histogram_lock_);
385 uint64_t rb_slow_path_count_gc_total_ GUARDED_BY(rb_slow_path_histogram_lock_);
386
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800387 accounting::ReadBarrierTable* rb_table_;
388 bool force_evacuate_all_; // True if all regions are evacuated.
Hiroshi Yamauchid8db5a22016-06-28 14:07:41 -0700389 Atomic<bool> updated_all_immune_objects_;
390 bool gc_grays_immune_objects_;
391 Mutex immune_gray_stack_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
392 std::vector<mirror::Object*> immune_gray_stack_ GUARDED_BY(immune_gray_stack_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800393
Mathieu Chartier3ed8ec12017-04-20 19:28:54 -0700394 // Class of java.lang.Object. Filled in from WellKnownClasses in FlipCallback. Must
395 // be filled in before flipping thread roots so that FillDummyObject can run. Not
396 // ObjPtr since the GC may transition to suspended and runnable between phases.
397 mirror::Class* java_lang_Object_;
398
Roland Levillainb1e1dc32018-07-10 19:19:31 +0100399 // Sweep array free buffer, used to sweep the spaces based on an array more
400 // efficiently, by recording dead objects to be freed in batches (see
401 // ConcurrentCopying::SweepArray).
402 MemMap sweep_array_free_buffer_mem_map_;
403
Mathieu Chartier3768ade2017-05-02 14:04:39 -0700404 class ActivateReadBarrierEntrypointsCallback;
405 class ActivateReadBarrierEntrypointsCheckpoint;
Mathieu Chartiera07f5592016-06-16 11:44:28 -0700406 class AssertToSpaceInvariantFieldVisitor;
Mathieu Chartiera07f5592016-06-16 11:44:28 -0700407 class AssertToSpaceInvariantRefsVisitor;
408 class ClearBlackPtrsVisitor;
409 class ComputeUnevacFromSpaceLiveRatioVisitor;
Hiroshi Yamauchifebd0cf2016-09-14 19:31:25 -0700410 class DisableMarkingCallback;
Mathieu Chartiera07f5592016-06-16 11:44:28 -0700411 class DisableMarkingCheckpoint;
Hiroshi Yamauchifebd0cf2016-09-14 19:31:25 -0700412 class DisableWeakRefAccessCallback;
Mathieu Chartiera07f5592016-06-16 11:44:28 -0700413 class FlipCallback;
Mathieu Chartier3768ade2017-05-02 14:04:39 -0700414 template <bool kConcurrent> class GrayImmuneObjectVisitor;
Hiroshi Yamauchid8db5a22016-06-28 14:07:41 -0700415 class ImmuneSpaceScanObjVisitor;
Mathieu Chartiera07f5592016-06-16 11:44:28 -0700416 class LostCopyVisitor;
Mathieu Chartier8d1a9962016-08-17 16:39:45 -0700417 template <bool kNoUnEvac> class RefFieldsVisitor;
Mathieu Chartiera07f5592016-06-16 11:44:28 -0700418 class RevokeThreadLocalMarkStackCheckpoint;
Hiroshi Yamauchid8db5a22016-06-28 14:07:41 -0700419 class ScopedGcGraysImmuneObjects;
420 class ThreadFlipVisitor;
Mathieu Chartier21328a12016-07-22 10:47:45 -0700421 class VerifyGrayImmuneObjectsVisitor;
Mathieu Chartiera07f5592016-06-16 11:44:28 -0700422 class VerifyNoFromSpaceRefsFieldVisitor;
Mathieu Chartiera07f5592016-06-16 11:44:28 -0700423 class VerifyNoFromSpaceRefsVisitor;
Mathieu Chartiera1467d02017-02-22 09:22:50 -0800424 class VerifyNoMissingCardMarkVisitor;
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800425
Mathieu Chartier3130cdf2015-05-03 15:20:23 -0700426 DISALLOW_IMPLICIT_CONSTRUCTORS(ConcurrentCopying);
Hiroshi Yamauchid5307ec2014-03-27 21:07:51 -0700427};
428
429} // namespace collector
430} // namespace gc
431} // namespace art
432
433#endif // ART_RUNTIME_GC_COLLECTOR_CONCURRENT_COPYING_H_