blob: f9f34017eba9b6778d6db6ba3676851306bd5d6d [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#ifndef ART_COMPILER_DRIVER_COMPILED_METHOD_STORAGE_H_
18#define ART_COMPILER_DRIVER_COMPILED_METHOD_STORAGE_H_
19
20#include <iosfwd>
Vladimir Markoca1e0382018-04-11 09:58:41 +000021#include <map>
Vladimir Marko35831e82015-09-11 11:59:18 +010022#include <memory>
23
David Brazdild9c90372016-09-14 16:53:55 +010024#include "base/array_ref.h"
Alex Lighte64300b2015-12-15 15:02:47 -080025#include "base/length_prefixed_array.h"
Vladimir Marko35831e82015-09-11 11:59:18 +010026#include "base/macros.h"
Vladimir Marko35831e82015-09-11 11:59:18 +010027#include "utils/dedupe_set.h"
28#include "utils/swap_space.h"
29
30namespace art {
31
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010032namespace linker {
Vladimir Marko35831e82015-09-11 11:59:18 +010033class LinkerPatch;
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010034} // namespace linker
Vladimir Marko35831e82015-09-11 11:59:18 +010035
36class CompiledMethodStorage {
37 public:
38 explicit CompiledMethodStorage(int swap_fd);
39 ~CompiledMethodStorage();
40
41 void DumpMemoryUsage(std::ostream& os, bool extended) const;
42
43 void SetDedupeEnabled(bool dedupe_enabled) {
44 dedupe_enabled_ = dedupe_enabled;
45 }
46 bool DedupeEnabled() const {
47 return dedupe_enabled_;
48 }
49
50 SwapAllocator<void> GetSwapSpaceAllocator() {
51 return SwapAllocator<void>(swap_space_.get());
52 }
53
54 const LengthPrefixedArray<uint8_t>* DeduplicateCode(const ArrayRef<const uint8_t>& code);
55 void ReleaseCode(const LengthPrefixedArray<uint8_t>* code);
Vladimir Markof67e8c32022-03-17 12:55:27 +000056 size_t UniqueCodeEntries() const;
Vladimir Marko35831e82015-09-11 11:59:18 +010057
Vladimir Marko35831e82015-09-11 11:59:18 +010058 const LengthPrefixedArray<uint8_t>* DeduplicateVMapTable(const ArrayRef<const uint8_t>& table);
59 void ReleaseVMapTable(const LengthPrefixedArray<uint8_t>* table);
Vladimir Markof67e8c32022-03-17 12:55:27 +000060 size_t UniqueVMapTableEntries() const;
Vladimir Marko35831e82015-09-11 11:59:18 +010061
Vladimir Marko35831e82015-09-11 11:59:18 +010062 const LengthPrefixedArray<uint8_t>* DeduplicateCFIInfo(const ArrayRef<const uint8_t>& cfi_info);
63 void ReleaseCFIInfo(const LengthPrefixedArray<uint8_t>* cfi_info);
Vladimir Markof67e8c32022-03-17 12:55:27 +000064 size_t UniqueCFIInfoEntries() const;
Vladimir Marko35831e82015-09-11 11:59:18 +010065
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010066 const LengthPrefixedArray<linker::LinkerPatch>* DeduplicateLinkerPatches(
67 const ArrayRef<const linker::LinkerPatch>& linker_patches);
68 void ReleaseLinkerPatches(const LengthPrefixedArray<linker::LinkerPatch>* linker_patches);
Vladimir Markof67e8c32022-03-17 12:55:27 +000069 size_t UniqueLinkerPatchesEntries() const;
Vladimir Marko35831e82015-09-11 11:59:18 +010070
Vladimir Markoca1e0382018-04-11 09:58:41 +000071 // Returns the code associated with the given patch.
72 // If the code has not been set, returns empty data.
73 // If `debug_name` is not null, stores the associated debug name in `*debug_name`.
74 ArrayRef<const uint8_t> GetThunkCode(const linker::LinkerPatch& linker_patch,
75 /*out*/ std::string* debug_name = nullptr);
76
77 // Sets the code and debug name associated with the given patch.
78 void SetThunkCode(const linker::LinkerPatch& linker_patch,
79 ArrayRef<const uint8_t> code,
80 const std::string& debug_name);
81
Vladimir Marko35831e82015-09-11 11:59:18 +010082 private:
Vladimir Markoca1e0382018-04-11 09:58:41 +000083 class ThunkMapKey;
84 class ThunkMapValue;
85 using ThunkMapValueType = std::pair<const ThunkMapKey, ThunkMapValue>;
86 using ThunkMap = std::map<ThunkMapKey,
87 ThunkMapValue,
88 std::less<ThunkMapKey>,
89 SwapAllocator<ThunkMapValueType>>;
90 static_assert(std::is_same<ThunkMapValueType, ThunkMap::value_type>::value, "Value type check.");
91
92 static ThunkMapKey GetThunkMapKey(const linker::LinkerPatch& linker_patch);
93
Vladimir Marko35831e82015-09-11 11:59:18 +010094 template <typename T, typename DedupeSetType>
95 const LengthPrefixedArray<T>* AllocateOrDeduplicateArray(const ArrayRef<const T>& data,
96 DedupeSetType* dedupe_set);
97
98 template <typename T>
99 void ReleaseArrayIfNotDeduplicated(const LengthPrefixedArray<T>* array);
100
101 // DeDuplication data structures.
102 template <typename ContentType>
103 class DedupeHashFunc;
104
105 template <typename T>
106 class LengthPrefixedArrayAlloc;
107
108 template <typename T>
109 using ArrayDedupeSet = DedupeSet<ArrayRef<const T>,
110 LengthPrefixedArray<T>,
111 LengthPrefixedArrayAlloc<T>,
112 size_t,
113 DedupeHashFunc<const T>,
114 4>;
115
116 // Swap pool and allocator used for native allocations. May be file-backed. Needs to be first
117 // as other fields rely on this.
118 std::unique_ptr<SwapSpace> swap_space_;
119
120 bool dedupe_enabled_;
121
122 ArrayDedupeSet<uint8_t> dedupe_code_;
Vladimir Marko35831e82015-09-11 11:59:18 +0100123 ArrayDedupeSet<uint8_t> dedupe_vmap_table_;
Vladimir Marko35831e82015-09-11 11:59:18 +0100124 ArrayDedupeSet<uint8_t> dedupe_cfi_info_;
Vladimir Markod8dbc8d2017-09-20 13:37:47 +0100125 ArrayDedupeSet<linker::LinkerPatch> dedupe_linker_patches_;
Vladimir Marko35831e82015-09-11 11:59:18 +0100126
Vladimir Markoca1e0382018-04-11 09:58:41 +0000127 Mutex thunk_map_lock_;
128 ThunkMap thunk_map_ GUARDED_BY(thunk_map_lock_);
129
Vladimir Marko35831e82015-09-11 11:59:18 +0100130 DISALLOW_COPY_AND_ASSIGN(CompiledMethodStorage);
131};
132
133} // namespace art
134
135#endif // ART_COMPILER_DRIVER_COMPILED_METHOD_STORAGE_H_