blob: 8e817e5bc5255e617c64f3ef17e9a8c367c4e146 [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>
Mathieu Chartier73d1e172014-04-11 17:53:48 -070024size_t SpaceBitmap<kAlignment>::ComputeBitmapSize(uint64_t capacity) {
25 const uint64_t kBytesCoveredPerWord = kAlignment * kBitsPerWord;
26 return (RoundUp(capacity, kBytesCoveredPerWord) / kBytesCoveredPerWord) * kWordSize;
27}
28
29template<size_t kAlignment>
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070030SpaceBitmap<kAlignment>* SpaceBitmap<kAlignment>::CreateFromMemMap(
31 const std::string& name, MemMap* mem_map, byte* heap_begin, size_t heap_capacity) {
Mathieu Chartier31e89252013-08-28 11:29:12 -070032 CHECK(mem_map != nullptr);
Andreas Gampecb8aea42014-04-02 15:39:58 -070033 uword* bitmap_begin = reinterpret_cast<uword*>(mem_map->Begin());
Mathieu Chartier73d1e172014-04-11 17:53:48 -070034 const size_t bitmap_size = ComputeBitmapSize(heap_capacity);
Mathieu Chartier31e89252013-08-28 11:29:12 -070035 return new SpaceBitmap(name, mem_map, bitmap_begin, bitmap_size, heap_begin);
36}
37
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070038template<size_t kAlignment>
Mathieu Chartierbbd695c2014-04-16 09:48:48 -070039SpaceBitmap<kAlignment>::SpaceBitmap(const std::string& name, MemMap* mem_map, uword* bitmap_begin,
40 size_t bitmap_size, const void* heap_begin)
41 : mem_map_(mem_map), bitmap_begin_(bitmap_begin), bitmap_size_(bitmap_size),
42 heap_begin_(reinterpret_cast<uintptr_t>(heap_begin)),
43 name_(name) {
44 CHECK(bitmap_begin_ != nullptr);
45 CHECK_NE(bitmap_size, 0U);
46}
47
48template<size_t kAlignment>
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070049SpaceBitmap<kAlignment>* SpaceBitmap<kAlignment>::Create(
50 const std::string& name, byte* heap_begin, size_t heap_capacity) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070051 // Round up since heap_capacity is not necessarily a multiple of kAlignment * kBitsPerWord.
Mathieu Chartier73d1e172014-04-11 17:53:48 -070052 const size_t bitmap_size = ComputeBitmapSize(heap_capacity);
Ian Rogers8d31bbd2013-10-13 10:44:14 -070053 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -070054 std::unique_ptr<MemMap> mem_map(MemMap::MapAnonymous(name.c_str(), nullptr, bitmap_size,
Ian Rogersef7d42f2014-01-06 12:55:46 -080055 PROT_READ | PROT_WRITE, false, &error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -070056 if (UNLIKELY(mem_map.get() == nullptr)) {
57 LOG(ERROR) << "Failed to allocate bitmap " << name << ": " << error_msg;
Mathieu Chartierbbd695c2014-04-16 09:48:48 -070058 return nullptr;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070059 }
Mathieu Chartier31e89252013-08-28 11:29:12 -070060 return CreateFromMemMap(name, mem_map.release(), heap_begin, heap_capacity);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070061}
62
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070063template<size_t kAlignment>
64void SpaceBitmap<kAlignment>::SetHeapLimit(uintptr_t new_end) {
Mathieu Chartierdcf8d722012-08-02 14:55:54 -070065 DCHECK(IsAligned<kBitsPerWord * kAlignment>(new_end));
66 size_t new_size = OffsetToIndex(new_end - heap_begin_) * kWordSize;
Mathieu Chartiercc236d72012-07-20 10:29:05 -070067 if (new_size < bitmap_size_) {
68 bitmap_size_ = new_size;
69 }
70 // Not sure if doing this trim is necessary, since nothing past the end of the heap capacity
71 // should be marked.
Mathieu Chartiercc236d72012-07-20 10:29:05 -070072}
73
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070074template<size_t kAlignment>
75void SpaceBitmap<kAlignment>::Clear() {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070076 if (bitmap_begin_ != NULL) {
Mathieu Chartier31e89252013-08-28 11:29:12 -070077 // This returns the memory to the system. Successive page faults will return zeroed memory.
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070078 int result = madvise(bitmap_begin_, bitmap_size_, MADV_DONTNEED);
79 if (result == -1) {
Mathieu Chartier7469ebf2012-09-24 16:28:36 -070080 PLOG(FATAL) << "madvise failed";
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070081 }
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070082 }
83}
84
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070085template<size_t kAlignment>
Mathieu Chartierbbd695c2014-04-16 09:48:48 -070086void SpaceBitmap<kAlignment>::CopyFrom(SpaceBitmap* source_bitmap) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -070087 DCHECK_EQ(Size(), source_bitmap->Size());
88 std::copy(source_bitmap->Begin(), source_bitmap->Begin() + source_bitmap->Size() / kWordSize, Begin());
89}
90
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070091template<size_t kAlignment>
Mathieu Chartierbbd695c2014-04-16 09:48:48 -070092void SpaceBitmap<kAlignment>::Walk(ObjectCallback* callback, void* arg) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070093 CHECK(bitmap_begin_ != NULL);
94 CHECK(callback != NULL);
Mathieu Chartier357e9be2012-08-01 11:00:14 -070095
96 uintptr_t end = OffsetToIndex(HeapLimit() - heap_begin_ - 1);
Andreas Gampecb8aea42014-04-02 15:39:58 -070097 uword* bitmap_begin = bitmap_begin_;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070098 for (uintptr_t i = 0; i <= end; ++i) {
Andreas Gampecb8aea42014-04-02 15:39:58 -070099 uword w = bitmap_begin[i];
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700100 if (w != 0) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700101 uintptr_t ptr_base = IndexToOffset(i) + heap_begin_;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700102 do {
Andreas Gampecb8aea42014-04-02 15:39:58 -0700103 const size_t shift = CTZ(w);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800104 mirror::Object* obj = reinterpret_cast<mirror::Object*>(ptr_base + shift * kAlignment);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700105 (*callback)(obj, arg);
Andreas Gampecb8aea42014-04-02 15:39:58 -0700106 w ^= (static_cast<uword>(1)) << shift;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700107 } while (w != 0);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700108 }
109 }
110}
111
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700112template<size_t kAlignment>
113void SpaceBitmap<kAlignment>::SweepWalk(const SpaceBitmap<kAlignment>& live_bitmap,
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700114 const SpaceBitmap<kAlignment>& mark_bitmap,
115 uintptr_t sweep_begin, uintptr_t sweep_end,
116 SpaceBitmap::SweepCallback* callback, void* arg) {
117 CHECK(live_bitmap.bitmap_begin_ != nullptr);
118 CHECK(mark_bitmap.bitmap_begin_ != nullptr);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700119 CHECK_EQ(live_bitmap.heap_begin_, mark_bitmap.heap_begin_);
120 CHECK_EQ(live_bitmap.bitmap_size_, mark_bitmap.bitmap_size_);
121 CHECK(callback != NULL);
122 CHECK_LE(sweep_begin, sweep_end);
123 CHECK_GE(sweep_begin, live_bitmap.heap_begin_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700124
125 if (sweep_end <= sweep_begin) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700126 return;
127 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700128
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800129 // TODO: rewrite the callbacks to accept a std::vector<mirror::Object*> rather than a mirror::Object**?
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700130 const size_t buffer_size = kWordSize * kBitsPerWord;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800131 mirror::Object* pointer_buf[buffer_size];
132 mirror::Object** pb = &pointer_buf[0];
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700133 size_t start = OffsetToIndex(sweep_begin - live_bitmap.heap_begin_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700134 size_t end = OffsetToIndex(sweep_end - live_bitmap.heap_begin_ - 1);
135 CHECK_LT(end, live_bitmap.Size() / kWordSize);
Andreas Gampecb8aea42014-04-02 15:39:58 -0700136 uword* live = live_bitmap.bitmap_begin_;
137 uword* mark = mark_bitmap.bitmap_begin_;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700138 for (size_t i = start; i <= end; i++) {
Andreas Gampecb8aea42014-04-02 15:39:58 -0700139 uword garbage = live[i] & ~mark[i];
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700140 if (UNLIKELY(garbage != 0)) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700141 uintptr_t ptr_base = IndexToOffset(i) + live_bitmap.heap_begin_;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700142 do {
Andreas Gampecb8aea42014-04-02 15:39:58 -0700143 const size_t shift = CTZ(garbage);
144 garbage ^= (static_cast<uword>(1)) << shift;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800145 *pb++ = reinterpret_cast<mirror::Object*>(ptr_base + shift * kAlignment);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700146 } while (garbage != 0);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700147 // Make sure that there are always enough slots available for an
148 // entire word of one bits.
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700149 if (pb >= &pointer_buf[buffer_size - kBitsPerWord]) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700150 (*callback)(pb - &pointer_buf[0], &pointer_buf[0], arg);
151 pb = &pointer_buf[0];
152 }
153 }
154 }
155 if (pb > &pointer_buf[0]) {
156 (*callback)(pb - &pointer_buf[0], &pointer_buf[0], arg);
157 }
158}
159
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700160template<size_t kAlignment>
161void SpaceBitmap<kAlignment>::WalkInstanceFields(SpaceBitmap<kAlignment>* visited,
162 ObjectCallback* callback, mirror::Object* obj,
163 mirror::Class* klass, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700164 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700165 // Visit fields of parent classes first.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800166 mirror::Class* super = klass->GetSuperClass();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700167 if (super != NULL) {
168 WalkInstanceFields(visited, callback, obj, super, arg);
169 }
170 // Walk instance fields
Brian Carlstromea46f952013-07-30 01:26:50 -0700171 mirror::ObjectArray<mirror::ArtField>* fields = klass->GetIFields();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700172 if (fields != NULL) {
173 for (int32_t i = 0; i < fields->GetLength(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700174 mirror::ArtField* field = fields->Get(i);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700175 FieldHelper fh(field);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700176 if (!fh.IsPrimitiveType()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800177 mirror::Object* value = field->GetObj(obj);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700178 if (value != NULL) {
179 WalkFieldsInOrder(visited, callback, value, arg);
180 }
181 }
182 }
183 }
184}
185
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700186template<size_t kAlignment>
187void SpaceBitmap<kAlignment>::WalkFieldsInOrder(SpaceBitmap<kAlignment>* visited,
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700188 ObjectCallback* callback, mirror::Object* obj,
189 void* arg) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700190 if (visited->Test(obj)) {
191 return;
192 }
193 // visit the object itself
194 (*callback)(obj, arg);
195 visited->Set(obj);
196 // Walk instance fields of all objects
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800197 mirror::Class* klass = obj->GetClass();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700198 WalkInstanceFields(visited, callback, obj, klass, arg);
199 // Walk static fields of a Class
200 if (obj->IsClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700201 mirror::ObjectArray<mirror::ArtField>* fields = klass->GetSFields();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700202 if (fields != NULL) {
203 for (int32_t i = 0; i < fields->GetLength(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700204 mirror::ArtField* field = fields->Get(i);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700205 FieldHelper fh(field);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700206 if (!fh.IsPrimitiveType()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800207 mirror::Object* value = field->GetObj(NULL);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700208 if (value != NULL) {
209 WalkFieldsInOrder(visited, callback, value, arg);
210 }
211 }
212 }
213 }
214 } else if (obj->IsObjectArray()) {
215 // Walk elements of an object array
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800216 mirror::ObjectArray<mirror::Object>* obj_array = obj->AsObjectArray<mirror::Object>();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700217 int32_t length = obj_array->GetLength();
218 for (int32_t i = 0; i < length; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800219 mirror::Object* value = obj_array->Get(i);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700220 if (value != NULL) {
221 WalkFieldsInOrder(visited, callback, value, arg);
222 }
223 }
224 }
225}
226
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700227template<size_t kAlignment>
228void SpaceBitmap<kAlignment>::InOrderWalk(ObjectCallback* callback, void* arg) {
Ian Rogers700a4022014-05-19 16:49:03 -0700229 std::unique_ptr<SpaceBitmap<kAlignment>> visited(
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700230 Create("bitmap for in-order walk", reinterpret_cast<byte*>(heap_begin_),
231 IndexToOffset(bitmap_size_ / kWordSize)));
232 CHECK(bitmap_begin_ != nullptr);
233 CHECK(callback != nullptr);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700234 uintptr_t end = Size() / kWordSize;
235 for (uintptr_t i = 0; i < end; ++i) {
Andreas Gampecb8aea42014-04-02 15:39:58 -0700236 // Need uint for unsigned shift.
237 uword w = bitmap_begin_[i];
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700238 if (UNLIKELY(w != 0)) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700239 uintptr_t ptr_base = IndexToOffset(i) + heap_begin_;
240 while (w != 0) {
Andreas Gampecb8aea42014-04-02 15:39:58 -0700241 const size_t shift = CTZ(w);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800242 mirror::Object* obj = reinterpret_cast<mirror::Object*>(ptr_base + shift * kAlignment);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700243 WalkFieldsInOrder(visited.get(), callback, obj, arg);
Andreas Gampecb8aea42014-04-02 15:39:58 -0700244 w ^= (static_cast<uword>(1)) << shift;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700245 }
246 }
247 }
248}
249
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700250template class SpaceBitmap<kObjectAlignment>;
251template class SpaceBitmap<kPageSize>;
252
Ian Rogers1d54e732013-05-02 21:10:01 -0700253} // namespace accounting
254} // namespace gc
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700255} // namespace art