blob: b1c20ca9e610312426d8f63eb667b03bdb4c7b0c [file] [log] [blame]
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -07001/*
2 * Copyright (C) 2012 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
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_GC_SPACE_LARGE_OBJECT_SPACE_H_
18#define ART_RUNTIME_GC_SPACE_LARGE_OBJECT_SPACE_H_
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070019
Mathieu Chartier0a9dc052013-07-25 11:01:28 -070020#include "gc/accounting/gc_allocator.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070021#include "dlmalloc_space.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080022#include "safe_map.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070023#include "space.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080024
25#include <set>
26#include <vector>
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070027
28namespace art {
Ian Rogers1d54e732013-05-02 21:10:01 -070029namespace gc {
30namespace space {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070031
Ian Rogers22a20862013-03-16 16:34:57 -070032// Abstraction implemented by all large object spaces.
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070033class LargeObjectSpace : public DiscontinuousSpace, public AllocSpace {
34 public:
Ian Rogers6fac4472014-02-25 17:01:10 -080035 SpaceType GetType() const OVERRIDE {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070036 return kSpaceTypeLargeObjectSpace;
37 }
38
Ian Rogers6fac4472014-02-25 17:01:10 -080039 void SwapBitmaps();
40 void CopyLiveToMarked();
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070041 virtual void Walk(DlMallocSpace::WalkCallback, void* arg) = 0;
42 virtual ~LargeObjectSpace() {}
43
Ian Rogers6fac4472014-02-25 17:01:10 -080044 uint64_t GetBytesAllocated() OVERRIDE {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070045 return num_bytes_allocated_;
46 }
47
Ian Rogers6fac4472014-02-25 17:01:10 -080048 uint64_t GetObjectsAllocated() OVERRIDE {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070049 return num_objects_allocated_;
50 }
51
Mathieu Chartierbbd695c2014-04-16 09:48:48 -070052 uint64_t GetTotalBytesAllocated() const {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070053 return total_bytes_allocated_;
54 }
55
Mathieu Chartierbbd695c2014-04-16 09:48:48 -070056 uint64_t GetTotalObjectsAllocated() const {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070057 return total_objects_allocated_;
58 }
59
Ian Rogers6fac4472014-02-25 17:01:10 -080060 size_t FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs) OVERRIDE;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070061
Ian Rogers6fac4472014-02-25 17:01:10 -080062 // LargeObjectSpaces don't have thread local state.
63 void RevokeThreadLocalBuffers(art::Thread*) OVERRIDE {
64 }
65 void RevokeAllThreadLocalBuffers() OVERRIDE {
66 }
67
68 bool IsAllocSpace() const OVERRIDE {
Mathieu Chartier590fee92013-09-13 13:46:47 -070069 return true;
70 }
71
Ian Rogers6fac4472014-02-25 17:01:10 -080072 AllocSpace* AsAllocSpace() OVERRIDE {
Mathieu Chartier590fee92013-09-13 13:46:47 -070073 return this;
74 }
75
Mathieu Chartier10fb83a2014-06-15 15:15:43 -070076 collector::ObjectBytePair Sweep(bool swap_bitmaps);
Mathieu Chartierdb7f37d2014-01-10 11:09:06 -080077
Mathieu Chartier31f44142014-04-08 14:40:03 -070078 virtual bool CanMoveObjects() const OVERRIDE {
79 return false;
80 }
81
Mathieu Chartierbbd695c2014-04-16 09:48:48 -070082 // Current address at which the space begins, which may vary as the space is filled.
83 byte* Begin() const {
84 return begin_;
85 }
86
87 // Current address at which the space ends, which may vary as the space is filled.
88 byte* End() const {
89 return end_;
90 }
91
Mathieu Chartierb363f662014-07-16 13:28:58 -070092 void LogFragmentationAllocFailure(std::ostream& os, size_t failed_alloc_bytes) OVERRIDE
93 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
94
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070095 protected:
Mathieu Chartierbbd695c2014-04-16 09:48:48 -070096 explicit LargeObjectSpace(const std::string& name, byte* begin, byte* end);
97
98 static void SweepCallback(size_t num_ptrs, mirror::Object** ptrs, void* arg);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070099
100 // Approximate number of bytes which have been allocated into the space.
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700101 uint64_t num_bytes_allocated_;
102 uint64_t num_objects_allocated_;
103 uint64_t total_bytes_allocated_;
104 uint64_t total_objects_allocated_;
105
106 // Begin and end, may change as more large objects are allocated.
107 byte* begin_;
108 byte* end_;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700109
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700110 friend class Space;
Ian Rogers1d54e732013-05-02 21:10:01 -0700111
112 private:
113 DISALLOW_COPY_AND_ASSIGN(LargeObjectSpace);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700114};
115
Ian Rogers22a20862013-03-16 16:34:57 -0700116// A discontinuous large object space implemented by individual mmap/munmap calls.
Mathieu Chartier0767c9a2014-03-26 12:53:19 -0700117class LargeObjectMapSpace : public LargeObjectSpace {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700118 public:
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700119 // Creates a large object space. Allocations into the large object space use memory maps instead
120 // of malloc.
121 static LargeObjectMapSpace* Create(const std::string& name);
122
123 // Return the storage space required by obj.
Ian Rogers6fac4472014-02-25 17:01:10 -0800124 size_t AllocationSize(mirror::Object* obj, size_t* usable_size);
125 mirror::Object* Alloc(Thread* self, size_t num_bytes, size_t* bytes_allocated,
126 size_t* usable_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800127 size_t Free(Thread* self, mirror::Object* ptr);
Ian Rogers6fac4472014-02-25 17:01:10 -0800128 void Walk(DlMallocSpace::WalkCallback, void* arg) OVERRIDE LOCKS_EXCLUDED(lock_);
Ian Rogersa3dd0b32013-03-19 19:30:59 -0700129 // TODO: disabling thread safety analysis as this may be called when we already hold lock_.
Ian Rogers1d54e732013-05-02 21:10:01 -0700130 bool Contains(const mirror::Object* obj) const NO_THREAD_SAFETY_ANALYSIS;
131
Mathieu Chartier0767c9a2014-03-26 12:53:19 -0700132 protected:
Brian Carlstrom93ba8932013-07-17 21:31:49 -0700133 explicit LargeObjectMapSpace(const std::string& name);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700134 virtual ~LargeObjectMapSpace() {}
135
136 // Used to ensure mutual exclusion when the allocation spaces data structures are being modified.
Ian Rogers22a20862013-03-16 16:34:57 -0700137 mutable Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Mathieu Chartier0a9dc052013-07-25 11:01:28 -0700138 std::vector<mirror::Object*,
Ian Rogers700a4022014-05-19 16:49:03 -0700139 accounting::GcAllocator<mirror::Object*>> large_objects_ GUARDED_BY(lock_);
Mathieu Chartier0a9dc052013-07-25 11:01:28 -0700140 typedef SafeMap<mirror::Object*, MemMap*, std::less<mirror::Object*>,
Ian Rogers700a4022014-05-19 16:49:03 -0700141 accounting::GcAllocator<std::pair<mirror::Object*, MemMap*>>> MemMaps;
Ian Rogers22a20862013-03-16 16:34:57 -0700142 MemMaps mem_maps_ GUARDED_BY(lock_);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700143};
144
Ian Rogers22a20862013-03-16 16:34:57 -0700145// A continuous large object space with a free-list to handle holes.
Ian Rogers6fac4472014-02-25 17:01:10 -0800146class FreeListSpace FINAL : public LargeObjectSpace {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700147 public:
148 virtual ~FreeListSpace();
149 static FreeListSpace* Create(const std::string& name, byte* requested_begin, size_t capacity);
150
Ian Rogers6fac4472014-02-25 17:01:10 -0800151 size_t AllocationSize(mirror::Object* obj, size_t* usable_size) OVERRIDE
152 EXCLUSIVE_LOCKS_REQUIRED(lock_);
153 mirror::Object* Alloc(Thread* self, size_t num_bytes, size_t* bytes_allocated,
154 size_t* usable_size) OVERRIDE;
155 size_t Free(Thread* self, mirror::Object* obj) OVERRIDE;
156 bool Contains(const mirror::Object* obj) const OVERRIDE;
157 void Walk(DlMallocSpace::WalkCallback callback, void* arg) OVERRIDE LOCKS_EXCLUDED(lock_);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700158
Ian Rogers22a20862013-03-16 16:34:57 -0700159 // Address at which the space begins.
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700160 byte* Begin() const {
161 return begin_;
162 }
163
164 // Address at which the space ends, which may vary as the space is filled.
165 byte* End() const {
166 return end_;
167 }
168
169 // Current size of space
170 size_t Size() const {
171 return End() - Begin();
172 }
Mathieu Chartier128c52c2012-10-16 14:12:41 -0700173
Ian Rogers1d54e732013-05-02 21:10:01 -0700174 void Dump(std::ostream& os) const;
Mathieu Chartier128c52c2012-10-16 14:12:41 -0700175
Mathieu Chartier0767c9a2014-03-26 12:53:19 -0700176 protected:
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700177 static const size_t kAlignment = kPageSize;
178
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700179 class AllocationHeader {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700180 public:
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700181 // Returns the allocation size, includes the header.
182 size_t AllocationSize() const {
183 return alloc_size_;
184 }
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700185
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700186 // Updates the allocation size in the header, the allocation size includes the header itself.
187 void SetAllocationSize(size_t size) {
188 DCHECK(IsAligned<kPageSize>(size));
189 alloc_size_ = size;
190 }
191
192 bool IsFree() const {
193 return AllocationSize() == 0;
194 }
195
196 // Returns the previous free allocation header by using the prev_free_ member to figure out
197 // where it is. If prev free is 0 then we just return ourself.
198 AllocationHeader* GetPrevFreeAllocationHeader() {
199 return reinterpret_cast<AllocationHeader*>(reinterpret_cast<uintptr_t>(this) - prev_free_);
200 }
201
202 // Returns the address of the object associated with this allocation header.
203 mirror::Object* GetObjectAddress() {
204 return reinterpret_cast<mirror::Object*>(reinterpret_cast<uintptr_t>(this) + sizeof(*this));
205 }
206
207 // Returns the next allocation header after the object associated with this allocation header.
208 AllocationHeader* GetNextAllocationHeader() {
209 DCHECK_NE(alloc_size_, 0U);
210 return reinterpret_cast<AllocationHeader*>(reinterpret_cast<uintptr_t>(this) + alloc_size_);
211 }
212
213 // Returns how many free bytes there is before the block.
214 size_t GetPrevFree() const {
215 return prev_free_;
216 }
217
218 // Update the size of the free block prior to the allocation.
219 void SetPrevFree(size_t prev_free) {
220 DCHECK(IsAligned<kPageSize>(prev_free));
221 prev_free_ = prev_free;
222 }
223
224 // Finds and returns the next non free allocation header after ourself.
225 // TODO: Optimize, currently O(n) for n free following pages.
226 AllocationHeader* GetNextNonFree();
227
228 // Used to implement best fit object allocation. Each allocation has an AllocationHeader which
229 // contains the size of the previous free block preceding it. Implemented in such a way that we
230 // can also find the iterator for any allocation header pointer.
231 class SortByPrevFree {
232 public:
233 bool operator()(const AllocationHeader* a, const AllocationHeader* b) const {
234 if (a->GetPrevFree() < b->GetPrevFree()) return true;
235 if (a->GetPrevFree() > b->GetPrevFree()) return false;
236 if (a->AllocationSize() < b->AllocationSize()) return true;
237 if (a->AllocationSize() > b->AllocationSize()) return false;
238 return reinterpret_cast<uintptr_t>(a) < reinterpret_cast<uintptr_t>(b);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700239 }
240 };
241
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700242 private:
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700243 // Contains the size of the previous free block, if 0 then the memory preceding us is an
244 // allocation.
245 size_t prev_free_;
246
247 // Allocation size of this object, 0 means that the allocation header is free memory.
248 size_t alloc_size_;
249
250 friend class FreeListSpace;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700251 };
252
253 FreeListSpace(const std::string& name, MemMap* mem_map, byte* begin, byte* end);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700254
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700255 // Removes header from the free blocks set by finding the corresponding iterator and erasing it.
256 void RemoveFreePrev(AllocationHeader* header) EXCLUSIVE_LOCKS_REQUIRED(lock_);
257
258 // Finds the allocation header corresponding to obj.
259 AllocationHeader* GetAllocationHeader(const mirror::Object* obj);
260
261 typedef std::set<AllocationHeader*, AllocationHeader::SortByPrevFree,
Ian Rogers700a4022014-05-19 16:49:03 -0700262 accounting::GcAllocator<AllocationHeader*>> FreeBlocks;
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700263
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700264 // There is not footer for any allocations at the end of the space, so we keep track of how much
265 // free space there is at the end manually.
Ian Rogers700a4022014-05-19 16:49:03 -0700266 std::unique_ptr<MemMap> mem_map_;
Ian Rogers22a20862013-03-16 16:34:57 -0700267 Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700268 size_t free_end_ GUARDED_BY(lock_);
269 FreeBlocks free_blocks_ GUARDED_BY(lock_);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700270};
271
Ian Rogers1d54e732013-05-02 21:10:01 -0700272} // namespace space
273} // namespace gc
274} // namespace art
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700275
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700276#endif // ART_RUNTIME_GC_SPACE_LARGE_OBJECT_SPACE_H_