blob: 35faff37748f888c836cbb2fccb2538956321271 [file] [log] [blame]
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_GC_ACCOUNTING_SPACE_BITMAP_H_
18#define ART_RUNTIME_GC_ACCOUNTING_SPACE_BITMAP_H_
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080019
Ian Rogers700a4022014-05-19 16:49:03 -070020#include <limits.h>
21#include <stdint.h>
22#include <memory>
23#include <set>
24#include <vector>
25
Ian Rogers719d1a32014-03-06 12:13:39 -080026#include "base/mutex.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080027#include "globals.h"
Mathieu Chartier83c8ee02014-01-28 14:50:23 -080028#include "object_callbacks.h"
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070029
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070030namespace 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 Object;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080034} // namespace mirror
Ian Rogers576ca0c2014-06-06 15:58:22 -070035class MemMap;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070036
Ian Rogers1d54e732013-05-02 21:10:01 -070037namespace gc {
38namespace accounting {
39
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070040template<size_t kAlignment>
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070041class SpaceBitmap {
42 public:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080043 typedef void ScanCallback(mirror::Object* obj, void* finger, void* arg);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080044 typedef void SweepCallback(size_t ptr_count, mirror::Object** ptrs, void* arg);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070045
Mathieu Chartier31e89252013-08-28 11:29:12 -070046 // Initialize a space bitmap so that it points to a bitmap large enough to cover a heap at
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070047 // heap_begin of heap_capacity bytes, where objects are guaranteed to be kAlignment-aligned.
Ian Rogers13735952014-10-08 12:43:28 -070048 static SpaceBitmap* Create(const std::string& name, uint8_t* heap_begin, size_t heap_capacity);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070049
Mathieu Chartier31e89252013-08-28 11:29:12 -070050 // Initialize a space bitmap using the provided mem_map as the live bits. Takes ownership of the
51 // mem map. The address range covered starts at heap_begin and is of size equal to heap_capacity.
52 // Objects are kAlignement-aligned.
53 static SpaceBitmap* CreateFromMemMap(const std::string& name, MemMap* mem_map,
Ian Rogers13735952014-10-08 12:43:28 -070054 uint8_t* heap_begin, size_t heap_capacity);
Mathieu Chartier31e89252013-08-28 11:29:12 -070055
Ian Rogers22d5e732014-07-15 22:23:51 -070056 ~SpaceBitmap();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070057
58 // <offset> is the difference from .base to a pointer address.
59 // <index> is the index of .bits that contains the bit representing
60 // <offset>.
Ian Rogersbe2a1df2014-07-10 00:56:36 -070061 static constexpr size_t OffsetToIndex(size_t offset) {
Ian Rogers13735952014-10-08 12:43:28 -070062 return offset / kAlignment / kBitsPerIntPtrT;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070063 }
64
Mathieu Chartier6f365cc2014-04-23 12:42:27 -070065 template<typename T>
Ian Rogersbe2a1df2014-07-10 00:56:36 -070066 static constexpr T IndexToOffset(T index) {
Ian Rogers13735952014-10-08 12:43:28 -070067 return static_cast<T>(index * kAlignment * kBitsPerIntPtrT);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070068 }
69
Andreas Gampecb8aea42014-04-02 15:39:58 -070070 // Bits are packed in the obvious way.
Ian Rogers13735952014-10-08 12:43:28 -070071 static constexpr uintptr_t OffsetToMask(uintptr_t offset) {
72 return (static_cast<size_t>(1)) << ((offset / kAlignment) % kBitsPerIntPtrT);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070073 }
74
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070075 bool Set(const mirror::Object* obj) ALWAYS_INLINE {
76 return Modify<true>(obj);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070077 }
78
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070079 bool Clear(const mirror::Object* obj) ALWAYS_INLINE {
80 return Modify<false>(obj);
Mathieu Chartier02b6a782012-10-26 13:51:26 -070081 }
82
83 // Returns true if the object was previously marked.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080084 bool AtomicTestAndSet(const mirror::Object* obj);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070085
Ian Rogers1d54e732013-05-02 21:10:01 -070086 // Fill the bitmap with zeroes. Returns the bitmap's memory to the system as a side-effect.
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070087 void Clear();
88
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080089 bool Test(const mirror::Object* obj) const;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070090
Ian Rogers506de0c2012-09-17 15:39:06 -070091 // Return true iff <obj> is within the range of pointers that this bitmap could potentially cover,
92 // even if a bit has not been set for it.
93 bool HasAddress(const void* obj) const {
94 // If obj < heap_begin_ then offset underflows to some very large value past the end of the
95 // bitmap.
buzbeecbd6d442012-11-17 14:11:25 -080096 const uintptr_t offset = reinterpret_cast<uintptr_t>(obj) - heap_begin_;
Ian Rogers506de0c2012-09-17 15:39:06 -070097 const size_t index = OffsetToIndex(offset);
Ian Rogers13735952014-10-08 12:43:28 -070098 return index < bitmap_size_ / sizeof(intptr_t);
Ian Rogers506de0c2012-09-17 15:39:06 -070099 }
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700100
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800101 void VisitRange(uintptr_t base, uintptr_t max, ObjectCallback* callback, void* arg) const;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700102
103 class ClearVisitor {
104 public:
105 explicit ClearVisitor(SpaceBitmap* const bitmap)
106 : bitmap_(bitmap) {
107 }
108
Brian Carlstromdf629502013-07-17 22:39:56 -0700109 void operator()(mirror::Object* obj) const {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700110 bitmap_->Clear(obj);
111 }
112 private:
113 SpaceBitmap* const bitmap_;
114 };
115
116 template <typename Visitor>
117 void VisitRange(uintptr_t visit_begin, uintptr_t visit_end, const Visitor& visitor) const {
Brian Carlstromdf629502013-07-17 22:39:56 -0700118 for (; visit_begin < visit_end; visit_begin += kAlignment) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800119 visitor(reinterpret_cast<mirror::Object*>(visit_begin));
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700120 }
121 }
122
Mathieu Chartiere9ea70b2014-04-14 15:52:08 -0700123 // Visit the live objects in the range [visit_begin, visit_end).
124 // TODO: Use lock annotations when clang is fixed.
125 // EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier184e3222013-08-03 14:02:57 -0700126 template <typename Visitor>
127 void VisitMarkedRange(uintptr_t visit_begin, uintptr_t visit_end, const Visitor& visitor) const
Mathieu Chartiere9ea70b2014-04-14 15:52:08 -0700128 NO_THREAD_SAFETY_ANALYSIS;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700129
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700130 // Visits set bits in address order. The callback is not permitted to change the bitmap bits or
131 // max during the traversal.
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800132 void Walk(ObjectCallback* callback, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700133 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700134
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700135 // Visits set bits with an in order traversal. The callback is not permitted to change the bitmap
136 // bits or max during the traversal.
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800137 void InOrderWalk(ObjectCallback* callback, void* arg)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800138 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700139
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700140 // Walk through the bitmaps in increasing address order, and find the object pointers that
141 // correspond to garbage objects. Call <callback> zero or more times with lists of these object
142 // pointers. The callback is not permitted to increase the max of either bitmap.
Mathieu Chartier184e3222013-08-03 14:02:57 -0700143 static void SweepWalk(const SpaceBitmap& live, const SpaceBitmap& mark, uintptr_t base,
144 uintptr_t max, SweepCallback* thunk, void* arg);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700145
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700146 void CopyFrom(SpaceBitmap* source_bitmap);
147
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700148 // Starting address of our internal storage.
Ian Rogers13735952014-10-08 12:43:28 -0700149 uintptr_t* Begin() {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700150 return bitmap_begin_;
151 }
152
153 // Size of our internal storage
154 size_t Size() const {
155 return bitmap_size_;
156 }
157
158 // Size in bytes of the memory that the bitmaps spans.
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700159 uint64_t HeapSize() const {
Ian Rogers13735952014-10-08 12:43:28 -0700160 return IndexToOffset<uint64_t>(Size() / sizeof(intptr_t));
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700161 }
162
Mathieu Chartier379d09f2015-01-08 11:28:13 -0800163 void SetHeapSize(size_t bytes) {
164 // TODO: Un-map the end of the mem map.
165 bitmap_size_ = OffsetToIndex(bytes) * sizeof(intptr_t);
166 CHECK_EQ(HeapSize(), bytes);
167 }
168
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700169 uintptr_t HeapBegin() const {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700170 return heap_begin_;
171 }
172
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700173 // The maximum address which the bitmap can span. (HeapBegin() <= object < HeapLimit()).
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700174 uint64_t HeapLimit() const {
175 return static_cast<uint64_t>(HeapBegin()) + HeapSize();
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700176 }
177
178 // Set the max address which can covered by the bitmap.
179 void SetHeapLimit(uintptr_t new_end);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700180
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700181 std::string GetName() const {
182 return name_;
183 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700184
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700185 void SetName(const std::string& name) {
186 name_ = name;
187 }
188
Ian Rogers576ca0c2014-06-06 15:58:22 -0700189 std::string Dump() const;
Ian Rogers1d54e732013-05-02 21:10:01 -0700190
Mathieu Chartierd39645e2015-06-09 17:50:29 -0700191 // Helper function for computing bitmap size based on a 64 bit capacity.
192 static size_t ComputeBitmapSize(uint64_t capacity);
193 static size_t ComputeHeapSize(uint64_t bitmap_bytes);
194
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700195 private:
196 // TODO: heap_end_ is initialized so that the heap bitmap is empty, this doesn't require the -1,
197 // however, we document that this is expected on heap_end_
Ian Rogers13735952014-10-08 12:43:28 -0700198 SpaceBitmap(const std::string& name, MemMap* mem_map, uintptr_t* bitmap_begin, size_t bitmap_size,
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700199 const void* heap_begin);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700200
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700201 template<bool kSetBit>
202 bool Modify(const mirror::Object* obj);
203
204 // For an unvisited object, visit it then all its children found via fields.
205 static void WalkFieldsInOrder(SpaceBitmap* visited, ObjectCallback* callback, mirror::Object* obj,
206 void* arg) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
207 // Walk instance fields of the given Class. Separate function to allow recursion on the super
208 // class.
209 static void WalkInstanceFields(SpaceBitmap<kAlignment>* visited, ObjectCallback* callback,
210 mirror::Object* obj, mirror::Class* klass, void* arg)
211 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700212
213 // Backing storage for bitmap.
Ian Rogers700a4022014-05-19 16:49:03 -0700214 std::unique_ptr<MemMap> mem_map_;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700215
216 // This bitmap itself, word sized for efficiency in scanning.
Ian Rogers13735952014-10-08 12:43:28 -0700217 uintptr_t* const bitmap_begin_;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700218
219 // Size of this bitmap.
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700220 size_t bitmap_size_;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700221
222 // The base address of the heap, which corresponds to the word containing the first bit in the
223 // bitmap.
224 const uintptr_t heap_begin_;
225
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700226 // Name of this bitmap.
227 std::string name_;
228};
229
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700230typedef SpaceBitmap<kObjectAlignment> ContinuousSpaceBitmap;
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700231typedef SpaceBitmap<kLargeObjectAlignment> LargeObjectBitmap;
232
233template<size_t kAlignment>
234std::ostream& operator << (std::ostream& stream, const SpaceBitmap<kAlignment>& bitmap);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700235
Ian Rogers1d54e732013-05-02 21:10:01 -0700236} // namespace accounting
237} // namespace gc
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700238} // namespace art
239
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700240#endif // ART_RUNTIME_GC_ACCOUNTING_SPACE_BITMAP_H_