blob: 7494084a9a5c4a0381dfee314185deb29fb27f03 [file] [log] [blame]
Orion Hodson4c3ade62021-02-10 14:07:10 +00001/*
2 * Copyright (C) 2021 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
Jiakai Zhangccbcfb12021-08-23 15:10:35 +000017#include "odrefresh.h"
Orion Hodson4c3ade62021-02-10 14:07:10 +000018
Jiakai Zhang4e6cc732021-09-17 04:46:24 +000019#include <unistd.h>
20
Jiakai Zhangccbcfb12021-08-23 15:10:35 +000021#include <functional>
22#include <memory>
23#include <string_view>
Victor Hsieh73c4f792021-10-01 18:13:52 +000024#include <utility>
Jiakai Zhang4e6cc732021-09-17 04:46:24 +000025#include <vector>
26
Jiakai Zhang83c38e22021-11-03 20:54:55 +000027#include "aidl/com/android/art/CompilerFilter.h"
28#include "aidl/com/android/art/DexoptBcpExtArgs.h"
29#include "aidl/com/android/art/DexoptSystemServerArgs.h"
30#include "aidl/com/android/art/Isa.h"
Jiakai Zhang34dcce52022-01-07 16:45:11 +000031#include "android-base/file.h"
Victor Hsiehd7506302021-09-24 17:08:23 +000032#include "android-base/parseint.h"
Jiakai Zhangccbcfb12021-08-23 15:10:35 +000033#include "android-base/scopeguard.h"
Jiakai Zhang617c6ab2021-09-24 05:06:07 +000034#include "android-base/stringprintf.h"
Jiakai Zhangccbcfb12021-08-23 15:10:35 +000035#include "android-base/strings.h"
36#include "arch/instruction_set.h"
Orion Hodson4c3ade62021-02-10 14:07:10 +000037#include "base/common_art_test.h"
38#include "base/file_utils.h"
Jiakai Zhang617c6ab2021-09-24 05:06:07 +000039#include "base/stl_util.h"
Jiakai Zhangccbcfb12021-08-23 15:10:35 +000040#include "exec_utils.h"
41#include "gmock/gmock.h"
42#include "gtest/gtest.h"
Jiakai Zhang617c6ab2021-09-24 05:06:07 +000043#include "odr_artifacts.h"
Jiakai Zhangccbcfb12021-08-23 15:10:35 +000044#include "odr_common.h"
45#include "odr_config.h"
Victor Hsieh73c4f792021-10-01 18:13:52 +000046#include "odr_dexopt.h"
Jiakai Zhangccbcfb12021-08-23 15:10:35 +000047#include "odr_fs_utils.h"
48#include "odr_metrics.h"
49#include "odrefresh/odrefresh.h"
Orion Hodson4c3ade62021-02-10 14:07:10 +000050
51namespace art {
52namespace odrefresh {
53
Jiakai Zhang83c38e22021-11-03 20:54:55 +000054using ::aidl::com::android::art::CompilerFilter;
55using ::aidl::com::android::art::DexoptBcpExtArgs;
56using ::aidl::com::android::art::DexoptSystemServerArgs;
57using ::aidl::com::android::art::Isa;
Jiakai Zhang9b7ddf62021-11-01 11:36:52 +000058using ::testing::_;
Jiakai Zhangccbcfb12021-08-23 15:10:35 +000059using ::testing::AllOf;
60using ::testing::Contains;
Jiakai Zhang83c38e22021-11-03 20:54:55 +000061using ::testing::ElementsAre;
Victor Hsieh73c4f792021-10-01 18:13:52 +000062using ::testing::Eq;
63using ::testing::Field;
64using ::testing::Ge;
Jiakai Zhang83c38e22021-11-03 20:54:55 +000065using ::testing::IsEmpty;
Jiakai Zhangccbcfb12021-08-23 15:10:35 +000066using ::testing::Return;
67
68constexpr int kReplace = 1;
69
70void CreateEmptyFile(const std::string& name) {
71 File* file = OS::CreateEmptyFile(name.c_str());
Victor Hsieh73c4f792021-10-01 18:13:52 +000072 ASSERT_TRUE(file != nullptr) << "Cannot create file " << name;
Jiakai Zhangccbcfb12021-08-23 15:10:35 +000073 file->Release();
74 delete file;
75}
76
Jiakai Zhang4e6cc732021-09-17 04:46:24 +000077android::base::ScopeGuard<std::function<void()>> ScopedCreateEmptyFile(const std::string& name) {
78 CreateEmptyFile(name);
79 return android::base::ScopeGuard([=]() { unlink(name.c_str()); });
80}
81
Victor Hsieh73c4f792021-10-01 18:13:52 +000082class MockOdrDexopt : public OdrDexopt {
Jiakai Zhangccbcfb12021-08-23 15:10:35 +000083 public:
84 // A workaround to avoid MOCK_METHOD on a method with an `std::string*` parameter, which will lead
85 // to a conflict between gmock and android-base/logging.h (b/132668253).
Jiakai Zhang9b7ddf62021-11-01 11:36:52 +000086 int DexoptBcpExtension(const DexoptBcpExtArgs& args, time_t, bool*, std::string*) override {
Victor Hsieh73c4f792021-10-01 18:13:52 +000087 return DoDexoptBcpExtension(args);
Jiakai Zhangccbcfb12021-08-23 15:10:35 +000088 }
89
Jiakai Zhang9b7ddf62021-11-01 11:36:52 +000090 int DexoptSystemServer(const DexoptSystemServerArgs& args, time_t, bool*, std::string*) override {
Victor Hsieh73c4f792021-10-01 18:13:52 +000091 return DoDexoptSystemServer(args);
92 }
93
94 MOCK_METHOD(int, DoDexoptBcpExtension, (const DexoptBcpExtArgs&));
95 MOCK_METHOD(int, DoDexoptSystemServer, (const DexoptSystemServerArgs&));
Jiakai Zhangccbcfb12021-08-23 15:10:35 +000096};
97
Jiakai Zhang4e6cc732021-09-17 04:46:24 +000098// Matches an FD of a file whose path matches `matcher`.
99MATCHER_P(FdOf, matcher, "") {
100 char path[PATH_MAX];
Victor Hsieh73c4f792021-10-01 18:13:52 +0000101 int fd = arg;
Jiakai Zhang4e6cc732021-09-17 04:46:24 +0000102 std::string proc_path = android::base::StringPrintf("/proc/self/fd/%d", fd);
103 ssize_t len = readlink(proc_path.c_str(), path, sizeof(path));
104 if (len < 0) {
105 return false;
106 }
Jiakai Zhangb1d90352021-09-22 09:08:26 +0000107 std::string path_str{path, static_cast<size_t>(len)};
Jiakai Zhang4e6cc732021-09-17 04:46:24 +0000108 return ExplainMatchResult(matcher, path_str, result_listener);
109}
110
Jiakai Zhang34dcce52022-01-07 16:45:11 +0000111void WriteFakeApexInfoList(const std::string& filename) {
112 std::string content = R"xml(
113<?xml version="1.0" encoding="utf-8"?>
114<apex-info-list>
115 <apex-info
116 moduleName="com.android.art"
117 modulePath="/data/apex/active/com.android.art@319999900.apex"
118 preinstalledModulePath="/system/apex/com.android.art.capex"
119 versionCode="319999900"
120 versionName=""
121 isFactory="false"
122 isActive="true"
123 lastUpdateMillis="12345678">
124 </apex-info>
125</apex-info-list>
126)xml";
127 android::base::WriteStringToFile(content, filename);
128}
129
Jiakai Zhangccbcfb12021-08-23 15:10:35 +0000130class OdRefreshTest : public CommonArtTest {
131 public:
132 OdRefreshTest() : config_("odrefresh") {}
133
134 protected:
135 void SetUp() override {
136 CommonArtTest::SetUp();
137
138 temp_dir_ = std::make_unique<ScratchDir>();
139 std::string_view temp_dir_path = temp_dir_->GetPath();
140 android::base::ConsumeSuffix(&temp_dir_path, "/");
141
142 std::string android_root_path = Concatenate({temp_dir_path, "/system"});
143 ASSERT_TRUE(EnsureDirectoryExists(android_root_path));
144 android_root_env_ = std::make_unique<ScopedUnsetEnvironmentVariable>("ANDROID_ROOT");
145 setenv("ANDROID_ROOT", android_root_path.c_str(), kReplace);
146
147 std::string android_art_root_path = Concatenate({temp_dir_path, "/apex/com.android.art"});
148 ASSERT_TRUE(EnsureDirectoryExists(android_art_root_path));
149 android_art_root_env_ = std::make_unique<ScopedUnsetEnvironmentVariable>("ANDROID_ART_ROOT");
150 setenv("ANDROID_ART_ROOT", android_art_root_path.c_str(), kReplace);
151
152 std::string art_apex_data_path = Concatenate({temp_dir_path, kArtApexDataDefaultPath});
153 ASSERT_TRUE(EnsureDirectoryExists(art_apex_data_path));
154 art_apex_data_env_ = std::make_unique<ScopedUnsetEnvironmentVariable>("ART_APEX_DATA");
155 setenv("ART_APEX_DATA", art_apex_data_path.c_str(), kReplace);
156
Jiakai Zhang4e6cc732021-09-17 04:46:24 +0000157 dalvik_cache_dir_ = art_apex_data_path + "/dalvik-cache";
158 ASSERT_TRUE(EnsureDirectoryExists(dalvik_cache_dir_ + "/x86_64"));
Jiakai Zhangccbcfb12021-08-23 15:10:35 +0000159
Victor Hsieh73c4f792021-10-01 18:13:52 +0000160 std::string system_etc_dir = Concatenate({android_root_path, "/etc"});
161 ASSERT_TRUE(EnsureDirectoryExists(system_etc_dir));
162 boot_profile_file_ = system_etc_dir + "/boot-image.prof";
163 CreateEmptyFile(boot_profile_file_);
164
Jiakai Zhang4e6cc732021-09-17 04:46:24 +0000165 framework_dir_ = android_root_path + "/framework";
166 framework_jar_ = framework_dir_ + "/framework.jar";
167 location_provider_jar_ = framework_dir_ + "/com.android.location.provider.jar";
168 services_jar_ = framework_dir_ + "/services.jar";
Jiakai Zhang9b7ddf62021-11-01 11:36:52 +0000169 services_foo_jar_ = framework_dir_ + "/services-foo.jar";
170 services_bar_jar_ = framework_dir_ + "/services-bar.jar";
Jiakai Zhang4e6cc732021-09-17 04:46:24 +0000171 std::string services_jar_prof = framework_dir_ + "/services.jar.prof";
Jiakai Zhangccbcfb12021-08-23 15:10:35 +0000172 std::string javalib_dir = android_art_root_path + "/javalib";
173 std::string boot_art = javalib_dir + "/boot.art";
174
175 // Create placeholder files.
Jiakai Zhang4e6cc732021-09-17 04:46:24 +0000176 ASSERT_TRUE(EnsureDirectoryExists(framework_dir_ + "/x86_64"));
Jiakai Zhangccbcfb12021-08-23 15:10:35 +0000177 CreateEmptyFile(framework_jar_);
178 CreateEmptyFile(location_provider_jar_);
179 CreateEmptyFile(services_jar_);
Jiakai Zhang9b7ddf62021-11-01 11:36:52 +0000180 CreateEmptyFile(services_foo_jar_);
181 CreateEmptyFile(services_bar_jar_);
Jiakai Zhangccbcfb12021-08-23 15:10:35 +0000182 CreateEmptyFile(services_jar_prof);
183 ASSERT_TRUE(EnsureDirectoryExists(javalib_dir));
184 CreateEmptyFile(boot_art);
185
Jiakai Zhang34dcce52022-01-07 16:45:11 +0000186 std::string apex_info_filename = Concatenate({temp_dir_path, "/apex-info-list.xml"});
187 WriteFakeApexInfoList(apex_info_filename);
188 config_.SetApexInfoListFile(apex_info_filename);
189
Jiakai Zhangccbcfb12021-08-23 15:10:35 +0000190 config_.SetArtBinDir(Concatenate({temp_dir_path, "/bin"}));
191 config_.SetBootClasspath(framework_jar_);
192 config_.SetDex2oatBootclasspath(framework_jar_);
193 config_.SetSystemServerClasspath(Concatenate({location_provider_jar_, ":", services_jar_}));
Jiakai Zhang9b7ddf62021-11-01 11:36:52 +0000194 config_.SetStandaloneSystemServerJars(Concatenate({services_foo_jar_, ":", services_bar_jar_}));
Jiakai Zhangccbcfb12021-08-23 15:10:35 +0000195 config_.SetIsa(InstructionSet::kX86_64);
196 config_.SetZygoteKind(ZygoteKind::kZygote64_32);
Victor Hsieha39356a2021-12-16 11:40:39 -0800197 config_.SetSystemServerCompilerFilter("speed"); // specify a default
Jiakai Zhang34dcce52022-01-07 16:45:11 +0000198 config_.SetArtifactDirectory(dalvik_cache_dir_);
Jiakai Zhangccbcfb12021-08-23 15:10:35 +0000199
Jiakai Zhang4e6cc732021-09-17 04:46:24 +0000200 std::string staging_dir = dalvik_cache_dir_ + "/staging";
Jiakai Zhangccbcfb12021-08-23 15:10:35 +0000201 ASSERT_TRUE(EnsureDirectoryExists(staging_dir));
202 config_.SetStagingDir(staging_dir);
203
Jiakai Zhang4e6cc732021-09-17 04:46:24 +0000204 metrics_ = std::make_unique<OdrMetrics>(dalvik_cache_dir_);
Jiakai Zhangccbcfb12021-08-23 15:10:35 +0000205 }
206
207 void TearDown() override {
208 metrics_.reset();
209 temp_dir_.reset();
210 android_root_env_.reset();
211 android_art_root_env_.reset();
212 art_apex_data_env_.reset();
213
214 CommonArtTest::TearDown();
215 }
216
Victor Hsieh73c4f792021-10-01 18:13:52 +0000217 std::pair<std::unique_ptr<OnDeviceRefresh>, MockOdrDexopt*> CreateOdRefresh() {
218 auto mock_odr_dexopt = std::make_unique<MockOdrDexopt>();
219 MockOdrDexopt* mock_odr_dexopt_ptr = mock_odr_dexopt.get();
Jiakai Zhang9b7ddf62021-11-01 11:36:52 +0000220 auto odrefresh = std::make_unique<OnDeviceRefresh>(config_,
221 dalvik_cache_dir_ + "/cache-info.xml",
222 std::make_unique<ExecUtils>(),
223 std::move(mock_odr_dexopt));
Victor Hsieh73c4f792021-10-01 18:13:52 +0000224 return std::make_pair(std::move(odrefresh), mock_odr_dexopt_ptr);
225 }
226
Jiakai Zhangccbcfb12021-08-23 15:10:35 +0000227 std::unique_ptr<ScratchDir> temp_dir_;
228 std::unique_ptr<ScopedUnsetEnvironmentVariable> android_root_env_;
229 std::unique_ptr<ScopedUnsetEnvironmentVariable> android_art_root_env_;
230 std::unique_ptr<ScopedUnsetEnvironmentVariable> art_apex_data_env_;
231 OdrConfig config_;
Jiakai Zhangccbcfb12021-08-23 15:10:35 +0000232 std::unique_ptr<OdrMetrics> metrics_;
Jiakai Zhangccbcfb12021-08-23 15:10:35 +0000233 std::string framework_jar_;
234 std::string location_provider_jar_;
235 std::string services_jar_;
Jiakai Zhang9b7ddf62021-11-01 11:36:52 +0000236 std::string services_foo_jar_;
237 std::string services_bar_jar_;
Jiakai Zhang4e6cc732021-09-17 04:46:24 +0000238 std::string dalvik_cache_dir_;
239 std::string framework_dir_;
Victor Hsieh73c4f792021-10-01 18:13:52 +0000240 std::string boot_profile_file_;
Jiakai Zhangccbcfb12021-08-23 15:10:35 +0000241};
242
Jiakai Zhang83c38e22021-11-03 20:54:55 +0000243TEST_F(OdRefreshTest, AllSystemServerJars) {
244 auto [odrefresh, mock_odr_dexopt] = CreateOdRefresh();
245
246 EXPECT_CALL(*mock_odr_dexopt,
247 DoDexoptSystemServer(
248 AllOf(Field(&DexoptSystemServerArgs::dexPath, Eq(location_provider_jar_)),
Jiakai Zhang9b7ddf62021-11-01 11:36:52 +0000249 Field(&DexoptSystemServerArgs::classloaderContext, IsEmpty()),
250 Field(&DexoptSystemServerArgs::classloaderContextAsParent, Eq(false)))))
251 .WillOnce(Return(0));
252 EXPECT_CALL(
253 *mock_odr_dexopt,
254 DoDexoptSystemServer(AllOf(
255 Field(&DexoptSystemServerArgs::dexPath, Eq(services_jar_)),
256 Field(&DexoptSystemServerArgs::classloaderContext, ElementsAre(location_provider_jar_)),
257 Field(&DexoptSystemServerArgs::classloaderContextAsParent, Eq(false)))))
Jiakai Zhang83c38e22021-11-03 20:54:55 +0000258 .WillOnce(Return(0));
259 EXPECT_CALL(*mock_odr_dexopt,
Jiakai Zhang9b7ddf62021-11-01 11:36:52 +0000260 DoDexoptSystemServer(
261 AllOf(Field(&DexoptSystemServerArgs::dexPath, Eq(services_foo_jar_)),
262 Field(&DexoptSystemServerArgs::classloaderContext,
263 ElementsAre(location_provider_jar_, services_jar_)),
264 Field(&DexoptSystemServerArgs::classloaderContextAsParent, Eq(true)))))
265 .WillOnce(Return(0));
266 EXPECT_CALL(*mock_odr_dexopt,
267 DoDexoptSystemServer(
268 AllOf(Field(&DexoptSystemServerArgs::dexPath, Eq(services_bar_jar_)),
269 Field(&DexoptSystemServerArgs::classloaderContext,
270 ElementsAre(location_provider_jar_, services_jar_)),
271 Field(&DexoptSystemServerArgs::classloaderContextAsParent, Eq(true)))))
Jiakai Zhang83c38e22021-11-03 20:54:55 +0000272 .WillOnce(Return(0));
273
274 EXPECT_EQ(
275 odrefresh->Compile(*metrics_,
276 CompilationOptions{
277 .system_server_jars_to_compile = odrefresh->AllSystemServerJars(),
278 }),
279 ExitCode::kCompilationSuccess);
280}
281
282TEST_F(OdRefreshTest, PartialSystemServerJars) {
283 auto [odrefresh, mock_odr_dexopt] = CreateOdRefresh();
284
Jiakai Zhang9b7ddf62021-11-01 11:36:52 +0000285 EXPECT_CALL(
286 *mock_odr_dexopt,
287 DoDexoptSystemServer(AllOf(
288 Field(&DexoptSystemServerArgs::dexPath, Eq(services_jar_)),
289 Field(&DexoptSystemServerArgs::classloaderContext, ElementsAre(location_provider_jar_)),
290 Field(&DexoptSystemServerArgs::classloaderContextAsParent, Eq(false)))))
291 .WillOnce(Return(0));
Jiakai Zhang83c38e22021-11-03 20:54:55 +0000292 EXPECT_CALL(*mock_odr_dexopt,
Jiakai Zhang9b7ddf62021-11-01 11:36:52 +0000293 DoDexoptSystemServer(
294 AllOf(Field(&DexoptSystemServerArgs::dexPath, Eq(services_bar_jar_)),
295 Field(&DexoptSystemServerArgs::classloaderContext,
296 ElementsAre(location_provider_jar_, services_jar_)),
297 Field(&DexoptSystemServerArgs::classloaderContextAsParent, Eq(true)))))
Jiakai Zhang83c38e22021-11-03 20:54:55 +0000298 .WillOnce(Return(0));
299
Jiakai Zhang9b7ddf62021-11-01 11:36:52 +0000300 EXPECT_EQ(
301 odrefresh->Compile(*metrics_,
302 CompilationOptions{
303 .system_server_jars_to_compile = {services_jar_, services_bar_jar_},
304 }),
305 ExitCode::kCompilationSuccess);
306}
307
308// Verifies that odrefresh can run properly when the STANDALONE_SYSTEM_SERVER_JARS variable is
309// missing, which is expected on Android S.
310TEST_F(OdRefreshTest, MissingStandaloneSystemServerJars) {
311 config_.SetStandaloneSystemServerJars("");
312 auto [odrefresh, mock_odr_dexopt] = CreateOdRefresh();
Jiakai Zhang34dcce52022-01-07 16:45:11 +0000313 EXPECT_CALL(*mock_odr_dexopt, DoDexoptSystemServer).WillRepeatedly(Return(0));
Jiakai Zhang9b7ddf62021-11-01 11:36:52 +0000314 EXPECT_EQ(
315 odrefresh->Compile(*metrics_,
316 CompilationOptions{
317 .system_server_jars_to_compile = odrefresh->AllSystemServerJars(),
318 }),
319 ExitCode::kCompilationSuccess);
Jiakai Zhang83c38e22021-11-03 20:54:55 +0000320}
321
Victor Hsiehd7506302021-09-24 17:08:23 +0000322TEST_F(OdRefreshTest, CompileSetsCompilerFilter) {
Jiakai Zhangeb65b412021-11-26 14:29:38 +0000323 {
Victor Hsieh73c4f792021-10-01 18:13:52 +0000324 auto [odrefresh, mock_odr_dexopt] = CreateOdRefresh();
325
Victor Hsieha39356a2021-12-16 11:40:39 -0800326 // Test setup: use "speed" compiler filter.
327 config_.SetSystemServerCompilerFilter("speed");
Victor Hsieh73c4f792021-10-01 18:13:52 +0000328
Jiakai Zhangeb65b412021-11-26 14:29:38 +0000329 // Uninteresting calls.
330 EXPECT_CALL(*mock_odr_dexopt, DoDexoptSystemServer(_))
331 .Times(odrefresh->AllSystemServerJars().size() - 2)
332 .WillRepeatedly(Return(0))
333 .RetiresOnSaturation();
334
Jiakai Zhang9b7ddf62021-11-01 11:36:52 +0000335 EXPECT_CALL(*mock_odr_dexopt,
336 DoDexoptSystemServer(AllOf(
337 Field(&DexoptSystemServerArgs::dexPath, Eq(location_provider_jar_)),
338 Field(&DexoptSystemServerArgs::compilerFilter, Eq(CompilerFilter::SPEED)))))
339 .WillOnce(Return(0))
340 .RetiresOnSaturation();
341 EXPECT_CALL(*mock_odr_dexopt,
342 DoDexoptSystemServer(AllOf(
343 Field(&DexoptSystemServerArgs::dexPath, Eq(services_jar_)),
344 Field(&DexoptSystemServerArgs::compilerFilter, Eq(CompilerFilter::SPEED)))))
345 .WillOnce(Return(0))
346 .RetiresOnSaturation();
Jiakai Zhang83c38e22021-11-03 20:54:55 +0000347 EXPECT_EQ(
348 odrefresh->Compile(*metrics_,
349 CompilationOptions{
350 .system_server_jars_to_compile = odrefresh->AllSystemServerJars(),
351 }),
352 ExitCode::kCompilationSuccess);
Jiakai Zhangccbcfb12021-08-23 15:10:35 +0000353 }
354
355 {
Victor Hsieh73c4f792021-10-01 18:13:52 +0000356 auto [odrefresh, mock_odr_dexopt] = CreateOdRefresh();
357
358 // Test setup: with "speed-profile" compiler filter in the request, only apply if there is a
359 // profile, otherwise fallback to speed.
Victor Hsieha39356a2021-12-16 11:40:39 -0800360 config_.SetSystemServerCompilerFilter("speed-profile");
Victor Hsieh73c4f792021-10-01 18:13:52 +0000361
Jiakai Zhangeb65b412021-11-26 14:29:38 +0000362 // Uninteresting calls.
363 EXPECT_CALL(*mock_odr_dexopt, DoDexoptSystemServer(_))
364 .Times(odrefresh->AllSystemServerJars().size() - 2)
365 .WillRepeatedly(Return(0))
366 .RetiresOnSaturation();
367
Jiakai Zhangccbcfb12021-08-23 15:10:35 +0000368 // services.jar has a profile, while location.provider.jar does not.
369 EXPECT_CALL(
Victor Hsieh73c4f792021-10-01 18:13:52 +0000370 *mock_odr_dexopt,
371 DoDexoptSystemServer(AllOf(
372 Field(&DexoptSystemServerArgs::dexPath, Eq(services_jar_)),
373 Field(&DexoptSystemServerArgs::profileFd, Ge(0)),
374 Field(&DexoptSystemServerArgs::compilerFilter, Eq(CompilerFilter::SPEED_PROFILE)))))
Jiakai Zhang9b7ddf62021-11-01 11:36:52 +0000375 .WillOnce(Return(0))
376 .RetiresOnSaturation();
377 EXPECT_CALL(*mock_odr_dexopt,
378 DoDexoptSystemServer(AllOf(
379 Field(&DexoptSystemServerArgs::dexPath, Eq(location_provider_jar_)),
380 Field(&DexoptSystemServerArgs::compilerFilter, Eq(CompilerFilter::SPEED)))))
381 .WillOnce(Return(0))
382 .RetiresOnSaturation();
Jiakai Zhang83c38e22021-11-03 20:54:55 +0000383 EXPECT_EQ(
384 odrefresh->Compile(*metrics_,
385 CompilationOptions{
386 .system_server_jars_to_compile = odrefresh->AllSystemServerJars(),
387 }),
388 ExitCode::kCompilationSuccess);
Jiakai Zhangccbcfb12021-08-23 15:10:35 +0000389 }
390
391 {
Victor Hsieh73c4f792021-10-01 18:13:52 +0000392 auto [odrefresh, mock_odr_dexopt] = CreateOdRefresh();
393
394 // Test setup: "verify" compiler filter should be simply applied.
Victor Hsieha39356a2021-12-16 11:40:39 -0800395 config_.SetSystemServerCompilerFilter("verify");
Victor Hsieh73c4f792021-10-01 18:13:52 +0000396
Jiakai Zhangeb65b412021-11-26 14:29:38 +0000397 // Uninteresting calls.
398 EXPECT_CALL(*mock_odr_dexopt, DoDexoptSystemServer(_))
399 .Times(odrefresh->AllSystemServerJars().size() - 2)
400 .WillRepeatedly(Return(0))
401 .RetiresOnSaturation();
402
Jiakai Zhang9b7ddf62021-11-01 11:36:52 +0000403 EXPECT_CALL(*mock_odr_dexopt,
404 DoDexoptSystemServer(AllOf(
405 Field(&DexoptSystemServerArgs::dexPath, Eq(location_provider_jar_)),
406 Field(&DexoptSystemServerArgs::compilerFilter, Eq(CompilerFilter::VERIFY)))))
407 .WillOnce(Return(0))
408 .RetiresOnSaturation();
409 EXPECT_CALL(*mock_odr_dexopt,
410 DoDexoptSystemServer(AllOf(
411 Field(&DexoptSystemServerArgs::dexPath, Eq(services_jar_)),
412 Field(&DexoptSystemServerArgs::compilerFilter, Eq(CompilerFilter::VERIFY)))))
413 .WillOnce(Return(0))
414 .RetiresOnSaturation();
Jiakai Zhang83c38e22021-11-03 20:54:55 +0000415 EXPECT_EQ(
416 odrefresh->Compile(*metrics_,
417 CompilationOptions{
418 .system_server_jars_to_compile = odrefresh->AllSystemServerJars(),
419 }),
420 ExitCode::kCompilationSuccess);
Jiakai Zhangccbcfb12021-08-23 15:10:35 +0000421 }
Orion Hodson4c3ade62021-02-10 14:07:10 +0000422}
423
Victor Hsieh73c4f792021-10-01 18:13:52 +0000424TEST_F(OdRefreshTest, OutputFilesAndIsa) {
425 auto [odrefresh, mock_odr_dexopt] = CreateOdRefresh();
426
Jiakai Zhang9b7ddf62021-11-01 11:36:52 +0000427 EXPECT_CALL(*mock_odr_dexopt,
428 DoDexoptBcpExtension(AllOf(Field(&DexoptBcpExtArgs::isa, Eq(Isa::X86_64)),
429 Field(&DexoptBcpExtArgs::imageFd, Ge(0)),
430 Field(&DexoptBcpExtArgs::vdexFd, Ge(0)),
431 Field(&DexoptBcpExtArgs::oatFd, Ge(0)))))
Victor Hsieh73c4f792021-10-01 18:13:52 +0000432 .WillOnce(Return(0));
433
Jiakai Zhang83c38e22021-11-03 20:54:55 +0000434 EXPECT_CALL(*mock_odr_dexopt,
435 DoDexoptSystemServer(AllOf(Field(&DexoptSystemServerArgs::isa, Eq(Isa::X86_64)),
436 Field(&DexoptSystemServerArgs::imageFd, Ge(0)),
437 Field(&DexoptSystemServerArgs::vdexFd, Ge(0)),
438 Field(&DexoptSystemServerArgs::oatFd, Ge(0)))))
439 .Times(odrefresh->AllSystemServerJars().size())
Victor Hsieh73c4f792021-10-01 18:13:52 +0000440 .WillRepeatedly(Return(0));
441
Jiakai Zhang83c38e22021-11-03 20:54:55 +0000442 EXPECT_EQ(
443 odrefresh->Compile(*metrics_,
444 CompilationOptions{
445 .compile_boot_extensions_for_isas = {InstructionSet::kX86_64},
446 .system_server_jars_to_compile = odrefresh->AllSystemServerJars(),
447 }),
448 ExitCode::kCompilationSuccess);
Victor Hsieh73c4f792021-10-01 18:13:52 +0000449}
450
Jiakai Zhang4e6cc732021-09-17 04:46:24 +0000451TEST_F(OdRefreshTest, CompileChoosesBootImage) {
452 {
Victor Hsieh73c4f792021-10-01 18:13:52 +0000453 auto [odrefresh, mock_odr_dexopt] = CreateOdRefresh();
454
Jiakai Zhang4e6cc732021-09-17 04:46:24 +0000455 // Boot image is on /data.
456 OdrArtifacts artifacts =
457 OdrArtifacts::ForBootImageExtension(dalvik_cache_dir_ + "/x86_64/boot-framework.art");
458 auto file1 = ScopedCreateEmptyFile(artifacts.ImagePath());
459 auto file2 = ScopedCreateEmptyFile(artifacts.VdexPath());
460 auto file3 = ScopedCreateEmptyFile(artifacts.OatPath());
461
462 EXPECT_CALL(
Victor Hsieh73c4f792021-10-01 18:13:52 +0000463 *mock_odr_dexopt,
Jiakai Zhang83c38e22021-11-03 20:54:55 +0000464 DoDexoptSystemServer(AllOf(Field(&DexoptSystemServerArgs::isBootImageOnSystem, Eq(false)),
465 Field(&DexoptSystemServerArgs::bootClasspathImageFds,
466 Contains(FdOf(artifacts.ImagePath()))),
467 Field(&DexoptSystemServerArgs::bootClasspathVdexFds,
468 Contains(FdOf(artifacts.VdexPath()))),
469 Field(&DexoptSystemServerArgs::bootClasspathOatFds,
470 Contains(FdOf(artifacts.OatPath()))))))
471 .Times(odrefresh->AllSystemServerJars().size())
Jiakai Zhang4e6cc732021-09-17 04:46:24 +0000472 .WillRepeatedly(Return(0));
Jiakai Zhang83c38e22021-11-03 20:54:55 +0000473 EXPECT_EQ(
474 odrefresh->Compile(*metrics_,
475 CompilationOptions{
476 .system_server_jars_to_compile = odrefresh->AllSystemServerJars(),
477 }),
478 ExitCode::kCompilationSuccess);
Jiakai Zhang4e6cc732021-09-17 04:46:24 +0000479 }
480
481 {
Victor Hsieh73c4f792021-10-01 18:13:52 +0000482 auto [odrefresh, mock_odr_dexopt] = CreateOdRefresh();
483
Jiakai Zhang4e6cc732021-09-17 04:46:24 +0000484 // Boot image is on /system.
485 OdrArtifacts artifacts =
486 OdrArtifacts::ForBootImageExtension(framework_dir_ + "/x86_64/boot-framework.art");
487 auto file1 = ScopedCreateEmptyFile(artifacts.ImagePath());
488 auto file2 = ScopedCreateEmptyFile(artifacts.VdexPath());
489 auto file3 = ScopedCreateEmptyFile(artifacts.OatPath());
490
Victor Hsieh73c4f792021-10-01 18:13:52 +0000491 EXPECT_CALL(
492 *mock_odr_dexopt,
Jiakai Zhang83c38e22021-11-03 20:54:55 +0000493 DoDexoptSystemServer(AllOf(Field(&DexoptSystemServerArgs::isBootImageOnSystem, Eq(true)),
494 Field(&DexoptSystemServerArgs::bootClasspathImageFds,
495 Contains(FdOf(artifacts.ImagePath()))),
496 Field(&DexoptSystemServerArgs::bootClasspathVdexFds,
497 Contains(FdOf(artifacts.VdexPath()))),
498 Field(&DexoptSystemServerArgs::bootClasspathOatFds,
499 Contains(FdOf(artifacts.OatPath()))))))
500 .Times(odrefresh->AllSystemServerJars().size())
Jiakai Zhang4e6cc732021-09-17 04:46:24 +0000501 .WillRepeatedly(Return(0));
Jiakai Zhang83c38e22021-11-03 20:54:55 +0000502 EXPECT_EQ(
503 odrefresh->Compile(*metrics_,
504 CompilationOptions{
505 .system_server_jars_to_compile = odrefresh->AllSystemServerJars(),
506 }),
507 ExitCode::kCompilationSuccess);
Jiakai Zhang4e6cc732021-09-17 04:46:24 +0000508 }
509}
510
Orion Hodson4c3ade62021-02-10 14:07:10 +0000511} // namespace odrefresh
512} // namespace art