Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | #include <algorithm> |
| 18 | #include <ostream> |
| 19 | |
| 20 | #include "compiled_method_storage.h" |
| 21 | |
| 22 | #include "base/logging.h" |
| 23 | #include "compiled_method.h" |
Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame^] | 24 | #include "linker/linker_patch.h" |
Andreas Gampe | b486a98 | 2017-06-01 13:45:54 -0700 | [diff] [blame] | 25 | #include "thread-current-inl.h" |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 26 | #include "utils.h" |
| 27 | #include "utils/dedupe_set-inl.h" |
| 28 | #include "utils/swap_space.h" |
| 29 | |
| 30 | namespace art { |
| 31 | |
| 32 | namespace { // anonymous namespace |
| 33 | |
| 34 | template <typename T> |
| 35 | const LengthPrefixedArray<T>* CopyArray(SwapSpace* swap_space, const ArrayRef<const T>& array) { |
| 36 | DCHECK(!array.empty()); |
| 37 | SwapAllocator<uint8_t> allocator(swap_space); |
| 38 | void* storage = allocator.allocate(LengthPrefixedArray<T>::ComputeSize(array.size())); |
| 39 | LengthPrefixedArray<T>* array_copy = new(storage) LengthPrefixedArray<T>(array.size()); |
| 40 | std::copy(array.begin(), array.end(), array_copy->begin()); |
| 41 | return array_copy; |
| 42 | } |
| 43 | |
| 44 | template <typename T> |
| 45 | void ReleaseArray(SwapSpace* swap_space, const LengthPrefixedArray<T>* array) { |
| 46 | SwapAllocator<uint8_t> allocator(swap_space); |
| 47 | size_t size = LengthPrefixedArray<T>::ComputeSize(array->size()); |
| 48 | array->~LengthPrefixedArray<T>(); |
| 49 | allocator.deallocate(const_cast<uint8_t*>(reinterpret_cast<const uint8_t*>(array)), size); |
| 50 | } |
| 51 | |
| 52 | } // anonymous namespace |
| 53 | |
| 54 | template <typename T, typename DedupeSetType> |
| 55 | inline const LengthPrefixedArray<T>* CompiledMethodStorage::AllocateOrDeduplicateArray( |
| 56 | const ArrayRef<const T>& data, |
| 57 | DedupeSetType* dedupe_set) { |
| 58 | if (data.empty()) { |
| 59 | return nullptr; |
| 60 | } else if (!DedupeEnabled()) { |
| 61 | return CopyArray(swap_space_.get(), data); |
| 62 | } else { |
| 63 | return dedupe_set->Add(Thread::Current(), data); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | template <typename T> |
| 68 | inline void CompiledMethodStorage::ReleaseArrayIfNotDeduplicated( |
| 69 | const LengthPrefixedArray<T>* array) { |
| 70 | if (array != nullptr && !DedupeEnabled()) { |
| 71 | ReleaseArray(swap_space_.get(), array); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | template <typename ContentType> |
| 76 | class CompiledMethodStorage::DedupeHashFunc { |
| 77 | private: |
| 78 | static constexpr bool kUseMurmur3Hash = true; |
| 79 | |
| 80 | public: |
| 81 | size_t operator()(const ArrayRef<ContentType>& array) const { |
| 82 | const uint8_t* data = reinterpret_cast<const uint8_t*>(array.data()); |
| 83 | // TODO: More reasonable assertion. |
| 84 | // static_assert(IsPowerOfTwo(sizeof(ContentType)), |
| 85 | // "ContentType is not power of two, don't know whether array layout is as assumed"); |
| 86 | uint32_t len = sizeof(ContentType) * array.size(); |
| 87 | if (kUseMurmur3Hash) { |
| 88 | static constexpr uint32_t c1 = 0xcc9e2d51; |
| 89 | static constexpr uint32_t c2 = 0x1b873593; |
| 90 | static constexpr uint32_t r1 = 15; |
| 91 | static constexpr uint32_t r2 = 13; |
| 92 | static constexpr uint32_t m = 5; |
| 93 | static constexpr uint32_t n = 0xe6546b64; |
| 94 | |
| 95 | uint32_t hash = 0; |
| 96 | |
| 97 | const int nblocks = len / 4; |
| 98 | typedef __attribute__((__aligned__(1))) uint32_t unaligned_uint32_t; |
| 99 | const unaligned_uint32_t *blocks = reinterpret_cast<const uint32_t*>(data); |
| 100 | int i; |
| 101 | for (i = 0; i < nblocks; i++) { |
| 102 | uint32_t k = blocks[i]; |
| 103 | k *= c1; |
| 104 | k = (k << r1) | (k >> (32 - r1)); |
| 105 | k *= c2; |
| 106 | |
| 107 | hash ^= k; |
| 108 | hash = ((hash << r2) | (hash >> (32 - r2))) * m + n; |
| 109 | } |
| 110 | |
| 111 | const uint8_t *tail = reinterpret_cast<const uint8_t*>(data + nblocks * 4); |
| 112 | uint32_t k1 = 0; |
| 113 | |
| 114 | switch (len & 3) { |
| 115 | case 3: |
| 116 | k1 ^= tail[2] << 16; |
| 117 | FALLTHROUGH_INTENDED; |
| 118 | case 2: |
| 119 | k1 ^= tail[1] << 8; |
| 120 | FALLTHROUGH_INTENDED; |
| 121 | case 1: |
| 122 | k1 ^= tail[0]; |
| 123 | |
| 124 | k1 *= c1; |
| 125 | k1 = (k1 << r1) | (k1 >> (32 - r1)); |
| 126 | k1 *= c2; |
| 127 | hash ^= k1; |
| 128 | } |
| 129 | |
| 130 | hash ^= len; |
| 131 | hash ^= (hash >> 16); |
| 132 | hash *= 0x85ebca6b; |
| 133 | hash ^= (hash >> 13); |
| 134 | hash *= 0xc2b2ae35; |
| 135 | hash ^= (hash >> 16); |
| 136 | |
| 137 | return hash; |
| 138 | } else { |
| 139 | size_t hash = 0x811c9dc5; |
| 140 | for (uint32_t i = 0; i < len; ++i) { |
| 141 | hash = (hash * 16777619) ^ data[i]; |
| 142 | } |
| 143 | hash += hash << 13; |
| 144 | hash ^= hash >> 7; |
| 145 | hash += hash << 3; |
| 146 | hash ^= hash >> 17; |
| 147 | hash += hash << 5; |
| 148 | return hash; |
| 149 | } |
| 150 | } |
| 151 | }; |
| 152 | |
| 153 | template <typename T> |
| 154 | class CompiledMethodStorage::LengthPrefixedArrayAlloc { |
| 155 | public: |
| 156 | explicit LengthPrefixedArrayAlloc(SwapSpace* swap_space) |
| 157 | : swap_space_(swap_space) { |
| 158 | } |
| 159 | |
| 160 | const LengthPrefixedArray<T>* Copy(const ArrayRef<const T>& array) { |
| 161 | return CopyArray(swap_space_, array); |
| 162 | } |
| 163 | |
| 164 | void Destroy(const LengthPrefixedArray<T>* array) { |
| 165 | ReleaseArray(swap_space_, array); |
| 166 | } |
| 167 | |
| 168 | private: |
| 169 | SwapSpace* const swap_space_; |
| 170 | }; |
| 171 | |
| 172 | CompiledMethodStorage::CompiledMethodStorage(int swap_fd) |
| 173 | : swap_space_(swap_fd == -1 ? nullptr : new SwapSpace(swap_fd, 10 * MB)), |
| 174 | dedupe_enabled_(true), |
| 175 | dedupe_code_("dedupe code", LengthPrefixedArrayAlloc<uint8_t>(swap_space_.get())), |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 176 | dedupe_method_info_("dedupe method info", |
| 177 | LengthPrefixedArrayAlloc<uint8_t>(swap_space_.get())), |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 178 | dedupe_vmap_table_("dedupe vmap table", |
| 179 | LengthPrefixedArrayAlloc<uint8_t>(swap_space_.get())), |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 180 | dedupe_cfi_info_("dedupe cfi info", LengthPrefixedArrayAlloc<uint8_t>(swap_space_.get())), |
| 181 | dedupe_linker_patches_("dedupe cfi info", |
Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame^] | 182 | LengthPrefixedArrayAlloc<linker::LinkerPatch>(swap_space_.get())) { |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | CompiledMethodStorage::~CompiledMethodStorage() { |
| 186 | // All done by member destructors. |
| 187 | } |
| 188 | |
| 189 | void CompiledMethodStorage::DumpMemoryUsage(std::ostream& os, bool extended) const { |
| 190 | if (swap_space_.get() != nullptr) { |
Anton Kirilov | dd9473b | 2016-01-28 15:08:01 +0000 | [diff] [blame] | 191 | const size_t swap_size = swap_space_->GetSize(); |
| 192 | os << " swap=" << PrettySize(swap_size) << " (" << swap_size << "B)"; |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 193 | } |
| 194 | if (extended) { |
| 195 | Thread* self = Thread::Current(); |
| 196 | os << "\nCode dedupe: " << dedupe_code_.DumpStats(self); |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 197 | os << "\nVmap table dedupe: " << dedupe_vmap_table_.DumpStats(self); |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 198 | os << "\nCFI info dedupe: " << dedupe_cfi_info_.DumpStats(self); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | const LengthPrefixedArray<uint8_t>* CompiledMethodStorage::DeduplicateCode( |
| 203 | const ArrayRef<const uint8_t>& code) { |
| 204 | return AllocateOrDeduplicateArray(code, &dedupe_code_); |
| 205 | } |
| 206 | |
| 207 | void CompiledMethodStorage::ReleaseCode(const LengthPrefixedArray<uint8_t>* code) { |
| 208 | ReleaseArrayIfNotDeduplicated(code); |
| 209 | } |
| 210 | |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 211 | const LengthPrefixedArray<uint8_t>* CompiledMethodStorage::DeduplicateMethodInfo( |
| 212 | const ArrayRef<const uint8_t>& src_map) { |
| 213 | return AllocateOrDeduplicateArray(src_map, &dedupe_method_info_); |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 214 | } |
| 215 | |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 216 | void CompiledMethodStorage::ReleaseMethodInfo(const LengthPrefixedArray<uint8_t>* method_info) { |
| 217 | ReleaseArrayIfNotDeduplicated(method_info); |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 218 | } |
| 219 | |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 220 | const LengthPrefixedArray<uint8_t>* CompiledMethodStorage::DeduplicateVMapTable( |
| 221 | const ArrayRef<const uint8_t>& table) { |
| 222 | return AllocateOrDeduplicateArray(table, &dedupe_vmap_table_); |
| 223 | } |
| 224 | |
| 225 | void CompiledMethodStorage::ReleaseVMapTable(const LengthPrefixedArray<uint8_t>* table) { |
| 226 | ReleaseArrayIfNotDeduplicated(table); |
| 227 | } |
| 228 | |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 229 | const LengthPrefixedArray<uint8_t>* CompiledMethodStorage::DeduplicateCFIInfo( |
| 230 | const ArrayRef<const uint8_t>& cfi_info) { |
| 231 | return AllocateOrDeduplicateArray(cfi_info, &dedupe_cfi_info_); |
| 232 | } |
| 233 | |
| 234 | void CompiledMethodStorage::ReleaseCFIInfo(const LengthPrefixedArray<uint8_t>* cfi_info) { |
| 235 | ReleaseArrayIfNotDeduplicated(cfi_info); |
| 236 | } |
| 237 | |
Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame^] | 238 | const LengthPrefixedArray<linker::LinkerPatch>* CompiledMethodStorage::DeduplicateLinkerPatches( |
| 239 | const ArrayRef<const linker::LinkerPatch>& linker_patches) { |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 240 | return AllocateOrDeduplicateArray(linker_patches, &dedupe_linker_patches_); |
| 241 | } |
| 242 | |
| 243 | void CompiledMethodStorage::ReleaseLinkerPatches( |
Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame^] | 244 | const LengthPrefixedArray<linker::LinkerPatch>* linker_patches) { |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 245 | ReleaseArrayIfNotDeduplicated(linker_patches); |
| 246 | } |
| 247 | |
| 248 | } // namespace art |