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 { |
| 28 | class MarkSweep; |
| 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 |
| 34 | // overridden by a ValgrindMallocSpace. |
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); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 44 | static RosAllocSpace* CreateFromMemMap(MemMap* mem_map, const std::string& name, |
| 45 | size_t starting_size, size_t initial_size, |
| 46 | size_t growth_limit, size_t capacity, |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 47 | bool low_memory_mode, bool can_move_objects); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 48 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 49 | mirror::Object* AllocWithGrowth(Thread* self, size_t num_bytes, size_t* bytes_allocated, |
| 50 | size_t* usable_size) OVERRIDE LOCKS_EXCLUDED(lock_); |
| 51 | mirror::Object* Alloc(Thread* self, size_t num_bytes, size_t* bytes_allocated, |
| 52 | size_t* usable_size) OVERRIDE { |
| 53 | return AllocNonvirtual(self, num_bytes, bytes_allocated, usable_size); |
| 54 | } |
Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 55 | mirror::Object* AllocThreadUnsafe(Thread* self, size_t num_bytes, size_t* bytes_allocated, |
| 56 | size_t* usable_size) |
| 57 | OVERRIDE EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 58 | return AllocNonvirtualThreadUnsafe(self, num_bytes, bytes_allocated, usable_size); |
| 59 | } |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 60 | size_t AllocationSize(mirror::Object* obj, size_t* usable_size) OVERRIDE { |
| 61 | return AllocationSizeNonvirtual(obj, usable_size); |
| 62 | } |
| 63 | size_t Free(Thread* self, mirror::Object* ptr) OVERRIDE |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 64 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 65 | size_t FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs) OVERRIDE |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 66 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 67 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 68 | mirror::Object* AllocNonvirtual(Thread* self, size_t num_bytes, size_t* bytes_allocated, |
| 69 | size_t* usable_size) { |
| 70 | // RosAlloc zeroes memory internally. |
| 71 | return AllocCommon(self, num_bytes, bytes_allocated, usable_size); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 72 | } |
Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 73 | mirror::Object* AllocNonvirtualThreadUnsafe(Thread* self, size_t num_bytes, |
| 74 | size_t* bytes_allocated, size_t* usable_size) { |
| 75 | // RosAlloc zeroes memory internally. Pass in false for thread unsafe. |
| 76 | return AllocCommon<false>(self, num_bytes, bytes_allocated, usable_size); |
| 77 | } |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 78 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 79 | // TODO: NO_THREAD_SAFETY_ANALYSIS because SizeOf() requires that mutator_lock is held. |
| 80 | size_t AllocationSizeNonvirtual(mirror::Object* obj, size_t* usable_size) |
| 81 | NO_THREAD_SAFETY_ANALYSIS; |
| 82 | |
| 83 | allocator::RosAlloc* GetRosAlloc() const { |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 84 | return rosalloc_; |
| 85 | } |
| 86 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 87 | size_t Trim() OVERRIDE; |
| 88 | void Walk(WalkCallback callback, void* arg) OVERRIDE LOCKS_EXCLUDED(lock_); |
| 89 | size_t GetFootprint() OVERRIDE; |
| 90 | size_t GetFootprintLimit() OVERRIDE; |
| 91 | void SetFootprintLimit(size_t limit) OVERRIDE; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 92 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 93 | void Clear() OVERRIDE; |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 94 | |
Andreas Gampe | d757632 | 2014-10-24 22:13:45 -0700 | [diff] [blame^] | 95 | MallocSpace* CreateInstance(MemMap* mem_map, const std::string& name, void* allocator, |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 96 | uint8_t* begin, uint8_t* end, uint8_t* limit, size_t growth_limit, |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 97 | bool can_move_objects) OVERRIDE; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 98 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 99 | uint64_t GetBytesAllocated() OVERRIDE; |
| 100 | uint64_t GetObjectsAllocated() OVERRIDE; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 101 | |
| 102 | void RevokeThreadLocalBuffers(Thread* thread); |
| 103 | void RevokeAllThreadLocalBuffers(); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 104 | void AssertThreadLocalBuffersAreRevoked(Thread* thread); |
Hiroshi Yamauchi | c93c530 | 2014-03-20 16:15:37 -0700 | [diff] [blame] | 105 | void AssertAllThreadLocalBuffersAreRevoked(); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 106 | |
| 107 | // Returns the class of a recently freed object. |
| 108 | mirror::Class* FindRecentFreedObject(const mirror::Object* obj); |
| 109 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 110 | bool IsRosAllocSpace() const OVERRIDE { |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 111 | return true; |
| 112 | } |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 113 | |
| 114 | RosAllocSpace* AsRosAllocSpace() OVERRIDE { |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 115 | return this; |
| 116 | } |
| 117 | |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 118 | void Verify() EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 119 | rosalloc_->Verify(); |
| 120 | } |
| 121 | |
Mathieu Chartier | 661974a | 2014-01-09 11:23:53 -0800 | [diff] [blame] | 122 | virtual ~RosAllocSpace(); |
| 123 | |
Hiroshi Yamauchi | 654dd48 | 2014-07-09 12:54:32 -0700 | [diff] [blame] | 124 | void LogFragmentationAllocFailure(std::ostream& os, size_t failed_alloc_bytes) OVERRIDE { |
| 125 | rosalloc_->LogFragmentationAllocFailure(os, failed_alloc_bytes); |
| 126 | } |
| 127 | |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 128 | protected: |
Andreas Gampe | d757632 | 2014-10-24 22:13:45 -0700 | [diff] [blame^] | 129 | RosAllocSpace(MemMap* mem_map, size_t initial_size, const std::string& name, |
| 130 | allocator::RosAlloc* rosalloc, uint8_t* begin, uint8_t* end, uint8_t* limit, |
| 131 | size_t growth_limit, bool can_move_objects, size_t starting_size, |
| 132 | bool low_memory_mode); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 133 | |
| 134 | private: |
Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 135 | template<bool kThreadSafe = true> |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 136 | mirror::Object* AllocCommon(Thread* self, size_t num_bytes, size_t* bytes_allocated, |
| 137 | size_t* usable_size); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 138 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 139 | void* CreateAllocator(void* base, size_t morecore_start, size_t initial_size, |
Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 140 | size_t maximum_size, bool low_memory_mode) OVERRIDE { |
Andreas Gampe | d757632 | 2014-10-24 22:13:45 -0700 | [diff] [blame^] | 141 | return CreateRosAlloc(base, morecore_start, initial_size, maximum_size, low_memory_mode, |
| 142 | RUNNING_ON_VALGRIND != 0); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 143 | } |
Hiroshi Yamauchi | 573f7d2 | 2013-12-17 11:54:23 -0800 | [diff] [blame] | 144 | 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^] | 145 | size_t maximum_size, bool low_memory_mode, |
| 146 | bool running_on_valgrind); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 147 | |
| 148 | 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] | 149 | void* arg, bool do_null_callback_at_end) |
| 150 | LOCKS_EXCLUDED(Locks::runtime_shutdown_lock_, Locks::thread_list_lock_); |
| 151 | void InspectAllRosAllocWithSuspendAll( |
| 152 | void (*callback)(void *start, void *end, size_t num_bytes, void* callback_arg), |
| 153 | void* arg, bool do_null_callback_at_end) |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 154 | LOCKS_EXCLUDED(Locks::runtime_shutdown_lock_, Locks::thread_list_lock_); |
| 155 | |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 156 | // Underlying rosalloc. |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 157 | allocator::RosAlloc* rosalloc_; |
| 158 | |
| 159 | const bool low_memory_mode_; |
Hiroshi Yamauchi | 4ce1f00 | 2013-11-18 14:49:09 -0800 | [diff] [blame] | 160 | |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 161 | friend class collector::MarkSweep; |
| 162 | |
| 163 | DISALLOW_COPY_AND_ASSIGN(RosAllocSpace); |
| 164 | }; |
| 165 | |
| 166 | } // namespace space |
| 167 | } // namespace gc |
| 168 | } // namespace art |
| 169 | |
| 170 | #endif // ART_RUNTIME_GC_SPACE_ROSALLOC_SPACE_H_ |