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 "space_bitmap-inl.h" 18 19 #include "android-base/stringprintf.h" 20 21 #include "art_field-inl.h" 22 #include "dex_file-inl.h" 23 #include "mem_map.h" 24 #include "mirror/object-inl.h" 25 #include "mirror/class-inl.h" 26 #include "mirror/object_array.h" 27 28 namespace art { 29 namespace gc { 30 namespace accounting { 31 32 using android::base::StringPrintf; 33 34 template<size_t kAlignment> 35 size_t SpaceBitmap<kAlignment>::ComputeBitmapSize(uint64_t capacity) { 36 const uint64_t kBytesCoveredPerWord = kAlignment * kBitsPerIntPtrT; 37 return (RoundUp(capacity, kBytesCoveredPerWord) / kBytesCoveredPerWord) * sizeof(intptr_t); 38 } 39 40 template<size_t kAlignment> 41 size_t SpaceBitmap<kAlignment>::ComputeHeapSize(uint64_t bitmap_bytes) { 42 return bitmap_bytes * kBitsPerByte * kAlignment; 43 } 44 45 template<size_t kAlignment> 46 SpaceBitmap<kAlignment>* SpaceBitmap<kAlignment>::CreateFromMemMap( 47 const std::string& name, MemMap* mem_map, uint8_t* heap_begin, size_t heap_capacity) { 48 CHECK(mem_map != nullptr); 49 uintptr_t* bitmap_begin = reinterpret_cast<uintptr_t*>(mem_map->Begin()); 50 const size_t bitmap_size = ComputeBitmapSize(heap_capacity); 51 return new SpaceBitmap(name, mem_map, bitmap_begin, bitmap_size, heap_begin, heap_capacity); 52 } 53 54 template<size_t kAlignment> 55 SpaceBitmap<kAlignment>::SpaceBitmap(const std::string& name, 56 MemMap* mem_map, 57 uintptr_t* bitmap_begin, 58 size_t bitmap_size, 59 const void* heap_begin, 60 size_t heap_capacity) 61 : mem_map_(mem_map), 62 bitmap_begin_(reinterpret_cast<Atomic<uintptr_t>*>(bitmap_begin)), 63 bitmap_size_(bitmap_size), 64 heap_begin_(reinterpret_cast<uintptr_t>(heap_begin)), 65 heap_limit_(reinterpret_cast<uintptr_t>(heap_begin) + heap_capacity), 66 name_(name) { 67 CHECK(bitmap_begin_ != nullptr); 68 CHECK_NE(bitmap_size, 0U); 69 } 70 71 template<size_t kAlignment> 72 SpaceBitmap<kAlignment>::~SpaceBitmap() {} 73 74 template<size_t kAlignment> 75 SpaceBitmap<kAlignment>* SpaceBitmap<kAlignment>::Create( 76 const std::string& name, uint8_t* heap_begin, size_t heap_capacity) { 77 // Round up since heap_capacity is not necessarily a multiple of kAlignment * kBitsPerWord. 78 const size_t bitmap_size = ComputeBitmapSize(heap_capacity); 79 std::string error_msg; 80 std::unique_ptr<MemMap> mem_map(MemMap::MapAnonymous(name.c_str(), nullptr, bitmap_size, 81 PROT_READ | PROT_WRITE, false, false, 82 &error_msg)); 83 if (UNLIKELY(mem_map.get() == nullptr)) { 84 LOG(ERROR) << "Failed to allocate bitmap " << name << ": " << error_msg; 85 return nullptr; 86 } 87 return CreateFromMemMap(name, mem_map.release(), heap_begin, heap_capacity); 88 } 89 90 template<size_t kAlignment> 91 void SpaceBitmap<kAlignment>::SetHeapLimit(uintptr_t new_end) { 92 DCHECK_ALIGNED(new_end, kBitsPerIntPtrT * kAlignment); 93 size_t new_size = OffsetToIndex(new_end - heap_begin_) * sizeof(intptr_t); 94 if (new_size < bitmap_size_) { 95 bitmap_size_ = new_size; 96 } 97 heap_limit_ = new_end; 98 // Not sure if doing this trim is necessary, since nothing past the end of the heap capacity 99 // should be marked. 100 } 101 102 template<size_t kAlignment> 103 std::string SpaceBitmap<kAlignment>::Dump() const { 104 return StringPrintf("%s: %p-%p", name_.c_str(), reinterpret_cast<void*>(HeapBegin()), 105 reinterpret_cast<void*>(HeapLimit())); 106 } 107 108 template<size_t kAlignment> 109 void SpaceBitmap<kAlignment>::Clear() { 110 if (bitmap_begin_ != nullptr) { 111 mem_map_->MadviseDontNeedAndZero(); 112 } 113 } 114 115 template<size_t kAlignment> 116 void SpaceBitmap<kAlignment>::ClearRange(const mirror::Object* begin, const mirror::Object* end) { 117 uintptr_t begin_offset = reinterpret_cast<uintptr_t>(begin) - heap_begin_; 118 uintptr_t end_offset = reinterpret_cast<uintptr_t>(end) - heap_begin_; 119 // Align begin and end to word boundaries. 120 while (begin_offset < end_offset && OffsetBitIndex(begin_offset) != 0) { 121 Clear(reinterpret_cast<mirror::Object*>(heap_begin_ + begin_offset)); 122 begin_offset += kAlignment; 123 } 124 while (begin_offset < end_offset && OffsetBitIndex(end_offset) != 0) { 125 end_offset -= kAlignment; 126 Clear(reinterpret_cast<mirror::Object*>(heap_begin_ + end_offset)); 127 } 128 const uintptr_t start_index = OffsetToIndex(begin_offset); 129 const uintptr_t end_index = OffsetToIndex(end_offset); 130 ZeroAndReleasePages(reinterpret_cast<uint8_t*>(&bitmap_begin_[start_index]), 131 (end_index - start_index) * sizeof(*bitmap_begin_)); 132 } 133 134 template<size_t kAlignment> 135 void SpaceBitmap<kAlignment>::CopyFrom(SpaceBitmap* source_bitmap) { 136 DCHECK_EQ(Size(), source_bitmap->Size()); 137 const size_t count = source_bitmap->Size() / sizeof(intptr_t); 138 Atomic<uintptr_t>* const src = source_bitmap->Begin(); 139 Atomic<uintptr_t>* const dest = Begin(); 140 for (size_t i = 0; i < count; ++i) { 141 dest[i].StoreRelaxed(src[i].LoadRelaxed()); 142 } 143 } 144 145 template<size_t kAlignment> 146 void SpaceBitmap<kAlignment>::SweepWalk(const SpaceBitmap<kAlignment>& live_bitmap, 147 const SpaceBitmap<kAlignment>& mark_bitmap, 148 uintptr_t sweep_begin, uintptr_t sweep_end, 149 SpaceBitmap::SweepCallback* callback, void* arg) { 150 CHECK(live_bitmap.bitmap_begin_ != nullptr); 151 CHECK(mark_bitmap.bitmap_begin_ != nullptr); 152 CHECK_EQ(live_bitmap.heap_begin_, mark_bitmap.heap_begin_); 153 CHECK_EQ(live_bitmap.bitmap_size_, mark_bitmap.bitmap_size_); 154 CHECK(callback != nullptr); 155 CHECK_LE(sweep_begin, sweep_end); 156 CHECK_GE(sweep_begin, live_bitmap.heap_begin_); 157 158 if (sweep_end <= sweep_begin) { 159 return; 160 } 161 162 // TODO: rewrite the callbacks to accept a std::vector<mirror::Object*> rather than a mirror::Object**? 163 constexpr size_t buffer_size = sizeof(intptr_t) * kBitsPerIntPtrT; 164 #ifdef __LP64__ 165 // Heap-allocate for smaller stack frame. 166 std::unique_ptr<mirror::Object*[]> pointer_buf_ptr(new mirror::Object*[buffer_size]); 167 mirror::Object** pointer_buf = pointer_buf_ptr.get(); 168 #else 169 // Stack-allocate buffer as it's small enough. 170 mirror::Object* pointer_buf[buffer_size]; 171 #endif 172 mirror::Object** pb = &pointer_buf[0]; 173 174 size_t start = OffsetToIndex(sweep_begin - live_bitmap.heap_begin_); 175 size_t end = OffsetToIndex(sweep_end - live_bitmap.heap_begin_ - 1); 176 CHECK_LT(end, live_bitmap.Size() / sizeof(intptr_t)); 177 Atomic<uintptr_t>* live = live_bitmap.bitmap_begin_; 178 Atomic<uintptr_t>* mark = mark_bitmap.bitmap_begin_; 179 for (size_t i = start; i <= end; i++) { 180 uintptr_t garbage = live[i].LoadRelaxed() & ~mark[i].LoadRelaxed(); 181 if (UNLIKELY(garbage != 0)) { 182 uintptr_t ptr_base = IndexToOffset(i) + live_bitmap.heap_begin_; 183 do { 184 const size_t shift = CTZ(garbage); 185 garbage ^= (static_cast<uintptr_t>(1)) << shift; 186 *pb++ = reinterpret_cast<mirror::Object*>(ptr_base + shift * kAlignment); 187 } while (garbage != 0); 188 // Make sure that there are always enough slots available for an 189 // entire word of one bits. 190 if (pb >= &pointer_buf[buffer_size - kBitsPerIntPtrT]) { 191 (*callback)(pb - &pointer_buf[0], &pointer_buf[0], arg); 192 pb = &pointer_buf[0]; 193 } 194 } 195 } 196 if (pb > &pointer_buf[0]) { 197 (*callback)(pb - &pointer_buf[0], &pointer_buf[0], arg); 198 } 199 } 200 201 template class SpaceBitmap<kObjectAlignment>; 202 template class SpaceBitmap<kPageSize>; 203 204 } // namespace accounting 205 } // namespace gc 206 } // namespace art 207