blob: a5a7691e12287f2394042b8cec4f6759fc9949d1 [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);
56
Vladimir Marko35831e82015-09-11 11:59:18 +010057 const LengthPrefixedArray<uint8_t>* DeduplicateVMapTable(const ArrayRef<const uint8_t>& table);
58 void ReleaseVMapTable(const LengthPrefixedArray<uint8_t>* table);
59
Vladimir Marko35831e82015-09-11 11:59:18 +010060 const LengthPrefixedArray<uint8_t>* DeduplicateCFIInfo(const ArrayRef<const uint8_t>& cfi_info);
61 void ReleaseCFIInfo(const LengthPrefixedArray<uint8_t>* cfi_info);
62
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010063 const LengthPrefixedArray<linker::LinkerPatch>* DeduplicateLinkerPatches(
64 const ArrayRef<const linker::LinkerPatch>& linker_patches);
65 void ReleaseLinkerPatches(const LengthPrefixedArray<linker::LinkerPatch>* linker_patches);
Vladimir Marko35831e82015-09-11 11:59:18 +010066
Vladimir Markoca1e0382018-04-11 09:58:41 +000067 // Returns the code associated with the given patch.
68 // If the code has not been set, returns empty data.
69 // If `debug_name` is not null, stores the associated debug name in `*debug_name`.
70 ArrayRef<const uint8_t> GetThunkCode(const linker::LinkerPatch& linker_patch,
71 /*out*/ std::string* debug_name = nullptr);
72
73 // Sets the code and debug name associated with the given patch.
74 void SetThunkCode(const linker::LinkerPatch& linker_patch,
75 ArrayRef<const uint8_t> code,
76 const std::string& debug_name);
77
Vladimir Marko35831e82015-09-11 11:59:18 +010078 private:
Vladimir Markoca1e0382018-04-11 09:58:41 +000079 class ThunkMapKey;
80 class ThunkMapValue;
81 using ThunkMapValueType = std::pair<const ThunkMapKey, ThunkMapValue>;
82 using ThunkMap = std::map<ThunkMapKey,
83 ThunkMapValue,
84 std::less<ThunkMapKey>,
85 SwapAllocator<ThunkMapValueType>>;
86 static_assert(std::is_same<ThunkMapValueType, ThunkMap::value_type>::value, "Value type check.");
87
88 static ThunkMapKey GetThunkMapKey(const linker::LinkerPatch& linker_patch);
89
Vladimir Marko35831e82015-09-11 11:59:18 +010090 template <typename T, typename DedupeSetType>
91 const LengthPrefixedArray<T>* AllocateOrDeduplicateArray(const ArrayRef<const T>& data,
92 DedupeSetType* dedupe_set);
93
94 template <typename T>
95 void ReleaseArrayIfNotDeduplicated(const LengthPrefixedArray<T>* array);
96
97 // DeDuplication data structures.
98 template <typename ContentType>
99 class DedupeHashFunc;
100
101 template <typename T>
102 class LengthPrefixedArrayAlloc;
103
104 template <typename T>
105 using ArrayDedupeSet = DedupeSet<ArrayRef<const T>,
106 LengthPrefixedArray<T>,
107 LengthPrefixedArrayAlloc<T>,
108 size_t,
109 DedupeHashFunc<const T>,
110 4>;
111
112 // Swap pool and allocator used for native allocations. May be file-backed. Needs to be first
113 // as other fields rely on this.
114 std::unique_ptr<SwapSpace> swap_space_;
115
116 bool dedupe_enabled_;
117
118 ArrayDedupeSet<uint8_t> dedupe_code_;
Vladimir Marko35831e82015-09-11 11:59:18 +0100119 ArrayDedupeSet<uint8_t> dedupe_vmap_table_;
Vladimir Marko35831e82015-09-11 11:59:18 +0100120 ArrayDedupeSet<uint8_t> dedupe_cfi_info_;
Vladimir Markod8dbc8d2017-09-20 13:37:47 +0100121 ArrayDedupeSet<linker::LinkerPatch> dedupe_linker_patches_;
Vladimir Marko35831e82015-09-11 11:59:18 +0100122
Vladimir Markoca1e0382018-04-11 09:58:41 +0000123 Mutex thunk_map_lock_;
124 ThunkMap thunk_map_ GUARDED_BY(thunk_map_lock_);
125
Vladimir Marko35831e82015-09-11 11:59:18 +0100126 DISALLOW_COPY_AND_ASSIGN(CompiledMethodStorage);
127};
128
129} // namespace art
130
131#endif // ART_COMPILER_DRIVER_COMPILED_METHOD_STORAGE_H_