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