blob: b4dd8c7b382582cc19cb7338d65fac1104512cd6 [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 Chartier8d562102014-03-12 17:42:10 -070025#include "immune_region.h"
Mathieu Chartier83c8ee02014-01-28 14:50:23 -080026#include "object_callbacks.h"
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070027#include "offsets.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028#include "UniquePtr.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -070029
30namespace art {
Ian Rogers1d54e732013-05-02 21:10:01 -070031
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080032namespace mirror {
Ian Rogers1d54e732013-05-02 21:10:01 -070033 class Class;
34 class Object;
35 template<class T> class ObjectArray;
36} // namespace mirror
37
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080038class StackVisitor;
39class Thread;
Mathieu Chartier893263b2014-03-04 11:07:42 -080040enum VisitRootFlags : uint8_t;
Ian Rogers1d54e732013-05-02 21:10:01 -070041
42namespace gc {
43
44namespace accounting {
45 template <typename T> class AtomicStack;
46 class MarkIfReachesAllocspaceVisitor;
47 class ModUnionClearCardVisitor;
48 class ModUnionVisitor;
49 class ModUnionTableBitmap;
50 class MarkStackChunk;
51 typedef AtomicStack<mirror::Object*> ObjectStack;
52 class SpaceBitmap;
53} // namespace accounting
54
55namespace space {
56 class ContinuousSpace;
57} // namespace space
58
Ian Rogers1d54e732013-05-02 21:10:01 -070059class Heap;
60
61namespace collector {
Carl Shapiro69759ea2011-07-21 18:13:35 -070062
Mathieu Chartier2b82db42012-11-14 17:29:05 -080063class MarkSweep : public GarbageCollector {
Carl Shapiro69759ea2011-07-21 18:13:35 -070064 public:
Ian Rogers1d54e732013-05-02 21:10:01 -070065 explicit MarkSweep(Heap* heap, bool is_concurrent, const std::string& name_prefix = "");
Carl Shapiro58551df2011-07-24 03:09:51 -070066
Ian Rogers1d54e732013-05-02 21:10:01 -070067 ~MarkSweep() {}
Carl Shapiro69759ea2011-07-21 18:13:35 -070068
Ian Rogers6fac4472014-02-25 17:01:10 -080069 virtual void InitializePhase() OVERRIDE;
70 virtual void MarkingPhase() OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Hiroshi Yamauchi3e417802014-03-20 12:03:02 -070071 virtual void HandleDirtyObjectsPhase() OVERRIDE EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers6fac4472014-02-25 17:01:10 -080072 virtual void ReclaimPhase() OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
73 virtual void FinishPhase() OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier2b82db42012-11-14 17:29:05 -080074 virtual void MarkReachableObjects()
75 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
76 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Ian Rogers6fac4472014-02-25 17:01:10 -080077
Hiroshi Yamauchi3e417802014-03-20 12:03:02 -070078 bool IsConcurrent() const {
79 return is_concurrent_;
80 }
Ian Rogers6fac4472014-02-25 17:01:10 -080081
82 virtual GcType GetGcType() const OVERRIDE {
Mathieu Chartier2b82db42012-11-14 17:29:05 -080083 return kGcTypeFull;
84 }
85
Hiroshi Yamauchi3e417802014-03-20 12:03:02 -070086 virtual CollectorType GetCollectorType() const OVERRIDE {
87 return is_concurrent_ ? kCollectorTypeCMS : kCollectorTypeMS;
88 }
89
Carl Shapiro58551df2011-07-24 03:09:51 -070090 // Initializes internal structures.
Jesse Wilson078f9b02011-11-18 17:51:47 -050091 void Init();
Carl Shapiro58551df2011-07-24 03:09:51 -070092
Mathieu Chartier7469ebf2012-09-24 16:28:36 -070093 // Find the default mark bitmap.
94 void FindDefaultMarkBitmap();
95
Mathieu Chartier893263b2014-03-04 11:07:42 -080096 // Marks all objects in the root set at the start of a garbage collection.
97 void MarkRoots(Thread* self)
Ian Rogersb726dcb2012-09-05 08:57:23 -070098 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
99 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700100
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700101 void MarkNonThreadRoots()
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800102 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
103 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700104
Mathieu Chartier893263b2014-03-04 11:07:42 -0800105 void MarkConcurrentRoots(VisitRootFlags flags)
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800106 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
107 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier9ebae1f2012-10-15 17:38:16 -0700108
Ian Rogers1d54e732013-05-02 21:10:01 -0700109 void MarkRootsCheckpoint(Thread* self)
110 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
111 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700112
Carl Shapiro58551df2011-07-24 03:09:51 -0700113 // Builds a mark stack and recursively mark until it empties.
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800114 void RecursiveMark()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700115 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
116 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700117
Ian Rogers1d54e732013-05-02 21:10:01 -0700118 // Bind the live bits to the mark bits of bitmaps for spaces that are never collected, ie
119 // the image. Mark that portion of the heap as immune.
120 virtual void BindBitmaps() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700121
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700122 // Builds a mark stack with objects on dirty cards and recursively mark until it empties.
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700123 void RecursiveMarkDirtyObjects(bool paused, byte minimum_age)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700124 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
125 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700126
Carl Shapiro69759ea2011-07-21 18:13:35 -0700127 // Remarks the root set after completing the concurrent mark.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700128 void ReMarkRoots()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700129 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
130 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700131
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800132 void ProcessReferences(Thread* self)
133 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro58551df2011-07-24 03:09:51 -0700134
Mathieu Chartier601276a2014-03-20 15:12:30 -0700135 void PreProcessReferences()
Mathieu Chartier1ad27842014-03-19 17:08:17 -0700136 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
137 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
138
Ian Rogers6fac4472014-02-25 17:01:10 -0800139 // Update and mark references from immune spaces. Virtual as overridden by StickyMarkSweep.
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700140 virtual void UpdateAndMarkModUnion()
141 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
142
Mathieu Chartierdda54f52014-02-24 09:58:40 -0800143 // Pre clean cards to reduce how much work is needed in the pause.
144 void PreCleanCards()
145 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
146 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
147
Ian Rogers6fac4472014-02-25 17:01:10 -0800148 // Sweeps unmarked objects to complete the garbage collection. Virtual as by default it sweeps
149 // all allocation spaces. Partial and sticky GCs want to just sweep a subset of the heap.
Ian Rogers1d54e732013-05-02 21:10:01 -0700150 virtual void Sweep(bool swap_bitmaps) EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700151
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700152 // Sweeps unmarked objects to complete the garbage collection.
Ian Rogers1d54e732013-05-02 21:10:01 -0700153 void SweepLargeObjects(bool swap_bitmaps) EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700154
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700155 // Sweep only pointers within an array. WARNING: Trashes objects.
Ian Rogers1d54e732013-05-02 21:10:01 -0700156 void SweepArray(accounting::ObjectStack* allocation_stack_, bool swap_bitmaps)
Ian Rogers719d1a32014-03-06 12:13:39 -0800157 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700158 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700159
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700160 // Blackens an object.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700161 void ScanObject(mirror::Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700162 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
163 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700164
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800165 // TODO: enable thread safety analysis when in use by multiple worker threads.
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700166 template <typename MarkVisitor>
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700167 void ScanObjectVisit(mirror::Object* obj, const MarkVisitor& visitor)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800168 NO_THREAD_SAFETY_ANALYSIS;
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700169
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700170 void SweepSystemWeaks()
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700171 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700172
Mathieu Chartier6aa3df92013-09-17 15:17:28 -0700173 static mirror::Object* VerifySystemWeakIsLiveCallback(mirror::Object* obj, void* arg)
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700174 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
175
176 void VerifySystemWeaks()
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700177 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700178
179 // Verify that an object is live, either in a live bitmap or in the allocation stack.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800180 void VerifyIsLive(const mirror::Object* obj)
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700181 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
182
Mathieu Chartierfd678be2012-08-30 14:50:54 -0700183 template <typename Visitor>
Mathieu Chartier590fee92013-09-13 13:46:47 -0700184 static void VisitObjectReferences(mirror::Object* obj, const Visitor& visitor, bool visit_class)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700185 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800186 Locks::mutator_lock_);
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700187
Mathieu Chartier3bb57c72014-02-18 11:38:45 -0800188 static mirror::Object* MarkObjectCallback(mirror::Object* obj, void* arg)
Mathieu Chartier39e32612013-11-12 16:28:05 -0800189 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
190 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
191
Mathieu Chartier815873e2014-02-13 18:02:13 -0800192 static void MarkRootCallback(mirror::Object** root, void* arg, uint32_t thread_id,
193 RootType root_type)
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800194 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700195 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
196
Mathieu Chartier893263b2014-03-04 11:07:42 -0800197 static void VerifyRootMarked(mirror::Object** root, void* arg, uint32_t /*thread_id*/,
198 RootType /*root_type*/)
199 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
200 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
201
Mathieu Chartier3bb57c72014-02-18 11:38:45 -0800202 static void ProcessMarkStackPausedCallback(void* arg)
203 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
Mathieu Chartier815873e2014-02-13 18:02:13 -0800204
205 static void MarkRootParallelCallback(mirror::Object** root, void* arg, uint32_t thread_id,
206 RootType root_type)
207 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartierac86a7c2012-11-12 15:03:16 -0800208
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700209 // Marks an object.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800210 void MarkObject(const mirror::Object* obj)
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800211 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
212 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
213
Sameer Abu Asala8439542013-02-14 16:06:42 -0800214 Barrier& GetBarrier() {
215 return *gc_barrier_;
216 }
217
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800218 protected:
Carl Shapiro69759ea2011-07-21 18:13:35 -0700219 // Returns true if the object has its bit set in the mark bitmap.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800220 bool IsMarked(const mirror::Object* object) const;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700221
Mathieu Chartier39e32612013-11-12 16:28:05 -0800222 static mirror::Object* IsMarkedCallback(mirror::Object* object, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700223 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Mathieu Chartier46a23632012-08-07 18:44:40 -0700224
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800225 static void VerifyImageRootVisitor(mirror::Object* root, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700226 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_,
227 Locks::mutator_lock_);
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700228
Mathieu Chartier9642c962013-08-05 17:40:36 -0700229 void MarkObjectNonNull(const mirror::Object* obj)
230 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
231 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700232
Mathieu Chartier9642c962013-08-05 17:40:36 -0700233 // Unmarks an object by clearing the bit inside of the corresponding bitmap, or if it is in a
234 // space set, removing the object from the set.
235 void UnMarkObjectNonNull(const mirror::Object* obj)
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700236 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
237 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
238
239 // Mark the vm thread roots.
Ian Rogers6fac4472014-02-25 17:01:10 -0800240 void MarkThreadRoots(Thread* self)
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700241 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
242 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartierac86a7c2012-11-12 15:03:16 -0800243
Mathieu Chartier9642c962013-08-05 17:40:36 -0700244 // Marks an object atomically, safe to use from multiple threads.
245 void MarkObjectNonNullParallel(const mirror::Object* obj);
246
247 // Marks or unmarks a large object based on whether or not set is true. If set is true, then we
248 // mark, otherwise we unmark.
249 bool MarkLargeObject(const mirror::Object* obj, bool set)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700250 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700251
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700252 // Returns true if we need to add obj to a mark stack.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800253 bool MarkObjectParallel(const mirror::Object* obj) NO_THREAD_SAFETY_ANALYSIS;
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700254
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700255 // Verify the roots of the heap and print out information related to any invalid roots.
256 // Called in MarkObject, so may we may not hold the mutator lock.
257 void VerifyRoots()
258 NO_THREAD_SAFETY_ANALYSIS;
259
Mathieu Chartierba311b42013-08-27 13:02:30 -0700260 // Expand mark stack to 2x its current size.
261 void ExpandMarkStack() EXCLUSIVE_LOCKS_REQUIRED(mark_stack_lock_);
262 void ResizeMarkStack(size_t new_size) EXCLUSIVE_LOCKS_REQUIRED(mark_stack_lock_);
Mathieu Chartierac86a7c2012-11-12 15:03:16 -0800263
Mathieu Chartier2775ee42013-08-20 17:43:47 -0700264 // Returns how many threads we should use for the current GC phase based on if we are paused,
265 // whether or not we care about pauses.
266 size_t GetThreadCount(bool paused) const;
267
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800268 static void VerifyRootCallback(const mirror::Object* root, void* arg, size_t vreg,
Ian Rogers40e3bac2012-11-20 00:09:14 -0800269 const StackVisitor *visitor);
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700270
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800271 void VerifyRoot(const mirror::Object* root, size_t vreg, const StackVisitor* visitor)
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700272 NO_THREAD_SAFETY_ANALYSIS;
273
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700274 template <typename Visitor>
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700275 static void VisitInstanceFieldsReferences(mirror::Class* klass, mirror::Object* obj,
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700276 const Visitor& visitor)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800277 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
Ian Rogers5d76c432011-10-31 21:42:49 -0700278
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700279 // Visit the header, static field references, and interface pointers of a class object.
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700280 template <typename Visitor>
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700281 static void VisitClassReferences(mirror::Class* klass, mirror::Object* obj,
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700282 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 VisitStaticFieldsReferences(mirror::Class* klass, const Visitor& visitor)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800287 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
Ian Rogers5d76c432011-10-31 21:42:49 -0700288
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700289 template <typename Visitor>
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700290 static void VisitFieldsReferences(mirror::Object* obj, uint32_t ref_offsets, bool is_static,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800291 const Visitor& visitor)
292 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
Ian Rogers5d76c432011-10-31 21:42:49 -0700293
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700294 // Visit all of the references in an object array.
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700295 template <typename Visitor>
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700296 static void VisitObjectArrayReferences(mirror::ObjectArray<mirror::Object>* array,
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700297 const Visitor& visitor)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800298 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
Ian Rogers5d76c432011-10-31 21:42:49 -0700299
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700300 // Visits the header and field references of a data object.
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700301 template <typename Visitor>
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700302 static void VisitOtherReferences(mirror::Class* klass, mirror::Object* obj,
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700303 const Visitor& visitor)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700304 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_) {
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700305 return VisitInstanceFieldsReferences(klass, obj, visitor);
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700306 }
Ian Rogers5d76c432011-10-31 21:42:49 -0700307
Carl Shapiro69759ea2011-07-21 18:13:35 -0700308 // Blackens objects grayed during a garbage collection.
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700309 void ScanGrayObjects(bool paused, byte minimum_age)
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800310 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
311 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700312
313 // Schedules an unmarked object for reference processing.
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700314 void DelayReferenceReferent(mirror::Class* klass, mirror::Object* reference)
Ian Rogers23435d02012-09-24 11:23:12 -0700315 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700316
317 // Recursively blackens objects on the mark stack.
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700318 void ProcessMarkStack(bool paused)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700319 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
320 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700321
Mathieu Chartier2775ee42013-08-20 17:43:47 -0700322 void ProcessMarkStackParallel(size_t thread_count)
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700323 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
324 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
325
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800326 void EnqueueFinalizerReferences(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 PreserveSomeSoftReferences(mirror::Object** ref)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700331 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
332 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700333
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800334 void ClearWhiteReferences(mirror::Object** list)
Ian Rogers23435d02012-09-24 11:23:12 -0700335 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700336
Mathieu Chartierc22c59e2014-02-24 15:16:06 -0800337 // Used to get around thread safety annotations. The call is from MarkingPhase and is guarded by
338 // IsExclusiveHeld.
339 void RevokeAllThreadLocalAllocationStacks(Thread* self) NO_THREAD_SAFETY_ANALYSIS;
340
Hiroshi Yamauchic93c5302014-03-20 16:15:37 -0700341 // Revoke all the thread-local buffers.
342 void RevokeAllThreadLocalBuffers();
343
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700344 // Whether or not we count how many of each type of object were scanned.
345 static const bool kCountScannedTypes = false;
346
Ian Rogers1d54e732013-05-02 21:10:01 -0700347 // Current space, we check this space first to avoid searching for the appropriate space for an
348 // object.
349 accounting::SpaceBitmap* current_mark_bitmap_;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700350
Ian Rogers1d54e732013-05-02 21:10:01 -0700351 accounting::ObjectStack* mark_stack_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700352
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700353 // Immune range, every object inside the immune range is assumed to be marked.
Mathieu Chartier8d562102014-03-12 17:42:10 -0700354 ImmuneRegion immune_region_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700355
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700356 // Parallel finger.
357 AtomicInteger atomic_finger_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700358 // Number of classes scanned, if kCountScannedTypes.
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700359 AtomicInteger class_count_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700360 // Number of arrays scanned, if kCountScannedTypes.
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700361 AtomicInteger array_count_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700362 // Number of non-class/arrays scanned, if kCountScannedTypes.
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700363 AtomicInteger other_count_;
364 AtomicInteger large_object_test_;
365 AtomicInteger large_object_mark_;
366 AtomicInteger classes_marked_;
367 AtomicInteger overhead_time_;
368 AtomicInteger work_chunks_created_;
369 AtomicInteger work_chunks_deleted_;
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800370 AtomicInteger reference_count_;
Mathieu Chartier0f72e412013-09-06 16:40:01 -0700371
372 // Verification.
373 size_t live_stack_freeze_size_;
Elliott Hughes352a4242011-10-31 15:15:21 -0700374
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700375 UniquePtr<Barrier> gc_barrier_;
Mathieu Chartierac86a7c2012-11-12 15:03:16 -0800376 Mutex large_object_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Mathieu Chartier958291c2013-08-27 18:14:55 -0700377 Mutex mark_stack_lock_ ACQUIRED_AFTER(Locks::classlinker_classes_lock_);
Ian Rogers1bd4b4c2013-04-18 17:47:42 -0700378
379 const bool is_concurrent_;
380
Mathieu Chartier02e25112013-08-14 16:14:24 -0700381 private:
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700382 friend class AddIfReachesAllocSpaceVisitor; // Used by mod-union table.
Mathieu Chartier0f72e412013-09-06 16:40:01 -0700383 friend class CardScanTask;
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700384 friend class CheckBitmapVisitor;
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700385 friend class CheckReferenceVisitor;
Ian Rogers1d54e732013-05-02 21:10:01 -0700386 friend class art::gc::Heap;
Elliott Hughes410c0c82011-09-01 17:58:25 -0700387 friend class InternTableEntryIsUnmarked;
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700388 friend class MarkIfReachesAllocspaceVisitor;
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700389 friend class ModUnionCheckReferences;
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700390 friend class ModUnionClearCardVisitor;
Mathieu Chartiere6e06512012-06-26 15:00:26 -0700391 friend class ModUnionReferenceVisitor;
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700392 friend class ModUnionVisitor;
393 friend class ModUnionTableBitmap;
Mathieu Chartiere6e06512012-06-26 15:00:26 -0700394 friend class ModUnionTableReferenceCache;
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700395 friend class ModUnionScanImageRootVisitor;
396 friend class ScanBitmapVisitor;
397 friend class ScanImageRootVisitor;
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700398 template<bool kUseFinger> friend class MarkStackTask;
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700399 friend class FifoMarkStackChunk;
Elliott Hughes410c0c82011-09-01 17:58:25 -0700400
Carl Shapiro69759ea2011-07-21 18:13:35 -0700401 DISALLOW_COPY_AND_ASSIGN(MarkSweep);
402};
403
Ian Rogers1d54e732013-05-02 21:10:01 -0700404} // namespace collector
405} // namespace gc
Carl Shapiro69759ea2011-07-21 18:13:35 -0700406} // namespace art
407
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700408#endif // ART_RUNTIME_GC_COLLECTOR_MARK_SWEEP_H_