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