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 | #ifndef ART_COMPILER_DRIVER_COMPILED_METHOD_STORAGE_H_ |
| 18 | #define ART_COMPILER_DRIVER_COMPILED_METHOD_STORAGE_H_ |
| 19 | |
| 20 | #include <iosfwd> |
Vladimir Marko | ca1e038 | 2018-04-11 09:58:41 +0000 | [diff] [blame] | 21 | #include <map> |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 22 | #include <memory> |
| 23 | |
David Brazdil | d9c9037 | 2016-09-14 16:53:55 +0100 | [diff] [blame] | 24 | #include "base/array_ref.h" |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 25 | #include "base/length_prefixed_array.h" |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 26 | #include "base/macros.h" |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 27 | #include "utils/dedupe_set.h" |
| 28 | #include "utils/swap_space.h" |
| 29 | |
| 30 | namespace art { |
| 31 | |
Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 32 | namespace linker { |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 33 | class LinkerPatch; |
Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 34 | } // namespace linker |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 35 | |
| 36 | class 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 Marko | f67e8c3 | 2022-03-17 12:55:27 +0000 | [diff] [blame] | 56 | size_t UniqueCodeEntries() const; |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 57 | |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 58 | const LengthPrefixedArray<uint8_t>* DeduplicateVMapTable(const ArrayRef<const uint8_t>& table); |
| 59 | void ReleaseVMapTable(const LengthPrefixedArray<uint8_t>* table); |
Vladimir Marko | f67e8c3 | 2022-03-17 12:55:27 +0000 | [diff] [blame] | 60 | size_t UniqueVMapTableEntries() const; |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 61 | |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 62 | const LengthPrefixedArray<uint8_t>* DeduplicateCFIInfo(const ArrayRef<const uint8_t>& cfi_info); |
| 63 | void ReleaseCFIInfo(const LengthPrefixedArray<uint8_t>* cfi_info); |
Vladimir Marko | f67e8c3 | 2022-03-17 12:55:27 +0000 | [diff] [blame] | 64 | size_t UniqueCFIInfoEntries() const; |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 65 | |
Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 66 | const LengthPrefixedArray<linker::LinkerPatch>* DeduplicateLinkerPatches( |
| 67 | const ArrayRef<const linker::LinkerPatch>& linker_patches); |
| 68 | void ReleaseLinkerPatches(const LengthPrefixedArray<linker::LinkerPatch>* linker_patches); |
Vladimir Marko | f67e8c3 | 2022-03-17 12:55:27 +0000 | [diff] [blame] | 69 | size_t UniqueLinkerPatchesEntries() const; |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 70 | |
Vladimir Marko | ca1e038 | 2018-04-11 09:58:41 +0000 | [diff] [blame] | 71 | // 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 Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 82 | private: |
Vladimir Marko | ca1e038 | 2018-04-11 09:58:41 +0000 | [diff] [blame] | 83 | 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 Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 94 | 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 Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 123 | ArrayDedupeSet<uint8_t> dedupe_vmap_table_; |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 124 | ArrayDedupeSet<uint8_t> dedupe_cfi_info_; |
Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 125 | ArrayDedupeSet<linker::LinkerPatch> dedupe_linker_patches_; |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 126 | |
Vladimir Marko | ca1e038 | 2018-04-11 09:58:41 +0000 | [diff] [blame] | 127 | Mutex thunk_map_lock_; |
| 128 | ThunkMap thunk_map_ GUARDED_BY(thunk_map_lock_); |
| 129 | |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 130 | DISALLOW_COPY_AND_ASSIGN(CompiledMethodStorage); |
| 131 | }; |
| 132 | |
| 133 | } // namespace art |
| 134 | |
| 135 | #endif // ART_COMPILER_DRIVER_COMPILED_METHOD_STORAGE_H_ |