blob: eca0e43a972bf530d704712a0c95ddaf0421c413 [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
Vladimir Marko80afd022015-05-19 18:08:00 +010020#include "base/time_utils.h"
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070021#include "gc/accounting/card_table.h"
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070022#include "gc/accounting/space_bitmap-inl.h"
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070023#include "gc/heap.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070024#include "memory_tool_malloc_space-inl.h"
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070025#include "mirror/class-inl.h"
26#include "mirror/object-inl.h"
27#include "runtime.h"
Andreas Gampe508fdf32017-06-05 16:42:13 -070028#include "scoped_thread_state_change-inl.h"
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070029#include "thread.h"
30#include "thread_list.h"
31#include "utils.h"
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070032
33namespace art {
34namespace gc {
35namespace space {
36
Mathieu Chartier73d1e172014-04-11 17:53:48 -070037static constexpr bool kPrefetchDuringRosAllocFreeList = false;
Mathieu Chartier8585bad2014-04-11 17:53:48 -070038static constexpr size_t kPrefetchLookAhead = 8;
39// Use this only for verification, it is not safe to use since the class of the object may have
40// been freed.
41static constexpr bool kVerifyFreedBytes = false;
Ian Rogers6fac4472014-02-25 17:01:10 -080042
Mathieu Chartier31f44142014-04-08 14:40:03 -070043// TODO: Fix
Evgenii Stepanov1e133742015-05-20 12:30:59 -070044// template class MemoryToolMallocSpace<RosAllocSpace, allocator::RosAlloc*>;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070045
Andreas Gamped7576322014-10-24 22:13:45 -070046RosAllocSpace::RosAllocSpace(MemMap* mem_map, size_t initial_size, const std::string& name,
Ian Rogers13735952014-10-08 12:43:28 -070047 art::gc::allocator::RosAlloc* rosalloc, uint8_t* begin, uint8_t* end,
48 uint8_t* limit, size_t growth_limit, bool can_move_objects,
Andreas Gamped7576322014-10-24 22:13:45 -070049 size_t starting_size, bool low_memory_mode)
Mathieu Chartier31f44142014-04-08 14:40:03 -070050 : MallocSpace(name, mem_map, begin, end, limit, growth_limit, true, can_move_objects,
51 starting_size, initial_size),
52 rosalloc_(rosalloc), low_memory_mode_(low_memory_mode) {
53 CHECK(rosalloc != nullptr);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070054}
55
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080056RosAllocSpace* RosAllocSpace::CreateFromMemMap(MemMap* mem_map, const std::string& name,
Ian Rogersa55cf412014-02-27 00:31:26 -080057 size_t starting_size, size_t initial_size,
58 size_t growth_limit, size_t capacity,
Mathieu Chartier31f44142014-04-08 14:40:03 -070059 bool low_memory_mode, bool can_move_objects) {
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080060 DCHECK(mem_map != nullptr);
Andreas Gamped7576322014-10-24 22:13:45 -070061
Evgenii Stepanov1e133742015-05-20 12:30:59 -070062 bool running_on_memory_tool = Runtime::Current()->IsRunningOnMemoryTool();
Andreas Gamped7576322014-10-24 22:13:45 -070063
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080064 allocator::RosAlloc* rosalloc = CreateRosAlloc(mem_map->Begin(), starting_size, initial_size,
Evgenii Stepanov1e133742015-05-20 12:30:59 -070065 capacity, low_memory_mode, running_on_memory_tool);
Mathieu Chartier2cebb242015-04-21 16:50:40 -070066 if (rosalloc == nullptr) {
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080067 LOG(ERROR) << "Failed to initialize rosalloc for alloc space (" << name << ")";
Mathieu Chartier2cebb242015-04-21 16:50:40 -070068 return nullptr;
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080069 }
70
lzang1385de732014-02-21 14:15:01 +080071 // Protect memory beyond the starting size. MoreCore will add r/w permissions when necessory
Ian Rogers13735952014-10-08 12:43:28 -070072 uint8_t* end = mem_map->Begin() + starting_size;
lzang1385de732014-02-21 14:15:01 +080073 if (capacity - starting_size > 0) {
74 CHECK_MEMORY_CALL(mprotect, (end, capacity - starting_size, PROT_NONE), name);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080075 }
76
77 // Everything is set so record in immutable structure and leave
Ian Rogers13735952014-10-08 12:43:28 -070078 uint8_t* begin = mem_map->Begin();
Evgenii Stepanov1e133742015-05-20 12:30:59 -070079 // TODO: Fix RosAllocSpace to support Valgrind/ASan. There is currently some issues with
Mathieu Chartier661974a2014-01-09 11:23:53 -080080 // AllocationSize caused by redzones. b/12944686
Evgenii Stepanov1e133742015-05-20 12:30:59 -070081 if (running_on_memory_tool) {
82 return new MemoryToolMallocSpace<RosAllocSpace, kDefaultMemoryToolRedZoneBytes, false, true>(
Andreas Gamped7576322014-10-24 22:13:45 -070083 mem_map, initial_size, name, rosalloc, begin, end, begin + capacity, growth_limit,
84 can_move_objects, starting_size, low_memory_mode);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080085 } else {
Andreas Gamped7576322014-10-24 22:13:45 -070086 return new RosAllocSpace(mem_map, initial_size, name, rosalloc, begin, end, begin + capacity,
87 growth_limit, can_move_objects, starting_size, low_memory_mode);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080088 }
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080089}
90
Mathieu Chartier661974a2014-01-09 11:23:53 -080091RosAllocSpace::~RosAllocSpace() {
92 delete rosalloc_;
93}
94
Ian Rogers6fac4472014-02-25 17:01:10 -080095RosAllocSpace* RosAllocSpace::Create(const std::string& name, size_t initial_size,
Ian Rogers13735952014-10-08 12:43:28 -070096 size_t growth_limit, size_t capacity, uint8_t* requested_begin,
Mathieu Chartier31f44142014-04-08 14:40:03 -070097 bool low_memory_mode, bool can_move_objects) {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070098 uint64_t start_time = 0;
99 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
100 start_time = NanoTime();
101 VLOG(startup) << "RosAllocSpace::Create entering " << name
102 << " initial_size=" << PrettySize(initial_size)
103 << " growth_limit=" << PrettySize(growth_limit)
104 << " capacity=" << PrettySize(capacity)
105 << " requested_begin=" << reinterpret_cast<void*>(requested_begin);
106 }
107
108 // Memory we promise to rosalloc before it asks for morecore.
109 // Note: making this value large means that large allocations are unlikely to succeed as rosalloc
110 // will ask for this memory from sys_alloc which will fail as the footprint (this value plus the
111 // size of the large allocation) will be greater than the footprint limit.
Hiroshi Yamauchi5ccd4982014-03-11 12:19:04 -0700112 size_t starting_size = Heap::kDefaultStartingSize;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700113 MemMap* mem_map = CreateMemMap(name, starting_size, &initial_size, &growth_limit, &capacity,
114 requested_begin);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700115 if (mem_map == nullptr) {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700116 LOG(ERROR) << "Failed to create mem map for alloc space (" << name << ") of size "
117 << PrettySize(capacity);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700118 return nullptr;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700119 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700120
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800121 RosAllocSpace* space = CreateFromMemMap(mem_map, name, starting_size, initial_size,
Mathieu Chartier31f44142014-04-08 14:40:03 -0700122 growth_limit, capacity, low_memory_mode,
123 can_move_objects);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700124 // We start out with only the initial size possibly containing objects.
125 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
126 LOG(INFO) << "RosAllocSpace::Create exiting (" << PrettyDuration(NanoTime() - start_time)
127 << " ) " << *space;
128 }
129 return space;
130}
131
Mathieu Chartier31f44142014-04-08 14:40:03 -0700132allocator::RosAlloc* RosAllocSpace::CreateRosAlloc(void* begin, size_t morecore_start,
133 size_t initial_size,
Andreas Gamped7576322014-10-24 22:13:45 -0700134 size_t maximum_size, bool low_memory_mode,
Evgenii Stepanov1e133742015-05-20 12:30:59 -0700135 bool running_on_memory_tool) {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700136 // clear errno to allow PLOG on error
137 errno = 0;
138 // create rosalloc using our backing storage starting at begin and
139 // with a footprint of morecore_start. When morecore_start bytes of
140 // memory is exhaused morecore will be called.
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800141 allocator::RosAlloc* rosalloc = new art::gc::allocator::RosAlloc(
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -0800142 begin, morecore_start, maximum_size,
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800143 low_memory_mode ?
144 art::gc::allocator::RosAlloc::kPageReleaseModeAll :
Andreas Gamped7576322014-10-24 22:13:45 -0700145 art::gc::allocator::RosAlloc::kPageReleaseModeSizeAndEnd,
Evgenii Stepanov1e133742015-05-20 12:30:59 -0700146 running_on_memory_tool);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700147 if (rosalloc != nullptr) {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700148 rosalloc->SetFootprintLimit(initial_size);
149 } else {
150 PLOG(ERROR) << "RosAlloc::Create failed";
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800151 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700152 return rosalloc;
153}
154
Ian Rogers6fac4472014-02-25 17:01:10 -0800155mirror::Object* RosAllocSpace::AllocWithGrowth(Thread* self, size_t num_bytes,
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700156 size_t* bytes_allocated, size_t* usable_size,
157 size_t* bytes_tl_bulk_allocated) {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700158 mirror::Object* result;
159 {
160 MutexLock mu(self, lock_);
161 // Grow as much as possible within the space.
162 size_t max_allowed = Capacity();
163 rosalloc_->SetFootprintLimit(max_allowed);
164 // Try the allocation.
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700165 result = AllocCommon(self, num_bytes, bytes_allocated, usable_size,
166 bytes_tl_bulk_allocated);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700167 // Shrink back down as small as possible.
168 size_t footprint = rosalloc_->Footprint();
169 rosalloc_->SetFootprintLimit(footprint);
170 }
171 // Note RosAlloc zeroes memory internally.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700172 // Return the new allocation or null.
Mathieu Chartier0651d412014-04-29 14:37:57 -0700173 CHECK(!kDebugSpaces || result == nullptr || Contains(result));
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700174 return result;
175}
176
Andreas Gamped7576322014-10-24 22:13:45 -0700177MallocSpace* RosAllocSpace::CreateInstance(MemMap* mem_map, const std::string& name,
178 void* allocator, uint8_t* begin, uint8_t* end,
179 uint8_t* limit, size_t growth_limit,
Mathieu Chartier31f44142014-04-08 14:40:03 -0700180 bool can_move_objects) {
Evgenii Stepanov1e133742015-05-20 12:30:59 -0700181 if (Runtime::Current()->IsRunningOnMemoryTool()) {
182 return new MemoryToolMallocSpace<RosAllocSpace, kDefaultMemoryToolRedZoneBytes, false, true>(
Andreas Gamped7576322014-10-24 22:13:45 -0700183 mem_map, initial_size_, name, reinterpret_cast<allocator::RosAlloc*>(allocator), begin, end,
184 limit, growth_limit, can_move_objects, starting_size_, low_memory_mode_);
185 } else {
186 return new RosAllocSpace(mem_map, initial_size_, name,
187 reinterpret_cast<allocator::RosAlloc*>(allocator), begin, end, limit,
188 growth_limit, can_move_objects, starting_size_, low_memory_mode_);
189 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700190}
191
192size_t RosAllocSpace::Free(Thread* self, mirror::Object* ptr) {
193 if (kDebugSpaces) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700194 CHECK(ptr != nullptr);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700195 CHECK(Contains(ptr)) << "Free (" << ptr << ") not in bounds of heap " << *this;
196 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700197 if (kRecentFreeCount > 0) {
198 MutexLock mu(self, lock_);
199 RegisterRecentFree(ptr);
200 }
Mathieu Chartier8585bad2014-04-11 17:53:48 -0700201 return rosalloc_->Free(self, ptr);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700202}
203
204size_t RosAllocSpace::FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs) {
Mathieu Chartier8585bad2014-04-11 17:53:48 -0700205 DCHECK(ptrs != nullptr);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700206
Mathieu Chartier8585bad2014-04-11 17:53:48 -0700207 size_t verify_bytes = 0;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700208 for (size_t i = 0; i < num_ptrs; i++) {
Mathieu Chartier8585bad2014-04-11 17:53:48 -0700209 if (kPrefetchDuringRosAllocFreeList && i + kPrefetchLookAhead < num_ptrs) {
210 __builtin_prefetch(reinterpret_cast<char*>(ptrs[i + kPrefetchLookAhead]));
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700211 }
Mathieu Chartier8585bad2014-04-11 17:53:48 -0700212 if (kVerifyFreedBytes) {
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700213 verify_bytes += AllocationSizeNonvirtual<true>(ptrs[i], nullptr);
Mathieu Chartier8585bad2014-04-11 17:53:48 -0700214 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700215 }
216
217 if (kRecentFreeCount > 0) {
218 MutexLock mu(self, lock_);
219 for (size_t i = 0; i < num_ptrs; i++) {
220 RegisterRecentFree(ptrs[i]);
221 }
222 }
223
224 if (kDebugSpaces) {
225 size_t num_broken_ptrs = 0;
226 for (size_t i = 0; i < num_ptrs; i++) {
227 if (!Contains(ptrs[i])) {
228 num_broken_ptrs++;
229 LOG(ERROR) << "FreeList[" << i << "] (" << ptrs[i] << ") not in bounds of heap " << *this;
230 } else {
231 size_t size = rosalloc_->UsableSize(ptrs[i]);
232 memset(ptrs[i], 0xEF, size);
233 }
234 }
235 CHECK_EQ(num_broken_ptrs, 0u);
236 }
237
Mathieu Chartier8585bad2014-04-11 17:53:48 -0700238 const size_t bytes_freed = rosalloc_->BulkFree(self, reinterpret_cast<void**>(ptrs), num_ptrs);
239 if (kVerifyFreedBytes) {
240 CHECK_EQ(verify_bytes, bytes_freed);
241 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700242 return bytes_freed;
243}
244
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700245size_t RosAllocSpace::Trim() {
Hiroshi Yamauchid9a88de2014-04-07 13:52:31 -0700246 VLOG(heap) << "RosAllocSpace::Trim() ";
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800247 {
Mathieu Chartiera9d82fe2016-01-25 20:06:11 -0800248 Thread* const self = Thread::Current();
249 // SOA required for Rosalloc::Trim() -> ArtRosAllocMoreCore() -> Heap::GetRosAllocSpace.
250 ScopedObjectAccess soa(self);
251 MutexLock mu(self, lock_);
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800252 // Trim to release memory at the end of the space.
253 rosalloc_->Trim();
254 }
255 // Attempt to release pages if it does not release all empty pages.
256 if (!rosalloc_->DoesReleaseAllPages()) {
Hiroshi Yamauchid9a88de2014-04-07 13:52:31 -0700257 return rosalloc_->ReleasePages();
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800258 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700259 return 0;
260}
261
262void RosAllocSpace::Walk(void(*callback)(void *start, void *end, size_t num_bytes, void* callback_arg),
263 void* arg) {
Hiroshi Yamauchi1cd53db2014-03-28 15:26:48 -0700264 InspectAllRosAlloc(callback, arg, true);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700265}
266
267size_t RosAllocSpace::GetFootprint() {
268 MutexLock mu(Thread::Current(), lock_);
269 return rosalloc_->Footprint();
270}
271
272size_t RosAllocSpace::GetFootprintLimit() {
273 MutexLock mu(Thread::Current(), lock_);
274 return rosalloc_->FootprintLimit();
275}
276
277void RosAllocSpace::SetFootprintLimit(size_t new_size) {
278 MutexLock mu(Thread::Current(), lock_);
279 VLOG(heap) << "RosAllocSpace::SetFootprintLimit " << PrettySize(new_size);
280 // Compare against the actual footprint, rather than the Size(), because the heap may not have
281 // grown all the way to the allowed size yet.
282 size_t current_space_size = rosalloc_->Footprint();
283 if (new_size < current_space_size) {
284 // Don't let the space grow any more.
285 new_size = current_space_size;
286 }
287 rosalloc_->SetFootprintLimit(new_size);
288}
289
290uint64_t RosAllocSpace::GetBytesAllocated() {
Hiroshi Yamauchi4ce1f002013-11-18 14:49:09 -0800291 size_t bytes_allocated = 0;
Hiroshi Yamauchi1cd53db2014-03-28 15:26:48 -0700292 InspectAllRosAlloc(art::gc::allocator::RosAlloc::BytesAllocatedCallback, &bytes_allocated, false);
Hiroshi Yamauchi4ce1f002013-11-18 14:49:09 -0800293 return bytes_allocated;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700294}
295
296uint64_t RosAllocSpace::GetObjectsAllocated() {
Hiroshi Yamauchi4ce1f002013-11-18 14:49:09 -0800297 size_t objects_allocated = 0;
Hiroshi Yamauchi1cd53db2014-03-28 15:26:48 -0700298 InspectAllRosAlloc(art::gc::allocator::RosAlloc::ObjectsAllocatedCallback, &objects_allocated, false);
Hiroshi Yamauchi4ce1f002013-11-18 14:49:09 -0800299 return objects_allocated;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700300}
301
Hiroshi Yamauchi1cd53db2014-03-28 15:26:48 -0700302void RosAllocSpace::InspectAllRosAllocWithSuspendAll(
303 void (*callback)(void *start, void *end, size_t num_bytes, void* callback_arg),
304 void* arg, bool do_null_callback_at_end) NO_THREAD_SAFETY_ANALYSIS {
305 // TODO: NO_THREAD_SAFETY_ANALYSIS.
306 Thread* self = Thread::Current();
Mathieu Chartier4f55e222015-09-04 13:26:21 -0700307 ScopedSuspendAll ssa(__FUNCTION__);
308 MutexLock mu(self, *Locks::runtime_shutdown_lock_);
309 MutexLock mu2(self, *Locks::thread_list_lock_);
310 rosalloc_->InspectAll(callback, arg);
311 if (do_null_callback_at_end) {
312 callback(nullptr, nullptr, 0, arg); // Indicate end of a space.
Hiroshi Yamauchi1cd53db2014-03-28 15:26:48 -0700313 }
Hiroshi Yamauchi1cd53db2014-03-28 15:26:48 -0700314}
315
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700316void RosAllocSpace::InspectAllRosAlloc(void (*callback)(void *start, void *end, size_t num_bytes, void* callback_arg),
Hiroshi Yamauchi1cd53db2014-03-28 15:26:48 -0700317 void* arg, bool do_null_callback_at_end) NO_THREAD_SAFETY_ANALYSIS {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700318 // TODO: NO_THREAD_SAFETY_ANALYSIS.
319 Thread* self = Thread::Current();
320 if (Locks::mutator_lock_->IsExclusiveHeld(self)) {
321 // The mutators are already suspended. For example, a call path
322 // from SignalCatcher::HandleSigQuit().
323 rosalloc_->InspectAll(callback, arg);
Hiroshi Yamauchi1cd53db2014-03-28 15:26:48 -0700324 if (do_null_callback_at_end) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700325 callback(nullptr, nullptr, 0, arg); // Indicate end of a space.
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700326 }
Hiroshi Yamauchi1cd53db2014-03-28 15:26:48 -0700327 } else if (Locks::mutator_lock_->IsSharedHeld(self)) {
328 // The mutators are not suspended yet and we have a shared access
329 // to the mutator lock. Temporarily release the shared access by
330 // transitioning to the suspend state, and suspend the mutators.
Mathieu Chartierf1d666e2015-09-03 16:13:34 -0700331 ScopedThreadSuspension sts(self, kSuspended);
Hiroshi Yamauchi1cd53db2014-03-28 15:26:48 -0700332 InspectAllRosAllocWithSuspendAll(callback, arg, do_null_callback_at_end);
Hiroshi Yamauchi1cd53db2014-03-28 15:26:48 -0700333 } else {
334 // The mutators are not suspended yet. Suspend the mutators.
335 InspectAllRosAllocWithSuspendAll(callback, arg, do_null_callback_at_end);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700336 }
337}
338
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700339size_t RosAllocSpace::RevokeThreadLocalBuffers(Thread* thread) {
340 return rosalloc_->RevokeThreadLocalRuns(thread);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700341}
342
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700343size_t RosAllocSpace::RevokeAllThreadLocalBuffers() {
344 return rosalloc_->RevokeAllThreadLocalRuns();
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700345}
346
Ian Rogers68d8b422014-07-17 11:09:10 -0700347void RosAllocSpace::AssertThreadLocalBuffersAreRevoked(Thread* thread) {
348 if (kIsDebugBuild) {
349 rosalloc_->AssertThreadLocalRunsAreRevoked(thread);
350 }
351}
352
Hiroshi Yamauchic93c5302014-03-20 16:15:37 -0700353void RosAllocSpace::AssertAllThreadLocalBuffersAreRevoked() {
354 if (kIsDebugBuild) {
355 rosalloc_->AssertAllThreadLocalRunsAreRevoked();
356 }
357}
358
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800359void RosAllocSpace::Clear() {
Mathieu Chartier31f44142014-04-08 14:40:03 -0700360 size_t footprint_limit = GetFootprintLimit();
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800361 madvise(GetMemMap()->Begin(), GetMemMap()->Size(), MADV_DONTNEED);
Mathieu Chartier31f44142014-04-08 14:40:03 -0700362 live_bitmap_->Clear();
363 mark_bitmap_->Clear();
Ian Rogersbe2a1df2014-07-10 00:56:36 -0700364 SetEnd(begin_ + starting_size_);
Mathieu Chartier31f44142014-04-08 14:40:03 -0700365 delete rosalloc_;
Hiroshi Yamauchi94c41df2014-12-18 21:19:24 -0800366 rosalloc_ = CreateRosAlloc(mem_map_->Begin(), starting_size_, initial_size_,
367 NonGrowthLimitCapacity(), low_memory_mode_,
Evgenii Stepanov1e133742015-05-20 12:30:59 -0700368 Runtime::Current()->IsRunningOnMemoryTool());
Mathieu Chartier31f44142014-04-08 14:40:03 -0700369 SetFootprintLimit(footprint_limit);
Mathieu Chartier15d34022014-02-26 17:16:38 -0800370}
371
Hiroshi Yamauchi565c2d92016-03-23 12:53:26 -0700372void RosAllocSpace::DumpStats(std::ostream& os) {
373 ScopedSuspendAll ssa(__FUNCTION__);
374 rosalloc_->DumpStats(os);
375}
376
Andreas Gamped4901292017-05-30 18:41:34 -0700377template<bool kMaybeIsRunningOnMemoryTool>
378size_t RosAllocSpace::AllocationSizeNonvirtual(mirror::Object* obj, size_t* usable_size) {
379 // obj is a valid object. Use its class in the header to get the size.
380 // Don't use verification since the object may be dead if we are sweeping.
381 size_t size = obj->SizeOf<kVerifyNone>();
382 bool add_redzones = false;
383 if (kMaybeIsRunningOnMemoryTool) {
384 add_redzones = RUNNING_ON_MEMORY_TOOL ? kMemoryToolAddsRedzones : 0;
385 if (add_redzones) {
386 size += 2 * kDefaultMemoryToolRedZoneBytes;
387 }
388 } else {
389 DCHECK_EQ(RUNNING_ON_MEMORY_TOOL, 0U);
390 }
391 size_t size_by_size = rosalloc_->UsableSize(size);
392 if (kIsDebugBuild) {
393 // On memory tool, the red zone has an impact...
394 const uint8_t* obj_ptr = reinterpret_cast<const uint8_t*>(obj);
395 size_t size_by_ptr = rosalloc_->UsableSize(
396 obj_ptr - (add_redzones ? kDefaultMemoryToolRedZoneBytes : 0));
397 if (size_by_size != size_by_ptr) {
398 LOG(INFO) << "Found a bad sized obj of size " << size
399 << " at " << std::hex << reinterpret_cast<intptr_t>(obj_ptr) << std::dec
400 << " size_by_size=" << size_by_size << " size_by_ptr=" << size_by_ptr;
401 }
402 DCHECK_EQ(size_by_size, size_by_ptr);
403 }
404 if (usable_size != nullptr) {
405 *usable_size = size_by_size;
406 }
407 return size_by_size;
408}
409
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700410} // namespace space
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800411
412namespace allocator {
413
414// Callback from rosalloc when it needs to increase the footprint.
Mathieu Chartiera9d82fe2016-01-25 20:06:11 -0800415void* ArtRosAllocMoreCore(allocator::RosAlloc* rosalloc, intptr_t increment)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700416 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800417 Heap* heap = Runtime::Current()->GetHeap();
418 art::gc::space::RosAllocSpace* rosalloc_space = heap->GetRosAllocSpace(rosalloc);
419 DCHECK(rosalloc_space != nullptr);
420 DCHECK_EQ(rosalloc_space->GetRosAlloc(), rosalloc);
421 return rosalloc_space->MoreCore(increment);
422}
423
424} // namespace allocator
425
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700426} // namespace gc
427} // namespace art