blob: fcc07a0224ece5accc934b9227bc7c4c577a0d57 [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 Chartiercbb2d202013-11-14 17:45:16 -080035template <const bool kInstrumented>
36inline mirror::Object* Heap::AllocObjectWithAllocator(Thread* self, mirror::Class* c,
37 size_t byte_count, AllocatorType allocator) {
Mathieu Chartier590fee92013-09-13 13:46:47 -070038 DebugCheckPreconditionsForAllocObject(c, byte_count);
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070039 // Since allocation can cause a GC which will need to SuspendAll, make sure all allocations are
40 // done in the runnable state where suspension is expected.
41 DCHECK_EQ(self->GetState(), kRunnable);
42 self->AssertThreadSuspensionIsAllowable();
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080043 mirror::Object* obj;
44 size_t bytes_allocated;
45 AllocationTimer alloc_timer(this, &obj);
46 if (UNLIKELY(ShouldAllocLargeObject(c, byte_count))) {
47 obj = TryToAllocate<kInstrumented>(self, kAllocatorTypeLOS, byte_count, false,
48 &bytes_allocated);
49 allocator = kAllocatorTypeLOS;
50 } else {
51 obj = TryToAllocate<kInstrumented>(self, allocator, byte_count, false, &bytes_allocated);
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070052 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080053
54 if (UNLIKELY(obj == nullptr)) {
55 SirtRef<mirror::Class> sirt_c(self, c);
56 obj = AllocateInternalWithGc(self, allocator, byte_count, &bytes_allocated);
57 if (obj == nullptr) {
58 return nullptr;
59 } else {
60 c = sirt_c.get();
61 }
62 }
63 obj->SetClass(c);
64 // TODO: Set array length here.
65 DCHECK_GT(bytes_allocated, 0u);
66 const size_t new_num_bytes_allocated =
67 static_cast<size_t>(num_bytes_allocated_.fetch_add(bytes_allocated)) + bytes_allocated;
68 // TODO: Deprecate.
69 if (kInstrumented) {
70 if (Runtime::Current()->HasStatsEnabled()) {
71 RuntimeStats* thread_stats = self->GetStats();
72 ++thread_stats->allocated_objects;
73 thread_stats->allocated_bytes += bytes_allocated;
74 RuntimeStats* global_stats = Runtime::Current()->GetStats();
75 ++global_stats->allocated_objects;
76 global_stats->allocated_bytes += bytes_allocated;
77 }
78 } else {
79 DCHECK(!Runtime::Current()->HasStatsEnabled());
80 }
81 if (AllocatorHasAllocationStack(allocator)) {
82 // This is safe to do since the GC will never free objects which are neither in the allocation
83 // stack or the live bitmap.
84 while (!allocation_stack_->AtomicPushBack(obj)) {
85 CollectGarbageInternal(collector::kGcTypeSticky, kGcCauseForAlloc, false);
86 }
87 }
88 if (kInstrumented) {
89 if (Dbg::IsAllocTrackingEnabled()) {
90 Dbg::RecordAllocation(c, bytes_allocated);
91 }
92 } else {
93 DCHECK(!Dbg::IsAllocTrackingEnabled());
94 }
95 if (AllocatorHasConcurrentGC(allocator)) {
96 CheckConcurrentGC(self, new_num_bytes_allocated, obj);
97 }
98 if (kIsDebugBuild) {
99 if (kDesiredHeapVerification > kNoHeapVerification) {
100 VerifyObject(obj);
101 }
102 self->VerifyStack();
103 }
104 return obj;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700105}
106
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800107template <const bool kInstrumented>
108inline mirror::Object* Heap::TryToAllocate(Thread* self, AllocatorType allocator_type,
109 size_t alloc_size, bool grow,
110 size_t* bytes_allocated) {
111 if (UNLIKELY(IsOutOfMemoryOnAllocation(alloc_size, grow))) {
112 return nullptr;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700113 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800114 if (kInstrumented) {
115 if (UNLIKELY(running_on_valgrind_ && allocator_type == kAllocatorTypeFreeList)) {
116 return non_moving_space_->Alloc(self, alloc_size, bytes_allocated);
117 }
118 }
119 mirror::Object* ret;
120 switch (allocator_type) {
121 case kAllocatorTypeBumpPointer: {
122 DCHECK(bump_pointer_space_ != nullptr);
123 alloc_size = RoundUp(alloc_size, space::BumpPointerSpace::kAlignment);
124 ret = bump_pointer_space_->AllocNonvirtual(alloc_size);
125 if (LIKELY(ret != nullptr)) {
126 *bytes_allocated = alloc_size;
127 }
128 break;
129 }
130 case kAllocatorTypeFreeList: {
131 if (kUseRosAlloc) {
132 ret = reinterpret_cast<space::RosAllocSpace*>(non_moving_space_)->AllocNonvirtual(
133 self, alloc_size, bytes_allocated);
134 } else {
135 ret = reinterpret_cast<space::DlMallocSpace*>(non_moving_space_)->AllocNonvirtual(
136 self, alloc_size, bytes_allocated);
137 }
138 break;
139 }
140 case kAllocatorTypeLOS: {
141 ret = large_object_space_->Alloc(self, alloc_size, bytes_allocated);
142 // Make sure that our large object didn't get placed anywhere within the space interval or
143 // else it breaks the immune range.
144 DCHECK(ret == nullptr ||
145 reinterpret_cast<byte*>(ret) < continuous_spaces_.front()->Begin() ||
146 reinterpret_cast<byte*>(ret) >= continuous_spaces_.back()->End());
147 break;
148 }
149 default: {
150 LOG(FATAL) << "Invalid allocator type";
151 ret = nullptr;
152 }
153 }
154 return ret;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700155}
156
Mathieu Chartier590fee92013-09-13 13:46:47 -0700157inline void Heap::DebugCheckPreconditionsForAllocObject(mirror::Class* c, size_t byte_count) {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700158 DCHECK(c == NULL || (c->IsClassClass() && byte_count >= sizeof(mirror::Class)) ||
159 (c->IsVariableSize() || c->GetObjectSize() == byte_count) ||
Ian Rogersdfb325e2013-10-30 01:00:44 -0700160 strlen(ClassHelper(c).GetDescriptor()) == 0);
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700161 DCHECK_GE(byte_count, sizeof(mirror::Object));
162}
163
164inline Heap::AllocationTimer::AllocationTimer(Heap* heap, mirror::Object** allocated_obj_ptr)
165 : heap_(heap), allocated_obj_ptr_(allocated_obj_ptr) {
166 if (kMeasureAllocationTime) {
167 allocation_start_time_ = NanoTime() / kTimeAdjust;
168 }
169}
170
171inline Heap::AllocationTimer::~AllocationTimer() {
172 if (kMeasureAllocationTime) {
173 mirror::Object* allocated_obj = *allocated_obj_ptr_;
174 // Only if the allocation succeeded, record the time.
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800175 if (allocated_obj != nullptr) {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700176 uint64_t allocation_end_time = NanoTime() / kTimeAdjust;
177 heap_->total_allocation_time_.fetch_add(allocation_end_time - allocation_start_time_);
178 }
179 }
180};
181
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800182inline bool Heap::ShouldAllocLargeObject(mirror::Class* c, size_t byte_count) const {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700183 // We need to have a zygote space or else our newly allocated large object can end up in the
184 // Zygote resulting in it being prematurely freed.
185 // We can only do this for primitive objects since large objects will not be within the card table
186 // range. This also means that we rely on SetClass not dirtying the object's card.
187 return byte_count >= kLargeObjectThreshold && have_zygote_space_ && c->IsPrimitiveArray();
188}
189
190inline bool Heap::IsOutOfMemoryOnAllocation(size_t alloc_size, bool grow) {
191 size_t new_footprint = num_bytes_allocated_ + alloc_size;
192 if (UNLIKELY(new_footprint > max_allowed_footprint_)) {
193 if (UNLIKELY(new_footprint > growth_limit_)) {
194 return true;
195 }
196 if (!concurrent_gc_) {
197 if (!grow) {
198 return true;
199 } else {
200 max_allowed_footprint_ = new_footprint;
201 }
202 }
203 }
204 return false;
205}
206
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800207inline void Heap::CheckConcurrentGC(Thread* self, size_t new_num_bytes_allocated,
208 mirror::Object* obj) {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700209 if (UNLIKELY(new_num_bytes_allocated >= concurrent_start_bytes_)) {
210 // The SirtRef is necessary since the calls in RequestConcurrentGC are a safepoint.
211 SirtRef<mirror::Object> ref(self, obj);
212 RequestConcurrentGC(self);
213 }
214}
215
216} // namespace gc
217} // namespace art
218
219#endif // ART_RUNTIME_GC_HEAP_INL_H_