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
|
/*
* Copyright (C) 2016 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 <gtest/gtest.h>
#include "base/unix_file/fd_file.h"
#include "common_runtime_test.h"
#include "compiler/profile_assistant.h"
#include "jit/offline_profiling_info.h"
namespace art {
class ProfileAssistantTest : public CommonRuntimeTest {
protected:
void SetupProfile(const std::string& id,
uint32_t checksum,
uint16_t number_of_methods,
const ScratchFile& profile,
ProfileCompilationInfo* info,
uint16_t start_method_index = 0) {
std::string dex_location1 = "location1" + id;
uint32_t dex_location_checksum1 = checksum;
std::string dex_location2 = "location2" + id;
uint32_t dex_location_checksum2 = 10 * checksum;
for (uint16_t i = start_method_index; i < start_method_index + number_of_methods; i++) {
ASSERT_TRUE(info->AddData(dex_location1, dex_location_checksum1, i));
ASSERT_TRUE(info->AddData(dex_location2, dex_location_checksum2, i));
}
ASSERT_TRUE(info->Save(GetFd(profile)));
ASSERT_EQ(0, profile.GetFile()->Flush());
ASSERT_TRUE(profile.GetFile()->ResetOffset());
}
uint32_t GetFd(const ScratchFile& file) const {
return static_cast<uint32_t>(file.GetFd());
}
};
TEST_F(ProfileAssistantTest, AdviseCompilationEmptyReferences) {
ScratchFile profile1;
ScratchFile profile2;
ScratchFile reference_profile1;
ScratchFile reference_profile2;
std::vector<uint32_t> profile_fds({
GetFd(profile1),
GetFd(profile2)});
std::vector<uint32_t> reference_profile_fds({
GetFd(reference_profile1),
GetFd(reference_profile2)});
const uint16_t kNumberOfMethodsToEnableCompilation = 100;
ProfileCompilationInfo info1;
SetupProfile("p1", 1, kNumberOfMethodsToEnableCompilation, profile1, &info1);
ProfileCompilationInfo info2;
SetupProfile("p2", 2, kNumberOfMethodsToEnableCompilation, profile2, &info2);
// We should advise compilation.
ProfileCompilationInfo* result;
ASSERT_TRUE(ProfileAssistant::ProcessProfiles(profile_fds, reference_profile_fds, &result));
ASSERT_TRUE(result != nullptr);
// The resulting compilation info must be equal to the merge of the inputs.
ProfileCompilationInfo expected;
ASSERT_TRUE(expected.Load(info1));
ASSERT_TRUE(expected.Load(info2));
ASSERT_TRUE(expected.Equals(*result));
// The information from profiles must be transfered to the reference profiles.
ProfileCompilationInfo file_info1;
ASSERT_TRUE(reference_profile1.GetFile()->ResetOffset());
ASSERT_TRUE(file_info1.Load(GetFd(reference_profile1)));
ASSERT_TRUE(file_info1.Equals(info1));
ProfileCompilationInfo file_info2;
ASSERT_TRUE(reference_profile2.GetFile()->ResetOffset());
ASSERT_TRUE(file_info2.Load(GetFd(reference_profile2)));
ASSERT_TRUE(file_info2.Equals(info2));
// Initial profiles must be cleared.
ASSERT_EQ(0, profile1.GetFile()->GetLength());
ASSERT_EQ(0, profile2.GetFile()->GetLength());
}
TEST_F(ProfileAssistantTest, AdviseCompilationNonEmptyReferences) {
ScratchFile profile1;
ScratchFile profile2;
ScratchFile reference_profile1;
ScratchFile reference_profile2;
std::vector<uint32_t> profile_fds({
GetFd(profile1),
GetFd(profile2)});
std::vector<uint32_t> reference_profile_fds({
GetFd(reference_profile1),
GetFd(reference_profile2)});
// The new profile info will contain the methods with indices 0-100.
const uint16_t kNumberOfMethodsToEnableCompilation = 100;
ProfileCompilationInfo info1;
SetupProfile("p1", 1, kNumberOfMethodsToEnableCompilation, profile1, &info1);
ProfileCompilationInfo info2;
SetupProfile("p2", 2, kNumberOfMethodsToEnableCompilation, profile2, &info2);
// The reference profile info will contain the methods with indices 50-150.
const uint16_t kNumberOfMethodsAlreadyCompiled = 100;
ProfileCompilationInfo reference_info1;
SetupProfile("p1", 1, kNumberOfMethodsAlreadyCompiled, reference_profile1,
&reference_info1, kNumberOfMethodsToEnableCompilation / 2);
ProfileCompilationInfo reference_info2;
SetupProfile("p2", 2, kNumberOfMethodsAlreadyCompiled, reference_profile2,
&reference_info2, kNumberOfMethodsToEnableCompilation / 2);
// We should advise compilation.
ProfileCompilationInfo* result;
ASSERT_TRUE(ProfileAssistant::ProcessProfiles(profile_fds, reference_profile_fds, &result));
ASSERT_TRUE(result != nullptr);
// The resulting compilation info must be equal to the merge of the inputs
ProfileCompilationInfo expected;
ASSERT_TRUE(expected.Load(info1));
ASSERT_TRUE(expected.Load(info2));
ASSERT_TRUE(expected.Load(reference_info1));
ASSERT_TRUE(expected.Load(reference_info2));
ASSERT_TRUE(expected.Equals(*result));
// The information from profiles must be transfered to the reference profiles.
ProfileCompilationInfo file_info1;
ProfileCompilationInfo merge1;
ASSERT_TRUE(merge1.Load(info1));
ASSERT_TRUE(merge1.Load(reference_info1));
ASSERT_TRUE(reference_profile1.GetFile()->ResetOffset());
ASSERT_TRUE(file_info1.Load(GetFd(reference_profile1)));
ASSERT_TRUE(file_info1.Equals(merge1));
ProfileCompilationInfo file_info2;
ProfileCompilationInfo merge2;
ASSERT_TRUE(merge2.Load(info2));
ASSERT_TRUE(merge2.Load(reference_info2));
ASSERT_TRUE(reference_profile2.GetFile()->ResetOffset());
ASSERT_TRUE(file_info2.Load(GetFd(reference_profile2)));
ASSERT_TRUE(file_info2.Equals(merge2));
// Initial profiles must be cleared.
ASSERT_EQ(0, profile1.GetFile()->GetLength());
ASSERT_EQ(0, profile2.GetFile()->GetLength());
}
TEST_F(ProfileAssistantTest, DoNotAdviseCompilation) {
ScratchFile profile1;
ScratchFile profile2;
ScratchFile reference_profile1;
ScratchFile reference_profile2;
std::vector<uint32_t> profile_fds({
GetFd(profile1),
GetFd(profile2)});
std::vector<uint32_t> reference_profile_fds({
GetFd(reference_profile1),
GetFd(reference_profile2)});
const uint16_t kNumberOfMethodsToSkipCompilation = 1;
ProfileCompilationInfo info1;
SetupProfile("p1", 1, kNumberOfMethodsToSkipCompilation, profile1, &info1);
ProfileCompilationInfo info2;
SetupProfile("p2", 2, kNumberOfMethodsToSkipCompilation, profile2, &info2);
// We should not advise compilation.
ProfileCompilationInfo* result = nullptr;
ASSERT_TRUE(ProfileAssistant::ProcessProfiles(profile_fds, reference_profile_fds, &result));
ASSERT_TRUE(result == nullptr);
// The information from profiles must remain the same.
ProfileCompilationInfo file_info1;
ASSERT_TRUE(profile1.GetFile()->ResetOffset());
ASSERT_TRUE(file_info1.Load(GetFd(profile1)));
ASSERT_TRUE(file_info1.Equals(info1));
ProfileCompilationInfo file_info2;
ASSERT_TRUE(profile2.GetFile()->ResetOffset());
ASSERT_TRUE(file_info2.Load(GetFd(profile2)));
ASSERT_TRUE(file_info2.Equals(info2));
// Reference profile files must remain empty.
ASSERT_EQ(0, reference_profile1.GetFile()->GetLength());
ASSERT_EQ(0, reference_profile2.GetFile()->GetLength());
}
TEST_F(ProfileAssistantTest, FailProcessingBecauseOfProfiles) {
ScratchFile profile1;
ScratchFile profile2;
ScratchFile reference_profile1;
ScratchFile reference_profile2;
std::vector<uint32_t> profile_fds({
GetFd(profile1),
GetFd(profile2)});
std::vector<uint32_t> reference_profile_fds({
GetFd(reference_profile1),
GetFd(reference_profile2)});
const uint16_t kNumberOfMethodsToEnableCompilation = 100;
// Assign different hashes for the same dex file. This will make merging of information to fail.
ProfileCompilationInfo info1;
SetupProfile("p1", 1, kNumberOfMethodsToEnableCompilation, profile1, &info1);
ProfileCompilationInfo info2;
SetupProfile("p1", 2, kNumberOfMethodsToEnableCompilation, profile2, &info2);
// We should fail processing.
ProfileCompilationInfo* result = nullptr;
ASSERT_FALSE(ProfileAssistant::ProcessProfiles(profile_fds, reference_profile_fds, &result));
ASSERT_TRUE(result == nullptr);
// The information from profiles must still remain the same.
ProfileCompilationInfo file_info1;
ASSERT_TRUE(profile1.GetFile()->ResetOffset());
ASSERT_TRUE(file_info1.Load(GetFd(profile1)));
ASSERT_TRUE(file_info1.Equals(info1));
ProfileCompilationInfo file_info2;
ASSERT_TRUE(profile2.GetFile()->ResetOffset());
ASSERT_TRUE(file_info2.Load(GetFd(profile2)));
ASSERT_TRUE(file_info2.Equals(info2));
// Reference profile files must still remain empty.
ASSERT_EQ(0, reference_profile1.GetFile()->GetLength());
ASSERT_EQ(0, reference_profile2.GetFile()->GetLength());
}
TEST_F(ProfileAssistantTest, FailProcessingBecauseOfReferenceProfiles) {
ScratchFile profile1;
ScratchFile reference_profile;
std::vector<uint32_t> profile_fds({
GetFd(profile1)});
std::vector<uint32_t> reference_profile_fds({
GetFd(reference_profile)});
const uint16_t kNumberOfMethodsToEnableCompilation = 100;
// Assign different hashes for the same dex file. This will make merging of information to fail.
ProfileCompilationInfo info1;
SetupProfile("p1", 1, kNumberOfMethodsToEnableCompilation, profile1, &info1);
ProfileCompilationInfo reference_info;
SetupProfile("p1", 2, kNumberOfMethodsToEnableCompilation, reference_profile, &reference_info);
// We should not advise compilation.
ProfileCompilationInfo* result = nullptr;
ASSERT_TRUE(profile1.GetFile()->ResetOffset());
ASSERT_TRUE(reference_profile.GetFile()->ResetOffset());
ASSERT_FALSE(ProfileAssistant::ProcessProfiles(profile_fds, reference_profile_fds, &result));
ASSERT_TRUE(result == nullptr);
// The information from profiles must still remain the same.
ProfileCompilationInfo file_info1;
ASSERT_TRUE(profile1.GetFile()->ResetOffset());
ASSERT_TRUE(file_info1.Load(GetFd(profile1)));
ASSERT_TRUE(file_info1.Equals(info1));
ProfileCompilationInfo file_info2;
ASSERT_TRUE(reference_profile.GetFile()->ResetOffset());
ASSERT_TRUE(file_info2.Load(GetFd(reference_profile)));
ASSERT_TRUE(file_info2.Equals(reference_info));
}
} // namespace art
|