Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 23 | namespace art { |
| 24 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 25 | std::string SpaceBitmap::GetName() const { |
| 26 | return name_; |
| 27 | } |
| 28 | |
| 29 | void SpaceBitmap::SetName(const std::string& name) { |
| 30 | name_ = name; |
| 31 | } |
| 32 | |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 33 | SpaceBitmap* 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. |
| 47 | SpaceBitmap::~SpaceBitmap() {} |
| 48 | |
Mathieu Chartier | dcf8d72 | 2012-08-02 14:55:54 -0700 | [diff] [blame] | 49 | void 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 Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 52 | 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 Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 61 | // Fill the bitmap with zeroes. Returns the bitmap's memory to the |
| 62 | // system as a side-effect. |
| 63 | void 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 Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 74 | void 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 Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 79 | // Visits set bits in address order. The callback is not permitted to |
| 80 | // change the bitmap bits or max during the traversal. |
| 81 | void SpaceBitmap::Walk(SpaceBitmap::Callback* callback, void* arg) { |
| 82 | CHECK(bitmap_begin_ != NULL); |
| 83 | CHECK(callback != NULL); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 84 | |
| 85 | uintptr_t end = OffsetToIndex(HeapLimit() - heap_begin_ - 1); |
| 86 | word* bitmap_begin = bitmap_begin_; |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 87 | for (uintptr_t i = 0; i <= end; ++i) { |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 88 | word w = bitmap_begin[i]; |
| 89 | if (w != 0) { |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 90 | uintptr_t ptr_base = IndexToOffset(i) + heap_begin_; |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 91 | do { |
Mathieu Chartier | dcf8d72 | 2012-08-02 14:55:54 -0700 | [diff] [blame] | 92 | const size_t shift = CLZ(w); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 93 | Object* obj = reinterpret_cast<Object*>(ptr_base + shift * kAlignment); |
| 94 | (*callback)(obj, arg); |
Mathieu Chartier | dcf8d72 | 2012-08-02 14:55:54 -0700 | [diff] [blame] | 95 | w ^= static_cast<size_t>(kWordHighBitMask) >> shift; |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 96 | } while (w != 0); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 97 | } |
| 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. |
| 106 | void 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 Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 117 | |
| 118 | if (sweep_end <= sweep_begin) { |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 119 | return; |
| 120 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 121 | |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 122 | // TODO: rewrite the callbacks to accept a std::vector<Object*> rather than a Object**? |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 123 | const size_t buffer_size = kWordSize * kBitsPerWord; |
| 124 | Object* pointer_buf[buffer_size]; |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 125 | Object** pb = &pointer_buf[0]; |
| 126 | size_t start = OffsetToIndex(sweep_begin - live_bitmap.heap_begin_); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 127 | size_t end = OffsetToIndex(sweep_end - live_bitmap.heap_begin_ - 1); |
| 128 | CHECK_LT(end, live_bitmap.Size() / kWordSize); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 129 | 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 Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 134 | uintptr_t ptr_base = IndexToOffset(i) + live_bitmap.heap_begin_; |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 135 | do { |
Mathieu Chartier | dcf8d72 | 2012-08-02 14:55:54 -0700 | [diff] [blame] | 136 | const size_t shift = CLZ(garbage); |
| 137 | garbage ^= static_cast<size_t>(kWordHighBitMask) >> shift; |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 138 | *pb++ = reinterpret_cast<Object*>(ptr_base + shift * kAlignment); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 139 | } while (garbage != 0); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 140 | // Make sure that there are always enough slots available for an |
| 141 | // entire word of one bits. |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 142 | if (pb >= &pointer_buf[buffer_size - kBitsPerWord]) { |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 143 | (*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 | |
| 159 | namespace art { |
| 160 | |
| 161 | static 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. |
| 166 | static void WalkInstanceFields(SpaceBitmap* visited, SpaceBitmap::Callback* callback, Object* obj, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 167 | Class* klass, void* arg) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 168 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 169 | // 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. |
| 191 | static void WalkFieldsInOrder(SpaceBitmap* visited, SpaceBitmap::Callback* callback, Object* obj, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 192 | void* arg) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 193 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 194 | 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. |
| 233 | void 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 Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 239 | uintptr_t end = Size() / kWordSize; |
| 240 | for (uintptr_t i = 0; i < end; ++i) { |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 241 | word w = bitmap_begin_[i]; |
| 242 | if (UNLIKELY(w != 0)) { |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 243 | uintptr_t ptr_base = IndexToOffset(i) + heap_begin_; |
| 244 | while (w != 0) { |
Mathieu Chartier | dcf8d72 | 2012-08-02 14:55:54 -0700 | [diff] [blame] | 245 | const size_t shift = CLZ(w); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 246 | Object* obj = reinterpret_cast<Object*>(ptr_base + shift * kAlignment); |
| 247 | WalkFieldsInOrder(visited.get(), callback, obj, arg); |
Mathieu Chartier | dcf8d72 | 2012-08-02 14:55:54 -0700 | [diff] [blame] | 248 | w ^= static_cast<size_t>(kWordHighBitMask) >> shift; |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 254 | std::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 Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 262 | } // namespace art |