blob: 0320e79f3892b2cc18958ac7e60567fd92e2f957 [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
Igor Murashkinaaebaa02015-01-26 10:55:53 -080034enum class LargeObjectSpaceType {
35 kDisabled,
36 kMap,
37 kFreeList,
Mathieu Chartier2dbe6272014-09-16 10:43:23 -070038};
39
Ian Rogers22a20862013-03-16 16:34:57 -070040// Abstraction implemented by all large object spaces.
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070041class LargeObjectSpace : public DiscontinuousSpace, public AllocSpace {
42 public:
Ian Rogers6fac4472014-02-25 17:01:10 -080043 SpaceType GetType() const OVERRIDE {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070044 return kSpaceTypeLargeObjectSpace;
45 }
Ian Rogers6fac4472014-02-25 17:01:10 -080046 void SwapBitmaps();
47 void CopyLiveToMarked();
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070048 virtual void Walk(DlMallocSpace::WalkCallback, void* arg) = 0;
49 virtual ~LargeObjectSpace() {}
50
Ian Rogers6fac4472014-02-25 17:01:10 -080051 uint64_t GetBytesAllocated() OVERRIDE {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070052 return num_bytes_allocated_;
53 }
Ian Rogers6fac4472014-02-25 17:01:10 -080054 uint64_t GetObjectsAllocated() OVERRIDE {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070055 return num_objects_allocated_;
56 }
Mathieu Chartierbbd695c2014-04-16 09:48:48 -070057 uint64_t GetTotalBytesAllocated() const {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070058 return total_bytes_allocated_;
59 }
Mathieu Chartierbbd695c2014-04-16 09:48:48 -070060 uint64_t GetTotalObjectsAllocated() const {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070061 return total_objects_allocated_;
62 }
Ian Rogers6fac4472014-02-25 17:01:10 -080063 size_t FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs) OVERRIDE;
Ian Rogers6fac4472014-02-25 17:01:10 -080064 // LargeObjectSpaces don't have thread local state.
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -070065 size_t RevokeThreadLocalBuffers(art::Thread*) OVERRIDE {
66 return 0U;
Ian Rogers6fac4472014-02-25 17:01:10 -080067 }
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -070068 size_t RevokeAllThreadLocalBuffers() OVERRIDE {
69 return 0U;
Ian Rogers6fac4472014-02-25 17:01:10 -080070 }
Ian Rogers6fac4472014-02-25 17:01:10 -080071 bool IsAllocSpace() const OVERRIDE {
Mathieu Chartier590fee92013-09-13 13:46:47 -070072 return true;
73 }
Ian Rogers6fac4472014-02-25 17:01:10 -080074 AllocSpace* AsAllocSpace() OVERRIDE {
Mathieu Chartier590fee92013-09-13 13:46:47 -070075 return this;
76 }
Mathieu Chartier10fb83a2014-06-15 15:15:43 -070077 collector::ObjectBytePair Sweep(bool swap_bitmaps);
Mathieu Chartier31f44142014-04-08 14:40:03 -070078 virtual bool CanMoveObjects() const OVERRIDE {
79 return false;
80 }
Mathieu Chartierbbd695c2014-04-16 09:48:48 -070081 // Current address at which the space begins, which may vary as the space is filled.
Ian Rogers13735952014-10-08 12:43:28 -070082 uint8_t* Begin() const {
Mathieu Chartierbbd695c2014-04-16 09:48:48 -070083 return begin_;
84 }
Mathieu Chartierbbd695c2014-04-16 09:48:48 -070085 // Current address at which the space ends, which may vary as the space is filled.
Ian Rogers13735952014-10-08 12:43:28 -070086 uint8_t* End() const {
Mathieu Chartierbbd695c2014-04-16 09:48:48 -070087 return end_;
88 }
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -070089 // Current size of space
90 size_t Size() const {
91 return End() - Begin();
92 }
93 // Return true if we contain the specified address.
94 bool Contains(const mirror::Object* obj) const {
Ian Rogers13735952014-10-08 12:43:28 -070095 const uint8_t* byte_obj = reinterpret_cast<const uint8_t*>(obj);
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -070096 return Begin() <= byte_obj && byte_obj < End();
97 }
Mathieu Chartierb363f662014-07-16 13:28:58 -070098 void LogFragmentationAllocFailure(std::ostream& os, size_t failed_alloc_bytes) OVERRIDE
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070099 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierb363f662014-07-16 13:28:58 -0700100
Mathieu Chartiere7158112015-06-03 13:32:15 -0700101 // Return true if the large object is a zygote large object. Potentially slow.
102 virtual bool IsZygoteLargeObject(Thread* self, mirror::Object* obj) const = 0;
103 // Called when we create the zygote space, mark all existing large objects as zygote large
104 // objects.
105 virtual void SetAllLargeObjectsAsZygoteObjects(Thread* self) = 0;
106
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700107 protected:
Ian Rogers13735952014-10-08 12:43:28 -0700108 explicit LargeObjectSpace(const std::string& name, uint8_t* begin, uint8_t* end);
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700109 static void SweepCallback(size_t num_ptrs, mirror::Object** ptrs, void* arg);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700110
111 // Approximate number of bytes which have been allocated into the space.
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700112 uint64_t num_bytes_allocated_;
113 uint64_t num_objects_allocated_;
114 uint64_t total_bytes_allocated_;
115 uint64_t total_objects_allocated_;
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700116 // Begin and end, may change as more large objects are allocated.
Ian Rogers13735952014-10-08 12:43:28 -0700117 uint8_t* begin_;
118 uint8_t* end_;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700119
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700120 friend class Space;
Ian Rogers1d54e732013-05-02 21:10:01 -0700121
122 private:
123 DISALLOW_COPY_AND_ASSIGN(LargeObjectSpace);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700124};
125
Ian Rogers22a20862013-03-16 16:34:57 -0700126// A discontinuous large object space implemented by individual mmap/munmap calls.
Mathieu Chartier0767c9a2014-03-26 12:53:19 -0700127class LargeObjectMapSpace : public LargeObjectSpace {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700128 public:
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700129 // Creates a large object space. Allocations into the large object space use memory maps instead
130 // of malloc.
131 static LargeObjectMapSpace* Create(const std::string& name);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700132 // Return the storage space required by obj.
Mathieu Chartier90443472015-07-16 20:32:27 -0700133 size_t AllocationSize(mirror::Object* obj, size_t* usable_size) REQUIRES(!lock_);
Ian Rogers6fac4472014-02-25 17:01:10 -0800134 mirror::Object* Alloc(Thread* self, size_t num_bytes, size_t* bytes_allocated,
Mathieu Chartier90443472015-07-16 20:32:27 -0700135 size_t* usable_size, size_t* bytes_tl_bulk_allocated)
136 REQUIRES(!lock_);
137 size_t Free(Thread* self, mirror::Object* ptr) REQUIRES(!lock_);
138 void Walk(DlMallocSpace::WalkCallback, void* arg) OVERRIDE REQUIRES(!lock_);
Ian Rogersa3dd0b32013-03-19 19:30:59 -0700139 // TODO: disabling thread safety analysis as this may be called when we already hold lock_.
Ian Rogers1d54e732013-05-02 21:10:01 -0700140 bool Contains(const mirror::Object* obj) const NO_THREAD_SAFETY_ANALYSIS;
141
Mathieu Chartier0767c9a2014-03-26 12:53:19 -0700142 protected:
Mathieu Chartiere7158112015-06-03 13:32:15 -0700143 struct LargeObject {
144 MemMap* mem_map;
145 bool is_zygote;
146 };
Brian Carlstrom93ba8932013-07-17 21:31:49 -0700147 explicit LargeObjectMapSpace(const std::string& name);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700148 virtual ~LargeObjectMapSpace() {}
149
Mathieu Chartier90443472015-07-16 20:32:27 -0700150 bool IsZygoteLargeObject(Thread* self, mirror::Object* obj) const OVERRIDE REQUIRES(!lock_);
151 void SetAllLargeObjectsAsZygoteObjects(Thread* self) OVERRIDE REQUIRES(!lock_);
Mathieu Chartiere7158112015-06-03 13:32:15 -0700152
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700153 // Used to ensure mutual exclusion when the allocation spaces data structures are being modified.
Ian Rogers22a20862013-03-16 16:34:57 -0700154 mutable Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Mathieu Chartiere7158112015-06-03 13:32:15 -0700155 AllocationTrackingSafeMap<mirror::Object*, LargeObject, kAllocatorTagLOSMaps> large_objects_
Mathieu Chartierbad02672014-08-25 13:08:22 -0700156 GUARDED_BY(lock_);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700157};
158
Ian Rogers22a20862013-03-16 16:34:57 -0700159// A continuous large object space with a free-list to handle holes.
Ian Rogers6fac4472014-02-25 17:01:10 -0800160class FreeListSpace FINAL : public LargeObjectSpace {
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700161 public:
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700162 static constexpr size_t kAlignment = kPageSize;
163
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700164 virtual ~FreeListSpace();
Ian Rogers13735952014-10-08 12:43:28 -0700165 static FreeListSpace* Create(const std::string& name, uint8_t* requested_begin, size_t capacity);
Ian Rogers6fac4472014-02-25 17:01:10 -0800166 size_t AllocationSize(mirror::Object* obj, size_t* usable_size) OVERRIDE
Mathieu Chartier90443472015-07-16 20:32:27 -0700167 REQUIRES(lock_);
Ian Rogers6fac4472014-02-25 17:01:10 -0800168 mirror::Object* Alloc(Thread* self, size_t num_bytes, size_t* bytes_allocated,
Mathieu Chartier90443472015-07-16 20:32:27 -0700169 size_t* usable_size, size_t* bytes_tl_bulk_allocated)
170 OVERRIDE REQUIRES(!lock_);
171 size_t Free(Thread* self, mirror::Object* obj) OVERRIDE REQUIRES(!lock_);
172 void Walk(DlMallocSpace::WalkCallback callback, void* arg) OVERRIDE REQUIRES(!lock_);
173 void Dump(std::ostream& os) const REQUIRES(!lock_);
Mathieu Chartier128c52c2012-10-16 14:12:41 -0700174
Mathieu Chartier0767c9a2014-03-26 12:53:19 -0700175 protected:
Ian Rogers13735952014-10-08 12:43:28 -0700176 FreeListSpace(const std::string& name, MemMap* mem_map, uint8_t* begin, uint8_t* end);
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700177 size_t GetSlotIndexForAddress(uintptr_t address) const {
178 DCHECK(Contains(reinterpret_cast<mirror::Object*>(address)));
179 return (address - reinterpret_cast<uintptr_t>(Begin())) / kAlignment;
180 }
181 size_t GetSlotIndexForAllocationInfo(const AllocationInfo* info) const;
182 AllocationInfo* GetAllocationInfoForAddress(uintptr_t address);
183 const AllocationInfo* GetAllocationInfoForAddress(uintptr_t address) const;
184 uintptr_t GetAllocationAddressForSlot(size_t slot) const {
185 return reinterpret_cast<uintptr_t>(Begin()) + slot * kAlignment;
186 }
187 uintptr_t GetAddressForAllocationInfo(const AllocationInfo* info) const {
188 return GetAllocationAddressForSlot(GetSlotIndexForAllocationInfo(info));
189 }
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700190 // Removes header from the free blocks set by finding the corresponding iterator and erasing it.
Mathieu Chartier90443472015-07-16 20:32:27 -0700191 void RemoveFreePrev(AllocationInfo* info) REQUIRES(lock_);
Mathieu Chartiere7158112015-06-03 13:32:15 -0700192 bool IsZygoteLargeObject(Thread* self, mirror::Object* obj) const OVERRIDE;
Mathieu Chartier90443472015-07-16 20:32:27 -0700193 void SetAllLargeObjectsAsZygoteObjects(Thread* self) OVERRIDE REQUIRES(!lock_);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700194
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700195 class SortByPrevFree {
196 public:
197 bool operator()(const AllocationInfo* a, const AllocationInfo* b) const;
198 };
199 typedef std::set<AllocationInfo*, SortByPrevFree,
200 TrackingAllocator<AllocationInfo*, kAllocatorTagLOSFreeList>> FreeBlocks;
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700201
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700202 // There is not footer for any allocations at the end of the space, so we keep track of how much
203 // free space there is at the end manually.
Ian Rogers700a4022014-05-19 16:49:03 -0700204 std::unique_ptr<MemMap> mem_map_;
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700205 // Side table for allocation info, one per page.
206 std::unique_ptr<MemMap> allocation_info_map_;
207 AllocationInfo* allocation_info_;
208
Mathieu Chartiere7158112015-06-03 13:32:15 -0700209 mutable Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Mathieu Chartieraf4edbd2014-09-08 17:42:48 -0700210 // Free bytes at the end of the space.
Mathieu Chartiereb5710e2013-07-25 15:19:42 -0700211 size_t free_end_ GUARDED_BY(lock_);
212 FreeBlocks free_blocks_ GUARDED_BY(lock_);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700213};
214
Ian Rogers1d54e732013-05-02 21:10:01 -0700215} // namespace space
216} // namespace gc
217} // namespace art
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700218
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700219#endif // ART_RUNTIME_GC_SPACE_LARGE_OBJECT_SPACE_H_