blob: 859f309ce5e0e339cf46fe473f3fcdab5a8ec4d9 [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"
Sameer Abu Asala8439542013-02-14 16:06:42 -080023#include "base/timing_logger.h"
Mathieu Chartier2b82db42012-11-14 17:29:05 -080024#include "garbage_collector.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080025#include "gc_type.h"
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070026#include "offsets.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080027#include "root_visitor.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028#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
Sameer Abu Asala8439542013-02-14 16:06:42 -0800236 Barrier& GetBarrier() {
237 return *gc_barrier_;
238 }
239
240 TimingLogger& GetTimings() {
241 return timings_;
242 }
243
244 CumulativeLogger& GetCumulativeTimings() {
245 return cumulative_timings_;
246 }
247
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800248 void ResetCumulativeStatistics();
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700249
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800250 protected:
Carl Shapiro69759ea2011-07-21 18:13:35 -0700251 // Returns true if the object has its bit set in the mark bitmap.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800252 bool IsMarked(const mirror::Object* object) const;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700253
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800254 static bool IsMarkedCallback(const mirror::Object* object, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700255 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700256
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800257 static bool IsMarkedArrayCallback(const mirror::Object* object, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700258 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Mathieu Chartier46a23632012-08-07 18:44:40 -0700259
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800260 static void ReMarkObjectVisitor(const mirror::Object* root, void* arg)
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800261 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700262 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700263
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800264 static void VerifyImageRootVisitor(mirror::Object* root, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700265 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_,
266 Locks::mutator_lock_);
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700267
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800268 void MarkObjectNonNull(const mirror::Object* obj, bool check_finger)
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800269 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700270 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700271
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800272 void MarkObjectNonNullParallel(const mirror::Object* obj, bool check_finger);
Mathieu Chartierac86a7c2012-11-12 15:03:16 -0800273
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800274 bool MarkLargeObject(const mirror::Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700275 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700276
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700277 // Returns true if we need to add obj to a mark stack.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800278 bool MarkObjectParallel(const mirror::Object* obj) NO_THREAD_SAFETY_ANALYSIS;
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700279
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800280 static void SweepCallback(size_t num_ptrs, mirror::Object** ptrs, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700281 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Carl Shapiro58551df2011-07-24 03:09:51 -0700282
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700283 // Special sweep for zygote that just marks objects / dirties cards.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800284 static void ZygoteSweepCallback(size_t num_ptrs, mirror::Object** ptrs, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700285 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Ian Rogers5d76c432011-10-31 21:42:49 -0700286
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800287 void CheckReference(const mirror::Object* obj, const mirror::Object* ref, MemberOffset offset,
288 bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700289 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700290
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800291 void CheckObject(const mirror::Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700292 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
Ian Rogers5d76c432011-10-31 21:42:49 -0700293
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700294 // Verify the roots of the heap and print out information related to any invalid roots.
295 // Called in MarkObject, so may we may not hold the mutator lock.
296 void VerifyRoots()
297 NO_THREAD_SAFETY_ANALYSIS;
298
Mathieu Chartierac86a7c2012-11-12 15:03:16 -0800299 // Expand mark stack to 2x its current size. Thread safe.
300 void ExpandMarkStack();
301
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800302 static void VerifyRootCallback(const mirror::Object* root, void* arg, size_t vreg,
Ian Rogers40e3bac2012-11-20 00:09:14 -0800303 const StackVisitor *visitor);
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700304
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800305 void VerifyRoot(const mirror::Object* root, size_t vreg, const StackVisitor* visitor)
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700306 NO_THREAD_SAFETY_ANALYSIS;
307
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700308 template <typename Visitor>
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800309 static void VisitInstanceFieldsReferences(const mirror::Class* klass, const mirror::Object* obj,
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700310 const Visitor& visitor)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800311 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
Ian Rogers5d76c432011-10-31 21:42:49 -0700312
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700313 // Visit the header, static field references, and interface pointers of a class object.
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700314 template <typename Visitor>
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800315 static void VisitClassReferences(const mirror::Class* klass, const mirror::Object* obj,
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700316 const Visitor& visitor)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800317 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
Ian Rogers5d76c432011-10-31 21:42:49 -0700318
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700319 template <typename Visitor>
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800320 static void VisitStaticFieldsReferences(const mirror::Class* klass, const Visitor& visitor)
321 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
Ian Rogers5d76c432011-10-31 21:42:49 -0700322
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700323 template <typename Visitor>
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800324 static void VisitFieldsReferences(const mirror::Object* obj, uint32_t ref_offsets, bool is_static,
325 const Visitor& visitor)
326 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
Ian Rogers5d76c432011-10-31 21:42:49 -0700327
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700328 // Visit all of the references in an object array.
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700329 template <typename Visitor>
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800330 static void VisitObjectArrayReferences(const mirror::ObjectArray<mirror::Object>* array,
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700331 const Visitor& visitor)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800332 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
Ian Rogers5d76c432011-10-31 21:42:49 -0700333
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700334 // Visits the header and field references of a data object.
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700335 template <typename Visitor>
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800336 static void VisitOtherReferences(const mirror::Class* klass, const mirror::Object* obj,
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700337 const Visitor& visitor)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700338 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_) {
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700339 return VisitInstanceFieldsReferences(klass, obj, visitor);
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700340 }
Ian Rogers5d76c432011-10-31 21:42:49 -0700341
Carl Shapiro69759ea2011-07-21 18:13:35 -0700342 // Blackens objects grayed during a garbage collection.
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800343 void ScanGrayObjects(byte minimum_age)
344 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
345 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700346
347 // Schedules an unmarked object for reference processing.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800348 void DelayReferenceReferent(mirror::Object* reference)
Ian Rogers23435d02012-09-24 11:23:12 -0700349 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700350
351 // Recursively blackens objects on the mark stack.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700352 void ProcessMarkStack()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700353 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
354 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700355
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700356 void ProcessMarkStackParallel()
357 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
358 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
359
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800360 void EnqueueFinalizerReferences(mirror::Object** ref)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700361 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
362 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700363
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800364 void PreserveSomeSoftReferences(mirror::Object** ref)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700365 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
366 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700367
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800368 void ClearWhiteReferences(mirror::Object** list)
Ian Rogers23435d02012-09-24 11:23:12 -0700369 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700370
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800371 void ProcessReferences(mirror::Object** soft_references, bool clear_soft_references,
372 mirror::Object** weak_references,
373 mirror::Object** finalizer_references,
374 mirror::Object** phantom_references)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700375 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
376 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700377
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800378 void SweepJniWeakGlobals(IsMarkedTester is_marked, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700379 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Carl Shapiro58551df2011-07-24 03:09:51 -0700380
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700381 // Whether or not we count how many of each type of object were scanned.
382 static const bool kCountScannedTypes = false;
383
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700384 // Current space, we check this space first to avoid searching for the appropriate space for an object.
385 SpaceBitmap* current_mark_bitmap_;
386
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700387 // Cache java.lang.Class for optimization.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800388 mirror::Class* java_lang_Class_;
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700389
Mathieu Chartierd8195f12012-10-05 12:21:28 -0700390 ObjectStack* mark_stack_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700391
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800392 mirror::Object* finger_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700393
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700394 // Immune range, every object inside the immune range is assumed to be marked.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800395 mirror::Object* immune_begin_;
396 mirror::Object* immune_end_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700397
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800398 mirror::Object* soft_reference_list_;
399 mirror::Object* weak_reference_list_;
400 mirror::Object* finalizer_reference_list_;
401 mirror::Object* phantom_reference_list_;
402 mirror::Object* cleared_reference_list_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700403
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700404 AtomicInteger freed_bytes_;
405 AtomicInteger freed_objects_;
406 AtomicInteger class_count_;
407 AtomicInteger array_count_;
408 AtomicInteger other_count_;
409 AtomicInteger large_object_test_;
410 AtomicInteger large_object_mark_;
411 AtomicInteger classes_marked_;
412 AtomicInteger overhead_time_;
413 AtomicInteger work_chunks_created_;
414 AtomicInteger work_chunks_deleted_;
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800415 AtomicInteger reference_count_;
Elliott Hughes352a4242011-10-31 15:15:21 -0700416
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800417 // Cumulative statistics.
418 uint64_t total_time_;
419 uint64_t total_paused_time_;
420 uint64_t total_freed_objects_;
421 uint64_t total_freed_bytes_;
422
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700423 UniquePtr<Barrier> gc_barrier_;
Mathieu Chartierac86a7c2012-11-12 15:03:16 -0800424 Mutex large_object_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
425 Mutex mark_stack_expand_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800426 TimingLogger timings_;
427 CumulativeLogger cumulative_timings_;
428
429 bool is_concurrent_;
430 bool clear_soft_references_;
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700431
Mathieu Chartiere6e06512012-06-26 15:00:26 -0700432 friend class AddIfReachesAllocSpaceVisitor; // Used by mod-union table.
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700433 friend class CheckBitmapVisitor;
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700434 friend class CheckObjectVisitor;
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700435 friend class CheckReferenceVisitor;
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800436 friend class Heap;
Elliott Hughes410c0c82011-09-01 17:58:25 -0700437 friend class InternTableEntryIsUnmarked;
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700438 friend class MarkIfReachesAllocspaceVisitor;
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700439 friend class ModUnionCheckReferences;
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700440 friend class ModUnionClearCardVisitor;
Mathieu Chartiere6e06512012-06-26 15:00:26 -0700441 friend class ModUnionReferenceVisitor;
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700442 friend class ModUnionVisitor;
443 friend class ModUnionTableBitmap;
Mathieu Chartiere6e06512012-06-26 15:00:26 -0700444 friend class ModUnionTableReferenceCache;
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700445 friend class ModUnionScanImageRootVisitor;
446 friend class ScanBitmapVisitor;
447 friend class ScanImageRootVisitor;
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700448 friend class MarkStackChunk;
449 friend class FifoMarkStackChunk;
Elliott Hughes410c0c82011-09-01 17:58:25 -0700450
Carl Shapiro69759ea2011-07-21 18:13:35 -0700451 DISALLOW_COPY_AND_ASSIGN(MarkSweep);
452};
453
454} // namespace art
455
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800456#endif // ART_SRC_GC_MARK_SWEEP_H_