blob: 31a153778dfa3b050d9a6330cb9fa57a734014c4 [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
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080017#include "space_bitmap-inl.h"
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070018
19namespace art {
Ian Rogers1d54e732013-05-02 21:10:01 -070020namespace gc {
21namespace accounting {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070022
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070023template<size_t kAlignment>
24SpaceBitmap<kAlignment>* SpaceBitmap<kAlignment>::CreateFromMemMap(
25 const std::string& name, MemMap* mem_map, byte* heap_begin, size_t heap_capacity) {
Mathieu Chartier31e89252013-08-28 11:29:12 -070026 CHECK(mem_map != nullptr);
Andreas Gampecb8aea42014-04-02 15:39:58 -070027 uword* bitmap_begin = reinterpret_cast<uword*>(mem_map->Begin());
Mathieu Chartierbbd695c2014-04-16 09:48:48 -070028 const uint64_t kBytesCoveredPerWord = kAlignment * kBitsPerWord;
29 size_t bitmap_size = (RoundUp(static_cast<uint64_t>(heap_capacity), kBytesCoveredPerWord) /
30 kBytesCoveredPerWord) * kWordSize;
Mathieu Chartier31e89252013-08-28 11:29:12 -070031 return new SpaceBitmap(name, mem_map, bitmap_begin, bitmap_size, heap_begin);
32}
33
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070034template<size_t kAlignment>
Mathieu Chartierbbd695c2014-04-16 09:48:48 -070035SpaceBitmap<kAlignment>::SpaceBitmap(const std::string& name, MemMap* mem_map, uword* bitmap_begin,
36 size_t bitmap_size, const void* heap_begin)
37 : mem_map_(mem_map), bitmap_begin_(bitmap_begin), bitmap_size_(bitmap_size),
38 heap_begin_(reinterpret_cast<uintptr_t>(heap_begin)),
39 name_(name) {
40 CHECK(bitmap_begin_ != nullptr);
41 CHECK_NE(bitmap_size, 0U);
42}
43
44template<size_t kAlignment>
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070045SpaceBitmap<kAlignment>* SpaceBitmap<kAlignment>::Create(
46 const std::string& name, byte* heap_begin, size_t heap_capacity) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070047 // Round up since heap_capacity is not necessarily a multiple of kAlignment * kBitsPerWord.
Mathieu Chartierbbd695c2014-04-16 09:48:48 -070048 const uint64_t kBytesCoveredPerWord = kAlignment * kBitsPerWord;
49 size_t bitmap_size = (RoundUp(static_cast<uint64_t>(heap_capacity), kBytesCoveredPerWord) /
50 kBytesCoveredPerWord) * kWordSize;
Ian Rogers8d31bbd2013-10-13 10:44:14 -070051 std::string error_msg;
Mathieu Chartierbbd695c2014-04-16 09:48:48 -070052 UniquePtr<MemMap> mem_map(MemMap::MapAnonymous(name.c_str(), nullptr, bitmap_size,
Ian Rogersef7d42f2014-01-06 12:55:46 -080053 PROT_READ | PROT_WRITE, false, &error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -070054 if (UNLIKELY(mem_map.get() == nullptr)) {
55 LOG(ERROR) << "Failed to allocate bitmap " << name << ": " << error_msg;
Mathieu Chartierbbd695c2014-04-16 09:48:48 -070056 return nullptr;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070057 }
Mathieu Chartier31e89252013-08-28 11:29:12 -070058 return CreateFromMemMap(name, mem_map.release(), heap_begin, heap_capacity);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070059}
60
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070061template<size_t kAlignment>
62void SpaceBitmap<kAlignment>::SetHeapLimit(uintptr_t new_end) {
Mathieu Chartierdcf8d722012-08-02 14:55:54 -070063 DCHECK(IsAligned<kBitsPerWord * kAlignment>(new_end));
64 size_t new_size = OffsetToIndex(new_end - heap_begin_) * kWordSize;
Mathieu Chartiercc236d72012-07-20 10:29:05 -070065 if (new_size < bitmap_size_) {
66 bitmap_size_ = new_size;
67 }
68 // Not sure if doing this trim is necessary, since nothing past the end of the heap capacity
69 // should be marked.
Mathieu Chartiercc236d72012-07-20 10:29:05 -070070}
71
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070072template<size_t kAlignment>
73void SpaceBitmap<kAlignment>::Clear() {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070074 if (bitmap_begin_ != NULL) {
Mathieu Chartier31e89252013-08-28 11:29:12 -070075 // This returns the memory to the system. Successive page faults will return zeroed memory.
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070076 int result = madvise(bitmap_begin_, bitmap_size_, MADV_DONTNEED);
77 if (result == -1) {
Mathieu Chartier7469ebf2012-09-24 16:28:36 -070078 PLOG(FATAL) << "madvise failed";
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070079 }
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070080 }
81}
82
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070083template<size_t kAlignment>
Mathieu Chartierbbd695c2014-04-16 09:48:48 -070084void SpaceBitmap<kAlignment>::CopyFrom(SpaceBitmap* source_bitmap) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -070085 DCHECK_EQ(Size(), source_bitmap->Size());
86 std::copy(source_bitmap->Begin(), source_bitmap->Begin() + source_bitmap->Size() / kWordSize, Begin());
87}
88
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070089template<size_t kAlignment>
Mathieu Chartierbbd695c2014-04-16 09:48:48 -070090void SpaceBitmap<kAlignment>::Walk(ObjectCallback* callback, void* arg) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070091 CHECK(bitmap_begin_ != NULL);
92 CHECK(callback != NULL);
Mathieu Chartier357e9be2012-08-01 11:00:14 -070093
94 uintptr_t end = OffsetToIndex(HeapLimit() - heap_begin_ - 1);
Andreas Gampecb8aea42014-04-02 15:39:58 -070095 uword* bitmap_begin = bitmap_begin_;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070096 for (uintptr_t i = 0; i <= end; ++i) {
Andreas Gampecb8aea42014-04-02 15:39:58 -070097 uword w = bitmap_begin[i];
Mathieu Chartier357e9be2012-08-01 11:00:14 -070098 if (w != 0) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070099 uintptr_t ptr_base = IndexToOffset(i) + heap_begin_;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700100 do {
Andreas Gampecb8aea42014-04-02 15:39:58 -0700101 const size_t shift = CTZ(w);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800102 mirror::Object* obj = reinterpret_cast<mirror::Object*>(ptr_base + shift * kAlignment);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700103 (*callback)(obj, arg);
Andreas Gampecb8aea42014-04-02 15:39:58 -0700104 w ^= (static_cast<uword>(1)) << shift;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700105 } while (w != 0);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700106 }
107 }
108}
109
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700110template<size_t kAlignment>
111void SpaceBitmap<kAlignment>::SweepWalk(const SpaceBitmap<kAlignment>& live_bitmap,
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700112 const SpaceBitmap<kAlignment>& mark_bitmap,
113 uintptr_t sweep_begin, uintptr_t sweep_end,
114 SpaceBitmap::SweepCallback* callback, void* arg) {
115 CHECK(live_bitmap.bitmap_begin_ != nullptr);
116 CHECK(mark_bitmap.bitmap_begin_ != nullptr);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700117 CHECK_EQ(live_bitmap.heap_begin_, mark_bitmap.heap_begin_);
118 CHECK_EQ(live_bitmap.bitmap_size_, mark_bitmap.bitmap_size_);
119 CHECK(callback != NULL);
120 CHECK_LE(sweep_begin, sweep_end);
121 CHECK_GE(sweep_begin, live_bitmap.heap_begin_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700122
123 if (sweep_end <= sweep_begin) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700124 return;
125 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700126
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800127 // TODO: rewrite the callbacks to accept a std::vector<mirror::Object*> rather than a mirror::Object**?
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700128 const size_t buffer_size = kWordSize * kBitsPerWord;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800129 mirror::Object* pointer_buf[buffer_size];
130 mirror::Object** pb = &pointer_buf[0];
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700131 size_t start = OffsetToIndex(sweep_begin - live_bitmap.heap_begin_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700132 size_t end = OffsetToIndex(sweep_end - live_bitmap.heap_begin_ - 1);
133 CHECK_LT(end, live_bitmap.Size() / kWordSize);
Andreas Gampecb8aea42014-04-02 15:39:58 -0700134 uword* live = live_bitmap.bitmap_begin_;
135 uword* mark = mark_bitmap.bitmap_begin_;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700136 for (size_t i = start; i <= end; i++) {
Andreas Gampecb8aea42014-04-02 15:39:58 -0700137 uword garbage = live[i] & ~mark[i];
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700138 if (UNLIKELY(garbage != 0)) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700139 uintptr_t ptr_base = IndexToOffset(i) + live_bitmap.heap_begin_;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700140 do {
Andreas Gampecb8aea42014-04-02 15:39:58 -0700141 const size_t shift = CTZ(garbage);
142 garbage ^= (static_cast<uword>(1)) << shift;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800143 *pb++ = reinterpret_cast<mirror::Object*>(ptr_base + shift * kAlignment);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700144 } while (garbage != 0);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700145 // Make sure that there are always enough slots available for an
146 // entire word of one bits.
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700147 if (pb >= &pointer_buf[buffer_size - kBitsPerWord]) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700148 (*callback)(pb - &pointer_buf[0], &pointer_buf[0], arg);
149 pb = &pointer_buf[0];
150 }
151 }
152 }
153 if (pb > &pointer_buf[0]) {
154 (*callback)(pb - &pointer_buf[0], &pointer_buf[0], arg);
155 }
156}
157
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700158template<size_t kAlignment>
159void SpaceBitmap<kAlignment>::WalkInstanceFields(SpaceBitmap<kAlignment>* visited,
160 ObjectCallback* callback, mirror::Object* obj,
161 mirror::Class* klass, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700162 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700163 // Visit fields of parent classes first.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800164 mirror::Class* super = klass->GetSuperClass();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700165 if (super != NULL) {
166 WalkInstanceFields(visited, callback, obj, super, arg);
167 }
168 // Walk instance fields
Brian Carlstromea46f952013-07-30 01:26:50 -0700169 mirror::ObjectArray<mirror::ArtField>* fields = klass->GetIFields();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700170 if (fields != NULL) {
171 for (int32_t i = 0; i < fields->GetLength(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700172 mirror::ArtField* field = fields->Get(i);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700173 FieldHelper fh(field);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700174 if (!fh.IsPrimitiveType()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800175 mirror::Object* value = field->GetObj(obj);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700176 if (value != NULL) {
177 WalkFieldsInOrder(visited, callback, value, arg);
178 }
179 }
180 }
181 }
182}
183
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700184template<size_t kAlignment>
185void SpaceBitmap<kAlignment>::WalkFieldsInOrder(SpaceBitmap<kAlignment>* visited,
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700186 ObjectCallback* callback, mirror::Object* obj,
187 void* arg) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700188 if (visited->Test(obj)) {
189 return;
190 }
191 // visit the object itself
192 (*callback)(obj, arg);
193 visited->Set(obj);
194 // Walk instance fields of all objects
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800195 mirror::Class* klass = obj->GetClass();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700196 WalkInstanceFields(visited, callback, obj, klass, arg);
197 // Walk static fields of a Class
198 if (obj->IsClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700199 mirror::ObjectArray<mirror::ArtField>* fields = klass->GetSFields();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700200 if (fields != NULL) {
201 for (int32_t i = 0; i < fields->GetLength(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700202 mirror::ArtField* field = fields->Get(i);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700203 FieldHelper fh(field);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700204 if (!fh.IsPrimitiveType()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800205 mirror::Object* value = field->GetObj(NULL);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700206 if (value != NULL) {
207 WalkFieldsInOrder(visited, callback, value, arg);
208 }
209 }
210 }
211 }
212 } else if (obj->IsObjectArray()) {
213 // Walk elements of an object array
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800214 mirror::ObjectArray<mirror::Object>* obj_array = obj->AsObjectArray<mirror::Object>();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700215 int32_t length = obj_array->GetLength();
216 for (int32_t i = 0; i < length; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800217 mirror::Object* value = obj_array->Get(i);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700218 if (value != NULL) {
219 WalkFieldsInOrder(visited, callback, value, arg);
220 }
221 }
222 }
223}
224
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700225template<size_t kAlignment>
226void SpaceBitmap<kAlignment>::InOrderWalk(ObjectCallback* callback, void* arg) {
227 UniquePtr<SpaceBitmap<kAlignment>> visited(
228 Create("bitmap for in-order walk", reinterpret_cast<byte*>(heap_begin_),
229 IndexToOffset(bitmap_size_ / kWordSize)));
230 CHECK(bitmap_begin_ != nullptr);
231 CHECK(callback != nullptr);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700232 uintptr_t end = Size() / kWordSize;
233 for (uintptr_t i = 0; i < end; ++i) {
Andreas Gampecb8aea42014-04-02 15:39:58 -0700234 // Need uint for unsigned shift.
235 uword w = bitmap_begin_[i];
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700236 if (UNLIKELY(w != 0)) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700237 uintptr_t ptr_base = IndexToOffset(i) + heap_begin_;
238 while (w != 0) {
Andreas Gampecb8aea42014-04-02 15:39:58 -0700239 const size_t shift = CTZ(w);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800240 mirror::Object* obj = reinterpret_cast<mirror::Object*>(ptr_base + shift * kAlignment);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700241 WalkFieldsInOrder(visited.get(), callback, obj, arg);
Andreas Gampecb8aea42014-04-02 15:39:58 -0700242 w ^= (static_cast<uword>(1)) << shift;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700243 }
244 }
245 }
246}
247
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700248template class SpaceBitmap<kObjectAlignment>;
249template class SpaceBitmap<kPageSize>;
250
Ian Rogers1d54e732013-05-02 21:10:01 -0700251} // namespace accounting
252} // namespace gc
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700253} // namespace art