Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -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 | |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_BASE_ALLOCATOR_H_ |
| 18 | #define ART_RUNTIME_BASE_ALLOCATOR_H_ |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 19 | |
Vladimir Marko | ca420e4 | 2018-01-03 13:37:04 +0000 | [diff] [blame] | 20 | #include <type_traits> |
| 21 | |
Mathieu Chartier | bad0267 | 2014-08-25 13:08:22 -0700 | [diff] [blame] | 22 | #include "atomic.h" |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 23 | #include "base/macros.h" |
Mathieu Chartier | bad0267 | 2014-08-25 13:08:22 -0700 | [diff] [blame] | 24 | #include "base/mutex.h" |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 25 | |
| 26 | namespace art { |
| 27 | |
Mathieu Chartier | bad0267 | 2014-08-25 13:08:22 -0700 | [diff] [blame] | 28 | static constexpr bool kEnableTrackingAllocator = false; |
| 29 | |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 30 | class Allocator { |
| 31 | public: |
| 32 | static Allocator* GetMallocAllocator(); |
| 33 | static Allocator* GetNoopAllocator(); |
| 34 | |
| 35 | Allocator() {} |
| 36 | virtual ~Allocator() {} |
| 37 | |
| 38 | virtual void* Alloc(size_t) = 0; |
| 39 | virtual void Free(void*) = 0; |
| 40 | |
| 41 | private: |
| 42 | DISALLOW_COPY_AND_ASSIGN(Allocator); |
| 43 | }; |
| 44 | |
Mathieu Chartier | bad0267 | 2014-08-25 13:08:22 -0700 | [diff] [blame] | 45 | // Used by TrackedAllocators. |
| 46 | enum AllocatorTag { |
| 47 | kAllocatorTagHeap, |
| 48 | kAllocatorTagMonitorList, |
| 49 | kAllocatorTagClassTable, |
| 50 | kAllocatorTagInternTable, |
| 51 | kAllocatorTagMaps, |
| 52 | kAllocatorTagLOS, |
| 53 | kAllocatorTagSafeMap, |
| 54 | kAllocatorTagLOSMaps, |
| 55 | kAllocatorTagReferenceTable, |
| 56 | kAllocatorTagHeapBitmap, |
| 57 | kAllocatorTagHeapBitmapLOS, |
| 58 | kAllocatorTagMonitorPool, |
| 59 | kAllocatorTagLOSFreeList, |
| 60 | kAllocatorTagVerifier, |
| 61 | kAllocatorTagRememberedSet, |
| 62 | kAllocatorTagModUnionCardSet, |
| 63 | kAllocatorTagModUnionReferenceArray, |
Andreas Gampe | 0a7993e | 2014-12-05 11:16:26 -0800 | [diff] [blame] | 64 | kAllocatorTagJNILibraries, |
Mathieu Chartier | bad0267 | 2014-08-25 13:08:22 -0700 | [diff] [blame] | 65 | kAllocatorTagCompileTimeClassPath, |
| 66 | kAllocatorTagOatFile, |
| 67 | kAllocatorTagDexFileVerifier, |
Mathieu Chartier | 58553c7 | 2014-09-16 16:25:55 -0700 | [diff] [blame] | 68 | kAllocatorTagRosAlloc, |
Mathieu Chartier | bad0267 | 2014-08-25 13:08:22 -0700 | [diff] [blame] | 69 | kAllocatorTagCount, // Must always be last element. |
| 70 | }; |
| 71 | std::ostream& operator<<(std::ostream& os, const AllocatorTag& tag); |
| 72 | |
Ian Rogers | 7e70b00 | 2014-10-08 11:47:24 -0700 | [diff] [blame] | 73 | namespace TrackedAllocators { |
Mathieu Chartier | bad0267 | 2014-08-25 13:08:22 -0700 | [diff] [blame] | 74 | |
Ian Rogers | 7e70b00 | 2014-10-08 11:47:24 -0700 | [diff] [blame] | 75 | // Running count of number of bytes used for this kind of allocation. Increased by allocations, |
| 76 | // decreased by deallocations. |
| 77 | extern Atomic<size_t> g_bytes_used[kAllocatorTagCount]; |
Mathieu Chartier | bad0267 | 2014-08-25 13:08:22 -0700 | [diff] [blame] | 78 | |
Ian Rogers | 7e70b00 | 2014-10-08 11:47:24 -0700 | [diff] [blame] | 79 | // Largest value of bytes used seen. |
| 80 | extern volatile size_t g_max_bytes_used[kAllocatorTagCount]; |
| 81 | |
| 82 | // Total number of bytes allocated of this kind. |
| 83 | extern Atomic<uint64_t> g_total_bytes_used[kAllocatorTagCount]; |
| 84 | |
| 85 | void Dump(std::ostream& os); |
| 86 | |
| 87 | inline void RegisterAllocation(AllocatorTag tag, size_t bytes) { |
| 88 | g_total_bytes_used[tag].FetchAndAddSequentiallyConsistent(bytes); |
| 89 | size_t new_bytes = g_bytes_used[tag].FetchAndAddSequentiallyConsistent(bytes) + bytes; |
| 90 | if (g_max_bytes_used[tag] < new_bytes) { |
| 91 | g_max_bytes_used[tag] = new_bytes; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | inline void RegisterFree(AllocatorTag tag, size_t bytes) { |
| 96 | g_bytes_used[tag].FetchAndSubSequentiallyConsistent(bytes); |
| 97 | } |
| 98 | |
| 99 | } // namespace TrackedAllocators |
| 100 | |
| 101 | // Tracking allocator for use with STL types, tracks how much memory is used. |
Mathieu Chartier | bad0267 | 2014-08-25 13:08:22 -0700 | [diff] [blame] | 102 | template<class T, AllocatorTag kTag> |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 103 | class TrackingAllocatorImpl : public std::allocator<T> { |
Mathieu Chartier | bad0267 | 2014-08-25 13:08:22 -0700 | [diff] [blame] | 104 | public: |
| 105 | typedef typename std::allocator<T>::value_type value_type; |
| 106 | typedef typename std::allocator<T>::size_type size_type; |
| 107 | typedef typename std::allocator<T>::difference_type difference_type; |
| 108 | typedef typename std::allocator<T>::pointer pointer; |
| 109 | typedef typename std::allocator<T>::const_pointer const_pointer; |
| 110 | typedef typename std::allocator<T>::reference reference; |
| 111 | typedef typename std::allocator<T>::const_reference const_reference; |
| 112 | |
| 113 | // Used internally by STL data structures. |
| 114 | template <class U> |
Igor Murashkin | 5573c37 | 2017-11-16 13:34:30 -0800 | [diff] [blame] | 115 | TrackingAllocatorImpl( |
Chih-Hung Hsieh | a593118 | 2016-09-01 15:08:13 -0700 | [diff] [blame] | 116 | const TrackingAllocatorImpl<U, kTag>& alloc ATTRIBUTE_UNUSED) noexcept {} |
Mathieu Chartier | bad0267 | 2014-08-25 13:08:22 -0700 | [diff] [blame] | 117 | |
| 118 | // Used internally by STL data structures. |
Andreas Gampe | 758a801 | 2015-04-03 21:28:42 -0700 | [diff] [blame] | 119 | TrackingAllocatorImpl() noexcept { |
Andreas Gampe | 575e78c | 2014-11-03 23:41:03 -0800 | [diff] [blame] | 120 | static_assert(kTag < kAllocatorTagCount, "kTag must be less than kAllocatorTagCount"); |
Mathieu Chartier | bad0267 | 2014-08-25 13:08:22 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | // Enables an allocator for objects of one type to allocate storage for objects of another type. |
| 124 | // Used internally by STL data structures. |
| 125 | template <class U> |
| 126 | struct rebind { |
| 127 | typedef TrackingAllocatorImpl<U, kTag> other; |
| 128 | }; |
| 129 | |
Roland Levillain | 4b8f1ec | 2015-08-26 18:34:03 +0100 | [diff] [blame] | 130 | pointer allocate(size_type n, const_pointer hint ATTRIBUTE_UNUSED = 0) { |
Mathieu Chartier | bad0267 | 2014-08-25 13:08:22 -0700 | [diff] [blame] | 131 | const size_t size = n * sizeof(T); |
| 132 | TrackedAllocators::RegisterAllocation(GetTag(), size); |
| 133 | return reinterpret_cast<pointer>(malloc(size)); |
| 134 | } |
| 135 | |
| 136 | template <typename PT> |
| 137 | void deallocate(PT p, size_type n) { |
| 138 | const size_t size = n * sizeof(T); |
| 139 | TrackedAllocators::RegisterFree(GetTag(), size); |
| 140 | free(p); |
| 141 | } |
| 142 | |
Ian Rogers | 7e70b00 | 2014-10-08 11:47:24 -0700 | [diff] [blame] | 143 | static constexpr AllocatorTag GetTag() { |
Mathieu Chartier | bad0267 | 2014-08-25 13:08:22 -0700 | [diff] [blame] | 144 | return kTag; |
| 145 | } |
| 146 | }; |
| 147 | |
| 148 | template<class T, AllocatorTag kTag> |
| 149 | // C++ doesn't allow template typedefs. This is a workaround template typedef which is |
| 150 | // TrackingAllocatorImpl<T> if kEnableTrackingAllocator is true, std::allocator<T> otherwise. |
Vladimir Marko | ca420e4 | 2018-01-03 13:37:04 +0000 | [diff] [blame] | 151 | using TrackingAllocator = typename std::conditional<kEnableTrackingAllocator, |
| 152 | TrackingAllocatorImpl<T, kTag>, |
| 153 | std::allocator<T>>::type; |
Mathieu Chartier | bad0267 | 2014-08-25 13:08:22 -0700 | [diff] [blame] | 154 | |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 155 | } // namespace art |
| 156 | |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 157 | #endif // ART_RUNTIME_BASE_ALLOCATOR_H_ |