blob: 273ae4f58f4f7e8257f821d6b036362126425020 [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
17#include "heap_bitmap.h"
18
19#include "logging.h"
20#include "UniquePtr.h"
21#include "utils.h"
22
23namespace art {
24
Mathieu Chartier357e9be2012-08-01 11:00:14 -070025std::string SpaceBitmap::GetName() const {
26 return name_;
27}
28
29void SpaceBitmap::SetName(const std::string& name) {
30 name_ = name;
31}
32
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -070033void SpaceSetMap::Walk(SpaceBitmap::Callback* callback, void* arg) {
34 for (Objects::iterator it = contained_.begin(); it != contained_.end(); ++it) {
35 callback(const_cast<Object*>(*it), arg);
36 }
37}
38
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070039SpaceBitmap* SpaceBitmap::Create(const std::string& name, byte* heap_begin, size_t heap_capacity) {
40 CHECK(heap_begin != NULL);
41 // Round up since heap_capacity is not necessarily a multiple of kAlignment * kBitsPerWord.
42 size_t bitmap_size = OffsetToIndex(RoundUp(heap_capacity, kAlignment * kBitsPerWord)) * kWordSize;
43 UniquePtr<MemMap> mem_map(MemMap::MapAnonymous(name.c_str(), NULL, bitmap_size, PROT_READ | PROT_WRITE));
44 if (mem_map.get() == NULL) {
45 LOG(ERROR) << "Failed to allocate bitmap " << name;
46 return NULL;
47 }
48 word* bitmap_begin = reinterpret_cast<word*>(mem_map->Begin());
49 return new SpaceBitmap(name, mem_map.release(), bitmap_begin, bitmap_size, heap_begin);
50}
51
52// Clean up any resources associated with the bitmap.
Mathieu Chartier7469ebf2012-09-24 16:28:36 -070053SpaceBitmap::~SpaceBitmap() {
54
55}
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070056
Mathieu Chartierdcf8d722012-08-02 14:55:54 -070057void SpaceBitmap::SetHeapLimit(uintptr_t new_end) {
58 DCHECK(IsAligned<kBitsPerWord * kAlignment>(new_end));
59 size_t new_size = OffsetToIndex(new_end - heap_begin_) * kWordSize;
Mathieu Chartiercc236d72012-07-20 10:29:05 -070060 if (new_size < bitmap_size_) {
61 bitmap_size_ = new_size;
62 }
63 // Not sure if doing this trim is necessary, since nothing past the end of the heap capacity
64 // should be marked.
65 // TODO: Fix this code is, it broken and causes rare heap corruption!
66 // mem_map_->Trim(reinterpret_cast<byte*>(heap_begin_ + bitmap_size_));
67}
68
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070069// Fill the bitmap with zeroes. Returns the bitmap's memory to the
70// system as a side-effect.
71void SpaceBitmap::Clear() {
72 if (bitmap_begin_ != NULL) {
73 // This returns the memory to the system. Successive page faults
74 // will return zeroed memory.
75 int result = madvise(bitmap_begin_, bitmap_size_, MADV_DONTNEED);
76 if (result == -1) {
Mathieu Chartier7469ebf2012-09-24 16:28:36 -070077 PLOG(FATAL) << "madvise failed";
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070078 }
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070079 }
80}
81
Mathieu Chartier357e9be2012-08-01 11:00:14 -070082void SpaceBitmap::CopyFrom(SpaceBitmap* source_bitmap) {
83 DCHECK_EQ(Size(), source_bitmap->Size());
84 std::copy(source_bitmap->Begin(), source_bitmap->Begin() + source_bitmap->Size() / kWordSize, Begin());
85}
86
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070087// Visits set bits in address order. The callback is not permitted to
88// change the bitmap bits or max during the traversal.
89void SpaceBitmap::Walk(SpaceBitmap::Callback* callback, void* arg) {
90 CHECK(bitmap_begin_ != NULL);
91 CHECK(callback != NULL);
Mathieu Chartier357e9be2012-08-01 11:00:14 -070092
93 uintptr_t end = OffsetToIndex(HeapLimit() - heap_begin_ - 1);
94 word* bitmap_begin = bitmap_begin_;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070095 for (uintptr_t i = 0; i <= end; ++i) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -070096 word w = bitmap_begin[i];
97 if (w != 0) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070098 uintptr_t ptr_base = IndexToOffset(i) + heap_begin_;
Mathieu Chartier357e9be2012-08-01 11:00:14 -070099 do {
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700100 const size_t shift = CLZ(w);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700101 Object* obj = reinterpret_cast<Object*>(ptr_base + shift * kAlignment);
102 (*callback)(obj, arg);
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700103 w ^= static_cast<size_t>(kWordHighBitMask) >> shift;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700104 } while (w != 0);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700105 }
106 }
107}
108
109// Walk through the bitmaps in increasing address order, and find the
110// object pointers that correspond to garbage objects. Call
111// <callback> zero or more times with lists of these object pointers.
112//
113// The callback is not permitted to increase the max of either bitmap.
114void SpaceBitmap::SweepWalk(const SpaceBitmap& live_bitmap,
115 const SpaceBitmap& mark_bitmap,
116 uintptr_t sweep_begin, uintptr_t sweep_end,
117 SpaceBitmap::SweepCallback* callback, void* arg) {
118 CHECK(live_bitmap.bitmap_begin_ != NULL);
119 CHECK(mark_bitmap.bitmap_begin_ != NULL);
120 CHECK_EQ(live_bitmap.heap_begin_, mark_bitmap.heap_begin_);
121 CHECK_EQ(live_bitmap.bitmap_size_, mark_bitmap.bitmap_size_);
122 CHECK(callback != NULL);
123 CHECK_LE(sweep_begin, sweep_end);
124 CHECK_GE(sweep_begin, live_bitmap.heap_begin_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700125
126 if (sweep_end <= sweep_begin) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700127 return;
128 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700129
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700130 // TODO: rewrite the callbacks to accept a std::vector<Object*> rather than a Object**?
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700131 const size_t buffer_size = kWordSize * kBitsPerWord;
132 Object* pointer_buf[buffer_size];
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700133 Object** pb = &pointer_buf[0];
134 size_t start = OffsetToIndex(sweep_begin - live_bitmap.heap_begin_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700135 size_t end = OffsetToIndex(sweep_end - live_bitmap.heap_begin_ - 1);
136 CHECK_LT(end, live_bitmap.Size() / kWordSize);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700137 word* live = live_bitmap.bitmap_begin_;
138 word* mark = mark_bitmap.bitmap_begin_;
139 for (size_t i = start; i <= end; i++) {
140 word garbage = live[i] & ~mark[i];
141 if (UNLIKELY(garbage != 0)) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700142 uintptr_t ptr_base = IndexToOffset(i) + live_bitmap.heap_begin_;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700143 do {
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700144 const size_t shift = CLZ(garbage);
145 garbage ^= static_cast<size_t>(kWordHighBitMask) >> shift;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700146 *pb++ = reinterpret_cast<Object*>(ptr_base + shift * kAlignment);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700147 } while (garbage != 0);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700148 // Make sure that there are always enough slots available for an
149 // entire word of one bits.
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700150 if (pb >= &pointer_buf[buffer_size - kBitsPerWord]) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700151 (*callback)(pb - &pointer_buf[0], &pointer_buf[0], arg);
152 pb = &pointer_buf[0];
153 }
154 }
155 }
156 if (pb > &pointer_buf[0]) {
157 (*callback)(pb - &pointer_buf[0], &pointer_buf[0], arg);
158 }
159}
160
161} // namespace art
162
163// Support needed for in order traversal
164#include "object.h"
165#include "object_utils.h"
166
167namespace art {
168
169static void WalkFieldsInOrder(SpaceBitmap* visited, SpaceBitmap::Callback* callback, Object* obj,
170 void* arg);
171
172// Walk instance fields of the given Class. Separate function to allow recursion on the super
173// class.
174static void WalkInstanceFields(SpaceBitmap* visited, SpaceBitmap::Callback* callback, Object* obj,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700175 Class* klass, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700176 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700177 // Visit fields of parent classes first.
178 Class* super = klass->GetSuperClass();
179 if (super != NULL) {
180 WalkInstanceFields(visited, callback, obj, super, arg);
181 }
182 // Walk instance fields
183 ObjectArray<Field>* fields = klass->GetIFields();
184 if (fields != NULL) {
185 for (int32_t i = 0; i < fields->GetLength(); i++) {
186 Field* field = fields->Get(i);
187 FieldHelper fh(field);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700188 if (!fh.IsPrimitiveType()) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700189 Object* value = field->GetObj(obj);
190 if (value != NULL) {
191 WalkFieldsInOrder(visited, callback, value, arg);
192 }
193 }
194 }
195 }
196}
197
198// For an unvisited object, visit it then all its children found via fields.
199static void WalkFieldsInOrder(SpaceBitmap* visited, SpaceBitmap::Callback* callback, Object* obj,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700200 void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700201 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700202 if (visited->Test(obj)) {
203 return;
204 }
205 // visit the object itself
206 (*callback)(obj, arg);
207 visited->Set(obj);
208 // Walk instance fields of all objects
209 Class* klass = obj->GetClass();
210 WalkInstanceFields(visited, callback, obj, klass, arg);
211 // Walk static fields of a Class
212 if (obj->IsClass()) {
213 ObjectArray<Field>* fields = klass->GetSFields();
214 if (fields != NULL) {
215 for (int32_t i = 0; i < fields->GetLength(); i++) {
216 Field* field = fields->Get(i);
217 FieldHelper fh(field);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700218 if (!fh.IsPrimitiveType()) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700219 Object* value = field->GetObj(NULL);
220 if (value != NULL) {
221 WalkFieldsInOrder(visited, callback, value, arg);
222 }
223 }
224 }
225 }
226 } else if (obj->IsObjectArray()) {
227 // Walk elements of an object array
228 ObjectArray<Object>* obj_array = obj->AsObjectArray<Object>();
229 int32_t length = obj_array->GetLength();
230 for (int32_t i = 0; i < length; i++) {
231 Object* value = obj_array->Get(i);
232 if (value != NULL) {
233 WalkFieldsInOrder(visited, callback, value, arg);
234 }
235 }
236 }
237}
238
239// Visits set bits with an in order traversal. The callback is not permitted to change the bitmap
240// bits or max during the traversal.
241void SpaceBitmap::InOrderWalk(SpaceBitmap::Callback* callback, void* arg) {
242 UniquePtr<SpaceBitmap> visited(Create("bitmap for in-order walk",
243 reinterpret_cast<byte*>(heap_begin_),
244 IndexToOffset(bitmap_size_ / kWordSize)));
245 CHECK(bitmap_begin_ != NULL);
246 CHECK(callback != NULL);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700247 uintptr_t end = Size() / kWordSize;
248 for (uintptr_t i = 0; i < end; ++i) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700249 word w = bitmap_begin_[i];
250 if (UNLIKELY(w != 0)) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700251 uintptr_t ptr_base = IndexToOffset(i) + heap_begin_;
252 while (w != 0) {
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700253 const size_t shift = CLZ(w);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700254 Object* obj = reinterpret_cast<Object*>(ptr_base + shift * kAlignment);
255 WalkFieldsInOrder(visited.get(), callback, obj, arg);
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700256 w ^= static_cast<size_t>(kWordHighBitMask) >> shift;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700257 }
258 }
259 }
260}
261
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700262std::string SpaceSetMap::GetName() const {
263 return name_;
264}
265
266void SpaceSetMap::SetName(const std::string& name) {
267 name_ = name;
268}
269
270SpaceSetMap::SpaceSetMap(const std::string& name) : name_(name) {
271
272}
273
274void SpaceSetMap::CopyFrom(const SpaceSetMap& space_set) {
275 contained_ = space_set.contained_;
276}
277
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700278std::ostream& operator << (std::ostream& stream, const SpaceBitmap& bitmap) {
279 return stream
280 << bitmap.GetName() << "["
281 << "begin=" << reinterpret_cast<const void*>(bitmap.HeapBegin())
282 << ",end=" << reinterpret_cast<const void*>(bitmap.HeapLimit())
283 << "]";
284 }
285
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700286} // namespace art