blob: cd07a817c2963ad7af20c454a6a6409589221729 [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 Chartierb062fdd2012-07-03 09:51:48 -070033SpaceBitmap* SpaceBitmap::Create(const std::string& name, byte* heap_begin, size_t heap_capacity) {
34 CHECK(heap_begin != NULL);
35 // Round up since heap_capacity is not necessarily a multiple of kAlignment * kBitsPerWord.
36 size_t bitmap_size = OffsetToIndex(RoundUp(heap_capacity, kAlignment * kBitsPerWord)) * kWordSize;
37 UniquePtr<MemMap> mem_map(MemMap::MapAnonymous(name.c_str(), NULL, bitmap_size, PROT_READ | PROT_WRITE));
38 if (mem_map.get() == NULL) {
39 LOG(ERROR) << "Failed to allocate bitmap " << name;
40 return NULL;
41 }
42 word* bitmap_begin = reinterpret_cast<word*>(mem_map->Begin());
43 return new SpaceBitmap(name, mem_map.release(), bitmap_begin, bitmap_size, heap_begin);
44}
45
46// Clean up any resources associated with the bitmap.
47SpaceBitmap::~SpaceBitmap() {}
48
Mathieu Chartierdcf8d722012-08-02 14:55:54 -070049void SpaceBitmap::SetHeapLimit(uintptr_t new_end) {
50 DCHECK(IsAligned<kBitsPerWord * kAlignment>(new_end));
51 size_t new_size = OffsetToIndex(new_end - heap_begin_) * kWordSize;
Mathieu Chartiercc236d72012-07-20 10:29:05 -070052 if (new_size < bitmap_size_) {
53 bitmap_size_ = new_size;
54 }
55 // Not sure if doing this trim is necessary, since nothing past the end of the heap capacity
56 // should be marked.
57 // TODO: Fix this code is, it broken and causes rare heap corruption!
58 // mem_map_->Trim(reinterpret_cast<byte*>(heap_begin_ + bitmap_size_));
59}
60
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070061// Fill the bitmap with zeroes. Returns the bitmap's memory to the
62// system as a side-effect.
63void SpaceBitmap::Clear() {
64 if (bitmap_begin_ != NULL) {
65 // This returns the memory to the system. Successive page faults
66 // will return zeroed memory.
67 int result = madvise(bitmap_begin_, bitmap_size_, MADV_DONTNEED);
68 if (result == -1) {
69 PLOG(WARNING) << "madvise failed";
70 }
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070071 }
72}
73
Mathieu Chartier357e9be2012-08-01 11:00:14 -070074void SpaceBitmap::CopyFrom(SpaceBitmap* source_bitmap) {
75 DCHECK_EQ(Size(), source_bitmap->Size());
76 std::copy(source_bitmap->Begin(), source_bitmap->Begin() + source_bitmap->Size() / kWordSize, Begin());
77}
78
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070079// Visits set bits in address order. The callback is not permitted to
80// change the bitmap bits or max during the traversal.
81void SpaceBitmap::Walk(SpaceBitmap::Callback* callback, void* arg) {
82 CHECK(bitmap_begin_ != NULL);
83 CHECK(callback != NULL);
Mathieu Chartier357e9be2012-08-01 11:00:14 -070084
85 uintptr_t end = OffsetToIndex(HeapLimit() - heap_begin_ - 1);
86 word* bitmap_begin = bitmap_begin_;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070087 for (uintptr_t i = 0; i <= end; ++i) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -070088 word w = bitmap_begin[i];
89 if (w != 0) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070090 uintptr_t ptr_base = IndexToOffset(i) + heap_begin_;
Mathieu Chartier357e9be2012-08-01 11:00:14 -070091 do {
Mathieu Chartierdcf8d722012-08-02 14:55:54 -070092 const size_t shift = CLZ(w);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070093 Object* obj = reinterpret_cast<Object*>(ptr_base + shift * kAlignment);
94 (*callback)(obj, arg);
Mathieu Chartierdcf8d722012-08-02 14:55:54 -070095 w ^= static_cast<size_t>(kWordHighBitMask) >> shift;
Mathieu Chartier357e9be2012-08-01 11:00:14 -070096 } while (w != 0);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070097 }
98 }
99}
100
101// Walk through the bitmaps in increasing address order, and find the
102// object pointers that correspond to garbage objects. Call
103// <callback> zero or more times with lists of these object pointers.
104//
105// The callback is not permitted to increase the max of either bitmap.
106void SpaceBitmap::SweepWalk(const SpaceBitmap& live_bitmap,
107 const SpaceBitmap& mark_bitmap,
108 uintptr_t sweep_begin, uintptr_t sweep_end,
109 SpaceBitmap::SweepCallback* callback, void* arg) {
110 CHECK(live_bitmap.bitmap_begin_ != NULL);
111 CHECK(mark_bitmap.bitmap_begin_ != NULL);
112 CHECK_EQ(live_bitmap.heap_begin_, mark_bitmap.heap_begin_);
113 CHECK_EQ(live_bitmap.bitmap_size_, mark_bitmap.bitmap_size_);
114 CHECK(callback != NULL);
115 CHECK_LE(sweep_begin, sweep_end);
116 CHECK_GE(sweep_begin, live_bitmap.heap_begin_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700117
118 if (sweep_end <= sweep_begin) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700119 return;
120 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700121
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700122 // TODO: rewrite the callbacks to accept a std::vector<Object*> rather than a Object**?
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700123 const size_t buffer_size = kWordSize * kBitsPerWord;
124 Object* pointer_buf[buffer_size];
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700125 Object** pb = &pointer_buf[0];
126 size_t start = OffsetToIndex(sweep_begin - live_bitmap.heap_begin_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700127 size_t end = OffsetToIndex(sweep_end - live_bitmap.heap_begin_ - 1);
128 CHECK_LT(end, live_bitmap.Size() / kWordSize);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700129 word* live = live_bitmap.bitmap_begin_;
130 word* mark = mark_bitmap.bitmap_begin_;
131 for (size_t i = start; i <= end; i++) {
132 word garbage = live[i] & ~mark[i];
133 if (UNLIKELY(garbage != 0)) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700134 uintptr_t ptr_base = IndexToOffset(i) + live_bitmap.heap_begin_;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700135 do {
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700136 const size_t shift = CLZ(garbage);
137 garbage ^= static_cast<size_t>(kWordHighBitMask) >> shift;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700138 *pb++ = reinterpret_cast<Object*>(ptr_base + shift * kAlignment);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700139 } while (garbage != 0);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700140 // Make sure that there are always enough slots available for an
141 // entire word of one bits.
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700142 if (pb >= &pointer_buf[buffer_size - kBitsPerWord]) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700143 (*callback)(pb - &pointer_buf[0], &pointer_buf[0], arg);
144 pb = &pointer_buf[0];
145 }
146 }
147 }
148 if (pb > &pointer_buf[0]) {
149 (*callback)(pb - &pointer_buf[0], &pointer_buf[0], arg);
150 }
151}
152
153} // namespace art
154
155// Support needed for in order traversal
156#include "object.h"
157#include "object_utils.h"
158
159namespace art {
160
161static void WalkFieldsInOrder(SpaceBitmap* visited, SpaceBitmap::Callback* callback, Object* obj,
162 void* arg);
163
164// Walk instance fields of the given Class. Separate function to allow recursion on the super
165// class.
166static void WalkInstanceFields(SpaceBitmap* visited, SpaceBitmap::Callback* callback, Object* obj,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700167 Class* klass, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700168 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700169 // Visit fields of parent classes first.
170 Class* super = klass->GetSuperClass();
171 if (super != NULL) {
172 WalkInstanceFields(visited, callback, obj, super, arg);
173 }
174 // Walk instance fields
175 ObjectArray<Field>* fields = klass->GetIFields();
176 if (fields != NULL) {
177 for (int32_t i = 0; i < fields->GetLength(); i++) {
178 Field* field = fields->Get(i);
179 FieldHelper fh(field);
180 if (!fh.GetType()->IsPrimitive()) {
181 Object* value = field->GetObj(obj);
182 if (value != NULL) {
183 WalkFieldsInOrder(visited, callback, value, arg);
184 }
185 }
186 }
187 }
188}
189
190// For an unvisited object, visit it then all its children found via fields.
191static void WalkFieldsInOrder(SpaceBitmap* visited, SpaceBitmap::Callback* callback, Object* obj,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700192 void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700193 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700194 if (visited->Test(obj)) {
195 return;
196 }
197 // visit the object itself
198 (*callback)(obj, arg);
199 visited->Set(obj);
200 // Walk instance fields of all objects
201 Class* klass = obj->GetClass();
202 WalkInstanceFields(visited, callback, obj, klass, arg);
203 // Walk static fields of a Class
204 if (obj->IsClass()) {
205 ObjectArray<Field>* fields = klass->GetSFields();
206 if (fields != NULL) {
207 for (int32_t i = 0; i < fields->GetLength(); i++) {
208 Field* field = fields->Get(i);
209 FieldHelper fh(field);
210 if (!fh.GetType()->IsPrimitive()) {
211 Object* value = field->GetObj(NULL);
212 if (value != NULL) {
213 WalkFieldsInOrder(visited, callback, value, arg);
214 }
215 }
216 }
217 }
218 } else if (obj->IsObjectArray()) {
219 // Walk elements of an object array
220 ObjectArray<Object>* obj_array = obj->AsObjectArray<Object>();
221 int32_t length = obj_array->GetLength();
222 for (int32_t i = 0; i < length; i++) {
223 Object* value = obj_array->Get(i);
224 if (value != NULL) {
225 WalkFieldsInOrder(visited, callback, value, arg);
226 }
227 }
228 }
229}
230
231// Visits set bits with an in order traversal. The callback is not permitted to change the bitmap
232// bits or max during the traversal.
233void SpaceBitmap::InOrderWalk(SpaceBitmap::Callback* callback, void* arg) {
234 UniquePtr<SpaceBitmap> visited(Create("bitmap for in-order walk",
235 reinterpret_cast<byte*>(heap_begin_),
236 IndexToOffset(bitmap_size_ / kWordSize)));
237 CHECK(bitmap_begin_ != NULL);
238 CHECK(callback != NULL);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700239 uintptr_t end = Size() / kWordSize;
240 for (uintptr_t i = 0; i < end; ++i) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700241 word w = bitmap_begin_[i];
242 if (UNLIKELY(w != 0)) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700243 uintptr_t ptr_base = IndexToOffset(i) + heap_begin_;
244 while (w != 0) {
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700245 const size_t shift = CLZ(w);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700246 Object* obj = reinterpret_cast<Object*>(ptr_base + shift * kAlignment);
247 WalkFieldsInOrder(visited.get(), callback, obj, arg);
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700248 w ^= static_cast<size_t>(kWordHighBitMask) >> shift;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700249 }
250 }
251 }
252}
253
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700254std::ostream& operator << (std::ostream& stream, const SpaceBitmap& bitmap) {
255 return stream
256 << bitmap.GetName() << "["
257 << "begin=" << reinterpret_cast<const void*>(bitmap.HeapBegin())
258 << ",end=" << reinterpret_cast<const void*>(bitmap.HeapLimit())
259 << "]";
260 }
261
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700262} // namespace art