blob: b0b9d90262057707a878621db0a116a481e91852 [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.
53SpaceBitmap::~SpaceBitmap() {}
54
Mathieu Chartierdcf8d722012-08-02 14:55:54 -070055void SpaceBitmap::SetHeapLimit(uintptr_t new_end) {
56 DCHECK(IsAligned<kBitsPerWord * kAlignment>(new_end));
57 size_t new_size = OffsetToIndex(new_end - heap_begin_) * kWordSize;
Mathieu Chartiercc236d72012-07-20 10:29:05 -070058 if (new_size < bitmap_size_) {
59 bitmap_size_ = new_size;
60 }
61 // Not sure if doing this trim is necessary, since nothing past the end of the heap capacity
62 // should be marked.
63 // TODO: Fix this code is, it broken and causes rare heap corruption!
64 // mem_map_->Trim(reinterpret_cast<byte*>(heap_begin_ + bitmap_size_));
65}
66
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070067// Fill the bitmap with zeroes. Returns the bitmap's memory to the
68// system as a side-effect.
69void SpaceBitmap::Clear() {
70 if (bitmap_begin_ != NULL) {
71 // This returns the memory to the system. Successive page faults
72 // will return zeroed memory.
73 int result = madvise(bitmap_begin_, bitmap_size_, MADV_DONTNEED);
74 if (result == -1) {
75 PLOG(WARNING) << "madvise failed";
76 }
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070077 }
78}
79
Mathieu Chartier357e9be2012-08-01 11:00:14 -070080void SpaceBitmap::CopyFrom(SpaceBitmap* source_bitmap) {
81 DCHECK_EQ(Size(), source_bitmap->Size());
82 std::copy(source_bitmap->Begin(), source_bitmap->Begin() + source_bitmap->Size() / kWordSize, Begin());
83}
84
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070085// Visits set bits in address order. The callback is not permitted to
86// change the bitmap bits or max during the traversal.
87void SpaceBitmap::Walk(SpaceBitmap::Callback* callback, void* arg) {
88 CHECK(bitmap_begin_ != NULL);
89 CHECK(callback != NULL);
Mathieu Chartier357e9be2012-08-01 11:00:14 -070090
91 uintptr_t end = OffsetToIndex(HeapLimit() - heap_begin_ - 1);
92 word* bitmap_begin = bitmap_begin_;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070093 for (uintptr_t i = 0; i <= end; ++i) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -070094 word w = bitmap_begin[i];
95 if (w != 0) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070096 uintptr_t ptr_base = IndexToOffset(i) + heap_begin_;
Mathieu Chartier357e9be2012-08-01 11:00:14 -070097 do {
Mathieu Chartierdcf8d722012-08-02 14:55:54 -070098 const size_t shift = CLZ(w);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070099 Object* obj = reinterpret_cast<Object*>(ptr_base + shift * kAlignment);
100 (*callback)(obj, arg);
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700101 w ^= static_cast<size_t>(kWordHighBitMask) >> shift;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700102 } while (w != 0);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700103 }
104 }
105}
106
107// Walk through the bitmaps in increasing address order, and find the
108// object pointers that correspond to garbage objects. Call
109// <callback> zero or more times with lists of these object pointers.
110//
111// The callback is not permitted to increase the max of either bitmap.
112void SpaceBitmap::SweepWalk(const SpaceBitmap& live_bitmap,
113 const SpaceBitmap& mark_bitmap,
114 uintptr_t sweep_begin, uintptr_t sweep_end,
115 SpaceBitmap::SweepCallback* callback, void* arg) {
116 CHECK(live_bitmap.bitmap_begin_ != NULL);
117 CHECK(mark_bitmap.bitmap_begin_ != NULL);
118 CHECK_EQ(live_bitmap.heap_begin_, mark_bitmap.heap_begin_);
119 CHECK_EQ(live_bitmap.bitmap_size_, mark_bitmap.bitmap_size_);
120 CHECK(callback != NULL);
121 CHECK_LE(sweep_begin, sweep_end);
122 CHECK_GE(sweep_begin, live_bitmap.heap_begin_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700123
124 if (sweep_end <= sweep_begin) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700125 return;
126 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700127
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700128 // TODO: rewrite the callbacks to accept a std::vector<Object*> rather than a Object**?
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700129 const size_t buffer_size = kWordSize * kBitsPerWord;
130 Object* pointer_buf[buffer_size];
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700131 Object** pb = &pointer_buf[0];
132 size_t start = OffsetToIndex(sweep_begin - live_bitmap.heap_begin_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700133 size_t end = OffsetToIndex(sweep_end - live_bitmap.heap_begin_ - 1);
134 CHECK_LT(end, live_bitmap.Size() / kWordSize);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700135 word* live = live_bitmap.bitmap_begin_;
136 word* mark = mark_bitmap.bitmap_begin_;
137 for (size_t i = start; i <= end; i++) {
138 word garbage = live[i] & ~mark[i];
139 if (UNLIKELY(garbage != 0)) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700140 uintptr_t ptr_base = IndexToOffset(i) + live_bitmap.heap_begin_;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700141 do {
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700142 const size_t shift = CLZ(garbage);
143 garbage ^= static_cast<size_t>(kWordHighBitMask) >> shift;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700144 *pb++ = reinterpret_cast<Object*>(ptr_base + shift * kAlignment);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700145 } while (garbage != 0);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700146 // Make sure that there are always enough slots available for an
147 // entire word of one bits.
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700148 if (pb >= &pointer_buf[buffer_size - kBitsPerWord]) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700149 (*callback)(pb - &pointer_buf[0], &pointer_buf[0], arg);
150 pb = &pointer_buf[0];
151 }
152 }
153 }
154 if (pb > &pointer_buf[0]) {
155 (*callback)(pb - &pointer_buf[0], &pointer_buf[0], arg);
156 }
157}
158
159} // namespace art
160
161// Support needed for in order traversal
162#include "object.h"
163#include "object_utils.h"
164
165namespace art {
166
167static void WalkFieldsInOrder(SpaceBitmap* visited, SpaceBitmap::Callback* callback, Object* obj,
168 void* arg);
169
170// Walk instance fields of the given Class. Separate function to allow recursion on the super
171// class.
172static void WalkInstanceFields(SpaceBitmap* visited, SpaceBitmap::Callback* callback, Object* obj,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700173 Class* klass, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700174 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700175 // Visit fields of parent classes first.
176 Class* super = klass->GetSuperClass();
177 if (super != NULL) {
178 WalkInstanceFields(visited, callback, obj, super, arg);
179 }
180 // Walk instance fields
181 ObjectArray<Field>* fields = klass->GetIFields();
182 if (fields != NULL) {
183 for (int32_t i = 0; i < fields->GetLength(); i++) {
184 Field* field = fields->Get(i);
185 FieldHelper fh(field);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700186 if (!fh.IsPrimitiveType()) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700187 Object* value = field->GetObj(obj);
188 if (value != NULL) {
189 WalkFieldsInOrder(visited, callback, value, arg);
190 }
191 }
192 }
193 }
194}
195
196// For an unvisited object, visit it then all its children found via fields.
197static void WalkFieldsInOrder(SpaceBitmap* visited, SpaceBitmap::Callback* callback, Object* obj,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700198 void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700199 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700200 if (visited->Test(obj)) {
201 return;
202 }
203 // visit the object itself
204 (*callback)(obj, arg);
205 visited->Set(obj);
206 // Walk instance fields of all objects
207 Class* klass = obj->GetClass();
208 WalkInstanceFields(visited, callback, obj, klass, arg);
209 // Walk static fields of a Class
210 if (obj->IsClass()) {
211 ObjectArray<Field>* fields = klass->GetSFields();
212 if (fields != NULL) {
213 for (int32_t i = 0; i < fields->GetLength(); i++) {
214 Field* field = fields->Get(i);
215 FieldHelper fh(field);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700216 if (!fh.IsPrimitiveType()) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700217 Object* value = field->GetObj(NULL);
218 if (value != NULL) {
219 WalkFieldsInOrder(visited, callback, value, arg);
220 }
221 }
222 }
223 }
224 } else if (obj->IsObjectArray()) {
225 // Walk elements of an object array
226 ObjectArray<Object>* obj_array = obj->AsObjectArray<Object>();
227 int32_t length = obj_array->GetLength();
228 for (int32_t i = 0; i < length; i++) {
229 Object* value = obj_array->Get(i);
230 if (value != NULL) {
231 WalkFieldsInOrder(visited, callback, value, arg);
232 }
233 }
234 }
235}
236
237// Visits set bits with an in order traversal. The callback is not permitted to change the bitmap
238// bits or max during the traversal.
239void SpaceBitmap::InOrderWalk(SpaceBitmap::Callback* callback, void* arg) {
240 UniquePtr<SpaceBitmap> visited(Create("bitmap for in-order walk",
241 reinterpret_cast<byte*>(heap_begin_),
242 IndexToOffset(bitmap_size_ / kWordSize)));
243 CHECK(bitmap_begin_ != NULL);
244 CHECK(callback != NULL);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700245 uintptr_t end = Size() / kWordSize;
246 for (uintptr_t i = 0; i < end; ++i) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700247 word w = bitmap_begin_[i];
248 if (UNLIKELY(w != 0)) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700249 uintptr_t ptr_base = IndexToOffset(i) + heap_begin_;
250 while (w != 0) {
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700251 const size_t shift = CLZ(w);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700252 Object* obj = reinterpret_cast<Object*>(ptr_base + shift * kAlignment);
253 WalkFieldsInOrder(visited.get(), callback, obj, arg);
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700254 w ^= static_cast<size_t>(kWordHighBitMask) >> shift;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700255 }
256 }
257 }
258}
259
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700260std::string SpaceSetMap::GetName() const {
261 return name_;
262}
263
264void SpaceSetMap::SetName(const std::string& name) {
265 name_ = name;
266}
267
268SpaceSetMap::SpaceSetMap(const std::string& name) : name_(name) {
269
270}
271
272void SpaceSetMap::CopyFrom(const SpaceSetMap& space_set) {
273 contained_ = space_set.contained_;
274}
275
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700276std::ostream& operator << (std::ostream& stream, const SpaceBitmap& bitmap) {
277 return stream
278 << bitmap.GetName() << "["
279 << "begin=" << reinterpret_cast<const void*>(bitmap.HeapBegin())
280 << ",end=" << reinterpret_cast<const void*>(bitmap.HeapLimit())
281 << "]";
282 }
283
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700284} // namespace art