blob: 5e1136b805da175d35e617a5146ed8782ae21265 [file] [log] [blame]
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -07001/*
2 * Copyright (C) 2013 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#ifndef ART_RUNTIME_GC_HEAP_INL_H_
18#define ART_RUNTIME_GC_HEAP_INL_H_
19
20#include "heap.h"
21
22#include "debugger.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070023#include "gc/space/bump_pointer_space-inl.h"
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070024#include "gc/space/dlmalloc_space-inl.h"
25#include "gc/space/large_object_space.h"
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070026#include "gc/space/rosalloc_space-inl.h"
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070027#include "object_utils.h"
28#include "runtime.h"
29#include "thread.h"
30#include "thread-inl.h"
31
32namespace art {
33namespace gc {
34
Mathieu Chartier692fafd2013-11-29 17:24:40 -080035template <bool kInstrumented, bool kCheckLargeObject, typename PreFenceVisitor>
Mathieu Chartier1febddf2013-11-20 12:33:14 -080036inline mirror::Object* Heap::AllocObjectWithAllocator(Thread* self, mirror::Class* klass,
37 size_t byte_count, AllocatorType allocator,
38 const PreFenceVisitor& pre_fence_visitor) {
39 DebugCheckPreconditionsForAllocObject(klass, byte_count);
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070040 // Since allocation can cause a GC which will need to SuspendAll, make sure all allocations are
41 // done in the runnable state where suspension is expected.
42 DCHECK_EQ(self->GetState(), kRunnable);
43 self->AssertThreadSuspensionIsAllowable();
Mathieu Chartierc528dba2013-11-26 12:00:11 -080044 // Need to check that we arent the large object allocator since the large object allocation code
45 // path this function. If we didn't check we would have an infinite loop.
Mathieu Chartier692fafd2013-11-29 17:24:40 -080046 if (kCheckLargeObject && UNLIKELY(ShouldAllocLargeObject(klass, byte_count))) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -080047 return AllocLargeObject<kInstrumented, PreFenceVisitor>(self, klass, byte_count,
48 pre_fence_visitor);
49 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080050 mirror::Object* obj;
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080051 AllocationTimer alloc_timer(this, &obj);
Mathieu Chartier692fafd2013-11-29 17:24:40 -080052 size_t bytes_allocated;
Mathieu Chartierc528dba2013-11-26 12:00:11 -080053 obj = TryToAllocate<kInstrumented, false>(self, allocator, byte_count, &bytes_allocated);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080054 if (UNLIKELY(obj == nullptr)) {
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080055 bool is_current_allocator = allocator == GetCurrentAllocator();
Mathieu Chartierc528dba2013-11-26 12:00:11 -080056 obj = AllocateInternalWithGc(self, allocator, byte_count, &bytes_allocated, &klass);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080057 if (obj == nullptr) {
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080058 bool after_is_current_allocator = allocator == GetCurrentAllocator();
59 if (is_current_allocator && !after_is_current_allocator) {
60 // If the allocator changed, we need to restart the allocation.
61 return AllocObject<kInstrumented>(self, klass, byte_count);
62 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080063 return nullptr;
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080064 }
65 }
Mathieu Chartier1febddf2013-11-20 12:33:14 -080066 obj->SetClass(klass);
67 pre_fence_visitor(obj);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080068 DCHECK_GT(bytes_allocated, 0u);
69 const size_t new_num_bytes_allocated =
Ian Rogersb122a4b2013-11-19 18:00:50 -080070 static_cast<size_t>(num_bytes_allocated_.FetchAndAdd(bytes_allocated)) + bytes_allocated;
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080071 // TODO: Deprecate.
72 if (kInstrumented) {
73 if (Runtime::Current()->HasStatsEnabled()) {
74 RuntimeStats* thread_stats = self->GetStats();
75 ++thread_stats->allocated_objects;
76 thread_stats->allocated_bytes += bytes_allocated;
77 RuntimeStats* global_stats = Runtime::Current()->GetStats();
78 ++global_stats->allocated_objects;
79 global_stats->allocated_bytes += bytes_allocated;
80 }
81 } else {
82 DCHECK(!Runtime::Current()->HasStatsEnabled());
83 }
84 if (AllocatorHasAllocationStack(allocator)) {
85 // This is safe to do since the GC will never free objects which are neither in the allocation
86 // stack or the live bitmap.
87 while (!allocation_stack_->AtomicPushBack(obj)) {
88 CollectGarbageInternal(collector::kGcTypeSticky, kGcCauseForAlloc, false);
89 }
90 }
91 if (kInstrumented) {
92 if (Dbg::IsAllocTrackingEnabled()) {
Mathieu Chartier1febddf2013-11-20 12:33:14 -080093 Dbg::RecordAllocation(klass, bytes_allocated);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080094 }
95 } else {
96 DCHECK(!Dbg::IsAllocTrackingEnabled());
97 }
Mathieu Chartier692fafd2013-11-29 17:24:40 -080098 // concurrent_gc_ isn't known at compile time so we can optimize by not checking it for
99 // the BumpPointer or TLAB allocators. This is nice since it allows the entire if statement to be
100 // optimized out. And for the other allocators, AllocatorMayHaveConcurrentGC is a constant since
101 // the allocator_type should be constant propagated.
102 if (AllocatorMayHaveConcurrentGC(allocator) && concurrent_gc_) {
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800103 CheckConcurrentGC(self, new_num_bytes_allocated, obj);
104 }
105 if (kIsDebugBuild) {
106 if (kDesiredHeapVerification > kNoHeapVerification) {
107 VerifyObject(obj);
108 }
109 self->VerifyStack();
110 }
111 return obj;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700112}
113
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800114template <bool kInstrumented, typename PreFenceVisitor>
115inline mirror::Object* Heap::AllocLargeObject(Thread* self, mirror::Class* klass,
116 size_t byte_count,
117 const PreFenceVisitor& pre_fence_visitor) {
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800118 return AllocObjectWithAllocator<kInstrumented, false, PreFenceVisitor>(self, klass, byte_count,
119 kAllocatorTypeLOS,
120 pre_fence_visitor);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800121}
122
123template <const bool kInstrumented, const bool kGrow>
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800124inline mirror::Object* Heap::TryToAllocate(Thread* self, AllocatorType allocator_type,
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800125 size_t alloc_size, size_t* bytes_allocated) {
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800126 if (UNLIKELY(IsOutOfMemoryOnAllocation<kGrow>(allocator_type, alloc_size))) {
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800127 return nullptr;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700128 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800129 mirror::Object* ret;
130 switch (allocator_type) {
131 case kAllocatorTypeBumpPointer: {
132 DCHECK(bump_pointer_space_ != nullptr);
133 alloc_size = RoundUp(alloc_size, space::BumpPointerSpace::kAlignment);
134 ret = bump_pointer_space_->AllocNonvirtual(alloc_size);
135 if (LIKELY(ret != nullptr)) {
136 *bytes_allocated = alloc_size;
137 }
138 break;
139 }
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800140 case kAllocatorTypeRosAlloc: {
141 if (kInstrumented && UNLIKELY(running_on_valgrind_)) {
142 // If running on valgrind, we should be using the instrumented path.
143 ret = rosalloc_space_->Alloc(self, alloc_size, bytes_allocated);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800144 } else {
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800145 DCHECK(!running_on_valgrind_);
146 ret = rosalloc_space_->AllocNonvirtual(self, alloc_size, bytes_allocated);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800147 }
148 break;
149 }
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800150 case kAllocatorTypeDlMalloc: {
151 if (kInstrumented && UNLIKELY(running_on_valgrind_)) {
152 // If running on valgrind, we should be using the instrumented path.
153 ret = dlmalloc_space_->Alloc(self, alloc_size, bytes_allocated);
154 } else {
155 DCHECK(!running_on_valgrind_);
156 ret = dlmalloc_space_->AllocNonvirtual(self, alloc_size, bytes_allocated);
157 }
158 break;
159 }
160 case kAllocatorTypeNonMoving: {
161 ret = non_moving_space_->Alloc(self, alloc_size, bytes_allocated);
162 break;
163 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800164 case kAllocatorTypeLOS: {
165 ret = large_object_space_->Alloc(self, alloc_size, bytes_allocated);
Hiroshi Yamauchi95a659f2013-11-22 14:43:45 -0800166 // Note that the bump pointer spaces aren't necessarily next to
167 // the other continuous spaces like the non-moving alloc space or
168 // the zygote space.
169 DCHECK(ret == nullptr || large_object_space_->Contains(ret));
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800170 break;
171 }
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800172 case kAllocatorTypeTLAB: {
173 alloc_size = RoundUp(alloc_size, space::BumpPointerSpace::kAlignment);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800174 if (UNLIKELY(self->TlabSize() < alloc_size)) {
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800175 // Try allocating a new thread local buffer, if the allocaiton fails the space must be
176 // full so return nullptr.
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800177 if (!bump_pointer_space_->AllocNewTlab(self, alloc_size + kDefaultTLABSize)) {
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800178 return nullptr;
179 }
180 }
181 // The allocation can't fail.
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800182 ret = self->AllocTlab(alloc_size);
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800183 DCHECK(ret != nullptr);
184 *bytes_allocated = alloc_size;
185 break;
186 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800187 default: {
188 LOG(FATAL) << "Invalid allocator type";
189 ret = nullptr;
190 }
191 }
192 return ret;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700193}
194
Mathieu Chartier590fee92013-09-13 13:46:47 -0700195inline void Heap::DebugCheckPreconditionsForAllocObject(mirror::Class* c, size_t byte_count) {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700196 DCHECK(c == NULL || (c->IsClassClass() && byte_count >= sizeof(mirror::Class)) ||
197 (c->IsVariableSize() || c->GetObjectSize() == byte_count) ||
Ian Rogersdfb325e2013-10-30 01:00:44 -0700198 strlen(ClassHelper(c).GetDescriptor()) == 0);
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700199 DCHECK_GE(byte_count, sizeof(mirror::Object));
200}
201
202inline Heap::AllocationTimer::AllocationTimer(Heap* heap, mirror::Object** allocated_obj_ptr)
203 : heap_(heap), allocated_obj_ptr_(allocated_obj_ptr) {
204 if (kMeasureAllocationTime) {
205 allocation_start_time_ = NanoTime() / kTimeAdjust;
206 }
207}
208
209inline Heap::AllocationTimer::~AllocationTimer() {
210 if (kMeasureAllocationTime) {
211 mirror::Object* allocated_obj = *allocated_obj_ptr_;
212 // Only if the allocation succeeded, record the time.
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800213 if (allocated_obj != nullptr) {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700214 uint64_t allocation_end_time = NanoTime() / kTimeAdjust;
Ian Rogersb122a4b2013-11-19 18:00:50 -0800215 heap_->total_allocation_time_.FetchAndAdd(allocation_end_time - allocation_start_time_);
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700216 }
217 }
218};
219
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800220inline bool Heap::ShouldAllocLargeObject(mirror::Class* c, size_t byte_count) const {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700221 // We need to have a zygote space or else our newly allocated large object can end up in the
222 // Zygote resulting in it being prematurely freed.
223 // We can only do this for primitive objects since large objects will not be within the card table
224 // range. This also means that we rely on SetClass not dirtying the object's card.
225 return byte_count >= kLargeObjectThreshold && have_zygote_space_ && c->IsPrimitiveArray();
226}
227
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800228template <bool kGrow>
229inline bool Heap::IsOutOfMemoryOnAllocation(AllocatorType allocator_type, size_t alloc_size) {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700230 size_t new_footprint = num_bytes_allocated_ + alloc_size;
231 if (UNLIKELY(new_footprint > max_allowed_footprint_)) {
232 if (UNLIKELY(new_footprint > growth_limit_)) {
233 return true;
234 }
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800235 if (!AllocatorMayHaveConcurrentGC(allocator_type) || !concurrent_gc_) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800236 if (!kGrow) {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700237 return true;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700238 }
Mathieu Chartier7bf82af2013-12-06 16:51:45 -0800239 // TODO: Grow for allocation is racy, fix it.
240 VLOG(heap) << "Growing heap from " << PrettySize(max_allowed_footprint_) << " to "
241 << PrettySize(new_footprint) << " for a " << PrettySize(alloc_size) << " allocation";
242 max_allowed_footprint_ = new_footprint;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700243 }
244 }
245 return false;
246}
247
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800248inline void Heap::CheckConcurrentGC(Thread* self, size_t new_num_bytes_allocated,
249 mirror::Object* obj) {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700250 if (UNLIKELY(new_num_bytes_allocated >= concurrent_start_bytes_)) {
251 // The SirtRef is necessary since the calls in RequestConcurrentGC are a safepoint.
252 SirtRef<mirror::Object> ref(self, obj);
253 RequestConcurrentGC(self);
254 }
255}
256
257} // namespace gc
258} // namespace art
259
260#endif // ART_RUNTIME_GC_HEAP_INL_H_