Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1 | /* |
| 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_ROSALLOC_SPACE_H_ |
| 18 | #define ART_RUNTIME_GC_SPACE_ROSALLOC_SPACE_H_ |
| 19 | |
| 20 | #include "gc/allocator/rosalloc.h" |
| 21 | #include "malloc_space.h" |
| 22 | #include "space.h" |
| 23 | |
| 24 | namespace art { |
| 25 | namespace gc { |
| 26 | |
| 27 | namespace collector { |
Igor Murashkin | 2ffb703 | 2017-11-08 13:35:21 -0800 | [diff] [blame] | 28 | class MarkSweep; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 29 | } // namespace collector |
| 30 | |
| 31 | namespace space { |
| 32 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 33 | // An alloc space implemented using a runs-of-slots memory allocator. Not final as may be |
Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 34 | // overridden by a MemoryToolMallocSpace. |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 35 | class RosAllocSpace : public MallocSpace { |
| 36 | public: |
| 37 | // Create a RosAllocSpace with the requested sizes. The requested |
| 38 | // base address is not guaranteed to be granted, if it is required, |
| 39 | // the caller should call Begin on the returned space to confirm the |
| 40 | // request was granted. |
| 41 | static RosAllocSpace* Create(const std::string& name, size_t initial_size, size_t growth_limit, |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 42 | size_t capacity, uint8_t* requested_begin, bool low_memory_mode, |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 43 | bool can_move_objects); |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 44 | static RosAllocSpace* CreateFromMemMap(MemMap&& mem_map, |
| 45 | const std::string& name, |
| 46 | size_t starting_size, |
| 47 | size_t initial_size, |
| 48 | size_t growth_limit, |
| 49 | size_t capacity, |
| 50 | bool low_memory_mode, |
| 51 | bool can_move_objects); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 52 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 53 | mirror::Object* AllocWithGrowth(Thread* self, size_t num_bytes, size_t* bytes_allocated, |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 54 | size_t* usable_size, size_t* bytes_tl_bulk_allocated) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 55 | OVERRIDE REQUIRES(!lock_); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 56 | mirror::Object* Alloc(Thread* self, size_t num_bytes, size_t* bytes_allocated, |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 57 | size_t* usable_size, size_t* bytes_tl_bulk_allocated) OVERRIDE { |
| 58 | return AllocNonvirtual(self, num_bytes, bytes_allocated, usable_size, |
| 59 | bytes_tl_bulk_allocated); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 60 | } |
Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 61 | mirror::Object* AllocThreadUnsafe(Thread* self, size_t num_bytes, size_t* bytes_allocated, |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 62 | size_t* usable_size, size_t* bytes_tl_bulk_allocated) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 63 | OVERRIDE REQUIRES(Locks::mutator_lock_) { |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 64 | return AllocNonvirtualThreadUnsafe(self, num_bytes, bytes_allocated, usable_size, |
| 65 | bytes_tl_bulk_allocated); |
Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 66 | } |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 67 | size_t AllocationSize(mirror::Object* obj, size_t* usable_size) OVERRIDE { |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 68 | return AllocationSizeNonvirtual<true>(obj, usable_size); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 69 | } |
| 70 | size_t Free(Thread* self, mirror::Object* ptr) OVERRIDE |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 71 | REQUIRES_SHARED(Locks::mutator_lock_); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 72 | size_t FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs) OVERRIDE |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 73 | REQUIRES_SHARED(Locks::mutator_lock_); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 74 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 75 | mirror::Object* AllocNonvirtual(Thread* self, size_t num_bytes, size_t* bytes_allocated, |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 76 | size_t* usable_size, size_t* bytes_tl_bulk_allocated) { |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 77 | // RosAlloc zeroes memory internally. |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 78 | return AllocCommon(self, num_bytes, bytes_allocated, usable_size, |
| 79 | bytes_tl_bulk_allocated); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 80 | } |
Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 81 | mirror::Object* AllocNonvirtualThreadUnsafe(Thread* self, size_t num_bytes, |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 82 | size_t* bytes_allocated, size_t* usable_size, |
| 83 | size_t* bytes_tl_bulk_allocated) { |
Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 84 | // RosAlloc zeroes memory internally. Pass in false for thread unsafe. |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 85 | return AllocCommon<false>(self, num_bytes, bytes_allocated, usable_size, |
| 86 | bytes_tl_bulk_allocated); |
Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 87 | } |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 88 | |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 89 | // Returns true if the given allocation request can be allocated in |
| 90 | // an existing thread local run without allocating a new run. |
| 91 | ALWAYS_INLINE bool CanAllocThreadLocal(Thread* self, size_t num_bytes); |
| 92 | // Allocate the given allocation request in an existing thread local |
| 93 | // run without allocating a new run. |
| 94 | ALWAYS_INLINE mirror::Object* AllocThreadLocal(Thread* self, size_t num_bytes, |
| 95 | size_t* bytes_allocated); |
| 96 | size_t MaxBytesBulkAllocatedFor(size_t num_bytes) OVERRIDE { |
| 97 | return MaxBytesBulkAllocatedForNonvirtual(num_bytes); |
| 98 | } |
| 99 | ALWAYS_INLINE size_t MaxBytesBulkAllocatedForNonvirtual(size_t num_bytes); |
| 100 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 101 | // TODO: NO_THREAD_SAFETY_ANALYSIS because SizeOf() requires that mutator_lock is held. |
Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 102 | template<bool kMaybeIsRunningOnMemoryTool> |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 103 | size_t AllocationSizeNonvirtual(mirror::Object* obj, size_t* usable_size) |
| 104 | NO_THREAD_SAFETY_ANALYSIS; |
| 105 | |
| 106 | allocator::RosAlloc* GetRosAlloc() const { |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 107 | return rosalloc_; |
| 108 | } |
| 109 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 110 | size_t Trim() OVERRIDE; |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 111 | void Walk(WalkCallback callback, void* arg) OVERRIDE REQUIRES(!lock_); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 112 | size_t GetFootprint() OVERRIDE; |
| 113 | size_t GetFootprintLimit() OVERRIDE; |
| 114 | void SetFootprintLimit(size_t limit) OVERRIDE; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 115 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 116 | void Clear() OVERRIDE; |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 117 | |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 118 | MallocSpace* CreateInstance(MemMap&& mem_map, |
| 119 | const std::string& name, |
| 120 | void* allocator, |
| 121 | uint8_t* begin, |
| 122 | uint8_t* end, |
| 123 | uint8_t* limit, |
| 124 | size_t growth_limit, |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 125 | bool can_move_objects) OVERRIDE; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 126 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 127 | uint64_t GetBytesAllocated() OVERRIDE; |
| 128 | uint64_t GetObjectsAllocated() OVERRIDE; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 129 | |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 130 | size_t RevokeThreadLocalBuffers(Thread* thread); |
| 131 | size_t RevokeAllThreadLocalBuffers(); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 132 | void AssertThreadLocalBuffersAreRevoked(Thread* thread); |
Hiroshi Yamauchi | c93c530 | 2014-03-20 16:15:37 -0700 | [diff] [blame] | 133 | void AssertAllThreadLocalBuffersAreRevoked(); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 134 | |
| 135 | // Returns the class of a recently freed object. |
| 136 | mirror::Class* FindRecentFreedObject(const mirror::Object* obj); |
| 137 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 138 | bool IsRosAllocSpace() const OVERRIDE { |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 139 | return true; |
| 140 | } |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 141 | |
| 142 | RosAllocSpace* AsRosAllocSpace() OVERRIDE { |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 143 | return this; |
| 144 | } |
| 145 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 146 | void Verify() REQUIRES(Locks::mutator_lock_) { |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 147 | rosalloc_->Verify(); |
| 148 | } |
| 149 | |
Mathieu Chartier | 661974a | 2014-01-09 11:23:53 -0800 | [diff] [blame] | 150 | virtual ~RosAllocSpace(); |
| 151 | |
Hiroshi Yamauchi | 654dd48 | 2014-07-09 12:54:32 -0700 | [diff] [blame] | 152 | void LogFragmentationAllocFailure(std::ostream& os, size_t failed_alloc_bytes) OVERRIDE { |
| 153 | rosalloc_->LogFragmentationAllocFailure(os, failed_alloc_bytes); |
| 154 | } |
| 155 | |
Hiroshi Yamauchi | 565c2d9 | 2016-03-23 12:53:26 -0700 | [diff] [blame] | 156 | void DumpStats(std::ostream& os); |
| 157 | |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 158 | protected: |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 159 | RosAllocSpace(MemMap&& mem_map, |
| 160 | size_t initial_size, |
| 161 | const std::string& name, |
| 162 | allocator::RosAlloc* rosalloc, |
| 163 | uint8_t* begin, |
| 164 | uint8_t* end, |
| 165 | uint8_t* limit, |
| 166 | size_t growth_limit, |
| 167 | bool can_move_objects, |
| 168 | size_t starting_size, |
Andreas Gampe | d757632 | 2014-10-24 22:13:45 -0700 | [diff] [blame] | 169 | bool low_memory_mode); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 170 | |
| 171 | private: |
Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 172 | template<bool kThreadSafe = true> |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 173 | mirror::Object* AllocCommon(Thread* self, size_t num_bytes, size_t* bytes_allocated, |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 174 | size_t* usable_size, size_t* bytes_tl_bulk_allocated); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 175 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 176 | void* CreateAllocator(void* base, size_t morecore_start, size_t initial_size, |
Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 177 | size_t maximum_size, bool low_memory_mode) OVERRIDE { |
Roland Levillain | 05e34f4 | 2018-05-24 13:19:05 +0000 | [diff] [blame] | 178 | return CreateRosAlloc( |
| 179 | base, morecore_start, initial_size, maximum_size, low_memory_mode, kRunningOnMemoryTool); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 180 | } |
Hiroshi Yamauchi | 573f7d2 | 2013-12-17 11:54:23 -0800 | [diff] [blame] | 181 | static allocator::RosAlloc* CreateRosAlloc(void* base, size_t morecore_start, size_t initial_size, |
Andreas Gampe | d757632 | 2014-10-24 22:13:45 -0700 | [diff] [blame] | 182 | size_t maximum_size, bool low_memory_mode, |
Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 183 | bool running_on_memory_tool); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 184 | |
| 185 | void InspectAllRosAlloc(void (*callback)(void *start, void *end, size_t num_bytes, void* callback_arg), |
Hiroshi Yamauchi | 1cd53db | 2014-03-28 15:26:48 -0700 | [diff] [blame] | 186 | void* arg, bool do_null_callback_at_end) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 187 | REQUIRES(!Locks::runtime_shutdown_lock_, !Locks::thread_list_lock_); |
Hiroshi Yamauchi | 1cd53db | 2014-03-28 15:26:48 -0700 | [diff] [blame] | 188 | void InspectAllRosAllocWithSuspendAll( |
| 189 | void (*callback)(void *start, void *end, size_t num_bytes, void* callback_arg), |
| 190 | void* arg, bool do_null_callback_at_end) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 191 | REQUIRES(!Locks::runtime_shutdown_lock_, !Locks::thread_list_lock_); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 192 | |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 193 | // Underlying rosalloc. |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 194 | allocator::RosAlloc* rosalloc_; |
| 195 | |
| 196 | const bool low_memory_mode_; |
Hiroshi Yamauchi | 4ce1f00 | 2013-11-18 14:49:09 -0800 | [diff] [blame] | 197 | |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 198 | friend class collector::MarkSweep; |
| 199 | |
| 200 | DISALLOW_COPY_AND_ASSIGN(RosAllocSpace); |
| 201 | }; |
| 202 | |
| 203 | } // namespace space |
| 204 | } // namespace gc |
| 205 | } // namespace art |
| 206 | |
| 207 | #endif // ART_RUNTIME_GC_SPACE_ROSALLOC_SPACE_H_ |