blob: c739333cee3e83548edd5f4af8388d701e35ccd3 [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
22#include "base/logging.h"
23#include "compiled_method.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010024#include "linker/linker_patch.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070025#include "thread-current-inl.h"
Vladimir Marko35831e82015-09-11 11:59:18 +010026#include "utils.h"
27#include "utils/dedupe_set-inl.h"
28#include "utils/swap_space.h"
29
30namespace art {
31
32namespace { // anonymous namespace
33
34template <typename T>
35const 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
44template <typename T>
45void 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
54template <typename T, typename DedupeSetType>
55inline 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
67template <typename T>
68inline void CompiledMethodStorage::ReleaseArrayIfNotDeduplicated(
69 const LengthPrefixedArray<T>* array) {
70 if (array != nullptr && !DedupeEnabled()) {
71 ReleaseArray(swap_space_.get(), array);
72 }
73}
74
75template <typename ContentType>
76class 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
153template <typename T>
154class 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
172CompiledMethodStorage::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 Chartiercbcedbf2017-03-12 22:24:50 -0700176 dedupe_method_info_("dedupe method info",
177 LengthPrefixedArrayAlloc<uint8_t>(swap_space_.get())),
Vladimir Marko35831e82015-09-11 11:59:18 +0100178 dedupe_vmap_table_("dedupe vmap table",
179 LengthPrefixedArrayAlloc<uint8_t>(swap_space_.get())),
Vladimir Marko35831e82015-09-11 11:59:18 +0100180 dedupe_cfi_info_("dedupe cfi info", LengthPrefixedArrayAlloc<uint8_t>(swap_space_.get())),
181 dedupe_linker_patches_("dedupe cfi info",
Vladimir Markod8dbc8d2017-09-20 13:37:47 +0100182 LengthPrefixedArrayAlloc<linker::LinkerPatch>(swap_space_.get())) {
Vladimir Marko35831e82015-09-11 11:59:18 +0100183}
184
185CompiledMethodStorage::~CompiledMethodStorage() {
186 // All done by member destructors.
187}
188
189void CompiledMethodStorage::DumpMemoryUsage(std::ostream& os, bool extended) const {
190 if (swap_space_.get() != nullptr) {
Anton Kirilovdd9473b2016-01-28 15:08:01 +0000191 const size_t swap_size = swap_space_->GetSize();
192 os << " swap=" << PrettySize(swap_size) << " (" << swap_size << "B)";
Vladimir Marko35831e82015-09-11 11:59:18 +0100193 }
194 if (extended) {
195 Thread* self = Thread::Current();
196 os << "\nCode dedupe: " << dedupe_code_.DumpStats(self);
Vladimir Marko35831e82015-09-11 11:59:18 +0100197 os << "\nVmap table dedupe: " << dedupe_vmap_table_.DumpStats(self);
Vladimir Marko35831e82015-09-11 11:59:18 +0100198 os << "\nCFI info dedupe: " << dedupe_cfi_info_.DumpStats(self);
199 }
200}
201
202const LengthPrefixedArray<uint8_t>* CompiledMethodStorage::DeduplicateCode(
203 const ArrayRef<const uint8_t>& code) {
204 return AllocateOrDeduplicateArray(code, &dedupe_code_);
205}
206
207void CompiledMethodStorage::ReleaseCode(const LengthPrefixedArray<uint8_t>* code) {
208 ReleaseArrayIfNotDeduplicated(code);
209}
210
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700211const LengthPrefixedArray<uint8_t>* CompiledMethodStorage::DeduplicateMethodInfo(
212 const ArrayRef<const uint8_t>& src_map) {
213 return AllocateOrDeduplicateArray(src_map, &dedupe_method_info_);
Vladimir Marko35831e82015-09-11 11:59:18 +0100214}
215
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700216void CompiledMethodStorage::ReleaseMethodInfo(const LengthPrefixedArray<uint8_t>* method_info) {
217 ReleaseArrayIfNotDeduplicated(method_info);
Vladimir Marko35831e82015-09-11 11:59:18 +0100218}
219
Vladimir Marko35831e82015-09-11 11:59:18 +0100220const LengthPrefixedArray<uint8_t>* CompiledMethodStorage::DeduplicateVMapTable(
221 const ArrayRef<const uint8_t>& table) {
222 return AllocateOrDeduplicateArray(table, &dedupe_vmap_table_);
223}
224
225void CompiledMethodStorage::ReleaseVMapTable(const LengthPrefixedArray<uint8_t>* table) {
226 ReleaseArrayIfNotDeduplicated(table);
227}
228
Vladimir Marko35831e82015-09-11 11:59:18 +0100229const LengthPrefixedArray<uint8_t>* CompiledMethodStorage::DeduplicateCFIInfo(
230 const ArrayRef<const uint8_t>& cfi_info) {
231 return AllocateOrDeduplicateArray(cfi_info, &dedupe_cfi_info_);
232}
233
234void CompiledMethodStorage::ReleaseCFIInfo(const LengthPrefixedArray<uint8_t>* cfi_info) {
235 ReleaseArrayIfNotDeduplicated(cfi_info);
236}
237
Vladimir Markod8dbc8d2017-09-20 13:37:47 +0100238const LengthPrefixedArray<linker::LinkerPatch>* CompiledMethodStorage::DeduplicateLinkerPatches(
239 const ArrayRef<const linker::LinkerPatch>& linker_patches) {
Vladimir Marko35831e82015-09-11 11:59:18 +0100240 return AllocateOrDeduplicateArray(linker_patches, &dedupe_linker_patches_);
241}
242
243void CompiledMethodStorage::ReleaseLinkerPatches(
Vladimir Markod8dbc8d2017-09-20 13:37:47 +0100244 const LengthPrefixedArray<linker::LinkerPatch>* linker_patches) {
Vladimir Marko35831e82015-09-11 11:59:18 +0100245 ReleaseArrayIfNotDeduplicated(linker_patches);
246}
247
248} // namespace art