blob: c8c2b6998f569c769aac51ebac8a4a0a26ca0e73 [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
Vladimir Marko35831e82015-09-11 11:59:18 +010024#include "compiled_method.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010025#include "linker/linker_patch.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070026#include "thread-current-inl.h"
Vladimir Marko35831e82015-09-11 11:59:18 +010027#include "utils.h"
28#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 {
140 size_t hash = 0x811c9dc5;
141 for (uint32_t i = 0; i < len; ++i) {
142 hash = (hash * 16777619) ^ data[i];
143 }
144 hash += hash << 13;
145 hash ^= hash >> 7;
146 hash += hash << 3;
147 hash ^= hash >> 17;
148 hash += hash << 5;
149 return hash;
150 }
151 }
152};
153
154template <typename T>
155class CompiledMethodStorage::LengthPrefixedArrayAlloc {
156 public:
157 explicit LengthPrefixedArrayAlloc(SwapSpace* swap_space)
158 : swap_space_(swap_space) {
159 }
160
161 const LengthPrefixedArray<T>* Copy(const ArrayRef<const T>& array) {
162 return CopyArray(swap_space_, array);
163 }
164
165 void Destroy(const LengthPrefixedArray<T>* array) {
166 ReleaseArray(swap_space_, array);
167 }
168
169 private:
170 SwapSpace* const swap_space_;
171};
172
173CompiledMethodStorage::CompiledMethodStorage(int swap_fd)
174 : swap_space_(swap_fd == -1 ? nullptr : new SwapSpace(swap_fd, 10 * MB)),
175 dedupe_enabled_(true),
176 dedupe_code_("dedupe code", LengthPrefixedArrayAlloc<uint8_t>(swap_space_.get())),
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700177 dedupe_method_info_("dedupe method info",
178 LengthPrefixedArrayAlloc<uint8_t>(swap_space_.get())),
Vladimir Marko35831e82015-09-11 11:59:18 +0100179 dedupe_vmap_table_("dedupe vmap table",
180 LengthPrefixedArrayAlloc<uint8_t>(swap_space_.get())),
Vladimir Marko35831e82015-09-11 11:59:18 +0100181 dedupe_cfi_info_("dedupe cfi info", LengthPrefixedArrayAlloc<uint8_t>(swap_space_.get())),
182 dedupe_linker_patches_("dedupe cfi info",
Vladimir Markod8dbc8d2017-09-20 13:37:47 +0100183 LengthPrefixedArrayAlloc<linker::LinkerPatch>(swap_space_.get())) {
Vladimir Marko35831e82015-09-11 11:59:18 +0100184}
185
186CompiledMethodStorage::~CompiledMethodStorage() {
187 // All done by member destructors.
188}
189
190void CompiledMethodStorage::DumpMemoryUsage(std::ostream& os, bool extended) const {
191 if (swap_space_.get() != nullptr) {
Anton Kirilovdd9473b2016-01-28 15:08:01 +0000192 const size_t swap_size = swap_space_->GetSize();
193 os << " swap=" << PrettySize(swap_size) << " (" << swap_size << "B)";
Vladimir Marko35831e82015-09-11 11:59:18 +0100194 }
195 if (extended) {
196 Thread* self = Thread::Current();
197 os << "\nCode dedupe: " << dedupe_code_.DumpStats(self);
Vladimir Marko35831e82015-09-11 11:59:18 +0100198 os << "\nVmap table dedupe: " << dedupe_vmap_table_.DumpStats(self);
Vladimir Marko35831e82015-09-11 11:59:18 +0100199 os << "\nCFI info dedupe: " << dedupe_cfi_info_.DumpStats(self);
200 }
201}
202
203const LengthPrefixedArray<uint8_t>* CompiledMethodStorage::DeduplicateCode(
204 const ArrayRef<const uint8_t>& code) {
205 return AllocateOrDeduplicateArray(code, &dedupe_code_);
206}
207
208void CompiledMethodStorage::ReleaseCode(const LengthPrefixedArray<uint8_t>* code) {
209 ReleaseArrayIfNotDeduplicated(code);
210}
211
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700212const LengthPrefixedArray<uint8_t>* CompiledMethodStorage::DeduplicateMethodInfo(
213 const ArrayRef<const uint8_t>& src_map) {
214 return AllocateOrDeduplicateArray(src_map, &dedupe_method_info_);
Vladimir Marko35831e82015-09-11 11:59:18 +0100215}
216
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700217void CompiledMethodStorage::ReleaseMethodInfo(const LengthPrefixedArray<uint8_t>* method_info) {
218 ReleaseArrayIfNotDeduplicated(method_info);
Vladimir Marko35831e82015-09-11 11:59:18 +0100219}
220
Vladimir Marko35831e82015-09-11 11:59:18 +0100221const LengthPrefixedArray<uint8_t>* CompiledMethodStorage::DeduplicateVMapTable(
222 const ArrayRef<const uint8_t>& table) {
223 return AllocateOrDeduplicateArray(table, &dedupe_vmap_table_);
224}
225
226void CompiledMethodStorage::ReleaseVMapTable(const LengthPrefixedArray<uint8_t>* table) {
227 ReleaseArrayIfNotDeduplicated(table);
228}
229
Vladimir Marko35831e82015-09-11 11:59:18 +0100230const LengthPrefixedArray<uint8_t>* CompiledMethodStorage::DeduplicateCFIInfo(
231 const ArrayRef<const uint8_t>& cfi_info) {
232 return AllocateOrDeduplicateArray(cfi_info, &dedupe_cfi_info_);
233}
234
235void CompiledMethodStorage::ReleaseCFIInfo(const LengthPrefixedArray<uint8_t>* cfi_info) {
236 ReleaseArrayIfNotDeduplicated(cfi_info);
237}
238
Vladimir Markod8dbc8d2017-09-20 13:37:47 +0100239const LengthPrefixedArray<linker::LinkerPatch>* CompiledMethodStorage::DeduplicateLinkerPatches(
240 const ArrayRef<const linker::LinkerPatch>& linker_patches) {
Vladimir Marko35831e82015-09-11 11:59:18 +0100241 return AllocateOrDeduplicateArray(linker_patches, &dedupe_linker_patches_);
242}
243
244void CompiledMethodStorage::ReleaseLinkerPatches(
Vladimir Markod8dbc8d2017-09-20 13:37:47 +0100245 const LengthPrefixedArray<linker::LinkerPatch>* linker_patches) {
Vladimir Marko35831e82015-09-11 11:59:18 +0100246 ReleaseArrayIfNotDeduplicated(linker_patches);
247}
248
249} // namespace art