malloc_space.h revision 661974a5561e5ccdfbac8cb5d8df8b7e6f3483b8
1cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi/* 2cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi * Copyright (C) 2013 The Android Open Source Project 3cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi * 4cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi * Licensed under the Apache License, Version 2.0 (the "License"); 5cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi * you may not use this file except in compliance with the License. 6cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi * You may obtain a copy of the License at 7cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi * 8cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi * http://www.apache.org/licenses/LICENSE-2.0 9cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi * 10cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi * Unless required by applicable law or agreed to in writing, software 11cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi * distributed under the License is distributed on an "AS IS" BASIS, 12cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi * See the License for the specific language governing permissions and 14cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi * limitations under the License. 15cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi */ 16cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi 17cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi#ifndef ART_RUNTIME_GC_SPACE_MALLOC_SPACE_H_ 18cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi#define ART_RUNTIME_GC_SPACE_MALLOC_SPACE_H_ 19cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi 20cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi#include "space.h" 21cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi 227cb7bbcfffb2716ef8d68ecb747954ec42c4bdc5Hiroshi Yamauchi#include <valgrind.h> 237cb7bbcfffb2716ef8d68ecb747954ec42c4bdc5Hiroshi Yamauchi#include <memcheck/memcheck.h> 247cb7bbcfffb2716ef8d68ecb747954ec42c4bdc5Hiroshi Yamauchi 25cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchinamespace art { 26cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchinamespace gc { 27cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi 28cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchinamespace collector { 29cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi class MarkSweep; 30cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi} // namespace collector 31cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi 32cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchinamespace space { 33cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi 34a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartierclass ZygoteSpace; 35a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier 36cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi// TODO: Remove define macro 37cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi#define CHECK_MEMORY_CALL(call, args, what) \ 38cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi do { \ 39cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi int rc = call args; \ 40cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi if (UNLIKELY(rc != 0)) { \ 41cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi errno = rc; \ 42cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi PLOG(FATAL) << # call << " failed for " << what; \ 43cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi } \ 44cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi } while (false) 45cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi 46cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi// A common parent of DlMallocSpace and RosAllocSpace. 47cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchiclass MallocSpace : public ContinuousMemMapAllocSpace { 48cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi public: 49cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi typedef void(*WalkCallback)(void *start, void *end, size_t num_bytes, void* callback_arg); 50cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi 51cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi SpaceType GetType() const { 52a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier return kSpaceTypeMallocSpace; 53cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi } 54cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi 55cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi // Allocate num_bytes allowing the underlying space to grow. 566fac447555dc94a935b78198479cce645c837b89Ian Rogers virtual mirror::Object* AllocWithGrowth(Thread* self, size_t num_bytes, 576fac447555dc94a935b78198479cce645c837b89Ian Rogers size_t* bytes_allocated, size_t* usable_size) = 0; 586fac447555dc94a935b78198479cce645c837b89Ian Rogers // Allocate num_bytes without allowing the underlying space to grow. 596fac447555dc94a935b78198479cce645c837b89Ian Rogers virtual mirror::Object* Alloc(Thread* self, size_t num_bytes, size_t* bytes_allocated, 606fac447555dc94a935b78198479cce645c837b89Ian Rogers size_t* usable_size) = 0; 616fac447555dc94a935b78198479cce645c837b89Ian Rogers // Return the storage space required by obj. If usable_size isn't nullptr then it is set to the 626fac447555dc94a935b78198479cce645c837b89Ian Rogers // amount of the storage space that may be used by obj. 636fac447555dc94a935b78198479cce645c837b89Ian Rogers virtual size_t AllocationSize(mirror::Object* obj, size_t* usable_size) = 0; 64ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers virtual size_t Free(Thread* self, mirror::Object* ptr) 65ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0; 66ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers virtual size_t FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs) 67ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0; 68cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi 69cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi#ifndef NDEBUG 70cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi virtual void CheckMoreCoreForPrecondition() {} // to be overridden in the debug build. 71cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi#else 72cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi void CheckMoreCoreForPrecondition() {} // no-op in the non-debug build. 73cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi#endif 74cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi 75cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi void* MoreCore(intptr_t increment); 76cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi 77cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi // Hands unused pages back to the system. 78cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi virtual size_t Trim() = 0; 79cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi 80cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi // Perform a mspace_inspect_all which calls back for each allocation chunk. The chunk may not be 81cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi // in use, indicated by num_bytes equaling zero. 82cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi virtual void Walk(WalkCallback callback, void* arg) = 0; 83cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi 84cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi // Returns the number of bytes that the space has currently obtained from the system. This is 85cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi // greater or equal to the amount of live data in the space. 86cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi virtual size_t GetFootprint() = 0; 87cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi 88cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi // Returns the number of bytes that the heap is allowed to obtain from the system via MoreCore. 89cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi virtual size_t GetFootprintLimit() = 0; 90cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi 91cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi // Set the maximum number of bytes that the heap is allowed to obtain from the system via 92cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi // MoreCore. Note this is used to stop the mspace growing beyond the limit to Capacity. When 93cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi // allocations fail we GC before increasing the footprint limit and allowing the mspace to grow. 94cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi virtual void SetFootprintLimit(size_t limit) = 0; 95cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi 96cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi // Removes the fork time growth limit on capacity, allowing the application to allocate up to the 97cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi // maximum reserved size of the heap. 98cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi void ClearGrowthLimit() { 99cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi growth_limit_ = NonGrowthLimitCapacity(); 100cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi } 101cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi 102cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi // Override capacity so that we only return the possibly limited capacity 103cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi size_t Capacity() const { 104cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi return growth_limit_; 105cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi } 106cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi 107cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi // The total amount of memory reserved for the alloc space. 108cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi size_t NonGrowthLimitCapacity() const { 109cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi return GetMemMap()->Size(); 110cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi } 111cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi 112cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi void Dump(std::ostream& os) const; 113cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi 114cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi void SetGrowthLimit(size_t growth_limit); 115cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi 116cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi virtual MallocSpace* CreateInstance(const std::string& name, MemMap* mem_map, void* allocator, 117cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi byte* begin, byte* end, byte* limit, size_t growth_limit) = 0; 118cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi 119a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier // Splits ourself into a zygote space and new malloc space which has our unused memory. When true, 120a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier // the low memory mode argument specifies that the heap wishes the created space to be more 121a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier // aggressive in releasing unused pages. Invalidates the space its called on. 122a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier ZygoteSpace* CreateZygoteSpace(const char* alloc_space_name, bool low_memory_mode, 123a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier MallocSpace** out_malloc_space) NO_THREAD_SAFETY_ANALYSIS; 124cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi virtual uint64_t GetBytesAllocated() = 0; 125cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi virtual uint64_t GetObjectsAllocated() = 0; 126cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi 127cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi // Returns the class of a recently freed object. 128cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi mirror::Class* FindRecentFreedObject(const mirror::Object* obj); 129cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi 130cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi protected: 131cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi MallocSpace(const std::string& name, MemMap* mem_map, byte* begin, byte* end, 132a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier byte* limit, size_t growth_limit, bool create_bitmaps = true); 133cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi 134cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi static MemMap* CreateMemMap(const std::string& name, size_t starting_size, size_t* initial_size, 135cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi size_t* growth_limit, size_t* capacity, byte* requested_begin); 136cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi 1376fac447555dc94a935b78198479cce645c837b89Ian Rogers // When true the low memory mode argument specifies that the heap wishes the created allocator to 1386fac447555dc94a935b78198479cce645c837b89Ian Rogers // be more aggressive in releasing unused pages. 139573f7d2d68e1838a0485e6b40d90c967526e00c2Hiroshi Yamauchi virtual void* CreateAllocator(void* base, size_t morecore_start, size_t initial_size, 14026d69ffc0ebc98fbc5f316d8cd3ee6ba5b2001acHiroshi Yamauchi size_t maximum_size, bool low_memory_mode) = 0; 141cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi 142661974a5561e5ccdfbac8cb5d8df8b7e6f3483b8Mathieu Chartier virtual void RegisterRecentFree(mirror::Object* ptr) 143ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) 144ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers EXCLUSIVE_LOCKS_REQUIRED(lock_); 145cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi 146a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier virtual accounting::SpaceBitmap::SweepCallback* GetSweepCallback() { 147a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier return &SweepCallback; 148a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier } 149cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi 150cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi // Recent allocation buffer. 151cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi static constexpr size_t kRecentFreeCount = kDebugSpaces ? (1 << 16) : 0; 152cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi static constexpr size_t kRecentFreeMask = kRecentFreeCount - 1; 153cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi std::pair<const mirror::Object*, mirror::Class*> recent_freed_objects_[kRecentFreeCount]; 154cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi size_t recent_free_pos_; 155cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi 156cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi static size_t bitmap_index_; 157cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi 158cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi // Used to ensure mutual exclusion when the allocation spaces data structures are being modified. 159cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; 160cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi 161cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi // The capacity of the alloc space until such time that ClearGrowthLimit is called. 162cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi // The underlying mem_map_ controls the maximum size we allow the heap to grow to. The growth 163cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi // limit is a value <= to the mem_map_ capacity used for ergonomic reasons because of the zygote. 164cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi // Prior to forking the zygote the heap will have a maximally sized mem_map_ but the growth_limit_ 165cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi // will be set to a lower value. The growth_limit_ is used as the capacity of the alloc_space_, 166cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi // however, capacity normally can't vary. In the case of the growth_limit_ it can be cleared 167cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi // one time by a call to ClearGrowthLimit. 168cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi size_t growth_limit_; 169cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi 170e5eedcb4a634246d1f912992853441f715d705ccHiroshi Yamauchi private: 171ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers static void SweepCallback(size_t num_ptrs, mirror::Object** ptrs, void* arg) 172ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 173a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier 174cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi DISALLOW_COPY_AND_ASSIGN(MallocSpace); 175cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi}; 176cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi 177cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi} // namespace space 178cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi} // namespace gc 179cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi} // namespace art 180cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi 181e5eedcb4a634246d1f912992853441f715d705ccHiroshi Yamauchi#endif // ART_RUNTIME_GC_SPACE_MALLOC_SPACE_H_ 182