blob: 0f882d33e1125e210327a03dbfe000084da56825 [file] [log] [blame]
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -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_SPACE_MALLOC_SPACE_H_
18#define ART_RUNTIME_GC_SPACE_MALLOC_SPACE_H_
19
20#include "space.h"
21
Hiroshi Yamauchi7cb7bbc2013-11-18 17:27:37 -080022#include <valgrind.h>
23#include <memcheck/memcheck.h>
24
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070025namespace art {
26namespace gc {
27
28namespace collector {
29 class MarkSweep;
30} // namespace collector
31
32namespace space {
33
34// TODO: Remove define macro
35#define CHECK_MEMORY_CALL(call, args, what) \
36 do { \
37 int rc = call args; \
38 if (UNLIKELY(rc != 0)) { \
39 errno = rc; \
40 PLOG(FATAL) << # call << " failed for " << what; \
41 } \
42 } while (false)
43
44// const bool kUseRosAlloc = true;
45
46// A common parent of DlMallocSpace and RosAllocSpace.
47class MallocSpace : public ContinuousMemMapAllocSpace {
48 public:
49 typedef void(*WalkCallback)(void *start, void *end, size_t num_bytes, void* callback_arg);
50
51 SpaceType GetType() const {
52 if (GetGcRetentionPolicy() == kGcRetentionPolicyFullCollect) {
53 return kSpaceTypeZygoteSpace;
54 } else {
55 return kSpaceTypeAllocSpace;
56 }
57 }
58
59 // Allocate num_bytes without allowing the underlying space to grow.
60 virtual mirror::Object* AllocWithGrowth(Thread* self, size_t num_bytes,
61 size_t* bytes_allocated) = 0;
62 // Allocate num_bytes allowing the underlying space to grow.
63 virtual mirror::Object* Alloc(Thread* self, size_t num_bytes, size_t* bytes_allocated) = 0;
64 // Return the storage space required by obj.
65 virtual size_t AllocationSize(const mirror::Object* obj) = 0;
66 virtual size_t Free(Thread* self, mirror::Object* ptr) = 0;
67 virtual size_t FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs) = 0;
68
69#ifndef NDEBUG
70 virtual void CheckMoreCoreForPrecondition() {} // to be overridden in the debug build.
71#else
72 void CheckMoreCoreForPrecondition() {} // no-op in the non-debug build.
73#endif
74
75 void* MoreCore(intptr_t increment);
76
77 // Hands unused pages back to the system.
78 virtual size_t Trim() = 0;
79
80 // Perform a mspace_inspect_all which calls back for each allocation chunk. The chunk may not be
81 // in use, indicated by num_bytes equaling zero.
82 virtual void Walk(WalkCallback callback, void* arg) = 0;
83
84 // Returns the number of bytes that the space has currently obtained from the system. This is
85 // greater or equal to the amount of live data in the space.
86 virtual size_t GetFootprint() = 0;
87
88 // Returns the number of bytes that the heap is allowed to obtain from the system via MoreCore.
89 virtual size_t GetFootprintLimit() = 0;
90
91 // Set the maximum number of bytes that the heap is allowed to obtain from the system via
92 // MoreCore. Note this is used to stop the mspace growing beyond the limit to Capacity. When
93 // allocations fail we GC before increasing the footprint limit and allowing the mspace to grow.
94 virtual void SetFootprintLimit(size_t limit) = 0;
95
96 // Removes the fork time growth limit on capacity, allowing the application to allocate up to the
97 // maximum reserved size of the heap.
98 void ClearGrowthLimit() {
99 growth_limit_ = NonGrowthLimitCapacity();
100 }
101
102 // Override capacity so that we only return the possibly limited capacity
103 size_t Capacity() const {
104 return growth_limit_;
105 }
106
107 // The total amount of memory reserved for the alloc space.
108 size_t NonGrowthLimitCapacity() const {
109 return GetMemMap()->Size();
110 }
111
112 accounting::SpaceBitmap* GetLiveBitmap() const {
113 return live_bitmap_.get();
114 }
115
116 accounting::SpaceBitmap* GetMarkBitmap() const {
117 return mark_bitmap_.get();
118 }
119
120 void Dump(std::ostream& os) const;
121
122 void SetGrowthLimit(size_t growth_limit);
123
124 // Swap the live and mark bitmaps of this space. This is used by the GC for concurrent sweeping.
125 void SwapBitmaps();
126
127 virtual MallocSpace* CreateInstance(const std::string& name, MemMap* mem_map, void* allocator,
128 byte* begin, byte* end, byte* limit, size_t growth_limit) = 0;
129
130 // Turn ourself into a zygote space and return a new alloc space which has our unused memory.
131 MallocSpace* CreateZygoteSpace(const char* alloc_space_name);
132
133 virtual uint64_t GetBytesAllocated() = 0;
134 virtual uint64_t GetObjectsAllocated() = 0;
135 virtual uint64_t GetTotalBytesAllocated() = 0;
136 virtual uint64_t GetTotalObjectsAllocated() = 0;
137
138 // Returns the old mark bitmap.
139 accounting::SpaceBitmap* BindLiveToMarkBitmap();
140 bool HasBoundBitmaps() const;
141 void UnBindBitmaps();
142
143 // Returns the class of a recently freed object.
144 mirror::Class* FindRecentFreedObject(const mirror::Object* obj);
145
146 // Used to ensure that failure happens when you free / allocate into an invalidated space. If we
147 // don't do this we may get heap corruption instead of a segfault at null.
148 virtual void InvalidateAllocator() = 0;
149
150 protected:
151 MallocSpace(const std::string& name, MemMap* mem_map, byte* begin, byte* end,
152 byte* limit, size_t growth_limit);
153
154 static MemMap* CreateMemMap(const std::string& name, size_t starting_size, size_t* initial_size,
155 size_t* growth_limit, size_t* capacity, byte* requested_begin);
156
157 virtual void* CreateAllocator(void* base, size_t morecore_start, size_t initial_size) = 0;
158
159 void RegisterRecentFree(mirror::Object* ptr) EXCLUSIVE_LOCKS_REQUIRED(lock_);
160
161 UniquePtr<accounting::SpaceBitmap> live_bitmap_;
162 UniquePtr<accounting::SpaceBitmap> mark_bitmap_;
163 UniquePtr<accounting::SpaceBitmap> temp_bitmap_;
164
165 // Recent allocation buffer.
166 static constexpr size_t kRecentFreeCount = kDebugSpaces ? (1 << 16) : 0;
167 static constexpr size_t kRecentFreeMask = kRecentFreeCount - 1;
168 std::pair<const mirror::Object*, mirror::Class*> recent_freed_objects_[kRecentFreeCount];
169 size_t recent_free_pos_;
170
171 static size_t bitmap_index_;
172
173 // Used to ensure mutual exclusion when the allocation spaces data structures are being modified.
174 Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
175
176 // The capacity of the alloc space until such time that ClearGrowthLimit is called.
177 // The underlying mem_map_ controls the maximum size we allow the heap to grow to. The growth
178 // limit is a value <= to the mem_map_ capacity used for ergonomic reasons because of the zygote.
179 // Prior to forking the zygote the heap will have a maximally sized mem_map_ but the growth_limit_
180 // will be set to a lower value. The growth_limit_ is used as the capacity of the alloc_space_,
181 // however, capacity normally can't vary. In the case of the growth_limit_ it can be cleared
182 // one time by a call to ClearGrowthLimit.
183 size_t growth_limit_;
184
185 friend class collector::MarkSweep;
186
Hiroshi Yamauchie5eedcb2013-11-18 11:55:39 -0800187 private:
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700188 DISALLOW_COPY_AND_ASSIGN(MallocSpace);
189};
190
Hiroshi Yamauchi7cb7bbc2013-11-18 17:27:37 -0800191// Number of bytes to use as a red zone (rdz). A red zone of this size will be placed before and
192// after each allocation. 8 bytes provides long/double alignment.
193static constexpr size_t kValgrindRedZoneBytes = 8;
194
195// A specialization of DlMallocSpace/RosAllocSpace that provides information to valgrind wrt allocations.
196template <typename BaseMallocSpaceType, typename AllocatorType>
197class ValgrindMallocSpace : public BaseMallocSpaceType {
198 public:
199 virtual mirror::Object* AllocWithGrowth(Thread* self, size_t num_bytes, size_t* bytes_allocated) {
200 void* obj_with_rdz = BaseMallocSpaceType::AllocWithGrowth(self, num_bytes + 2 * kValgrindRedZoneBytes,
201 bytes_allocated);
202 if (obj_with_rdz == NULL) {
203 return NULL;
204 }
205 mirror::Object* result = reinterpret_cast<mirror::Object*>(
206 reinterpret_cast<byte*>(obj_with_rdz) + kValgrindRedZoneBytes);
207 // Make redzones as no access.
208 VALGRIND_MAKE_MEM_NOACCESS(obj_with_rdz, kValgrindRedZoneBytes);
209 VALGRIND_MAKE_MEM_NOACCESS(reinterpret_cast<byte*>(result) + num_bytes, kValgrindRedZoneBytes);
210 return result;
211 }
212
213 virtual mirror::Object* Alloc(Thread* self, size_t num_bytes, size_t* bytes_allocated) {
214 void* obj_with_rdz = BaseMallocSpaceType::Alloc(self, num_bytes + 2 * kValgrindRedZoneBytes,
215 bytes_allocated);
216 if (obj_with_rdz == NULL) {
217 return NULL;
218 }
219 mirror::Object* result = reinterpret_cast<mirror::Object*>(
220 reinterpret_cast<byte*>(obj_with_rdz) + kValgrindRedZoneBytes);
221 // Make redzones as no access.
222 VALGRIND_MAKE_MEM_NOACCESS(obj_with_rdz, kValgrindRedZoneBytes);
223 VALGRIND_MAKE_MEM_NOACCESS(reinterpret_cast<byte*>(result) + num_bytes, kValgrindRedZoneBytes);
224 return result;
225 }
226
227 virtual size_t AllocationSize(const mirror::Object* obj) {
228 size_t result = BaseMallocSpaceType::AllocationSize(reinterpret_cast<const mirror::Object*>(
229 reinterpret_cast<const byte*>(obj) - kValgrindRedZoneBytes));
230 return result - 2 * kValgrindRedZoneBytes;
231 }
232
233 virtual size_t Free(Thread* self, mirror::Object* ptr) {
234 void* obj_after_rdz = reinterpret_cast<void*>(ptr);
235 void* obj_with_rdz = reinterpret_cast<byte*>(obj_after_rdz) - kValgrindRedZoneBytes;
236 // Make redzones undefined.
237 size_t allocation_size = BaseMallocSpaceType::AllocationSize(
238 reinterpret_cast<mirror::Object*>(obj_with_rdz));
239 VALGRIND_MAKE_MEM_UNDEFINED(obj_with_rdz, allocation_size);
240 size_t freed = BaseMallocSpaceType::Free(self, reinterpret_cast<mirror::Object*>(obj_with_rdz));
241 return freed - 2 * kValgrindRedZoneBytes;
242 }
243
244 virtual size_t FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs) {
245 size_t freed = 0;
246 for (size_t i = 0; i < num_ptrs; i++) {
247 freed += Free(self, ptrs[i]);
248 }
249 return freed;
250 }
251
252 ValgrindMallocSpace(const std::string& name, MemMap* mem_map, AllocatorType allocator, byte* begin,
253 byte* end, byte* limit, size_t growth_limit, size_t initial_size) :
254 BaseMallocSpaceType(name, mem_map, allocator, begin, end, limit, growth_limit) {
255 VALGRIND_MAKE_MEM_UNDEFINED(mem_map->Begin() + initial_size, mem_map->Size() - initial_size);
256 }
257
258 virtual ~ValgrindMallocSpace() {
259 }
260
261 private:
262 DISALLOW_COPY_AND_ASSIGN(ValgrindMallocSpace);
263};
264
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700265} // namespace space
266} // namespace gc
267} // namespace art
268
Hiroshi Yamauchie5eedcb2013-11-18 11:55:39 -0800269#endif // ART_RUNTIME_GC_SPACE_MALLOC_SPACE_H_