blob: ae7cc785947689d028a49104ddd7ea30d7d19eb2 [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 Zhang34dcce52022-01-07 16:45:11 +000027#include "android-base/file.h"
Victor Hsiehd7506302021-09-24 17:08:23 +000028#include "android-base/parseint.h"
Jiakai Zhangccbcfb12021-08-23 15:10:35 +000029#include "android-base/scopeguard.h"
Jiakai Zhang617c6ab2021-09-24 05:06:07 +000030#include "android-base/stringprintf.h"
Jiakai Zhangccbcfb12021-08-23 15:10:35 +000031#include "android-base/strings.h"
32#include "arch/instruction_set.h"
Orion Hodson4c3ade62021-02-10 14:07:10 +000033#include "base/common_art_test.h"
34#include "base/file_utils.h"
Jiakai Zhang617c6ab2021-09-24 05:06:07 +000035#include "base/stl_util.h"
Jiakai Zhangccbcfb12021-08-23 15:10:35 +000036#include "exec_utils.h"
37#include "gmock/gmock.h"
38#include "gtest/gtest.h"
Jiakai Zhang617c6ab2021-09-24 05:06:07 +000039#include "odr_artifacts.h"
Jiakai Zhangccbcfb12021-08-23 15:10:35 +000040#include "odr_common.h"
41#include "odr_config.h"
42#include "odr_fs_utils.h"
43#include "odr_metrics.h"
44#include "odrefresh/odrefresh.h"
Orion Hodson4c3ade62021-02-10 14:07:10 +000045
46namespace art {
47namespace odrefresh {
48
Jiakai Zhang9b7ddf62021-11-01 11:36:52 +000049using ::testing::_;
Jiakai Zhangccbcfb12021-08-23 15:10:35 +000050using ::testing::AllOf;
51using ::testing::Contains;
Victor Hsieh7b798142022-01-18 15:13:14 -080052using ::testing::HasSubstr;
Jiakai Zhang8046b622022-01-28 21:40:17 +000053using ::testing::Not;
Jiakai Zhangccbcfb12021-08-23 15:10:35 +000054using ::testing::Return;
55
56constexpr int kReplace = 1;
57
58void CreateEmptyFile(const std::string& name) {
59 File* file = OS::CreateEmptyFile(name.c_str());
Victor Hsieh73c4f792021-10-01 18:13:52 +000060 ASSERT_TRUE(file != nullptr) << "Cannot create file " << name;
Jiakai Zhangccbcfb12021-08-23 15:10:35 +000061 file->Release();
62 delete file;
63}
64
Jiakai Zhang4e6cc732021-09-17 04:46:24 +000065android::base::ScopeGuard<std::function<void()>> ScopedCreateEmptyFile(const std::string& name) {
66 CreateEmptyFile(name);
67 return android::base::ScopeGuard([=]() { unlink(name.c_str()); });
68}
69
Victor Hsieh7b798142022-01-18 15:13:14 -080070class MockExecUtils : public ExecUtils {
Jiakai Zhangccbcfb12021-08-23 15:10:35 +000071 public:
72 // A workaround to avoid MOCK_METHOD on a method with an `std::string*` parameter, which will lead
73 // to a conflict between gmock and android-base/logging.h (b/132668253).
Victor Hsieh7b798142022-01-18 15:13:14 -080074 int ExecAndReturnCode(std::vector<std::string>& arg_vector,
75 time_t,
76 bool*,
77 std::string*) const override {
78 return DoExecAndReturnCode(arg_vector);
Jiakai Zhangccbcfb12021-08-23 15:10:35 +000079 }
80
Victor Hsieh7b798142022-01-18 15:13:14 -080081 MOCK_METHOD(int, DoExecAndReturnCode, (std::vector<std::string> & arg_vector), (const));
Jiakai Zhangccbcfb12021-08-23 15:10:35 +000082};
83
Victor Hsieh7b798142022-01-18 15:13:14 -080084// Matches a flag that starts with `flag` and is a colon-separated list that contains an element
85// that matches `matcher`.
86MATCHER_P2(FlagContains, flag, matcher, "") {
87 std::string_view value = arg;
88 if (!android::base::ConsumePrefix(&value, flag)) {
89 return false;
90 }
91 for (std::string_view s : SplitString(value, ':')) {
92 if (ExplainMatchResult(matcher, s, result_listener)) {
93 return true;
94 }
95 }
96 return false;
97}
98
Jiakai Zhang4e6cc732021-09-17 04:46:24 +000099// Matches an FD of a file whose path matches `matcher`.
100MATCHER_P(FdOf, matcher, "") {
101 char path[PATH_MAX];
Victor Hsieh7b798142022-01-18 15:13:14 -0800102 int fd;
103 if (!android::base::ParseInt(std::string{arg}, &fd)) {
104 return false;
105 }
Jiakai Zhang4e6cc732021-09-17 04:46:24 +0000106 std::string proc_path = android::base::StringPrintf("/proc/self/fd/%d", fd);
107 ssize_t len = readlink(proc_path.c_str(), path, sizeof(path));
108 if (len < 0) {
109 return false;
110 }
Jiakai Zhangb1d90352021-09-22 09:08:26 +0000111 std::string path_str{path, static_cast<size_t>(len)};
Jiakai Zhang4e6cc732021-09-17 04:46:24 +0000112 return ExplainMatchResult(matcher, path_str, result_listener);
113}
114
Jiakai Zhang34dcce52022-01-07 16:45:11 +0000115void WriteFakeApexInfoList(const std::string& filename) {
116 std::string content = R"xml(
117<?xml version="1.0" encoding="utf-8"?>
118<apex-info-list>
119 <apex-info
120 moduleName="com.android.art"
121 modulePath="/data/apex/active/com.android.art@319999900.apex"
122 preinstalledModulePath="/system/apex/com.android.art.capex"
123 versionCode="319999900"
124 versionName=""
125 isFactory="false"
126 isActive="true"
127 lastUpdateMillis="12345678">
128 </apex-info>
129</apex-info-list>
130)xml";
131 android::base::WriteStringToFile(content, filename);
132}
133
Jiakai Zhangccbcfb12021-08-23 15:10:35 +0000134class OdRefreshTest : public CommonArtTest {
135 public:
136 OdRefreshTest() : config_("odrefresh") {}
137
138 protected:
139 void SetUp() override {
140 CommonArtTest::SetUp();
141
142 temp_dir_ = std::make_unique<ScratchDir>();
143 std::string_view temp_dir_path = temp_dir_->GetPath();
144 android::base::ConsumeSuffix(&temp_dir_path, "/");
145
146 std::string android_root_path = Concatenate({temp_dir_path, "/system"});
147 ASSERT_TRUE(EnsureDirectoryExists(android_root_path));
148 android_root_env_ = std::make_unique<ScopedUnsetEnvironmentVariable>("ANDROID_ROOT");
149 setenv("ANDROID_ROOT", android_root_path.c_str(), kReplace);
150
151 std::string android_art_root_path = Concatenate({temp_dir_path, "/apex/com.android.art"});
152 ASSERT_TRUE(EnsureDirectoryExists(android_art_root_path));
153 android_art_root_env_ = std::make_unique<ScopedUnsetEnvironmentVariable>("ANDROID_ART_ROOT");
154 setenv("ANDROID_ART_ROOT", android_art_root_path.c_str(), kReplace);
155
156 std::string art_apex_data_path = Concatenate({temp_dir_path, kArtApexDataDefaultPath});
157 ASSERT_TRUE(EnsureDirectoryExists(art_apex_data_path));
158 art_apex_data_env_ = std::make_unique<ScopedUnsetEnvironmentVariable>("ART_APEX_DATA");
159 setenv("ART_APEX_DATA", art_apex_data_path.c_str(), kReplace);
160
Jiakai Zhang4e6cc732021-09-17 04:46:24 +0000161 dalvik_cache_dir_ = art_apex_data_path + "/dalvik-cache";
162 ASSERT_TRUE(EnsureDirectoryExists(dalvik_cache_dir_ + "/x86_64"));
Jiakai Zhangccbcfb12021-08-23 15:10:35 +0000163
Victor Hsieh73c4f792021-10-01 18:13:52 +0000164 std::string system_etc_dir = Concatenate({android_root_path, "/etc"});
165 ASSERT_TRUE(EnsureDirectoryExists(system_etc_dir));
Jiakai Zhang884e22f2021-12-23 20:04:46 +0000166 framework_profile_ = system_etc_dir + "/boot-image.prof";
167 CreateEmptyFile(framework_profile_);
168 std::string art_etc_dir = Concatenate({android_art_root_path, "/etc"});
169 ASSERT_TRUE(EnsureDirectoryExists(art_etc_dir));
170 art_profile_ = art_etc_dir + "/boot-image.prof";
171 CreateEmptyFile(art_profile_);
Victor Hsieh73c4f792021-10-01 18:13:52 +0000172
Jiakai Zhang4e6cc732021-09-17 04:46:24 +0000173 framework_dir_ = android_root_path + "/framework";
174 framework_jar_ = framework_dir_ + "/framework.jar";
175 location_provider_jar_ = framework_dir_ + "/com.android.location.provider.jar";
176 services_jar_ = framework_dir_ + "/services.jar";
Jiakai Zhang9b7ddf62021-11-01 11:36:52 +0000177 services_foo_jar_ = framework_dir_ + "/services-foo.jar";
178 services_bar_jar_ = framework_dir_ + "/services-bar.jar";
Jiakai Zhang4e6cc732021-09-17 04:46:24 +0000179 std::string services_jar_prof = framework_dir_ + "/services.jar.prof";
Jiakai Zhang884e22f2021-12-23 20:04:46 +0000180 art_javalib_dir_ = android_art_root_path + "/javalib";
181 core_oj_jar_ = art_javalib_dir_ + "/core-oj.jar";
Jiakai Zhangccbcfb12021-08-23 15:10:35 +0000182
183 // Create placeholder files.
Jiakai Zhang4e6cc732021-09-17 04:46:24 +0000184 ASSERT_TRUE(EnsureDirectoryExists(framework_dir_ + "/x86_64"));
Jiakai Zhangccbcfb12021-08-23 15:10:35 +0000185 CreateEmptyFile(framework_jar_);
186 CreateEmptyFile(location_provider_jar_);
187 CreateEmptyFile(services_jar_);
Jiakai Zhang9b7ddf62021-11-01 11:36:52 +0000188 CreateEmptyFile(services_foo_jar_);
189 CreateEmptyFile(services_bar_jar_);
Jiakai Zhangccbcfb12021-08-23 15:10:35 +0000190 CreateEmptyFile(services_jar_prof);
Jiakai Zhang884e22f2021-12-23 20:04:46 +0000191 ASSERT_TRUE(EnsureDirectoryExists(art_javalib_dir_));
192 CreateEmptyFile(core_oj_jar_);
Jiakai Zhangccbcfb12021-08-23 15:10:35 +0000193
Jiakai Zhang34dcce52022-01-07 16:45:11 +0000194 std::string apex_info_filename = Concatenate({temp_dir_path, "/apex-info-list.xml"});
195 WriteFakeApexInfoList(apex_info_filename);
196 config_.SetApexInfoListFile(apex_info_filename);
197
Jiakai Zhangccbcfb12021-08-23 15:10:35 +0000198 config_.SetArtBinDir(Concatenate({temp_dir_path, "/bin"}));
Jiakai Zhang884e22f2021-12-23 20:04:46 +0000199 config_.SetBootClasspath(Concatenate({core_oj_jar_, ":", framework_jar_}));
200 config_.SetDex2oatBootclasspath(Concatenate({core_oj_jar_, ":", framework_jar_}));
Jiakai Zhangccbcfb12021-08-23 15:10:35 +0000201 config_.SetSystemServerClasspath(Concatenate({location_provider_jar_, ":", services_jar_}));
Jiakai Zhang9b7ddf62021-11-01 11:36:52 +0000202 config_.SetStandaloneSystemServerJars(Concatenate({services_foo_jar_, ":", services_bar_jar_}));
Jiakai Zhangccbcfb12021-08-23 15:10:35 +0000203 config_.SetIsa(InstructionSet::kX86_64);
204 config_.SetZygoteKind(ZygoteKind::kZygote64_32);
Victor Hsieha39356a2021-12-16 11:40:39 -0800205 config_.SetSystemServerCompilerFilter("speed"); // specify a default
Jiakai Zhang34dcce52022-01-07 16:45:11 +0000206 config_.SetArtifactDirectory(dalvik_cache_dir_);
Jiakai Zhangccbcfb12021-08-23 15:10:35 +0000207
Jiakai Zhang4e6cc732021-09-17 04:46:24 +0000208 std::string staging_dir = dalvik_cache_dir_ + "/staging";
Jiakai Zhangccbcfb12021-08-23 15:10:35 +0000209 ASSERT_TRUE(EnsureDirectoryExists(staging_dir));
210 config_.SetStagingDir(staging_dir);
211
Victor Hsieh7b798142022-01-18 15:13:14 -0800212 auto mock_exec_utils = std::make_unique<MockExecUtils>();
213 mock_exec_utils_ = mock_exec_utils.get();
214
Jiakai Zhang4e6cc732021-09-17 04:46:24 +0000215 metrics_ = std::make_unique<OdrMetrics>(dalvik_cache_dir_);
Victor Hsieh7b798142022-01-18 15:13:14 -0800216 odrefresh_ = std::make_unique<OnDeviceRefresh>(
217 config_, dalvik_cache_dir_ + "/cache-info.xml", std::move(mock_exec_utils));
Jiakai Zhangccbcfb12021-08-23 15:10:35 +0000218 }
219
220 void TearDown() override {
221 metrics_.reset();
222 temp_dir_.reset();
223 android_root_env_.reset();
224 android_art_root_env_.reset();
225 art_apex_data_env_.reset();
226
227 CommonArtTest::TearDown();
228 }
229
230 std::unique_ptr<ScratchDir> temp_dir_;
231 std::unique_ptr<ScopedUnsetEnvironmentVariable> android_root_env_;
232 std::unique_ptr<ScopedUnsetEnvironmentVariable> android_art_root_env_;
233 std::unique_ptr<ScopedUnsetEnvironmentVariable> art_apex_data_env_;
234 OdrConfig config_;
Victor Hsieh7b798142022-01-18 15:13:14 -0800235 MockExecUtils* mock_exec_utils_;
236 std::unique_ptr<OnDeviceRefresh> odrefresh_;
Jiakai Zhangccbcfb12021-08-23 15:10:35 +0000237 std::unique_ptr<OdrMetrics> metrics_;
Jiakai Zhang884e22f2021-12-23 20:04:46 +0000238 std::string core_oj_jar_;
Jiakai Zhangccbcfb12021-08-23 15:10:35 +0000239 std::string framework_jar_;
240 std::string location_provider_jar_;
241 std::string services_jar_;
Jiakai Zhang9b7ddf62021-11-01 11:36:52 +0000242 std::string services_foo_jar_;
243 std::string services_bar_jar_;
Jiakai Zhang4e6cc732021-09-17 04:46:24 +0000244 std::string dalvik_cache_dir_;
245 std::string framework_dir_;
Jiakai Zhang884e22f2021-12-23 20:04:46 +0000246 std::string art_javalib_dir_;
247 std::string framework_profile_;
248 std::string art_profile_;
Jiakai Zhangccbcfb12021-08-23 15:10:35 +0000249};
250
Jiakai Zhang884e22f2021-12-23 20:04:46 +0000251TEST_F(OdRefreshTest, BootClasspathJars) {
Victor Hsieh7b798142022-01-18 15:13:14 -0800252 EXPECT_CALL(*mock_exec_utils_,
253 DoExecAndReturnCode(AllOf(
254 Contains(Concatenate({"--dex-file=", core_oj_jar_})),
255 Contains(Concatenate({"--dex-file=", framework_jar_})),
256 Contains(FlagContains("--dex-fd=", FdOf(core_oj_jar_))),
257 Contains(FlagContains("--dex-fd=", FdOf(framework_jar_))),
258 Contains(FlagContains("--profile-file-fd=", FdOf(art_profile_))),
259 Contains(FlagContains("--profile-file-fd=", FdOf(framework_profile_))),
260 Contains(Concatenate({"--oat-location=", dalvik_cache_dir_, "/x86_64/boot.oat"})),
261 Contains(HasSubstr("--base=")),
262 Contains("--compiler-filter=speed-profile"))))
Jiakai Zhang884e22f2021-12-23 20:04:46 +0000263 .WillOnce(Return(0));
264
Victor Hsieh7b798142022-01-18 15:13:14 -0800265 EXPECT_EQ(odrefresh_->Compile(*metrics_,
266 CompilationOptions{
267 .compile_boot_classpath_for_isas = {InstructionSet::kX86_64},
268 }),
Jiakai Zhang884e22f2021-12-23 20:04:46 +0000269 ExitCode::kCompilationSuccess);
270}
271
Jiakai Zhang8046b622022-01-28 21:40:17 +0000272TEST_F(OdRefreshTest, BootClasspathJarsFallback) {
273 // Simulate the case where dex2oat fails when generating the full boot image.
274 EXPECT_CALL(*mock_exec_utils_,
275 DoExecAndReturnCode(AllOf(Contains(Concatenate({"--dex-file=", core_oj_jar_})),
276 Contains(Concatenate({"--dex-file=", framework_jar_})))))
277 .Times(2)
278 .WillRepeatedly(Return(1));
279
280 // It should fall back to generating a minimal boot image.
281 EXPECT_CALL(
282 *mock_exec_utils_,
283 DoExecAndReturnCode(AllOf(Contains(Concatenate({"--dex-file=", core_oj_jar_})),
284 Not(Contains(Concatenate({"--dex-file=", framework_jar_}))))))
285 .Times(2)
286 .WillOnce(Return(0));
287
288 EXPECT_EQ(
289 odrefresh_->Compile(
290 *metrics_,
291 CompilationOptions{
292 .compile_boot_classpath_for_isas = {InstructionSet::kX86, InstructionSet::kX86_64},
293 .system_server_jars_to_compile = odrefresh_->AllSystemServerJars(),
294 }),
295 ExitCode::kCompilationFailed);
296}
297
Jiakai Zhang83c38e22021-11-03 20:54:55 +0000298TEST_F(OdRefreshTest, AllSystemServerJars) {
Victor Hsieh7b798142022-01-18 15:13:14 -0800299 EXPECT_CALL(*mock_exec_utils_,
300 DoExecAndReturnCode(AllOf(
301 Contains(Concatenate({"--dex-file=", location_provider_jar_})),
302 Contains("--class-loader-context=PCL[]"))))
Jiakai Zhang9b7ddf62021-11-01 11:36:52 +0000303 .WillOnce(Return(0));
Victor Hsieh7b798142022-01-18 15:13:14 -0800304 EXPECT_CALL(*mock_exec_utils_,
305 DoExecAndReturnCode(AllOf(
306 Contains(Concatenate({"--dex-file=", services_jar_})),
307 Contains(Concatenate({"--class-loader-context=PCL[", location_provider_jar_,
308 "]"})))))
Jiakai Zhang83c38e22021-11-03 20:54:55 +0000309 .WillOnce(Return(0));
Victor Hsieh7b798142022-01-18 15:13:14 -0800310 EXPECT_CALL(*mock_exec_utils_,
311 DoExecAndReturnCode(AllOf(
312 Contains(Concatenate({"--dex-file=", services_foo_jar_})),
313 Contains(Concatenate({"--class-loader-context=PCL[];PCL[", location_provider_jar_,
314 ":", services_jar_, "]"})))))
Jiakai Zhang9b7ddf62021-11-01 11:36:52 +0000315 .WillOnce(Return(0));
Victor Hsieh7b798142022-01-18 15:13:14 -0800316 EXPECT_CALL(*mock_exec_utils_,
317 DoExecAndReturnCode(AllOf(
318 Contains(Concatenate({"--dex-file=", services_bar_jar_})),
319 Contains(Concatenate({"--class-loader-context=PCL[];PCL[", location_provider_jar_,
320 ":", services_jar_, "]"})))))
Jiakai Zhang83c38e22021-11-03 20:54:55 +0000321 .WillOnce(Return(0));
322
323 EXPECT_EQ(
Victor Hsieh7b798142022-01-18 15:13:14 -0800324 odrefresh_->Compile(*metrics_,
325 CompilationOptions{
326 .system_server_jars_to_compile = odrefresh_->AllSystemServerJars(),
327 }),
Jiakai Zhang83c38e22021-11-03 20:54:55 +0000328 ExitCode::kCompilationSuccess);
329}
330
331TEST_F(OdRefreshTest, PartialSystemServerJars) {
Victor Hsieh7b798142022-01-18 15:13:14 -0800332 EXPECT_CALL(*mock_exec_utils_,
333 DoExecAndReturnCode(AllOf(
334 Contains(Concatenate({"--dex-file=", services_jar_})),
335 Contains(Concatenate({"--class-loader-context=PCL[", location_provider_jar_, "]"})))))
Jiakai Zhang9b7ddf62021-11-01 11:36:52 +0000336 .WillOnce(Return(0));
Victor Hsieh7b798142022-01-18 15:13:14 -0800337 EXPECT_CALL(*mock_exec_utils_,
338 DoExecAndReturnCode(AllOf(
339 Contains(Concatenate({"--dex-file=", services_bar_jar_})),
340 Contains(Concatenate({"--class-loader-context=PCL[];PCL[", location_provider_jar_,
341 ":", services_jar_, "]"})))))
Jiakai Zhang83c38e22021-11-03 20:54:55 +0000342 .WillOnce(Return(0));
343
Jiakai Zhang9b7ddf62021-11-01 11:36:52 +0000344 EXPECT_EQ(
Victor Hsieh7b798142022-01-18 15:13:14 -0800345 odrefresh_->Compile(*metrics_,
346 CompilationOptions{
347 .system_server_jars_to_compile = {services_jar_, services_bar_jar_},
348 }),
Jiakai Zhang9b7ddf62021-11-01 11:36:52 +0000349 ExitCode::kCompilationSuccess);
350}
351
352// Verifies that odrefresh can run properly when the STANDALONE_SYSTEM_SERVER_JARS variable is
353// missing, which is expected on Android S.
354TEST_F(OdRefreshTest, MissingStandaloneSystemServerJars) {
355 config_.SetStandaloneSystemServerJars("");
Victor Hsieh7b798142022-01-18 15:13:14 -0800356 EXPECT_CALL(*mock_exec_utils_, DoExecAndReturnCode(_)).WillRepeatedly(Return(0));
Jiakai Zhang9b7ddf62021-11-01 11:36:52 +0000357 EXPECT_EQ(
Victor Hsieh7b798142022-01-18 15:13:14 -0800358 odrefresh_->Compile(*metrics_,
359 CompilationOptions{
360 .system_server_jars_to_compile = odrefresh_->AllSystemServerJars(),
361 }),
Jiakai Zhang9b7ddf62021-11-01 11:36:52 +0000362 ExitCode::kCompilationSuccess);
Jiakai Zhang83c38e22021-11-03 20:54:55 +0000363}
364
Victor Hsieh7b798142022-01-18 15:13:14 -0800365TEST_F(OdRefreshTest, CompileSetsCompilerFilterToSpeed) {
366 // Test setup: use "speed" compiler filter.
367 config_.SetSystemServerCompilerFilter("speed");
Victor Hsieh73c4f792021-10-01 18:13:52 +0000368
Victor Hsieh7b798142022-01-18 15:13:14 -0800369 // Uninteresting calls.
370 EXPECT_CALL(
371 *mock_exec_utils_, DoExecAndReturnCode(_))
372 .Times(odrefresh_->AllSystemServerJars().size() - 2)
373 .WillRepeatedly(Return(0));
Victor Hsieh73c4f792021-10-01 18:13:52 +0000374
Victor Hsieh7b798142022-01-18 15:13:14 -0800375 EXPECT_CALL(
376 *mock_exec_utils_,
377 DoExecAndReturnCode(AllOf(Contains(Concatenate({"--dex-file=", location_provider_jar_})),
378 Not(Contains(HasSubstr("--profile-file-fd="))),
379 Contains("--compiler-filter=speed"))))
380 .WillOnce(Return(0));
381 EXPECT_CALL(
382 *mock_exec_utils_,
383 DoExecAndReturnCode(AllOf(Contains(Concatenate({"--dex-file=", services_jar_})),
384 Not(Contains(HasSubstr("--profile-file-fd="))),
385 Contains("--compiler-filter=speed"))))
386 .WillOnce(Return(0));
387 EXPECT_EQ(
388 odrefresh_->Compile(*metrics_,
389 CompilationOptions{
390 .system_server_jars_to_compile = odrefresh_->AllSystemServerJars(),
391 }),
392 ExitCode::kCompilationSuccess);
393}
Jiakai Zhangeb65b412021-11-26 14:29:38 +0000394
Victor Hsieh7b798142022-01-18 15:13:14 -0800395TEST_F(OdRefreshTest, CompileSetsCompilerFilterToSpeedProfile) {
396 // Test setup: with "speed-profile" compiler filter in the request, only apply if there is a
397 // profile, otherwise fallback to speed.
398 config_.SetSystemServerCompilerFilter("speed-profile");
Jiakai Zhangccbcfb12021-08-23 15:10:35 +0000399
Victor Hsieh7b798142022-01-18 15:13:14 -0800400 // Uninteresting calls.
401 EXPECT_CALL(
402 *mock_exec_utils_, DoExecAndReturnCode(_))
403 .Times(odrefresh_->AllSystemServerJars().size() - 2)
404 .WillRepeatedly(Return(0));
Victor Hsieh73c4f792021-10-01 18:13:52 +0000405
Victor Hsieh7b798142022-01-18 15:13:14 -0800406 // services.jar has a profile, while location.provider.jar does not.
407 EXPECT_CALL(
408 *mock_exec_utils_,
409 DoExecAndReturnCode(AllOf(Contains(Concatenate({"--dex-file=", location_provider_jar_})),
410 Not(Contains(HasSubstr("--profile-file-fd="))),
411 Contains("--compiler-filter=speed"))))
412 .WillOnce(Return(0));
413 EXPECT_CALL(
414 *mock_exec_utils_,
415 DoExecAndReturnCode(AllOf(Contains(Concatenate({"--dex-file=", services_jar_})),
416 Contains(HasSubstr("--profile-file-fd=")),
417 Contains("--compiler-filter=speed-profile"))))
418 .WillOnce(Return(0));
419 EXPECT_EQ(
420 odrefresh_->Compile(*metrics_,
421 CompilationOptions{
422 .system_server_jars_to_compile = odrefresh_->AllSystemServerJars(),
423 }),
424 ExitCode::kCompilationSuccess);
425}
Victor Hsieh73c4f792021-10-01 18:13:52 +0000426
Victor Hsieh7b798142022-01-18 15:13:14 -0800427TEST_F(OdRefreshTest, CompileSetsCompilerFilterToVerify) {
428 // Test setup: use "speed" compiler filter.
429 config_.SetSystemServerCompilerFilter("verify");
Jiakai Zhangeb65b412021-11-26 14:29:38 +0000430
Victor Hsieh7b798142022-01-18 15:13:14 -0800431 // Uninteresting calls.
432 EXPECT_CALL(
433 *mock_exec_utils_, DoExecAndReturnCode(_))
434 .Times(odrefresh_->AllSystemServerJars().size() - 2)
435 .WillRepeatedly(Return(0));
Jiakai Zhangccbcfb12021-08-23 15:10:35 +0000436
Victor Hsieh7b798142022-01-18 15:13:14 -0800437 EXPECT_CALL(
438 *mock_exec_utils_,
439 DoExecAndReturnCode(AllOf(Contains(Concatenate({"--dex-file=", location_provider_jar_})),
440 Not(Contains(HasSubstr("--profile-file-fd="))),
441 Contains("--compiler-filter=verify"))))
442 .WillOnce(Return(0));
443 EXPECT_CALL(
444 *mock_exec_utils_,
445 DoExecAndReturnCode(AllOf(Contains(Concatenate({"--dex-file=", services_jar_})),
446 Not(Contains(HasSubstr("--profile-file-fd="))),
447 Contains("--compiler-filter=verify"))))
448 .WillOnce(Return(0));
449 EXPECT_EQ(
450 odrefresh_->Compile(*metrics_,
451 CompilationOptions{
452 .system_server_jars_to_compile = odrefresh_->AllSystemServerJars(),
453 }),
454 ExitCode::kCompilationSuccess);
Orion Hodson4c3ade62021-02-10 14:07:10 +0000455}
456
Victor Hsieh73c4f792021-10-01 18:13:52 +0000457TEST_F(OdRefreshTest, OutputFilesAndIsa) {
Victor Hsieh7b798142022-01-18 15:13:14 -0800458 EXPECT_CALL(
459 *mock_exec_utils_,
460 DoExecAndReturnCode(AllOf(Contains("--instruction-set=x86_64"),
461 Contains(HasSubstr("--image-fd=")),
462 Contains(HasSubstr("--output-vdex-fd=")),
463 Contains(HasSubstr("--oat-fd=")))))
Victor Hsieh73c4f792021-10-01 18:13:52 +0000464 .WillOnce(Return(0));
465
Victor Hsieh7b798142022-01-18 15:13:14 -0800466 EXPECT_CALL(
467 *mock_exec_utils_,
468 DoExecAndReturnCode(AllOf(Contains("--instruction-set=x86_64"),
469 Contains(HasSubstr("--app-image-fd=")),
470 Contains(HasSubstr("--output-vdex-fd=")),
471 Contains(HasSubstr("--oat-fd=")))))
472 .Times(odrefresh_->AllSystemServerJars().size())
Victor Hsieh73c4f792021-10-01 18:13:52 +0000473 .WillRepeatedly(Return(0));
474
Jiakai Zhang83c38e22021-11-03 20:54:55 +0000475 EXPECT_EQ(
Victor Hsieh7b798142022-01-18 15:13:14 -0800476 odrefresh_->Compile(*metrics_,
477 CompilationOptions{
478 .compile_boot_classpath_for_isas = {InstructionSet::kX86_64},
479 .system_server_jars_to_compile = odrefresh_->AllSystemServerJars(),
480 }),
Jiakai Zhang83c38e22021-11-03 20:54:55 +0000481 ExitCode::kCompilationSuccess);
Victor Hsieh73c4f792021-10-01 18:13:52 +0000482}
483
Victor Hsieh7b798142022-01-18 15:13:14 -0800484TEST_F(OdRefreshTest, CompileChoosesBootImage_OnData) {
485 // Boot image is on /data.
486 OdrArtifacts artifacts = OdrArtifacts::ForBootImage(dalvik_cache_dir_ + "/x86_64/boot.art");
487 auto file1 = ScopedCreateEmptyFile(artifacts.ImagePath());
488 auto file2 = ScopedCreateEmptyFile(artifacts.VdexPath());
489 auto file3 = ScopedCreateEmptyFile(artifacts.OatPath());
Victor Hsieh73c4f792021-10-01 18:13:52 +0000490
Victor Hsieh7b798142022-01-18 15:13:14 -0800491 EXPECT_CALL(
492 *mock_exec_utils_,
493 DoExecAndReturnCode(AllOf(
494 Contains(Concatenate({"--boot-image=", dalvik_cache_dir_, "/boot.art"})),
495 Contains(FlagContains("-Xbootclasspathimagefds:", FdOf(artifacts.ImagePath()))),
496 Contains(FlagContains("-Xbootclasspathvdexfds:", FdOf(artifacts.VdexPath()))),
497 Contains(FlagContains("-Xbootclasspathoatfds:", FdOf(artifacts.OatPath()))))))
498 .Times(odrefresh_->AllSystemServerJars().size())
499 .WillRepeatedly(Return(0));
500 EXPECT_EQ(
501 odrefresh_->Compile(*metrics_,
502 CompilationOptions{
503 .system_server_jars_to_compile = odrefresh_->AllSystemServerJars(),
504 }),
505 ExitCode::kCompilationSuccess);
506}
Jiakai Zhang4e6cc732021-09-17 04:46:24 +0000507
Victor Hsieh7b798142022-01-18 15:13:14 -0800508TEST_F(OdRefreshTest, CompileChoosesBootImage_OnSystem) {
509 // Boot image is on /system.
510 OdrArtifacts artifacts =
511 OdrArtifacts::ForBootImage(framework_dir_ + "/x86_64/boot-framework.art");
512 auto file1 = ScopedCreateEmptyFile(artifacts.ImagePath());
513 auto file2 = ScopedCreateEmptyFile(artifacts.VdexPath());
514 auto file3 = ScopedCreateEmptyFile(artifacts.OatPath());
Jiakai Zhang4e6cc732021-09-17 04:46:24 +0000515
Victor Hsieh7b798142022-01-18 15:13:14 -0800516 // Ignore the execution for compiling the boot classpath.
517 EXPECT_CALL(
518 *mock_exec_utils_, DoExecAndReturnCode(Contains(HasSubstr("--image-fd="))))
519 .WillRepeatedly(Return(0));
520 EXPECT_CALL(
521 *mock_exec_utils_,
522 DoExecAndReturnCode(AllOf(
523 Contains(Concatenate({"--boot-image=",
524 GetPrebuiltPrimaryBootImageDir(), "/boot.art:",
525 framework_dir_, "/boot-framework.art"})),
526 Contains(FlagContains("-Xbootclasspathimagefds:", FdOf(artifacts.ImagePath()))),
527 Contains(FlagContains("-Xbootclasspathvdexfds:", FdOf(artifacts.VdexPath()))),
528 Contains(FlagContains("-Xbootclasspathoatfds:", FdOf(artifacts.OatPath()))))))
529 .Times(odrefresh_->AllSystemServerJars().size())
530 .WillRepeatedly(Return(0));
531 EXPECT_EQ(
532 odrefresh_->Compile(*metrics_,
533 CompilationOptions{
534 .system_server_jars_to_compile = odrefresh_->AllSystemServerJars(),
535 }),
536 ExitCode::kCompilationSuccess);
Jiakai Zhang4e6cc732021-09-17 04:46:24 +0000537}
538
Orion Hodson4c3ade62021-02-10 14:07:10 +0000539} // namespace odrefresh
540} // namespace art