blob: 9d5e090a29f90f881c66d00c0085bb4d51e86507 [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 Chartierbad02672014-08-25 13:08:22 -070020#include "base/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
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -070032class AllocationInfo;
33
Ian Rogers22a20862013-03-16 16:34:57 -070034// Abstraction implemented by all large object spaces.
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070035class LargeObjectSpace : public DiscontinuousSpace, public AllocSpace {
36 public:
Ian Rogers6fac4472014-02-25 17:01:10 -080037 SpaceType GetType() const OVERRIDE {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070038 return kSpaceTypeLargeObjectSpace;
39 }
Ian Rogers6fac4472014-02-25 17:01:10 -080040 void SwapBitmaps();
41 void CopyLiveToMarked();
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070042 virtual void Walk(DlMallocSpace::WalkCallback, void* arg) = 0;
43 virtual ~LargeObjectSpace() {}
44
Ian Rogers6fac4472014-02-25 17:01:10 -080045 uint64_t GetBytesAllocated() OVERRIDE {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070046 return num_bytes_allocated_;
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 }
Mathieu Chartierbbd695c2014-04-16 09:48:48 -070051 uint64_t GetTotalBytesAllocated() const {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070052 return total_bytes_allocated_;
53 }
Mathieu Chartierbbd695c2014-04-16 09:48:48 -070054 uint64_t GetTotalObjectsAllocated() const {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070055 return total_objects_allocated_;
56 }
Ian Rogers6fac4472014-02-25 17:01:10 -080057 size_t FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs) OVERRIDE;
Ian Rogers6fac4472014-02-25 17:01:10 -080058 // LargeObjectSpaces don't have thread local state.
59 void RevokeThreadLocalBuffers(art::Thread*) OVERRIDE {
60 }
61 void RevokeAllThreadLocalBuffers() OVERRIDE {
62 }
Ian Rogers6fac4472014-02-25 17:01:10 -080063 bool IsAllocSpace() const OVERRIDE {
Mathieu Chartier590fee92013-09-13 13:46:47 -070064 return true;
65 }
Ian Rogers6fac4472014-02-25 17:01:10 -080066 AllocSpace* AsAllocSpace() OVERRIDE {
Mathieu Chartier590fee92013-09-13 13:46:47 -070067 return this;
68 }
Mathieu Chartier10fb83a2014-06-15 15:15:43 -070069 collector::ObjectBytePair Sweep(bool swap_bitmaps);
Mathieu Chartier31f44142014-04-08 14:40:03 -070070 virtual bool CanMoveObjects() const OVERRIDE {
71 return false;
72 }
Mathieu Chartierbbd695c2014-04-16 09:48:48 -070073 // Current address at which the space begins, which may vary as the space is filled.
74 byte* Begin() const {
75 return begin_;
76 }
Mathieu Chartierbbd695c2014-04-16 09:48:48 -070077 // Current address at which the space ends, which may vary as the space is filled.
78 byte* End() const {
79 return end_;
80 }
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -070081 // Current size of space
82 size_t Size() const {
83 return End() - Begin();
84 }
85 // Return true if we contain the specified address.
86 bool Contains(const mirror::Object* obj) const {
87 const byte* byte_obj = reinterpret_cast<const byte*>(obj);
88 return Begin() <= byte_obj && byte_obj < End();
89 }
Mathieu Chartierb363f662014-07-16 13:28:58 -070090 void LogFragmentationAllocFailure(std::ostream& os, size_t failed_alloc_bytes) OVERRIDE
91 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
92
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070093 protected:
Mathieu Chartierbbd695c2014-04-16 09:48:48 -070094 explicit LargeObjectSpace(const std::string& name, byte* begin, byte* end);
Mathieu Chartierbbd695c2014-04-16 09:48:48 -070095 static void SweepCallback(size_t num_ptrs, mirror::Object** ptrs, void* arg);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070096
97 // Approximate number of bytes which have been allocated into the space.
Mathieu Chartierbbd695c2014-04-16 09:48:48 -070098 uint64_t num_bytes_allocated_;
99 uint64_t num_objects_allocated_;
100 uint64_t total_bytes_allocated_;
101 uint64_t total_objects_allocated_;
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700102 // Begin and end, may change as more large objects are allocated.
103 byte* begin_;
104 byte* end_;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700105
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700106 friend class Space;
Ian Rogers1d54e732013-05-02 21:10:01 -0700107
108 private:
109 DISALLOW_COPY_AND_ASSIGN(LargeObjectSpace);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700110};
111
Ian Rogers22a20862013-03-16 16:34:57 -0700112// A discontinuous large object space implemented by individual mmap/munmap calls.
Mathieu Chartier0767c9a2014-03-26 12:53:19 -0700113class LargeObjectMapSpace : public LargeObjectSpace {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700114 public:
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700115 // Creates a large object space. Allocations into the large object space use memory maps instead
116 // of malloc.
117 static LargeObjectMapSpace* Create(const std::string& name);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700118 // Return the storage space required by obj.
Ian Rogers6fac4472014-02-25 17:01:10 -0800119 size_t AllocationSize(mirror::Object* obj, size_t* usable_size);
120 mirror::Object* Alloc(Thread* self, size_t num_bytes, size_t* bytes_allocated,
121 size_t* usable_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800122 size_t Free(Thread* self, mirror::Object* ptr);
Ian Rogers6fac4472014-02-25 17:01:10 -0800123 void Walk(DlMallocSpace::WalkCallback, void* arg) OVERRIDE LOCKS_EXCLUDED(lock_);
Ian Rogersa3dd0b32013-03-19 19:30:59 -0700124 // TODO: disabling thread safety analysis as this may be called when we already hold lock_.
Ian Rogers1d54e732013-05-02 21:10:01 -0700125 bool Contains(const mirror::Object* obj) const NO_THREAD_SAFETY_ANALYSIS;
126
Mathieu Chartier0767c9a2014-03-26 12:53:19 -0700127 protected:
Brian Carlstrom93ba8932013-07-17 21:31:49 -0700128 explicit LargeObjectMapSpace(const std::string& name);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700129 virtual ~LargeObjectMapSpace() {}
130
131 // Used to ensure mutual exclusion when the allocation spaces data structures are being modified.
Ian Rogers22a20862013-03-16 16:34:57 -0700132 mutable Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Mathieu Chartierbad02672014-08-25 13:08:22 -0700133 std::vector<mirror::Object*, TrackingAllocator<mirror::Object*, kAllocatorTagLOS>> large_objects_
134 GUARDED_BY(lock_);
Mathieu Chartier0a9dc052013-07-25 11:01:28 -0700135 typedef SafeMap<mirror::Object*, MemMap*, std::less<mirror::Object*>,
Mathieu Chartierbad02672014-08-25 13:08:22 -0700136 TrackingAllocator<std::pair<mirror::Object*, MemMap*>, kAllocatorTagLOSMaps>> MemMaps;
Ian Rogers22a20862013-03-16 16:34:57 -0700137 MemMaps mem_maps_ GUARDED_BY(lock_);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700138};
139
Ian Rogers22a20862013-03-16 16:34:57 -0700140// A continuous large object space with a free-list to handle holes.
Ian Rogers6fac4472014-02-25 17:01:10 -0800141class FreeListSpace FINAL : public LargeObjectSpace {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700142 public:
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700143 static constexpr size_t kAlignment = kPageSize;
144
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700145 virtual ~FreeListSpace();
146 static FreeListSpace* Create(const std::string& name, byte* requested_begin, size_t capacity);
Ian Rogers6fac4472014-02-25 17:01:10 -0800147 size_t AllocationSize(mirror::Object* obj, size_t* usable_size) OVERRIDE
148 EXCLUSIVE_LOCKS_REQUIRED(lock_);
149 mirror::Object* Alloc(Thread* self, size_t num_bytes, size_t* bytes_allocated,
150 size_t* usable_size) OVERRIDE;
151 size_t Free(Thread* self, mirror::Object* obj) OVERRIDE;
Ian Rogers6fac4472014-02-25 17:01:10 -0800152 void Walk(DlMallocSpace::WalkCallback callback, void* arg) OVERRIDE LOCKS_EXCLUDED(lock_);
Ian Rogers1d54e732013-05-02 21:10:01 -0700153 void Dump(std::ostream& os) const;
Mathieu Chartier128c52c2012-10-16 14:12:41 -0700154
Mathieu Chartier0767c9a2014-03-26 12:53:19 -0700155 protected:
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700156 FreeListSpace(const std::string& name, MemMap* mem_map, byte* begin, byte* end);
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700157 size_t GetSlotIndexForAddress(uintptr_t address) const {
158 DCHECK(Contains(reinterpret_cast<mirror::Object*>(address)));
159 return (address - reinterpret_cast<uintptr_t>(Begin())) / kAlignment;
160 }
161 size_t GetSlotIndexForAllocationInfo(const AllocationInfo* info) const;
162 AllocationInfo* GetAllocationInfoForAddress(uintptr_t address);
163 const AllocationInfo* GetAllocationInfoForAddress(uintptr_t address) const;
164 uintptr_t GetAllocationAddressForSlot(size_t slot) const {
165 return reinterpret_cast<uintptr_t>(Begin()) + slot * kAlignment;
166 }
167 uintptr_t GetAddressForAllocationInfo(const AllocationInfo* info) const {
168 return GetAllocationAddressForSlot(GetSlotIndexForAllocationInfo(info));
169 }
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700170 // Removes header from the free blocks set by finding the corresponding iterator and erasing it.
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700171 void RemoveFreePrev(AllocationInfo* info) EXCLUSIVE_LOCKS_REQUIRED(lock_);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700172
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700173 class SortByPrevFree {
174 public:
175 bool operator()(const AllocationInfo* a, const AllocationInfo* b) const;
176 };
177 typedef std::set<AllocationInfo*, SortByPrevFree,
178 TrackingAllocator<AllocationInfo*, kAllocatorTagLOSFreeList>> FreeBlocks;
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700179
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700180 // There is not footer for any allocations at the end of the space, so we keep track of how much
181 // free space there is at the end manually.
Ian Rogers700a4022014-05-19 16:49:03 -0700182 std::unique_ptr<MemMap> mem_map_;
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700183 // Side table for allocation info, one per page.
184 std::unique_ptr<MemMap> allocation_info_map_;
185 AllocationInfo* allocation_info_;
186
Ian Rogers22a20862013-03-16 16:34:57 -0700187 Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700188 // Free bytes at the end of the space.
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700189 size_t free_end_ GUARDED_BY(lock_);
190 FreeBlocks free_blocks_ GUARDED_BY(lock_);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700191};
192
Ian Rogers1d54e732013-05-02 21:10:01 -0700193} // namespace space
194} // namespace gc
195} // namespace art
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700196
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700197#endif // ART_RUNTIME_GC_SPACE_LARGE_OBJECT_SPACE_H_