blob: 6a48cf7e9911dc441c20b1920a4c2e574fea1ea6 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 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 */
Carl Shapiro69759ea2011-07-21 18:13:35 -070016
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_GC_COLLECTOR_MARK_SWEEP_H_
18#define ART_RUNTIME_GC_COLLECTOR_MARK_SWEEP_H_
Carl Shapiro69759ea2011-07-21 18:13:35 -070019
Ian Rogersef7d42f2014-01-06 12:55:46 -080020#include "atomic.h"
Sameer Abu Asal4aeb5672013-02-19 15:30:35 -080021#include "barrier.h"
Elliott Hughes76160052012-12-12 16:31:20 -080022#include "base/macros.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080023#include "base/mutex.h"
Mathieu Chartier2b82db42012-11-14 17:29:05 -080024#include "garbage_collector.h"
Mathieu Chartier83c8ee02014-01-28 14:50:23 -080025#include "object_callbacks.h"
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070026#include "offsets.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080027#include "UniquePtr.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -070028
29namespace art {
Ian Rogers1d54e732013-05-02 21:10:01 -070030
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080031namespace mirror {
Ian Rogers1d54e732013-05-02 21:10:01 -070032 class Class;
33 class Object;
34 template<class T> class ObjectArray;
35} // namespace mirror
36
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080037class StackVisitor;
38class Thread;
Ian Rogers1d54e732013-05-02 21:10:01 -070039
40namespace gc {
41
42namespace accounting {
43 template <typename T> class AtomicStack;
44 class MarkIfReachesAllocspaceVisitor;
45 class ModUnionClearCardVisitor;
46 class ModUnionVisitor;
47 class ModUnionTableBitmap;
48 class MarkStackChunk;
49 typedef AtomicStack<mirror::Object*> ObjectStack;
50 class SpaceBitmap;
51} // namespace accounting
52
53namespace space {
54 class ContinuousSpace;
55} // namespace space
56
Ian Rogers1d54e732013-05-02 21:10:01 -070057class Heap;
58
59namespace collector {
Carl Shapiro69759ea2011-07-21 18:13:35 -070060
Mathieu Chartier2b82db42012-11-14 17:29:05 -080061class MarkSweep : public GarbageCollector {
Carl Shapiro69759ea2011-07-21 18:13:35 -070062 public:
Ian Rogers1d54e732013-05-02 21:10:01 -070063 explicit MarkSweep(Heap* heap, bool is_concurrent, const std::string& name_prefix = "");
Carl Shapiro58551df2011-07-24 03:09:51 -070064
Ian Rogers1d54e732013-05-02 21:10:01 -070065 ~MarkSweep() {}
Carl Shapiro69759ea2011-07-21 18:13:35 -070066
Mathieu Chartier2b82db42012-11-14 17:29:05 -080067 virtual void InitializePhase();
68 virtual bool IsConcurrent() const;
69 virtual bool HandleDirtyObjectsPhase() EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_);
70 virtual void MarkingPhase() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
71 virtual void ReclaimPhase() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartierad2541a2013-10-25 10:05:23 -070072 virtual void FinishPhase() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier2b82db42012-11-14 17:29:05 -080073 virtual void MarkReachableObjects()
74 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
75 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
76 virtual GcType GetGcType() const {
77 return kGcTypeFull;
78 }
79
Carl Shapiro58551df2011-07-24 03:09:51 -070080 // Initializes internal structures.
Jesse Wilson078f9b02011-11-18 17:51:47 -050081 void Init();
Carl Shapiro58551df2011-07-24 03:09:51 -070082
Mathieu Chartier7469ebf2012-09-24 16:28:36 -070083 // Find the default mark bitmap.
84 void FindDefaultMarkBitmap();
85
Carl Shapiro69759ea2011-07-21 18:13:35 -070086 // Marks the root set at the start of a garbage collection.
Ian Rogers00f7d0e2012-07-19 15:28:27 -070087 void MarkRoots()
Ian Rogersb726dcb2012-09-05 08:57:23 -070088 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
89 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -070090
Mathieu Chartier858f1c52012-10-17 17:45:55 -070091 void MarkNonThreadRoots()
Mathieu Chartierc528dba2013-11-26 12:00:11 -080092 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
93 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier858f1c52012-10-17 17:45:55 -070094
Mathieu Chartierc528dba2013-11-26 12:00:11 -080095 void MarkConcurrentRoots()
96 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
97 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier9ebae1f2012-10-15 17:38:16 -070098
Ian Rogers1d54e732013-05-02 21:10:01 -070099 void MarkRootsCheckpoint(Thread* self)
100 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
101 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700102
Carl Shapiro58551df2011-07-24 03:09:51 -0700103 // Builds a mark stack and recursively mark until it empties.
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800104 void RecursiveMark()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700105 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
106 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700107
Ian Rogers1d54e732013-05-02 21:10:01 -0700108 // Make a space immune, immune spaces have all live objects marked - that is the mark and
109 // live bitmaps are bound together.
110 void ImmuneSpace(space::ContinuousSpace* space)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800111 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
112 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800113
Mathieu Chartier590fee92013-09-13 13:46:47 -0700114 bool IsImmuneSpace(const space::ContinuousSpace* space) const;
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700115 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
116
Ian Rogers1d54e732013-05-02 21:10:01 -0700117 // Bind the live bits to the mark bits of bitmaps for spaces that are never collected, ie
118 // the image. Mark that portion of the heap as immune.
119 virtual void BindBitmaps() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700120
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700121 // Builds a mark stack with objects on dirty cards and recursively mark until it empties.
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700122 void RecursiveMarkDirtyObjects(bool paused, byte minimum_age)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700123 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
124 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700125
Carl Shapiro69759ea2011-07-21 18:13:35 -0700126 // Remarks the root set after completing the concurrent mark.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700127 void ReMarkRoots()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700128 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
129 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700130
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800131 void ProcessReferences(Thread* self)
132 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro58551df2011-07-24 03:09:51 -0700133
Mathieu Chartier590fee92013-09-13 13:46:47 -0700134 // Update and mark references from immune spaces.
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700135 virtual void UpdateAndMarkModUnion()
136 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
137
Carl Shapiro69759ea2011-07-21 18:13:35 -0700138 // Sweeps unmarked objects to complete the garbage collection.
Ian Rogers1d54e732013-05-02 21:10:01 -0700139 virtual void Sweep(bool swap_bitmaps) EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700140
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700141 // Sweeps unmarked objects to complete the garbage collection.
Ian Rogers1d54e732013-05-02 21:10:01 -0700142 void SweepLargeObjects(bool swap_bitmaps) EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700143
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700144 // Sweep only pointers within an array. WARNING: Trashes objects.
Ian Rogers1d54e732013-05-02 21:10:01 -0700145 void SweepArray(accounting::ObjectStack* allocation_stack_, bool swap_bitmaps)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700146 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700147
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700148 // Blackens an object.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700149 void ScanObject(mirror::Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700150 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
151 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700152
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800153 // TODO: enable thread safety analysis when in use by multiple worker threads.
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700154 template <typename MarkVisitor>
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700155 void ScanObjectVisit(mirror::Object* obj, const MarkVisitor& visitor)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800156 NO_THREAD_SAFETY_ANALYSIS;
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700157
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800158 // Everything inside the immune range is assumed to be marked.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800159 void SetImmuneRange(mirror::Object* begin, mirror::Object* end);
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800160
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700161 void SweepSystemWeaks()
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700162 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700163
Mathieu Chartier6aa3df92013-09-17 15:17:28 -0700164 static mirror::Object* VerifySystemWeakIsLiveCallback(mirror::Object* obj, void* arg)
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700165 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
166
167 void VerifySystemWeaks()
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700168 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700169
170 // Verify that an object is live, either in a live bitmap or in the allocation stack.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800171 void VerifyIsLive(const mirror::Object* obj)
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700172 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
173
Mathieu Chartierfd678be2012-08-30 14:50:54 -0700174 template <typename Visitor>
Mathieu Chartier590fee92013-09-13 13:46:47 -0700175 static void VisitObjectReferences(mirror::Object* obj, const Visitor& visitor, bool visit_class)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700176 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800177 Locks::mutator_lock_);
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700178
Mathieu Chartier39e32612013-11-12 16:28:05 -0800179 static mirror::Object* RecursiveMarkObjectCallback(mirror::Object* obj, void* arg)
180 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
181 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
182
Mathieu Chartier815873e2014-02-13 18:02:13 -0800183 static void MarkRootCallback(mirror::Object** root, void* arg, uint32_t thread_id,
184 RootType root_type)
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800185 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700186 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
187
Mathieu Chartier815873e2014-02-13 18:02:13 -0800188 static mirror::Object* MarkObjectCallback(mirror::Object* object, void* arg)
189 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
190 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
191
192 static void MarkRootParallelCallback(mirror::Object** root, void* arg, uint32_t thread_id,
193 RootType root_type)
194 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartierac86a7c2012-11-12 15:03:16 -0800195
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700196 // Marks an object.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800197 void MarkObject(const mirror::Object* obj)
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800198 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
199 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
200
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800201 void MarkRoot(const mirror::Object* obj)
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800202 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700203 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
204
Sameer Abu Asala8439542013-02-14 16:06:42 -0800205 Barrier& GetBarrier() {
206 return *gc_barrier_;
207 }
208
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800209 protected:
Carl Shapiro69759ea2011-07-21 18:13:35 -0700210 // Returns true if the object has its bit set in the mark bitmap.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800211 bool IsMarked(const mirror::Object* object) const;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700212
Mathieu Chartier39e32612013-11-12 16:28:05 -0800213 static mirror::Object* IsMarkedCallback(mirror::Object* object, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700214 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Mathieu Chartier46a23632012-08-07 18:44:40 -0700215
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800216 static void VerifyImageRootVisitor(mirror::Object* root, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700217 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_,
218 Locks::mutator_lock_);
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700219
Mathieu Chartier9642c962013-08-05 17:40:36 -0700220 void MarkObjectNonNull(const mirror::Object* obj)
221 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
222 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700223
Mathieu Chartier9642c962013-08-05 17:40:36 -0700224 // Unmarks an object by clearing the bit inside of the corresponding bitmap, or if it is in a
225 // space set, removing the object from the set.
226 void UnMarkObjectNonNull(const mirror::Object* obj)
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700227 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
228 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
229
230 // Mark the vm thread roots.
231 virtual void MarkThreadRoots(Thread* self)
232 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
233 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartierac86a7c2012-11-12 15:03:16 -0800234
Mathieu Chartier9642c962013-08-05 17:40:36 -0700235 // Marks an object atomically, safe to use from multiple threads.
236 void MarkObjectNonNullParallel(const mirror::Object* obj);
237
238 // Marks or unmarks a large object based on whether or not set is true. If set is true, then we
239 // mark, otherwise we unmark.
240 bool MarkLargeObject(const mirror::Object* obj, bool set)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700241 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700242
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700243 // Returns true if we need to add obj to a mark stack.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800244 bool MarkObjectParallel(const mirror::Object* obj) NO_THREAD_SAFETY_ANALYSIS;
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700245
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700246 // Verify the roots of the heap and print out information related to any invalid roots.
247 // Called in MarkObject, so may we may not hold the mutator lock.
248 void VerifyRoots()
249 NO_THREAD_SAFETY_ANALYSIS;
250
Mathieu Chartierba311b42013-08-27 13:02:30 -0700251 // Expand mark stack to 2x its current size.
252 void ExpandMarkStack() EXCLUSIVE_LOCKS_REQUIRED(mark_stack_lock_);
253 void ResizeMarkStack(size_t new_size) EXCLUSIVE_LOCKS_REQUIRED(mark_stack_lock_);
Mathieu Chartierac86a7c2012-11-12 15:03:16 -0800254
Mathieu Chartier2775ee42013-08-20 17:43:47 -0700255 // Returns how many threads we should use for the current GC phase based on if we are paused,
256 // whether or not we care about pauses.
257 size_t GetThreadCount(bool paused) const;
258
Mathieu Chartier9642c962013-08-05 17:40:36 -0700259 // Returns true if an object is inside of the immune region (assumed to be marked).
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700260 bool IsImmune(const mirror::Object* obj) const ALWAYS_INLINE {
Mathieu Chartier9642c962013-08-05 17:40:36 -0700261 return obj >= immune_begin_ && obj < immune_end_;
262 }
263
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800264 static void VerifyRootCallback(const mirror::Object* root, void* arg, size_t vreg,
Ian Rogers40e3bac2012-11-20 00:09:14 -0800265 const StackVisitor *visitor);
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700266
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800267 void VerifyRoot(const mirror::Object* root, size_t vreg, const StackVisitor* visitor)
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700268 NO_THREAD_SAFETY_ANALYSIS;
269
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700270 template <typename Visitor>
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700271 static void VisitInstanceFieldsReferences(mirror::Class* klass, mirror::Object* obj,
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700272 const Visitor& visitor)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800273 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
Ian Rogers5d76c432011-10-31 21:42:49 -0700274
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700275 // Visit the header, static field references, and interface pointers of a class object.
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700276 template <typename Visitor>
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700277 static void VisitClassReferences(mirror::Class* klass, mirror::Object* obj,
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700278 const Visitor& visitor)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800279 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
Ian Rogers5d76c432011-10-31 21:42:49 -0700280
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700281 template <typename Visitor>
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700282 static void VisitStaticFieldsReferences(mirror::Class* klass, const Visitor& visitor)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800283 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
Ian Rogers5d76c432011-10-31 21:42:49 -0700284
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700285 template <typename Visitor>
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700286 static void VisitFieldsReferences(mirror::Object* obj, uint32_t ref_offsets, bool is_static,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800287 const Visitor& visitor)
288 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
Ian Rogers5d76c432011-10-31 21:42:49 -0700289
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700290 // Visit all of the references in an object array.
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700291 template <typename Visitor>
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700292 static void VisitObjectArrayReferences(mirror::ObjectArray<mirror::Object>* array,
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700293 const Visitor& visitor)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800294 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
Ian Rogers5d76c432011-10-31 21:42:49 -0700295
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700296 // Visits the header and field references of a data object.
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700297 template <typename Visitor>
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700298 static void VisitOtherReferences(mirror::Class* klass, mirror::Object* obj,
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700299 const Visitor& visitor)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700300 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_) {
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700301 return VisitInstanceFieldsReferences(klass, obj, visitor);
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700302 }
Ian Rogers5d76c432011-10-31 21:42:49 -0700303
Carl Shapiro69759ea2011-07-21 18:13:35 -0700304 // Blackens objects grayed during a garbage collection.
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700305 void ScanGrayObjects(bool paused, byte minimum_age)
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800306 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
307 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700308
309 // Schedules an unmarked object for reference processing.
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700310 void DelayReferenceReferent(mirror::Class* klass, mirror::Object* reference)
Ian Rogers23435d02012-09-24 11:23:12 -0700311 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700312
313 // Recursively blackens objects on the mark stack.
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700314 void ProcessMarkStack(bool paused)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700315 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
316 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700317
Mathieu Chartier2775ee42013-08-20 17:43:47 -0700318 void ProcessMarkStackParallel(size_t thread_count)
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700319 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
320 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
321
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800322 void EnqueueFinalizerReferences(mirror::Object** ref)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700323 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
324 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700325
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800326 void PreserveSomeSoftReferences(mirror::Object** ref)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700327 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
328 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700329
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800330 void ClearWhiteReferences(mirror::Object** list)
Ian Rogers23435d02012-09-24 11:23:12 -0700331 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700332
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700333 // Whether or not we count how many of each type of object were scanned.
334 static const bool kCountScannedTypes = false;
335
Ian Rogers1d54e732013-05-02 21:10:01 -0700336 // Current space, we check this space first to avoid searching for the appropriate space for an
337 // object.
338 accounting::SpaceBitmap* current_mark_bitmap_;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700339
Ian Rogers1d54e732013-05-02 21:10:01 -0700340 accounting::ObjectStack* mark_stack_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700341
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700342 // Immune range, every object inside the immune range is assumed to be marked.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800343 mirror::Object* immune_begin_;
344 mirror::Object* immune_end_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700345
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700346 // Parallel finger.
347 AtomicInteger atomic_finger_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700348 // Number of classes scanned, if kCountScannedTypes.
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700349 AtomicInteger class_count_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700350 // Number of arrays scanned, if kCountScannedTypes.
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700351 AtomicInteger array_count_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700352 // Number of non-class/arrays scanned, if kCountScannedTypes.
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700353 AtomicInteger other_count_;
354 AtomicInteger large_object_test_;
355 AtomicInteger large_object_mark_;
356 AtomicInteger classes_marked_;
357 AtomicInteger overhead_time_;
358 AtomicInteger work_chunks_created_;
359 AtomicInteger work_chunks_deleted_;
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800360 AtomicInteger reference_count_;
Mathieu Chartier0f72e412013-09-06 16:40:01 -0700361
362 // Verification.
363 size_t live_stack_freeze_size_;
Elliott Hughes352a4242011-10-31 15:15:21 -0700364
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700365 UniquePtr<Barrier> gc_barrier_;
Mathieu Chartierac86a7c2012-11-12 15:03:16 -0800366 Mutex large_object_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Mathieu Chartier958291c2013-08-27 18:14:55 -0700367 Mutex mark_stack_lock_ ACQUIRED_AFTER(Locks::classlinker_classes_lock_);
Ian Rogers1bd4b4c2013-04-18 17:47:42 -0700368
369 const bool is_concurrent_;
370
Mathieu Chartier02e25112013-08-14 16:14:24 -0700371 private:
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700372 friend class AddIfReachesAllocSpaceVisitor; // Used by mod-union table.
Mathieu Chartier0f72e412013-09-06 16:40:01 -0700373 friend class CardScanTask;
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700374 friend class CheckBitmapVisitor;
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700375 friend class CheckReferenceVisitor;
Ian Rogers1d54e732013-05-02 21:10:01 -0700376 friend class art::gc::Heap;
Elliott Hughes410c0c82011-09-01 17:58:25 -0700377 friend class InternTableEntryIsUnmarked;
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700378 friend class MarkIfReachesAllocspaceVisitor;
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700379 friend class ModUnionCheckReferences;
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700380 friend class ModUnionClearCardVisitor;
Mathieu Chartiere6e06512012-06-26 15:00:26 -0700381 friend class ModUnionReferenceVisitor;
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700382 friend class ModUnionVisitor;
383 friend class ModUnionTableBitmap;
Mathieu Chartiere6e06512012-06-26 15:00:26 -0700384 friend class ModUnionTableReferenceCache;
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700385 friend class ModUnionScanImageRootVisitor;
386 friend class ScanBitmapVisitor;
387 friend class ScanImageRootVisitor;
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700388 template<bool kUseFinger> friend class MarkStackTask;
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700389 friend class FifoMarkStackChunk;
Elliott Hughes410c0c82011-09-01 17:58:25 -0700390
Carl Shapiro69759ea2011-07-21 18:13:35 -0700391 DISALLOW_COPY_AND_ASSIGN(MarkSweep);
392};
393
Ian Rogers1d54e732013-05-02 21:10:01 -0700394} // namespace collector
395} // namespace gc
Carl Shapiro69759ea2011-07-21 18:13:35 -0700396} // namespace art
397
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700398#endif // ART_RUNTIME_GC_COLLECTOR_MARK_SWEEP_H_