blob: aa8277edb4d4d17d464a44669070fed37d257692 [file] [log] [blame]
Vladimir Marko35831e82015-09-11 11:59:18 +01001/*
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
Andreas Gampe57943812017-12-06 21:39:13 -080022#include <android-base/logging.h>
23
David Sehrc431b9d2018-03-02 12:01:51 -080024#include "base/utils.h"
Vladimir Marko35831e82015-09-11 11:59:18 +010025#include "compiled_method.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010026#include "linker/linker_patch.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070027#include "thread-current-inl.h"
Vladimir Marko35831e82015-09-11 11:59:18 +010028#include "utils/dedupe_set-inl.h"
29#include "utils/swap_space.h"
30
31namespace art {
32
33namespace { // anonymous namespace
34
35template <typename T>
36const LengthPrefixedArray<T>* CopyArray(SwapSpace* swap_space, const ArrayRef<const T>& array) {
37 DCHECK(!array.empty());
38 SwapAllocator<uint8_t> allocator(swap_space);
39 void* storage = allocator.allocate(LengthPrefixedArray<T>::ComputeSize(array.size()));
40 LengthPrefixedArray<T>* array_copy = new(storage) LengthPrefixedArray<T>(array.size());
41 std::copy(array.begin(), array.end(), array_copy->begin());
42 return array_copy;
43}
44
45template <typename T>
46void ReleaseArray(SwapSpace* swap_space, const LengthPrefixedArray<T>* array) {
47 SwapAllocator<uint8_t> allocator(swap_space);
48 size_t size = LengthPrefixedArray<T>::ComputeSize(array->size());
49 array->~LengthPrefixedArray<T>();
50 allocator.deallocate(const_cast<uint8_t*>(reinterpret_cast<const uint8_t*>(array)), size);
51}
52
53} // anonymous namespace
54
55template <typename T, typename DedupeSetType>
56inline const LengthPrefixedArray<T>* CompiledMethodStorage::AllocateOrDeduplicateArray(
57 const ArrayRef<const T>& data,
58 DedupeSetType* dedupe_set) {
59 if (data.empty()) {
60 return nullptr;
61 } else if (!DedupeEnabled()) {
62 return CopyArray(swap_space_.get(), data);
63 } else {
64 return dedupe_set->Add(Thread::Current(), data);
65 }
66}
67
68template <typename T>
69inline void CompiledMethodStorage::ReleaseArrayIfNotDeduplicated(
70 const LengthPrefixedArray<T>* array) {
71 if (array != nullptr && !DedupeEnabled()) {
72 ReleaseArray(swap_space_.get(), array);
73 }
74}
75
76template <typename ContentType>
77class CompiledMethodStorage::DedupeHashFunc {
78 private:
79 static constexpr bool kUseMurmur3Hash = true;
80
81 public:
82 size_t operator()(const ArrayRef<ContentType>& array) const {
83 const uint8_t* data = reinterpret_cast<const uint8_t*>(array.data());
84 // TODO: More reasonable assertion.
85 // static_assert(IsPowerOfTwo(sizeof(ContentType)),
86 // "ContentType is not power of two, don't know whether array layout is as assumed");
87 uint32_t len = sizeof(ContentType) * array.size();
88 if (kUseMurmur3Hash) {
89 static constexpr uint32_t c1 = 0xcc9e2d51;
90 static constexpr uint32_t c2 = 0x1b873593;
91 static constexpr uint32_t r1 = 15;
92 static constexpr uint32_t r2 = 13;
93 static constexpr uint32_t m = 5;
94 static constexpr uint32_t n = 0xe6546b64;
95
96 uint32_t hash = 0;
97
98 const int nblocks = len / 4;
99 typedef __attribute__((__aligned__(1))) uint32_t unaligned_uint32_t;
100 const unaligned_uint32_t *blocks = reinterpret_cast<const uint32_t*>(data);
101 int i;
102 for (i = 0; i < nblocks; i++) {
103 uint32_t k = blocks[i];
104 k *= c1;
105 k = (k << r1) | (k >> (32 - r1));
106 k *= c2;
107
108 hash ^= k;
109 hash = ((hash << r2) | (hash >> (32 - r2))) * m + n;
110 }
111
112 const uint8_t *tail = reinterpret_cast<const uint8_t*>(data + nblocks * 4);
113 uint32_t k1 = 0;
114
115 switch (len & 3) {
116 case 3:
117 k1 ^= tail[2] << 16;
118 FALLTHROUGH_INTENDED;
119 case 2:
120 k1 ^= tail[1] << 8;
121 FALLTHROUGH_INTENDED;
122 case 1:
123 k1 ^= tail[0];
124
125 k1 *= c1;
126 k1 = (k1 << r1) | (k1 >> (32 - r1));
127 k1 *= c2;
128 hash ^= k1;
129 }
130
131 hash ^= len;
132 hash ^= (hash >> 16);
133 hash *= 0x85ebca6b;
134 hash ^= (hash >> 13);
135 hash *= 0xc2b2ae35;
136 hash ^= (hash >> 16);
137
138 return hash;
139 } else {
Mathieu Chartier66c9df12018-01-16 14:45:20 -0800140 return HashBytes(data, len);
Vladimir Marko35831e82015-09-11 11:59:18 +0100141 }
142 }
143};
144
145template <typename T>
146class CompiledMethodStorage::LengthPrefixedArrayAlloc {
147 public:
148 explicit LengthPrefixedArrayAlloc(SwapSpace* swap_space)
149 : swap_space_(swap_space) {
150 }
151
152 const LengthPrefixedArray<T>* Copy(const ArrayRef<const T>& array) {
153 return CopyArray(swap_space_, array);
154 }
155
156 void Destroy(const LengthPrefixedArray<T>* array) {
157 ReleaseArray(swap_space_, array);
158 }
159
160 private:
161 SwapSpace* const swap_space_;
162};
163
Vladimir Markoca1e0382018-04-11 09:58:41 +0000164class CompiledMethodStorage::ThunkMapKey {
165 public:
166 ThunkMapKey(linker::LinkerPatch::Type type, uint32_t custom_value1, uint32_t custom_value2)
167 : type_(type), custom_value1_(custom_value1), custom_value2_(custom_value2) {}
168
169 bool operator<(const ThunkMapKey& other) const {
170 if (custom_value1_ != other.custom_value1_) {
171 return custom_value1_ < other.custom_value1_;
172 }
173 if (custom_value2_ != other.custom_value2_) {
174 return custom_value2_ < other.custom_value2_;
175 }
176 return type_ < other.type_;
177 }
178
179 private:
180 linker::LinkerPatch::Type type_;
181 uint32_t custom_value1_;
182 uint32_t custom_value2_;
183};
184
185class CompiledMethodStorage::ThunkMapValue {
186 public:
187 ThunkMapValue(std::vector<uint8_t, SwapAllocator<uint8_t>>&& code,
188 const std::string& debug_name)
189 : code_(std::move(code)), debug_name_(debug_name) {}
190
191 ArrayRef<const uint8_t> GetCode() const {
192 return ArrayRef<const uint8_t>(code_);
193 }
194
195 const std::string& GetDebugName() const {
196 return debug_name_;
197 }
198
199 private:
200 std::vector<uint8_t, SwapAllocator<uint8_t>> code_;
201 std::string debug_name_;
202};
203
Vladimir Marko35831e82015-09-11 11:59:18 +0100204CompiledMethodStorage::CompiledMethodStorage(int swap_fd)
205 : swap_space_(swap_fd == -1 ? nullptr : new SwapSpace(swap_fd, 10 * MB)),
206 dedupe_enabled_(true),
207 dedupe_code_("dedupe code", LengthPrefixedArrayAlloc<uint8_t>(swap_space_.get())),
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700208 dedupe_method_info_("dedupe method info",
209 LengthPrefixedArrayAlloc<uint8_t>(swap_space_.get())),
Vladimir Marko35831e82015-09-11 11:59:18 +0100210 dedupe_vmap_table_("dedupe vmap table",
211 LengthPrefixedArrayAlloc<uint8_t>(swap_space_.get())),
Vladimir Marko35831e82015-09-11 11:59:18 +0100212 dedupe_cfi_info_("dedupe cfi info", LengthPrefixedArrayAlloc<uint8_t>(swap_space_.get())),
213 dedupe_linker_patches_("dedupe cfi info",
Vladimir Markoca1e0382018-04-11 09:58:41 +0000214 LengthPrefixedArrayAlloc<linker::LinkerPatch>(swap_space_.get())),
215 thunk_map_lock_("thunk_map_lock"),
216 thunk_map_(std::less<ThunkMapKey>(), SwapAllocator<ThunkMapValueType>(swap_space_.get())) {
Vladimir Marko35831e82015-09-11 11:59:18 +0100217}
218
219CompiledMethodStorage::~CompiledMethodStorage() {
220 // All done by member destructors.
221}
222
223void CompiledMethodStorage::DumpMemoryUsage(std::ostream& os, bool extended) const {
224 if (swap_space_.get() != nullptr) {
Anton Kirilovdd9473b2016-01-28 15:08:01 +0000225 const size_t swap_size = swap_space_->GetSize();
226 os << " swap=" << PrettySize(swap_size) << " (" << swap_size << "B)";
Vladimir Marko35831e82015-09-11 11:59:18 +0100227 }
228 if (extended) {
229 Thread* self = Thread::Current();
230 os << "\nCode dedupe: " << dedupe_code_.DumpStats(self);
Vladimir Marko35831e82015-09-11 11:59:18 +0100231 os << "\nVmap table dedupe: " << dedupe_vmap_table_.DumpStats(self);
Vladimir Marko35831e82015-09-11 11:59:18 +0100232 os << "\nCFI info dedupe: " << dedupe_cfi_info_.DumpStats(self);
233 }
234}
235
236const LengthPrefixedArray<uint8_t>* CompiledMethodStorage::DeduplicateCode(
237 const ArrayRef<const uint8_t>& code) {
238 return AllocateOrDeduplicateArray(code, &dedupe_code_);
239}
240
241void CompiledMethodStorage::ReleaseCode(const LengthPrefixedArray<uint8_t>* code) {
242 ReleaseArrayIfNotDeduplicated(code);
243}
244
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700245const LengthPrefixedArray<uint8_t>* CompiledMethodStorage::DeduplicateMethodInfo(
246 const ArrayRef<const uint8_t>& src_map) {
247 return AllocateOrDeduplicateArray(src_map, &dedupe_method_info_);
Vladimir Marko35831e82015-09-11 11:59:18 +0100248}
249
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700250void CompiledMethodStorage::ReleaseMethodInfo(const LengthPrefixedArray<uint8_t>* method_info) {
251 ReleaseArrayIfNotDeduplicated(method_info);
Vladimir Marko35831e82015-09-11 11:59:18 +0100252}
253
Vladimir Marko35831e82015-09-11 11:59:18 +0100254const LengthPrefixedArray<uint8_t>* CompiledMethodStorage::DeduplicateVMapTable(
255 const ArrayRef<const uint8_t>& table) {
256 return AllocateOrDeduplicateArray(table, &dedupe_vmap_table_);
257}
258
259void CompiledMethodStorage::ReleaseVMapTable(const LengthPrefixedArray<uint8_t>* table) {
260 ReleaseArrayIfNotDeduplicated(table);
261}
262
Vladimir Marko35831e82015-09-11 11:59:18 +0100263const LengthPrefixedArray<uint8_t>* CompiledMethodStorage::DeduplicateCFIInfo(
264 const ArrayRef<const uint8_t>& cfi_info) {
265 return AllocateOrDeduplicateArray(cfi_info, &dedupe_cfi_info_);
266}
267
268void CompiledMethodStorage::ReleaseCFIInfo(const LengthPrefixedArray<uint8_t>* cfi_info) {
269 ReleaseArrayIfNotDeduplicated(cfi_info);
270}
271
Vladimir Markod8dbc8d2017-09-20 13:37:47 +0100272const LengthPrefixedArray<linker::LinkerPatch>* CompiledMethodStorage::DeduplicateLinkerPatches(
273 const ArrayRef<const linker::LinkerPatch>& linker_patches) {
Vladimir Marko35831e82015-09-11 11:59:18 +0100274 return AllocateOrDeduplicateArray(linker_patches, &dedupe_linker_patches_);
275}
276
277void CompiledMethodStorage::ReleaseLinkerPatches(
Vladimir Markod8dbc8d2017-09-20 13:37:47 +0100278 const LengthPrefixedArray<linker::LinkerPatch>* linker_patches) {
Vladimir Marko35831e82015-09-11 11:59:18 +0100279 ReleaseArrayIfNotDeduplicated(linker_patches);
280}
281
Vladimir Markoca1e0382018-04-11 09:58:41 +0000282CompiledMethodStorage::ThunkMapKey CompiledMethodStorage::GetThunkMapKey(
283 const linker::LinkerPatch& linker_patch) {
284 uint32_t custom_value1 = 0u;
285 uint32_t custom_value2 = 0u;
286 switch (linker_patch.GetType()) {
287 case linker::LinkerPatch::Type::kBakerReadBarrierBranch:
288 custom_value1 = linker_patch.GetBakerCustomValue1();
289 custom_value2 = linker_patch.GetBakerCustomValue2();
290 break;
291 case linker::LinkerPatch::Type::kCallRelative:
292 // No custom values.
293 break;
294 default:
295 LOG(FATAL) << "Unexpected patch type: " << linker_patch.GetType();
296 UNREACHABLE();
297 }
298 return ThunkMapKey(linker_patch.GetType(), custom_value1, custom_value2);
299}
300
301ArrayRef<const uint8_t> CompiledMethodStorage::GetThunkCode(const linker::LinkerPatch& linker_patch,
302 /*out*/ std::string* debug_name) {
303 ThunkMapKey key = GetThunkMapKey(linker_patch);
304 MutexLock lock(Thread::Current(), thunk_map_lock_);
305 auto it = thunk_map_.find(key);
306 if (it != thunk_map_.end()) {
307 const ThunkMapValue& value = it->second;
308 if (debug_name != nullptr) {
309 *debug_name = value.GetDebugName();
310 }
311 return value.GetCode();
312 } else {
313 if (debug_name != nullptr) {
314 *debug_name = std::string();
315 }
316 return ArrayRef<const uint8_t>();
317 }
318}
319
320void CompiledMethodStorage::SetThunkCode(const linker::LinkerPatch& linker_patch,
321 ArrayRef<const uint8_t> code,
322 const std::string& debug_name) {
323 DCHECK(!code.empty());
324 ThunkMapKey key = GetThunkMapKey(linker_patch);
325 std::vector<uint8_t, SwapAllocator<uint8_t>> code_copy(
326 code.begin(), code.end(), SwapAllocator<uint8_t>(swap_space_.get()));
327 ThunkMapValue value(std::move(code_copy), debug_name);
328 MutexLock lock(Thread::Current(), thunk_map_lock_);
329 // Note: Multiple threads can try and compile the same thunk, so this may not create a new entry.
330 thunk_map_.emplace(key, std::move(value));
331}
332
Vladimir Marko35831e82015-09-11 11:59:18 +0100333} // namespace art