blob: 4c8b05f924ee211b605e32e6118d1646e86fea7c [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
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800130 // Turn ourself into a zygote space and return a new alloc space
131 // which has our unused memory. When true, the low memory mode
132 // argument specifies that the heap wishes the created space to be
133 // more aggressive in releasing unused pages.
134 MallocSpace* CreateZygoteSpace(const char* alloc_space_name, bool low_memory_mode);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700135
136 virtual uint64_t GetBytesAllocated() = 0;
137 virtual uint64_t GetObjectsAllocated() = 0;
138 virtual uint64_t GetTotalBytesAllocated() = 0;
139 virtual uint64_t GetTotalObjectsAllocated() = 0;
140
141 // Returns the old mark bitmap.
142 accounting::SpaceBitmap* BindLiveToMarkBitmap();
143 bool HasBoundBitmaps() const;
144 void UnBindBitmaps();
145
146 // Returns the class of a recently freed object.
147 mirror::Class* FindRecentFreedObject(const mirror::Object* obj);
148
149 // Used to ensure that failure happens when you free / allocate into an invalidated space. If we
150 // don't do this we may get heap corruption instead of a segfault at null.
151 virtual void InvalidateAllocator() = 0;
152
153 protected:
154 MallocSpace(const std::string& name, MemMap* mem_map, byte* begin, byte* end,
155 byte* limit, size_t growth_limit);
156
157 static MemMap* CreateMemMap(const std::string& name, size_t starting_size, size_t* initial_size,
158 size_t* growth_limit, size_t* capacity, byte* requested_begin);
159
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800160 // When true the low memory mode argument specifies that the heap
161 // wishes the created allocator to be more aggressive in releasing
162 // unused pages.
163 virtual void* CreateAllocator(void* base, size_t morecore_start, size_t initial_size,
164 bool low_memory_mode) = 0;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700165
166 void RegisterRecentFree(mirror::Object* ptr) EXCLUSIVE_LOCKS_REQUIRED(lock_);
167
168 UniquePtr<accounting::SpaceBitmap> live_bitmap_;
169 UniquePtr<accounting::SpaceBitmap> mark_bitmap_;
170 UniquePtr<accounting::SpaceBitmap> temp_bitmap_;
171
172 // Recent allocation buffer.
173 static constexpr size_t kRecentFreeCount = kDebugSpaces ? (1 << 16) : 0;
174 static constexpr size_t kRecentFreeMask = kRecentFreeCount - 1;
175 std::pair<const mirror::Object*, mirror::Class*> recent_freed_objects_[kRecentFreeCount];
176 size_t recent_free_pos_;
177
178 static size_t bitmap_index_;
179
180 // Used to ensure mutual exclusion when the allocation spaces data structures are being modified.
181 Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
182
183 // The capacity of the alloc space until such time that ClearGrowthLimit is called.
184 // The underlying mem_map_ controls the maximum size we allow the heap to grow to. The growth
185 // limit is a value <= to the mem_map_ capacity used for ergonomic reasons because of the zygote.
186 // Prior to forking the zygote the heap will have a maximally sized mem_map_ but the growth_limit_
187 // will be set to a lower value. The growth_limit_ is used as the capacity of the alloc_space_,
188 // however, capacity normally can't vary. In the case of the growth_limit_ it can be cleared
189 // one time by a call to ClearGrowthLimit.
190 size_t growth_limit_;
191
192 friend class collector::MarkSweep;
193
Hiroshi Yamauchie5eedcb2013-11-18 11:55:39 -0800194 private:
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700195 DISALLOW_COPY_AND_ASSIGN(MallocSpace);
196};
197
Hiroshi Yamauchi7cb7bbc2013-11-18 17:27:37 -0800198// Number of bytes to use as a red zone (rdz). A red zone of this size will be placed before and
199// after each allocation. 8 bytes provides long/double alignment.
200static constexpr size_t kValgrindRedZoneBytes = 8;
201
202// A specialization of DlMallocSpace/RosAllocSpace that provides information to valgrind wrt allocations.
203template <typename BaseMallocSpaceType, typename AllocatorType>
204class ValgrindMallocSpace : public BaseMallocSpaceType {
205 public:
206 virtual mirror::Object* AllocWithGrowth(Thread* self, size_t num_bytes, size_t* bytes_allocated) {
207 void* obj_with_rdz = BaseMallocSpaceType::AllocWithGrowth(self, num_bytes + 2 * kValgrindRedZoneBytes,
208 bytes_allocated);
209 if (obj_with_rdz == NULL) {
210 return NULL;
211 }
212 mirror::Object* result = reinterpret_cast<mirror::Object*>(
213 reinterpret_cast<byte*>(obj_with_rdz) + kValgrindRedZoneBytes);
214 // Make redzones as no access.
215 VALGRIND_MAKE_MEM_NOACCESS(obj_with_rdz, kValgrindRedZoneBytes);
216 VALGRIND_MAKE_MEM_NOACCESS(reinterpret_cast<byte*>(result) + num_bytes, kValgrindRedZoneBytes);
217 return result;
218 }
219
220 virtual mirror::Object* Alloc(Thread* self, size_t num_bytes, size_t* bytes_allocated) {
221 void* obj_with_rdz = BaseMallocSpaceType::Alloc(self, num_bytes + 2 * kValgrindRedZoneBytes,
222 bytes_allocated);
223 if (obj_with_rdz == NULL) {
224 return NULL;
225 }
226 mirror::Object* result = reinterpret_cast<mirror::Object*>(
227 reinterpret_cast<byte*>(obj_with_rdz) + kValgrindRedZoneBytes);
228 // Make redzones as no access.
229 VALGRIND_MAKE_MEM_NOACCESS(obj_with_rdz, kValgrindRedZoneBytes);
230 VALGRIND_MAKE_MEM_NOACCESS(reinterpret_cast<byte*>(result) + num_bytes, kValgrindRedZoneBytes);
231 return result;
232 }
233
234 virtual size_t AllocationSize(const mirror::Object* obj) {
235 size_t result = BaseMallocSpaceType::AllocationSize(reinterpret_cast<const mirror::Object*>(
236 reinterpret_cast<const byte*>(obj) - kValgrindRedZoneBytes));
237 return result - 2 * kValgrindRedZoneBytes;
238 }
239
240 virtual size_t Free(Thread* self, mirror::Object* ptr) {
241 void* obj_after_rdz = reinterpret_cast<void*>(ptr);
242 void* obj_with_rdz = reinterpret_cast<byte*>(obj_after_rdz) - kValgrindRedZoneBytes;
243 // Make redzones undefined.
244 size_t allocation_size = BaseMallocSpaceType::AllocationSize(
245 reinterpret_cast<mirror::Object*>(obj_with_rdz));
246 VALGRIND_MAKE_MEM_UNDEFINED(obj_with_rdz, allocation_size);
247 size_t freed = BaseMallocSpaceType::Free(self, reinterpret_cast<mirror::Object*>(obj_with_rdz));
248 return freed - 2 * kValgrindRedZoneBytes;
249 }
250
251 virtual size_t FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs) {
252 size_t freed = 0;
253 for (size_t i = 0; i < num_ptrs; i++) {
254 freed += Free(self, ptrs[i]);
255 }
256 return freed;
257 }
258
259 ValgrindMallocSpace(const std::string& name, MemMap* mem_map, AllocatorType allocator, byte* begin,
260 byte* end, byte* limit, size_t growth_limit, size_t initial_size) :
261 BaseMallocSpaceType(name, mem_map, allocator, begin, end, limit, growth_limit) {
262 VALGRIND_MAKE_MEM_UNDEFINED(mem_map->Begin() + initial_size, mem_map->Size() - initial_size);
263 }
264
265 virtual ~ValgrindMallocSpace() {
266 }
267
268 private:
269 DISALLOW_COPY_AND_ASSIGN(ValgrindMallocSpace);
270};
271
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700272} // namespace space
273} // namespace gc
274} // namespace art
275
Hiroshi Yamauchie5eedcb2013-11-18 11:55:39 -0800276#endif // ART_RUNTIME_GC_SPACE_MALLOC_SPACE_H_