blob: 0d43bee7955f6a95d96207ee4892d4d612ff5325 [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
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080017#ifndef ART_SRC_GC_MARK_SWEEP_H_
18#define ART_SRC_GC_MARK_SWEEP_H_
Carl Shapiro69759ea2011-07-21 18:13:35 -070019
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080020#include "atomic_integer.h"
Elliott Hughes76160052012-12-12 16:31:20 -080021#include "base/macros.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080022#include "base/mutex.h"
Mathieu Chartier2b82db42012-11-14 17:29:05 -080023#include "garbage_collector.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080024#include "gc_type.h"
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070025#include "offsets.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080026#include "root_visitor.h"
27#include "timing_logger.h"
28#include "UniquePtr.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -070029
30namespace art {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080031namespace mirror {
32class Class;
33class Object;
34template<class T> class ObjectArray;
35}
36template <typename T> class AtomicStack;
Mathieu Chartier858f1c52012-10-17 17:45:55 -070037class Barrier;
Mathieu Chartierb43b7d42012-06-19 13:15:09 -070038class CheckObjectVisitor;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080039class ContinuousSpace;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080040class Heap;
Mathieu Chartierb43b7d42012-06-19 13:15:09 -070041class MarkIfReachesAllocspaceVisitor;
42class ModUnionClearCardVisitor;
43class ModUnionVisitor;
44class ModUnionTableBitmap;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080045typedef AtomicStack<mirror::Object*> ObjectStack;
46class SpaceBitmap;
47class StackVisitor;
48class Thread;
Mathieu Chartier02b6a782012-10-26 13:51:26 -070049class MarkStackChunk;
Carl Shapiro69759ea2011-07-21 18:13:35 -070050
Mathieu Chartier2b82db42012-11-14 17:29:05 -080051class MarkSweep : public GarbageCollector {
Carl Shapiro69759ea2011-07-21 18:13:35 -070052 public:
Mathieu Chartier2b82db42012-11-14 17:29:05 -080053 explicit MarkSweep(Heap* heap, bool is_concurrent);
Carl Shapiro58551df2011-07-24 03:09:51 -070054
Carl Shapiro69759ea2011-07-21 18:13:35 -070055 ~MarkSweep();
56
Mathieu Chartier2b82db42012-11-14 17:29:05 -080057 virtual std::string GetName() const;
58 virtual void InitializePhase();
59 virtual bool IsConcurrent() const;
60 virtual bool HandleDirtyObjectsPhase() EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_);
61 virtual void MarkingPhase() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
62 virtual void ReclaimPhase() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
63 virtual void FinishPhase();
64 virtual void MarkReachableObjects()
65 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
66 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
67 virtual GcType GetGcType() const {
68 return kGcTypeFull;
69 }
70
Carl Shapiro58551df2011-07-24 03:09:51 -070071 // Initializes internal structures.
Jesse Wilson078f9b02011-11-18 17:51:47 -050072 void Init();
Carl Shapiro58551df2011-07-24 03:09:51 -070073
Mathieu Chartier7469ebf2012-09-24 16:28:36 -070074 // Find the default mark bitmap.
75 void FindDefaultMarkBitmap();
76
Carl Shapiro69759ea2011-07-21 18:13:35 -070077 // Marks the root set at the start of a garbage collection.
Ian Rogers00f7d0e2012-07-19 15:28:27 -070078 void MarkRoots()
Ian Rogersb726dcb2012-09-05 08:57:23 -070079 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
80 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -070081
Mathieu Chartier858f1c52012-10-17 17:45:55 -070082 void MarkNonThreadRoots()
83 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
84
Mathieu Chartier9ebae1f2012-10-15 17:38:16 -070085 void MarkConcurrentRoots();
86 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
87
Mathieu Chartier858f1c52012-10-17 17:45:55 -070088 void MarkRootsCheckpoint();
Mathieu Chartier2b82db42012-11-14 17:29:05 -080089 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Mathieu Chartier858f1c52012-10-17 17:45:55 -070090
Mathieu Chartier262e5ff2012-06-01 17:35:38 -070091 // Verify that image roots point to only marked objects within the alloc space.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080092 void VerifyImageRoots()
93 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
94 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier262e5ff2012-06-01 17:35:38 -070095
Carl Shapiro58551df2011-07-24 03:09:51 -070096 // Builds a mark stack and recursively mark until it empties.
Mathieu Chartier2b82db42012-11-14 17:29:05 -080097 void RecursiveMark()
Ian Rogersb726dcb2012-09-05 08:57:23 -070098 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
99 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700100
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800101 // Make a space immune, immune spaces are assumed to have all live objects marked.
102 void ImmuneSpace(ContinuousSpace* space)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800103 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
104 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800105
106 // Bind the live bits to the mark bits of bitmaps based on the gc type.
107 virtual void BindBitmaps()
108 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700109
110 void BindLiveToMarkBitmap(ContinuousSpace* space)
111 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
112
113 void UnBindBitmaps()
114 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Carl Shapiro58551df2011-07-24 03:09:51 -0700115
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700116 // Builds a mark stack with objects on dirty cards and recursively mark until it empties.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800117 void RecursiveMarkDirtyObjects(byte minimum_age)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700118 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
119 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700120
Carl Shapiro69759ea2011-07-21 18:13:35 -0700121 // Remarks the root set after completing the concurrent mark.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700122 void ReMarkRoots()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700123 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
124 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700125
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800126 void ProcessReferences(Thread* self)
127 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro58551df2011-07-24 03:09:51 -0700128
Carl Shapiro69759ea2011-07-21 18:13:35 -0700129 // Sweeps unmarked objects to complete the garbage collection.
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800130 virtual void Sweep(TimingLogger& timings, bool swap_bitmaps)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700131 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700132
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700133 // Sweeps unmarked objects to complete the garbage collection.
134 void SweepLargeObjects(bool swap_bitmaps)
135 EXCLUSIVE_LOCKS_REQUIRED(GlobalSynchronization::heap_bitmap_lock_);
136
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700137 // Sweep only pointers within an array. WARNING: Trashes objects.
Mathieu Chartierd8195f12012-10-05 12:21:28 -0700138 void SweepArray(TimingLogger& logger, ObjectStack* allocation_stack_, bool swap_bitmaps)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700139 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700140
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800141 // Swap bitmaps (if we are a full Gc then we swap the zygote bitmap too).
142 virtual void SwapBitmaps() EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
143 void SwapLargeObjects() EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
144
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800145 mirror::Object* GetClearedReferences() {
Elliott Hughesadb460d2011-10-05 17:02:34 -0700146 return cleared_reference_list_;
147 }
148
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700149 // Proxy for external access to ScanObject.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800150 void ScanRoot(const mirror::Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700151 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
152 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700153
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700154 // Blackens an object.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800155 void ScanObject(const mirror::Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700156 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
157 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700158
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800159 // TODO: enable thread safety analysis when in use by multiple worker threads.
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700160 template <typename MarkVisitor>
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800161 void ScanObjectVisit(const mirror::Object* obj, const MarkVisitor& visitor)
162 NO_THREAD_SAFETY_ANALYSIS;
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700163
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800164 void SetFinger(mirror::Object* new_finger) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700165 finger_ = new_finger;
166 }
167
168 void DisableFinger() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800169 SetFinger(reinterpret_cast<mirror::Object*>(~static_cast<uintptr_t>(0)));
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700170 }
171
172 size_t GetFreedBytes() const {
173 return freed_bytes_;
174 }
175
176 size_t GetFreedObjects() const {
177 return freed_objects_;
178 }
179
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800180 uint64_t GetTotalTime() const {
181 return total_time_;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700182 }
183
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800184 uint64_t GetTotalPausedTime() const {
185 return total_paused_time_;
186 }
187
188 uint64_t GetTotalFreedObjects() const {
189 return total_freed_objects_;
190 }
191
192 uint64_t GetTotalFreedBytes() const {
193 return total_freed_bytes_;
194 }
195
196 // Everything inside the immune range is assumed to be marked.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800197 void SetImmuneRange(mirror::Object* begin, mirror::Object* end);
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800198
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700199 void SweepSystemWeaks()
200 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
201
202 // Only sweep the weaks which are inside of an allocation stack.
203 void SweepSystemWeaksArray(ObjectStack* allocations)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700204 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700205
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800206 static bool VerifyIsLiveCallback(const mirror::Object* obj, void* arg)
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700207 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
208
209 void VerifySystemWeaks()
210 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
211
212 // Verify that an object is live, either in a live bitmap or in the allocation stack.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800213 void VerifyIsLive(const mirror::Object* obj)
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700214 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
215
Mathieu Chartierfd678be2012-08-30 14:50:54 -0700216 template <typename Visitor>
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800217 static void VisitObjectReferences(const mirror::Object* obj, const Visitor& visitor)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700218 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800219 Locks::mutator_lock_);
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700220
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800221 static void MarkObjectCallback(const mirror::Object* root, void* arg)
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800222 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700223 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
224
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800225 static void MarkRootParallelCallback(const mirror::Object* root, void* arg);
Mathieu Chartierac86a7c2012-11-12 15:03:16 -0800226
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700227 // Marks an object.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800228 void MarkObject(const mirror::Object* obj)
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800229 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
230 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
231
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800232 void MarkRoot(const mirror::Object* obj)
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800233 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700234 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
235
236 Barrier& GetBarrier();
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800237 const TimingLogger& GetTimings() const;
238 const CumulativeLogger& GetCumulativeTimings() const;
239 void ResetCumulativeStatistics();
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700240
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800241 protected:
Carl Shapiro69759ea2011-07-21 18:13:35 -0700242 // Returns true if the object has its bit set in the mark bitmap.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800243 bool IsMarked(const mirror::Object* object) const;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700244
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800245 static bool IsMarkedCallback(const mirror::Object* object, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700246 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700247
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800248 static bool IsMarkedArrayCallback(const mirror::Object* object, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700249 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Mathieu Chartier46a23632012-08-07 18:44:40 -0700250
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800251 static void ReMarkObjectVisitor(const mirror::Object* root, void* arg)
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800252 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700253 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700254
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800255 static void VerifyImageRootVisitor(mirror::Object* root, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700256 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_,
257 Locks::mutator_lock_);
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700258
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800259 void MarkObjectNonNull(const mirror::Object* obj, bool check_finger)
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800260 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700261 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700262
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800263 void MarkObjectNonNullParallel(const mirror::Object* obj, bool check_finger);
Mathieu Chartierac86a7c2012-11-12 15:03:16 -0800264
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800265 bool MarkLargeObject(const mirror::Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700266 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700267
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700268 // Returns true if we need to add obj to a mark stack.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800269 bool MarkObjectParallel(const mirror::Object* obj) NO_THREAD_SAFETY_ANALYSIS;
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700270
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800271 static void SweepCallback(size_t num_ptrs, mirror::Object** ptrs, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700272 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Carl Shapiro58551df2011-07-24 03:09:51 -0700273
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700274 // Special sweep for zygote that just marks objects / dirties cards.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800275 static void ZygoteSweepCallback(size_t num_ptrs, mirror::Object** ptrs, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700276 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Ian Rogers5d76c432011-10-31 21:42:49 -0700277
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800278 void CheckReference(const mirror::Object* obj, const mirror::Object* ref, MemberOffset offset,
279 bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700280 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700281
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800282 void CheckObject(const mirror::Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700283 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
Ian Rogers5d76c432011-10-31 21:42:49 -0700284
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700285 // Verify the roots of the heap and print out information related to any invalid roots.
286 // Called in MarkObject, so may we may not hold the mutator lock.
287 void VerifyRoots()
288 NO_THREAD_SAFETY_ANALYSIS;
289
Mathieu Chartierac86a7c2012-11-12 15:03:16 -0800290 // Expand mark stack to 2x its current size. Thread safe.
291 void ExpandMarkStack();
292
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800293 static void VerifyRootCallback(const mirror::Object* root, void* arg, size_t vreg,
Ian Rogers40e3bac2012-11-20 00:09:14 -0800294 const StackVisitor *visitor);
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700295
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800296 void VerifyRoot(const mirror::Object* root, size_t vreg, const StackVisitor* visitor)
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700297 NO_THREAD_SAFETY_ANALYSIS;
298
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700299 template <typename Visitor>
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800300 static void VisitInstanceFieldsReferences(const mirror::Class* klass, const mirror::Object* obj,
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700301 const Visitor& visitor)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800302 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
Ian Rogers5d76c432011-10-31 21:42:49 -0700303
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700304 // Visit the header, static field references, and interface pointers of a class object.
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700305 template <typename Visitor>
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800306 static void VisitClassReferences(const mirror::Class* klass, const mirror::Object* obj,
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700307 const Visitor& visitor)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800308 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
Ian Rogers5d76c432011-10-31 21:42:49 -0700309
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700310 template <typename Visitor>
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800311 static void VisitStaticFieldsReferences(const mirror::Class* klass, const Visitor& visitor)
312 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
Ian Rogers5d76c432011-10-31 21:42:49 -0700313
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700314 template <typename Visitor>
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800315 static void VisitFieldsReferences(const mirror::Object* obj, uint32_t ref_offsets, bool is_static,
316 const Visitor& visitor)
317 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
Ian Rogers5d76c432011-10-31 21:42:49 -0700318
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700319 // Visit all of the references in an object array.
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700320 template <typename Visitor>
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800321 static void VisitObjectArrayReferences(const mirror::ObjectArray<mirror::Object>* array,
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700322 const Visitor& visitor)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800323 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
Ian Rogers5d76c432011-10-31 21:42:49 -0700324
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700325 // Visits the header and field references of a data object.
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700326 template <typename Visitor>
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800327 static void VisitOtherReferences(const mirror::Class* klass, const mirror::Object* obj,
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700328 const Visitor& visitor)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700329 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_) {
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700330 return VisitInstanceFieldsReferences(klass, obj, visitor);
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700331 }
Ian Rogers5d76c432011-10-31 21:42:49 -0700332
Carl Shapiro69759ea2011-07-21 18:13:35 -0700333 // Blackens objects grayed during a garbage collection.
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800334 void ScanGrayObjects(byte minimum_age)
335 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
336 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700337
338 // Schedules an unmarked object for reference processing.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800339 void DelayReferenceReferent(mirror::Object* reference)
Ian Rogers23435d02012-09-24 11:23:12 -0700340 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700341
342 // Recursively blackens objects on the mark stack.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700343 void ProcessMarkStack()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700344 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
345 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700346
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700347 void ProcessMarkStackParallel()
348 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
349 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
350
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800351 void EnqueueFinalizerReferences(mirror::Object** ref)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700352 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
353 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700354
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800355 void PreserveSomeSoftReferences(mirror::Object** ref)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700356 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
357 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700358
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800359 void ClearWhiteReferences(mirror::Object** list)
Ian Rogers23435d02012-09-24 11:23:12 -0700360 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700361
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800362 void ProcessReferences(mirror::Object** soft_references, bool clear_soft_references,
363 mirror::Object** weak_references,
364 mirror::Object** finalizer_references,
365 mirror::Object** phantom_references)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700366 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
367 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700368
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800369 void SweepJniWeakGlobals(IsMarkedTester is_marked, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700370 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Carl Shapiro58551df2011-07-24 03:09:51 -0700371
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700372 // Whether or not we count how many of each type of object were scanned.
373 static const bool kCountScannedTypes = false;
374
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700375 // Current space, we check this space first to avoid searching for the appropriate space for an object.
376 SpaceBitmap* current_mark_bitmap_;
377
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700378 // Cache java.lang.Class for optimization.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800379 mirror::Class* java_lang_Class_;
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700380
Mathieu Chartierd8195f12012-10-05 12:21:28 -0700381 ObjectStack* mark_stack_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700382
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800383 mirror::Object* finger_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700384
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700385 // Immune range, every object inside the immune range is assumed to be marked.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800386 mirror::Object* immune_begin_;
387 mirror::Object* immune_end_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700388
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800389 mirror::Object* soft_reference_list_;
390 mirror::Object* weak_reference_list_;
391 mirror::Object* finalizer_reference_list_;
392 mirror::Object* phantom_reference_list_;
393 mirror::Object* cleared_reference_list_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700394
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700395 AtomicInteger freed_bytes_;
396 AtomicInteger freed_objects_;
397 AtomicInteger class_count_;
398 AtomicInteger array_count_;
399 AtomicInteger other_count_;
400 AtomicInteger large_object_test_;
401 AtomicInteger large_object_mark_;
402 AtomicInteger classes_marked_;
403 AtomicInteger overhead_time_;
404 AtomicInteger work_chunks_created_;
405 AtomicInteger work_chunks_deleted_;
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800406 AtomicInteger reference_count_;
Elliott Hughes352a4242011-10-31 15:15:21 -0700407
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800408 // Cumulative statistics.
409 uint64_t total_time_;
410 uint64_t total_paused_time_;
411 uint64_t total_freed_objects_;
412 uint64_t total_freed_bytes_;
413
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700414 UniquePtr<Barrier> gc_barrier_;
Mathieu Chartierac86a7c2012-11-12 15:03:16 -0800415 Mutex large_object_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
416 Mutex mark_stack_expand_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800417 TimingLogger timings_;
418 CumulativeLogger cumulative_timings_;
419
420 bool is_concurrent_;
421 bool clear_soft_references_;
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700422
Mathieu Chartiere6e06512012-06-26 15:00:26 -0700423 friend class AddIfReachesAllocSpaceVisitor; // Used by mod-union table.
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700424 friend class CheckBitmapVisitor;
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700425 friend class CheckObjectVisitor;
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700426 friend class CheckReferenceVisitor;
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800427 friend class Heap;
Elliott Hughes410c0c82011-09-01 17:58:25 -0700428 friend class InternTableEntryIsUnmarked;
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700429 friend class MarkIfReachesAllocspaceVisitor;
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700430 friend class ModUnionCheckReferences;
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700431 friend class ModUnionClearCardVisitor;
Mathieu Chartiere6e06512012-06-26 15:00:26 -0700432 friend class ModUnionReferenceVisitor;
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700433 friend class ModUnionVisitor;
434 friend class ModUnionTableBitmap;
Mathieu Chartiere6e06512012-06-26 15:00:26 -0700435 friend class ModUnionTableReferenceCache;
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700436 friend class ModUnionScanImageRootVisitor;
437 friend class ScanBitmapVisitor;
438 friend class ScanImageRootVisitor;
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700439 friend class MarkStackChunk;
440 friend class FifoMarkStackChunk;
Elliott Hughes410c0c82011-09-01 17:58:25 -0700441
Carl Shapiro69759ea2011-07-21 18:13:35 -0700442 DISALLOW_COPY_AND_ASSIGN(MarkSweep);
443};
444
445} // namespace art
446
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800447#endif // ART_SRC_GC_MARK_SWEEP_H_