1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
|
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "boot_image_profile.h"
#include <memory>
#include <set>
#include "android-base/file.h"
#include "base/unix_file/fd_file.h"
#include "dex/class_accessor-inl.h"
#include "dex/descriptors_names.h"
#include "dex/dex_file-inl.h"
#include "dex/method_reference.h"
#include "dex/type_reference.h"
#include "profile/profile_compilation_info.h"
namespace art {
using Hotness = ProfileCompilationInfo::MethodHotness;
static const std::string kMethodSep = "->"; // NOLINT [runtime/string] [4]
static const std::string kPackageUseDelim = "@"; // NOLINT [runtime/string] [4]
static constexpr char kMethodFlagStringHot = 'H';
static constexpr char kMethodFlagStringStartup = 'S';
static constexpr char kMethodFlagStringPostStartup = 'P';
// Returns the type descriptor of the given reference.
static std::string GetTypeDescriptor(const TypeReference& ref) {
const dex::TypeId& type_id = ref.dex_file->GetTypeId(ref.TypeIndex());
return ref.dex_file->GetTypeDescriptor(type_id);
}
// Returns the method representation used in the text format of the boot image profile.
static std::string BootImageRepresentation(const MethodReference& ref) {
const DexFile* dex_file = ref.dex_file;
const dex::MethodId& id = ref.GetMethodId();
std::string signature_string(dex_file->GetMethodSignature(id).ToString());
std::string type_string(dex_file->GetTypeDescriptor(dex_file->GetTypeId(id.class_idx_)));
std::string method_name(dex_file->GetMethodName(id));
return type_string +
kMethodSep +
method_name +
signature_string;
}
// Returns the class representation used in the text format of the boot image profile.
static std::string BootImageRepresentation(const TypeReference& ref) {
return GetTypeDescriptor(ref);
}
// Returns the class representation used in preloaded classes.
static std::string PreloadedClassesRepresentation(const TypeReference& ref) {
std::string descriptor = GetTypeDescriptor(ref);
return DescriptorToDot(descriptor.c_str());
}
// Formats the list of packages from the item metadata as a debug string.
static std::string GetPackageUseString(const FlattenProfileData::ItemMetadata& metadata) {
std::string result;
for (const auto& it : metadata.GetAnnotations()) {
result += it.GetOriginPackageName() + ",";
}
return metadata.GetAnnotations().empty()
? result
: result.substr(0, result.size() - 1);
}
// Converts a method representation to its final profile format.
static std::string MethodToProfileFormat(
const std::string& method,
const FlattenProfileData::ItemMetadata& metadata,
bool output_package_use) {
std::string flags_string;
if (metadata.HasFlagSet(Hotness::kFlagHot)) {
flags_string += kMethodFlagStringHot;
}
if (metadata.HasFlagSet(Hotness::kFlagStartup)) {
flags_string += kMethodFlagStringStartup;
}
if (metadata.HasFlagSet(Hotness::kFlagPostStartup)) {
flags_string += kMethodFlagStringPostStartup;
}
std::string extra;
if (output_package_use) {
extra = kPackageUseDelim + GetPackageUseString(metadata);
}
return flags_string + method + extra;
}
// Converts a class representation to its final profile or preloaded classes format.
static std::string ClassToProfileFormat(
const std::string& classString,
const FlattenProfileData::ItemMetadata& metadata,
bool output_package_use) {
std::string extra;
if (output_package_use) {
extra = kPackageUseDelim + GetPackageUseString(metadata);
}
return classString + extra;
}
// Tries to asses if the given type reference is a clean class.
static bool MaybeIsClassClean(const TypeReference& ref) {
const dex::ClassDef* class_def = ref.dex_file->FindClassDef(ref.TypeIndex());
if (class_def == nullptr) {
return false;
}
ClassAccessor accessor(*ref.dex_file, *class_def);
for (auto& it : accessor.GetStaticFields()) {
if (!it.IsFinal()) {
// Not final static field will probably dirty the class.
return false;
}
}
for (auto& it : accessor.GetMethods()) {
uint32_t flags = it.GetAccessFlags();
if ((flags & kAccNative) != 0) {
// Native method will get dirtied.
return false;
}
if ((flags & kAccConstructor) != 0 && (flags & kAccStatic) != 0) {
// Class initializer, may get dirtied (not sure).
return false;
}
}
return true;
}
// Returns true iff the item should be included in the profile.
// (i.e. it passes the given aggregation thresholds)
static bool IncludeItemInProfile(uint32_t max_aggregation_count,
uint32_t item_threshold,
const FlattenProfileData::ItemMetadata& metadata,
const BootImageOptions& options) {
CHECK_NE(max_aggregation_count, 0u);
float item_percent = metadata.GetAnnotations().size() / static_cast<float>(max_aggregation_count);
for (const auto& annotIt : metadata.GetAnnotations()) {
const auto&thresholdIt =
options.special_packages_thresholds.find(annotIt.GetOriginPackageName());
if (thresholdIt != options.special_packages_thresholds.end()) {
if (item_percent >= (thresholdIt->second / 100.f)) {
return true;
}
}
}
return item_percent >= (item_threshold / 100.f);
}
// Returns true iff a method with the given metada should be included in the profile.
static bool IncludeMethodInProfile(uint32_t max_aggregation_count,
const FlattenProfileData::ItemMetadata& metadata,
const BootImageOptions& options) {
return IncludeItemInProfile(max_aggregation_count, options.method_threshold, metadata, options);
}
// Returns true iff a class with the given metada should be included in the profile.
static bool IncludeClassInProfile(const TypeReference& type_ref,
uint32_t max_aggregation_count,
const FlattenProfileData::ItemMetadata& metadata,
const BootImageOptions& options) {
uint32_t threshold = MaybeIsClassClean(type_ref)
? options.image_class_clean_threshold
: options.image_class_threshold;
return IncludeItemInProfile(max_aggregation_count, threshold, metadata, options);
}
// Returns true iff a class with the given metada should be included in the list of
// prelaoded classes.
static bool IncludeInPreloadedClasses(const std::string& class_name,
uint32_t max_aggregation_count,
const FlattenProfileData::ItemMetadata& metadata,
const BootImageOptions& options) {
bool denylisted = options.preloaded_classes_denylist.find(class_name) !=
options.preloaded_classes_denylist.end();
return !denylisted && IncludeItemInProfile(
max_aggregation_count, options.preloaded_class_threshold, metadata, options);
}
bool GenerateBootImageProfile(
const std::vector<std::unique_ptr<const DexFile>>& dex_files,
const std::vector<std::string>& profile_files,
const BootImageOptions& options,
const std::string& boot_profile_out_path,
const std::string& preloaded_classes_out_path) {
if (boot_profile_out_path.empty()) {
LOG(ERROR) << "No output file specified";
return false;
}
bool generate_preloaded_classes = !preloaded_classes_out_path.empty();
std::unique_ptr<FlattenProfileData> flattend_data(new FlattenProfileData());
for (const std::string& profile_file : profile_files) {
ProfileCompilationInfo profile(/*for_boot_image=*/ true);
if (!profile.Load(profile_file, /*clear_if_invalid=*/ false)) {
LOG(ERROR) << "Profile is not a valid: " << profile_file;
return false;
}
std::unique_ptr<FlattenProfileData> currentData = profile.ExtractProfileData(dex_files);
flattend_data->MergeData(*currentData);
}
// We want the output sorted by the method/class name.
// So we use an intermediate map for that.
// There's no attempt to optimize this as it's not part of any critical path,
// and mostly executed on hosts.
SafeMap<std::string, FlattenProfileData::ItemMetadata> profile_methods;
SafeMap<std::string, FlattenProfileData::ItemMetadata> profile_classes;
SafeMap<std::string, FlattenProfileData::ItemMetadata> preloaded_classes;
for (const auto& it : flattend_data->GetMethodData()) {
if (IncludeMethodInProfile(flattend_data->GetMaxAggregationForMethods(), it.second, options)) {
FlattenProfileData::ItemMetadata metadata(it.second);
if (options.upgrade_startup_to_hot
&& ((metadata.GetFlags() & Hotness::Flag::kFlagStartup) != 0)) {
metadata.AddFlag(Hotness::Flag::kFlagHot);
}
profile_methods.Put(BootImageRepresentation(it.first), metadata);
}
}
for (const auto& it : flattend_data->GetClassData()) {
const TypeReference& type_ref = it.first;
const FlattenProfileData::ItemMetadata& metadata = it.second;
if (IncludeClassInProfile(type_ref,
flattend_data->GetMaxAggregationForClasses(),
metadata,
options)) {
profile_classes.Put(BootImageRepresentation(it.first), it.second);
}
std::string preloaded_class_representation = PreloadedClassesRepresentation(it.first);
if (generate_preloaded_classes && IncludeInPreloadedClasses(
preloaded_class_representation,
flattend_data->GetMaxAggregationForClasses(),
metadata,
options)) {
preloaded_classes.Put(preloaded_class_representation, it.second);
}
}
// Create the output content
std::string profile_content;
std::string preloaded_content;
for (const auto& it : profile_classes) {
profile_content += ClassToProfileFormat(it.first, it.second, options.append_package_use_list)
+ "\n";
}
for (const auto& it : profile_methods) {
profile_content += MethodToProfileFormat(it.first, it.second, options.append_package_use_list)
+ "\n";
}
if (generate_preloaded_classes) {
for (const auto& it : preloaded_classes) {
preloaded_content +=
ClassToProfileFormat(it.first, it.second, options.append_package_use_list) + "\n";
}
}
return android::base::WriteStringToFile(profile_content, boot_profile_out_path)
&& (!generate_preloaded_classes
|| android::base::WriteStringToFile(preloaded_content, preloaded_classes_out_path));
}
} // namespace art
|