blob: e7865363a104514fe7ee7b33fcc6c8352f425940 [file] [log] [blame]
Hiroshi Yamauchi7cb7bbc2013-11-18 17:27:37 -08001
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07002/*
3 * Copyright (C) 2013 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070018#include "rosalloc_space-inl.h"
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070019
Andreas Gampe170331f2017-12-07 18:41:03 -080020#include "base/logging.h" // For VLOG.
Vladimir Marko80afd022015-05-19 18:08:00 +010021#include "base/time_utils.h"
David Sehrc431b9d2018-03-02 12:01:51 -080022#include "base/utils.h"
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070023#include "gc/accounting/card_table.h"
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070024#include "gc/accounting/space_bitmap-inl.h"
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070025#include "gc/heap.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070026#include "memory_tool_malloc_space-inl.h"
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070027#include "mirror/class-inl.h"
28#include "mirror/object-inl.h"
29#include "runtime.h"
Andreas Gampe508fdf32017-06-05 16:42:13 -070030#include "scoped_thread_state_change-inl.h"
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070031#include "thread.h"
32#include "thread_list.h"
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070033
34namespace art {
35namespace gc {
36namespace space {
37
Mathieu Chartier73d1e172014-04-11 17:53:48 -070038static constexpr bool kPrefetchDuringRosAllocFreeList = false;
Mathieu Chartier8585bad2014-04-11 17:53:48 -070039static constexpr size_t kPrefetchLookAhead = 8;
40// Use this only for verification, it is not safe to use since the class of the object may have
41// been freed.
42static constexpr bool kVerifyFreedBytes = false;
Ian Rogers6fac4472014-02-25 17:01:10 -080043
Mathieu Chartier31f44142014-04-08 14:40:03 -070044// TODO: Fix
Evgenii Stepanov1e133742015-05-20 12:30:59 -070045// template class MemoryToolMallocSpace<RosAllocSpace, allocator::RosAlloc*>;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070046
Andreas Gamped7576322014-10-24 22:13:45 -070047RosAllocSpace::RosAllocSpace(MemMap* mem_map, size_t initial_size, const std::string& name,
Ian Rogers13735952014-10-08 12:43:28 -070048 art::gc::allocator::RosAlloc* rosalloc, uint8_t* begin, uint8_t* end,
49 uint8_t* limit, size_t growth_limit, bool can_move_objects,
Andreas Gamped7576322014-10-24 22:13:45 -070050 size_t starting_size, bool low_memory_mode)
Mathieu Chartier31f44142014-04-08 14:40:03 -070051 : MallocSpace(name, mem_map, begin, end, limit, growth_limit, true, can_move_objects,
52 starting_size, initial_size),
53 rosalloc_(rosalloc), low_memory_mode_(low_memory_mode) {
54 CHECK(rosalloc != nullptr);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070055}
56
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080057RosAllocSpace* RosAllocSpace::CreateFromMemMap(MemMap* mem_map, const std::string& name,
Ian Rogersa55cf412014-02-27 00:31:26 -080058 size_t starting_size, size_t initial_size,
59 size_t growth_limit, size_t capacity,
Mathieu Chartier31f44142014-04-08 14:40:03 -070060 bool low_memory_mode, bool can_move_objects) {
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080061 DCHECK(mem_map != nullptr);
Andreas Gamped7576322014-10-24 22:13:45 -070062
Evgenii Stepanov1e133742015-05-20 12:30:59 -070063 bool running_on_memory_tool = Runtime::Current()->IsRunningOnMemoryTool();
Andreas Gamped7576322014-10-24 22:13:45 -070064
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080065 allocator::RosAlloc* rosalloc = CreateRosAlloc(mem_map->Begin(), starting_size, initial_size,
Evgenii Stepanov1e133742015-05-20 12:30:59 -070066 capacity, low_memory_mode, running_on_memory_tool);
Mathieu Chartier2cebb242015-04-21 16:50:40 -070067 if (rosalloc == nullptr) {
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080068 LOG(ERROR) << "Failed to initialize rosalloc for alloc space (" << name << ")";
Mathieu Chartier2cebb242015-04-21 16:50:40 -070069 return nullptr;
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080070 }
71
lzang1385de732014-02-21 14:15:01 +080072 // Protect memory beyond the starting size. MoreCore will add r/w permissions when necessory
Ian Rogers13735952014-10-08 12:43:28 -070073 uint8_t* end = mem_map->Begin() + starting_size;
lzang1385de732014-02-21 14:15:01 +080074 if (capacity - starting_size > 0) {
Mathieu Chartier3425d022017-10-03 16:22:05 -070075 CheckedCall(mprotect, name.c_str(), end, capacity - starting_size, PROT_NONE);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080076 }
77
78 // Everything is set so record in immutable structure and leave
Ian Rogers13735952014-10-08 12:43:28 -070079 uint8_t* begin = mem_map->Begin();
Andreas Gampe8b362a82018-05-22 20:54:14 +000080 // TODO: Fix RosAllocSpace to support Valgrind/ASan. There is currently some issues with
Mathieu Chartier661974a2014-01-09 11:23:53 -080081 // AllocationSize caused by redzones. b/12944686
Evgenii Stepanov1e133742015-05-20 12:30:59 -070082 if (running_on_memory_tool) {
83 return new MemoryToolMallocSpace<RosAllocSpace, kDefaultMemoryToolRedZoneBytes, false, true>(
Andreas Gamped7576322014-10-24 22:13:45 -070084 mem_map, initial_size, name, rosalloc, begin, end, begin + capacity, growth_limit,
85 can_move_objects, starting_size, low_memory_mode);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080086 } else {
Andreas Gamped7576322014-10-24 22:13:45 -070087 return new RosAllocSpace(mem_map, initial_size, name, rosalloc, begin, end, begin + capacity,
88 growth_limit, can_move_objects, starting_size, low_memory_mode);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080089 }
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080090}
91
Mathieu Chartier661974a2014-01-09 11:23:53 -080092RosAllocSpace::~RosAllocSpace() {
93 delete rosalloc_;
94}
95
Ian Rogers6fac4472014-02-25 17:01:10 -080096RosAllocSpace* RosAllocSpace::Create(const std::string& name, size_t initial_size,
Ian Rogers13735952014-10-08 12:43:28 -070097 size_t growth_limit, size_t capacity, uint8_t* requested_begin,
Mathieu Chartier31f44142014-04-08 14:40:03 -070098 bool low_memory_mode, bool can_move_objects) {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070099 uint64_t start_time = 0;
100 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
101 start_time = NanoTime();
102 VLOG(startup) << "RosAllocSpace::Create entering " << name
103 << " initial_size=" << PrettySize(initial_size)
104 << " growth_limit=" << PrettySize(growth_limit)
105 << " capacity=" << PrettySize(capacity)
106 << " requested_begin=" << reinterpret_cast<void*>(requested_begin);
107 }
108
109 // Memory we promise to rosalloc before it asks for morecore.
110 // Note: making this value large means that large allocations are unlikely to succeed as rosalloc
111 // will ask for this memory from sys_alloc which will fail as the footprint (this value plus the
112 // size of the large allocation) will be greater than the footprint limit.
Hiroshi Yamauchi5ccd4982014-03-11 12:19:04 -0700113 size_t starting_size = Heap::kDefaultStartingSize;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700114 MemMap* mem_map = CreateMemMap(name, starting_size, &initial_size, &growth_limit, &capacity,
115 requested_begin);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700116 if (mem_map == nullptr) {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700117 LOG(ERROR) << "Failed to create mem map for alloc space (" << name << ") of size "
118 << PrettySize(capacity);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700119 return nullptr;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700120 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700121
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800122 RosAllocSpace* space = CreateFromMemMap(mem_map, name, starting_size, initial_size,
Mathieu Chartier31f44142014-04-08 14:40:03 -0700123 growth_limit, capacity, low_memory_mode,
124 can_move_objects);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700125 // We start out with only the initial size possibly containing objects.
126 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
127 LOG(INFO) << "RosAllocSpace::Create exiting (" << PrettyDuration(NanoTime() - start_time)
128 << " ) " << *space;
129 }
130 return space;
131}
132
Mathieu Chartier31f44142014-04-08 14:40:03 -0700133allocator::RosAlloc* RosAllocSpace::CreateRosAlloc(void* begin, size_t morecore_start,
134 size_t initial_size,
Andreas Gamped7576322014-10-24 22:13:45 -0700135 size_t maximum_size, bool low_memory_mode,
Evgenii Stepanov1e133742015-05-20 12:30:59 -0700136 bool running_on_memory_tool) {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700137 // clear errno to allow PLOG on error
138 errno = 0;
139 // create rosalloc using our backing storage starting at begin and
140 // with a footprint of morecore_start. When morecore_start bytes of
141 // memory is exhaused morecore will be called.
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800142 allocator::RosAlloc* rosalloc = new art::gc::allocator::RosAlloc(
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -0800143 begin, morecore_start, maximum_size,
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800144 low_memory_mode ?
145 art::gc::allocator::RosAlloc::kPageReleaseModeAll :
Andreas Gamped7576322014-10-24 22:13:45 -0700146 art::gc::allocator::RosAlloc::kPageReleaseModeSizeAndEnd,
Evgenii Stepanov1e133742015-05-20 12:30:59 -0700147 running_on_memory_tool);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700148 if (rosalloc != nullptr) {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700149 rosalloc->SetFootprintLimit(initial_size);
150 } else {
151 PLOG(ERROR) << "RosAlloc::Create failed";
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800152 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700153 return rosalloc;
154}
155
Ian Rogers6fac4472014-02-25 17:01:10 -0800156mirror::Object* RosAllocSpace::AllocWithGrowth(Thread* self, size_t num_bytes,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700157 size_t* bytes_allocated, size_t* usable_size,
158 size_t* bytes_tl_bulk_allocated) {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700159 mirror::Object* result;
160 {
161 MutexLock mu(self, lock_);
162 // Grow as much as possible within the space.
163 size_t max_allowed = Capacity();
164 rosalloc_->SetFootprintLimit(max_allowed);
165 // Try the allocation.
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700166 result = AllocCommon(self, num_bytes, bytes_allocated, usable_size,
167 bytes_tl_bulk_allocated);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700168 // Shrink back down as small as possible.
169 size_t footprint = rosalloc_->Footprint();
170 rosalloc_->SetFootprintLimit(footprint);
171 }
172 // Note RosAlloc zeroes memory internally.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700173 // Return the new allocation or null.
Mathieu Chartier0651d412014-04-29 14:37:57 -0700174 CHECK(!kDebugSpaces || result == nullptr || Contains(result));
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700175 return result;
176}
177
Andreas Gamped7576322014-10-24 22:13:45 -0700178MallocSpace* RosAllocSpace::CreateInstance(MemMap* mem_map, const std::string& name,
179 void* allocator, uint8_t* begin, uint8_t* end,
180 uint8_t* limit, size_t growth_limit,
Mathieu Chartier31f44142014-04-08 14:40:03 -0700181 bool can_move_objects) {
Evgenii Stepanov1e133742015-05-20 12:30:59 -0700182 if (Runtime::Current()->IsRunningOnMemoryTool()) {
183 return new MemoryToolMallocSpace<RosAllocSpace, kDefaultMemoryToolRedZoneBytes, false, true>(
Andreas Gamped7576322014-10-24 22:13:45 -0700184 mem_map, initial_size_, name, reinterpret_cast<allocator::RosAlloc*>(allocator), begin, end,
185 limit, growth_limit, can_move_objects, starting_size_, low_memory_mode_);
186 } else {
187 return new RosAllocSpace(mem_map, initial_size_, name,
188 reinterpret_cast<allocator::RosAlloc*>(allocator), begin, end, limit,
189 growth_limit, can_move_objects, starting_size_, low_memory_mode_);
190 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700191}
192
193size_t RosAllocSpace::Free(Thread* self, mirror::Object* ptr) {
194 if (kDebugSpaces) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700195 CHECK(ptr != nullptr);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700196 CHECK(Contains(ptr)) << "Free (" << ptr << ") not in bounds of heap " << *this;
197 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700198 if (kRecentFreeCount > 0) {
199 MutexLock mu(self, lock_);
200 RegisterRecentFree(ptr);
201 }
Mathieu Chartier8585bad2014-04-11 17:53:48 -0700202 return rosalloc_->Free(self, ptr);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700203}
204
205size_t RosAllocSpace::FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs) {
Mathieu Chartier8585bad2014-04-11 17:53:48 -0700206 DCHECK(ptrs != nullptr);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700207
Mathieu Chartier8585bad2014-04-11 17:53:48 -0700208 size_t verify_bytes = 0;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700209 for (size_t i = 0; i < num_ptrs; i++) {
Mathieu Chartier8585bad2014-04-11 17:53:48 -0700210 if (kPrefetchDuringRosAllocFreeList && i + kPrefetchLookAhead < num_ptrs) {
211 __builtin_prefetch(reinterpret_cast<char*>(ptrs[i + kPrefetchLookAhead]));
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700212 }
Mathieu Chartier8585bad2014-04-11 17:53:48 -0700213 if (kVerifyFreedBytes) {
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700214 verify_bytes += AllocationSizeNonvirtual<true>(ptrs[i], nullptr);
Mathieu Chartier8585bad2014-04-11 17:53:48 -0700215 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700216 }
217
218 if (kRecentFreeCount > 0) {
219 MutexLock mu(self, lock_);
220 for (size_t i = 0; i < num_ptrs; i++) {
221 RegisterRecentFree(ptrs[i]);
222 }
223 }
224
225 if (kDebugSpaces) {
226 size_t num_broken_ptrs = 0;
227 for (size_t i = 0; i < num_ptrs; i++) {
228 if (!Contains(ptrs[i])) {
229 num_broken_ptrs++;
230 LOG(ERROR) << "FreeList[" << i << "] (" << ptrs[i] << ") not in bounds of heap " << *this;
231 } else {
232 size_t size = rosalloc_->UsableSize(ptrs[i]);
233 memset(ptrs[i], 0xEF, size);
234 }
235 }
236 CHECK_EQ(num_broken_ptrs, 0u);
237 }
238
Mathieu Chartier8585bad2014-04-11 17:53:48 -0700239 const size_t bytes_freed = rosalloc_->BulkFree(self, reinterpret_cast<void**>(ptrs), num_ptrs);
240 if (kVerifyFreedBytes) {
241 CHECK_EQ(verify_bytes, bytes_freed);
242 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700243 return bytes_freed;
244}
245
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700246size_t RosAllocSpace::Trim() {
Hiroshi Yamauchid9a88de2014-04-07 13:52:31 -0700247 VLOG(heap) << "RosAllocSpace::Trim() ";
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800248 {
Mathieu Chartiera9d82fe2016-01-25 20:06:11 -0800249 Thread* const self = Thread::Current();
250 // SOA required for Rosalloc::Trim() -> ArtRosAllocMoreCore() -> Heap::GetRosAllocSpace.
251 ScopedObjectAccess soa(self);
252 MutexLock mu(self, lock_);
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800253 // Trim to release memory at the end of the space.
254 rosalloc_->Trim();
255 }
256 // Attempt to release pages if it does not release all empty pages.
257 if (!rosalloc_->DoesReleaseAllPages()) {
Hiroshi Yamauchid9a88de2014-04-07 13:52:31 -0700258 return rosalloc_->ReleasePages();
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800259 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700260 return 0;
261}
262
263void RosAllocSpace::Walk(void(*callback)(void *start, void *end, size_t num_bytes, void* callback_arg),
264 void* arg) {
Hiroshi Yamauchi1cd53db2014-03-28 15:26:48 -0700265 InspectAllRosAlloc(callback, arg, true);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700266}
267
268size_t RosAllocSpace::GetFootprint() {
269 MutexLock mu(Thread::Current(), lock_);
270 return rosalloc_->Footprint();
271}
272
273size_t RosAllocSpace::GetFootprintLimit() {
274 MutexLock mu(Thread::Current(), lock_);
275 return rosalloc_->FootprintLimit();
276}
277
278void RosAllocSpace::SetFootprintLimit(size_t new_size) {
279 MutexLock mu(Thread::Current(), lock_);
280 VLOG(heap) << "RosAllocSpace::SetFootprintLimit " << PrettySize(new_size);
281 // Compare against the actual footprint, rather than the Size(), because the heap may not have
282 // grown all the way to the allowed size yet.
283 size_t current_space_size = rosalloc_->Footprint();
284 if (new_size < current_space_size) {
285 // Don't let the space grow any more.
286 new_size = current_space_size;
287 }
288 rosalloc_->SetFootprintLimit(new_size);
289}
290
291uint64_t RosAllocSpace::GetBytesAllocated() {
Hiroshi Yamauchi4ce1f002013-11-18 14:49:09 -0800292 size_t bytes_allocated = 0;
Hiroshi Yamauchi1cd53db2014-03-28 15:26:48 -0700293 InspectAllRosAlloc(art::gc::allocator::RosAlloc::BytesAllocatedCallback, &bytes_allocated, false);
Hiroshi Yamauchi4ce1f002013-11-18 14:49:09 -0800294 return bytes_allocated;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700295}
296
297uint64_t RosAllocSpace::GetObjectsAllocated() {
Hiroshi Yamauchi4ce1f002013-11-18 14:49:09 -0800298 size_t objects_allocated = 0;
Hiroshi Yamauchi1cd53db2014-03-28 15:26:48 -0700299 InspectAllRosAlloc(art::gc::allocator::RosAlloc::ObjectsAllocatedCallback, &objects_allocated, false);
Hiroshi Yamauchi4ce1f002013-11-18 14:49:09 -0800300 return objects_allocated;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700301}
302
Hiroshi Yamauchi1cd53db2014-03-28 15:26:48 -0700303void RosAllocSpace::InspectAllRosAllocWithSuspendAll(
304 void (*callback)(void *start, void *end, size_t num_bytes, void* callback_arg),
305 void* arg, bool do_null_callback_at_end) NO_THREAD_SAFETY_ANALYSIS {
306 // TODO: NO_THREAD_SAFETY_ANALYSIS.
307 Thread* self = Thread::Current();
Mathieu Chartier4f55e222015-09-04 13:26:21 -0700308 ScopedSuspendAll ssa(__FUNCTION__);
309 MutexLock mu(self, *Locks::runtime_shutdown_lock_);
310 MutexLock mu2(self, *Locks::thread_list_lock_);
311 rosalloc_->InspectAll(callback, arg);
312 if (do_null_callback_at_end) {
313 callback(nullptr, nullptr, 0, arg); // Indicate end of a space.
Hiroshi Yamauchi1cd53db2014-03-28 15:26:48 -0700314 }
Hiroshi Yamauchi1cd53db2014-03-28 15:26:48 -0700315}
316
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700317void RosAllocSpace::InspectAllRosAlloc(void (*callback)(void *start, void *end, size_t num_bytes, void* callback_arg),
Hiroshi Yamauchi1cd53db2014-03-28 15:26:48 -0700318 void* arg, bool do_null_callback_at_end) NO_THREAD_SAFETY_ANALYSIS {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700319 // TODO: NO_THREAD_SAFETY_ANALYSIS.
320 Thread* self = Thread::Current();
321 if (Locks::mutator_lock_->IsExclusiveHeld(self)) {
322 // The mutators are already suspended. For example, a call path
323 // from SignalCatcher::HandleSigQuit().
324 rosalloc_->InspectAll(callback, arg);
Hiroshi Yamauchi1cd53db2014-03-28 15:26:48 -0700325 if (do_null_callback_at_end) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700326 callback(nullptr, nullptr, 0, arg); // Indicate end of a space.
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700327 }
Hiroshi Yamauchi1cd53db2014-03-28 15:26:48 -0700328 } else if (Locks::mutator_lock_->IsSharedHeld(self)) {
329 // The mutators are not suspended yet and we have a shared access
330 // to the mutator lock. Temporarily release the shared access by
331 // transitioning to the suspend state, and suspend the mutators.
Mathieu Chartierf1d666e2015-09-03 16:13:34 -0700332 ScopedThreadSuspension sts(self, kSuspended);
Hiroshi Yamauchi1cd53db2014-03-28 15:26:48 -0700333 InspectAllRosAllocWithSuspendAll(callback, arg, do_null_callback_at_end);
Hiroshi Yamauchi1cd53db2014-03-28 15:26:48 -0700334 } else {
335 // The mutators are not suspended yet. Suspend the mutators.
336 InspectAllRosAllocWithSuspendAll(callback, arg, do_null_callback_at_end);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700337 }
338}
339
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700340size_t RosAllocSpace::RevokeThreadLocalBuffers(Thread* thread) {
341 return rosalloc_->RevokeThreadLocalRuns(thread);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700342}
343
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700344size_t RosAllocSpace::RevokeAllThreadLocalBuffers() {
345 return rosalloc_->RevokeAllThreadLocalRuns();
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700346}
347
Ian Rogers68d8b422014-07-17 11:09:10 -0700348void RosAllocSpace::AssertThreadLocalBuffersAreRevoked(Thread* thread) {
349 if (kIsDebugBuild) {
350 rosalloc_->AssertThreadLocalRunsAreRevoked(thread);
351 }
352}
353
Hiroshi Yamauchic93c5302014-03-20 16:15:37 -0700354void RosAllocSpace::AssertAllThreadLocalBuffersAreRevoked() {
355 if (kIsDebugBuild) {
356 rosalloc_->AssertAllThreadLocalRunsAreRevoked();
357 }
358}
359
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800360void RosAllocSpace::Clear() {
Mathieu Chartier31f44142014-04-08 14:40:03 -0700361 size_t footprint_limit = GetFootprintLimit();
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800362 madvise(GetMemMap()->Begin(), GetMemMap()->Size(), MADV_DONTNEED);
Mathieu Chartier31f44142014-04-08 14:40:03 -0700363 live_bitmap_->Clear();
364 mark_bitmap_->Clear();
Ian Rogersbe2a1df2014-07-10 00:56:36 -0700365 SetEnd(begin_ + starting_size_);
Mathieu Chartier31f44142014-04-08 14:40:03 -0700366 delete rosalloc_;
Hiroshi Yamauchi94c41df2014-12-18 21:19:24 -0800367 rosalloc_ = CreateRosAlloc(mem_map_->Begin(), starting_size_, initial_size_,
368 NonGrowthLimitCapacity(), low_memory_mode_,
Evgenii Stepanov1e133742015-05-20 12:30:59 -0700369 Runtime::Current()->IsRunningOnMemoryTool());
Mathieu Chartier31f44142014-04-08 14:40:03 -0700370 SetFootprintLimit(footprint_limit);
Mathieu Chartier15d34022014-02-26 17:16:38 -0800371}
372
Hiroshi Yamauchi565c2d92016-03-23 12:53:26 -0700373void RosAllocSpace::DumpStats(std::ostream& os) {
374 ScopedSuspendAll ssa(__FUNCTION__);
375 rosalloc_->DumpStats(os);
376}
377
Andreas Gamped4901292017-05-30 18:41:34 -0700378template<bool kMaybeIsRunningOnMemoryTool>
379size_t RosAllocSpace::AllocationSizeNonvirtual(mirror::Object* obj, size_t* usable_size) {
380 // obj is a valid object. Use its class in the header to get the size.
381 // Don't use verification since the object may be dead if we are sweeping.
382 size_t size = obj->SizeOf<kVerifyNone>();
383 bool add_redzones = false;
384 if (kMaybeIsRunningOnMemoryTool) {
Andreas Gampe8b362a82018-05-22 20:54:14 +0000385 add_redzones = RUNNING_ON_MEMORY_TOOL ? kMemoryToolAddsRedzones : 0;
Andreas Gamped4901292017-05-30 18:41:34 -0700386 if (add_redzones) {
387 size += 2 * kDefaultMemoryToolRedZoneBytes;
388 }
389 } else {
Andreas Gampe8b362a82018-05-22 20:54:14 +0000390 DCHECK_EQ(RUNNING_ON_MEMORY_TOOL, 0U);
Andreas Gamped4901292017-05-30 18:41:34 -0700391 }
392 size_t size_by_size = rosalloc_->UsableSize(size);
393 if (kIsDebugBuild) {
394 // On memory tool, the red zone has an impact...
395 const uint8_t* obj_ptr = reinterpret_cast<const uint8_t*>(obj);
396 size_t size_by_ptr = rosalloc_->UsableSize(
397 obj_ptr - (add_redzones ? kDefaultMemoryToolRedZoneBytes : 0));
398 if (size_by_size != size_by_ptr) {
399 LOG(INFO) << "Found a bad sized obj of size " << size
400 << " at " << std::hex << reinterpret_cast<intptr_t>(obj_ptr) << std::dec
401 << " size_by_size=" << size_by_size << " size_by_ptr=" << size_by_ptr;
402 }
403 DCHECK_EQ(size_by_size, size_by_ptr);
404 }
405 if (usable_size != nullptr) {
406 *usable_size = size_by_size;
407 }
408 return size_by_size;
409}
410
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700411} // namespace space
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800412
413namespace allocator {
414
415// Callback from rosalloc when it needs to increase the footprint.
Mathieu Chartiera9d82fe2016-01-25 20:06:11 -0800416void* ArtRosAllocMoreCore(allocator::RosAlloc* rosalloc, intptr_t increment)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700417 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800418 Heap* heap = Runtime::Current()->GetHeap();
419 art::gc::space::RosAllocSpace* rosalloc_space = heap->GetRosAllocSpace(rosalloc);
420 DCHECK(rosalloc_space != nullptr);
421 DCHECK_EQ(rosalloc_space->GetRosAlloc(), rosalloc);
422 return rosalloc_space->MoreCore(increment);
423}
424
425} // namespace allocator
426
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700427} // namespace gc
428} // namespace art