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> 21 #include <map> 22 #include <memory> 23 24 #include "base/array_ref.h" 25 #include "base/length_prefixed_array.h" 26 #include "base/macros.h" 27 #include "utils/dedupe_set.h" 28 #include "utils/swap_space.h" 29 30 namespace art { 31 32 namespace linker { 33 class LinkerPatch; 34 } // namespace linker 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 SetDedupeEnabled(bool dedupe_enabled)43 void SetDedupeEnabled(bool dedupe_enabled) { 44 dedupe_enabled_ = dedupe_enabled; 45 } DedupeEnabled()46 bool DedupeEnabled() const { 47 return dedupe_enabled_; 48 } 49 GetSwapSpaceAllocator()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); 56 size_t UniqueCodeEntries() const; 57 58 const LengthPrefixedArray<uint8_t>* DeduplicateVMapTable(const ArrayRef<const uint8_t>& table); 59 void ReleaseVMapTable(const LengthPrefixedArray<uint8_t>* table); 60 size_t UniqueVMapTableEntries() const; 61 62 const LengthPrefixedArray<uint8_t>* DeduplicateCFIInfo(const ArrayRef<const uint8_t>& cfi_info); 63 void ReleaseCFIInfo(const LengthPrefixedArray<uint8_t>* cfi_info); 64 size_t UniqueCFIInfoEntries() const; 65 66 const LengthPrefixedArray<linker::LinkerPatch>* DeduplicateLinkerPatches( 67 const ArrayRef<const linker::LinkerPatch>& linker_patches); 68 void ReleaseLinkerPatches(const LengthPrefixedArray<linker::LinkerPatch>* linker_patches); 69 size_t UniqueLinkerPatchesEntries() const; 70 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 82 private: 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 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_; 123 ArrayDedupeSet<uint8_t> dedupe_vmap_table_; 124 ArrayDedupeSet<uint8_t> dedupe_cfi_info_; 125 ArrayDedupeSet<linker::LinkerPatch> dedupe_linker_patches_; 126 127 Mutex thunk_map_lock_; 128 ThunkMap thunk_map_ GUARDED_BY(thunk_map_lock_); 129 130 DISALLOW_COPY_AND_ASSIGN(CompiledMethodStorage); 131 }; 132 133 } // namespace art 134 135 #endif // ART_COMPILER_DRIVER_COMPILED_METHOD_STORAGE_H_ 136