| Orion Hodson | 4c3ade6 | 2021-02-10 14:07:10 +0000 | [diff] [blame] | 1 | /* |
| 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 Zhang | b91dad2 | 2021-08-16 03:20:07 +0000 | [diff] [blame] | 17 | #include "odrefresh.h" |
| Orion Hodson | 4c3ade6 | 2021-02-10 14:07:10 +0000 | [diff] [blame] | 18 | |
| Jiakai Zhang | b91dad2 | 2021-08-16 03:20:07 +0000 | [diff] [blame] | 19 | #include <functional> |
| 20 | #include <memory> |
| 21 | #include <string_view> |
| 22 | |
| 23 | #include "android-base/properties.h" |
| 24 | #include "android-base/scopeguard.h" |
| 25 | #include "android-base/strings.h" |
| 26 | #include "arch/instruction_set.h" |
| Orion Hodson | 4c3ade6 | 2021-02-10 14:07:10 +0000 | [diff] [blame] | 27 | #include "base/common_art_test.h" |
| 28 | #include "base/file_utils.h" |
| Jiakai Zhang | b91dad2 | 2021-08-16 03:20:07 +0000 | [diff] [blame] | 29 | #include "exec_utils.h" |
| 30 | #include "gmock/gmock.h" |
| 31 | #include "gtest/gtest.h" |
| 32 | #include "odr_common.h" |
| 33 | #include "odr_config.h" |
| 34 | #include "odr_fs_utils.h" |
| 35 | #include "odr_metrics.h" |
| 36 | #include "odrefresh/odrefresh.h" |
| Orion Hodson | 4c3ade6 | 2021-02-10 14:07:10 +0000 | [diff] [blame] | 37 | |
| 38 | namespace art { |
| 39 | namespace odrefresh { |
| 40 | |
| Jiakai Zhang | b91dad2 | 2021-08-16 03:20:07 +0000 | [diff] [blame] | 41 | using ::testing::AllOf; |
| 42 | using ::testing::Contains; |
| 43 | using ::testing::HasSubstr; |
| 44 | using ::testing::Not; |
| 45 | using ::testing::Return; |
| 46 | |
| 47 | constexpr int kReplace = 1; |
| 48 | |
| 49 | void CreateEmptyFile(const std::string& name) { |
| 50 | File* file = OS::CreateEmptyFile(name.c_str()); |
| 51 | ASSERT_TRUE(file != nullptr); |
| 52 | file->Release(); |
| 53 | delete file; |
| 54 | } |
| 55 | |
| 56 | android::base::ScopeGuard<std::function<void()>> ScopedSetProperty(const std::string& key, |
| 57 | const std::string& value) { |
| 58 | std::string old_value = android::base::GetProperty(key, /*default_value=*/{}); |
| 59 | android::base::SetProperty(key, value); |
| 60 | return android::base::ScopeGuard([=]() { android::base::SetProperty(key, old_value); }); |
| 61 | } |
| 62 | |
| 63 | class MockExecUtils : public ExecUtils { |
| 64 | public: |
| 65 | // A workaround to avoid MOCK_METHOD on a method with an `std::string*` parameter, which will lead |
| 66 | // to a conflict between gmock and android-base/logging.h (b/132668253). |
| 67 | int ExecAndReturnCode(std::vector<std::string>& arg_vector, |
| 68 | time_t, |
| 69 | bool*, |
| 70 | std::string*) const override { |
| 71 | return DoExecAndReturnCode(arg_vector); |
| 72 | } |
| 73 | |
| 74 | MOCK_METHOD(int, DoExecAndReturnCode, (std::vector<std::string> & arg_vector), (const)); |
| 75 | }; |
| 76 | |
| 77 | class OdRefreshTest : public CommonArtTest { |
| 78 | public: |
| 79 | OdRefreshTest() : config_("odrefresh") {} |
| 80 | |
| 81 | protected: |
| 82 | void SetUp() override { |
| 83 | CommonArtTest::SetUp(); |
| 84 | |
| 85 | temp_dir_ = std::make_unique<ScratchDir>(); |
| 86 | std::string_view temp_dir_path = temp_dir_->GetPath(); |
| 87 | android::base::ConsumeSuffix(&temp_dir_path, "/"); |
| 88 | |
| 89 | std::string android_root_path = Concatenate({temp_dir_path, "/system"}); |
| 90 | ASSERT_TRUE(EnsureDirectoryExists(android_root_path)); |
| 91 | android_root_env_ = std::make_unique<ScopedUnsetEnvironmentVariable>("ANDROID_ROOT"); |
| 92 | setenv("ANDROID_ROOT", android_root_path.c_str(), kReplace); |
| 93 | |
| 94 | std::string android_art_root_path = Concatenate({temp_dir_path, "/apex/com.android.art"}); |
| 95 | ASSERT_TRUE(EnsureDirectoryExists(android_art_root_path)); |
| 96 | android_art_root_env_ = std::make_unique<ScopedUnsetEnvironmentVariable>("ANDROID_ART_ROOT"); |
| 97 | setenv("ANDROID_ART_ROOT", android_art_root_path.c_str(), kReplace); |
| 98 | |
| 99 | std::string art_apex_data_path = Concatenate({temp_dir_path, kArtApexDataDefaultPath}); |
| 100 | ASSERT_TRUE(EnsureDirectoryExists(art_apex_data_path)); |
| 101 | art_apex_data_env_ = std::make_unique<ScopedUnsetEnvironmentVariable>("ART_APEX_DATA"); |
| 102 | setenv("ART_APEX_DATA", art_apex_data_path.c_str(), kReplace); |
| 103 | |
| 104 | std::string dalvik_cache_dir = art_apex_data_path + "/dalvik-cache"; |
| 105 | ASSERT_TRUE(EnsureDirectoryExists(dalvik_cache_dir)); |
| 106 | |
| 107 | std::string framework_dir = android_root_path + "/framework"; |
| 108 | framework_jar_ = framework_dir + "/framework.jar"; |
| 109 | location_provider_jar_ = framework_dir + "/com.android.location.provider.jar"; |
| 110 | services_jar_ = framework_dir + "/services.jar"; |
| 111 | std::string services_jar_prof = framework_dir + "/services.jar.prof"; |
| 112 | std::string javalib_dir = android_art_root_path + "/javalib"; |
| 113 | std::string boot_art = javalib_dir + "/boot.art"; |
| 114 | |
| 115 | // Create placeholder files. |
| 116 | ASSERT_TRUE(EnsureDirectoryExists(framework_dir)); |
| 117 | CreateEmptyFile(framework_jar_); |
| 118 | CreateEmptyFile(location_provider_jar_); |
| 119 | CreateEmptyFile(services_jar_); |
| 120 | CreateEmptyFile(services_jar_prof); |
| 121 | ASSERT_TRUE(EnsureDirectoryExists(javalib_dir)); |
| 122 | CreateEmptyFile(boot_art); |
| 123 | |
| 124 | config_.SetApexInfoListFile(Concatenate({temp_dir_path, "/apex-info-list.xml"})); |
| 125 | config_.SetArtBinDir(Concatenate({temp_dir_path, "/bin"})); |
| 126 | config_.SetBootClasspath(framework_jar_); |
| 127 | config_.SetDex2oatBootclasspath(framework_jar_); |
| 128 | config_.SetSystemServerClasspath(Concatenate({location_provider_jar_, ":", services_jar_})); |
| 129 | config_.SetIsa(InstructionSet::kX86_64); |
| 130 | config_.SetZygoteKind(ZygoteKind::kZygote64_32); |
| 131 | |
| 132 | std::string staging_dir = dalvik_cache_dir + "/staging"; |
| 133 | ASSERT_TRUE(EnsureDirectoryExists(staging_dir)); |
| 134 | config_.SetStagingDir(staging_dir); |
| 135 | |
| 136 | auto mock_exec_utils = std::make_unique<MockExecUtils>(); |
| 137 | mock_exec_utils_ = mock_exec_utils.get(); |
| 138 | |
| 139 | metrics_ = std::make_unique<OdrMetrics>(dalvik_cache_dir); |
| 140 | odrefresh_ = std::make_unique<OnDeviceRefresh>( |
| 141 | config_, dalvik_cache_dir + "/cache-info.xml", std::move(mock_exec_utils)); |
| 142 | } |
| 143 | |
| 144 | void TearDown() override { |
| 145 | metrics_.reset(); |
| 146 | temp_dir_.reset(); |
| 147 | android_root_env_.reset(); |
| 148 | android_art_root_env_.reset(); |
| 149 | art_apex_data_env_.reset(); |
| 150 | |
| 151 | CommonArtTest::TearDown(); |
| 152 | } |
| 153 | |
| 154 | std::unique_ptr<ScratchDir> temp_dir_; |
| 155 | std::unique_ptr<ScopedUnsetEnvironmentVariable> android_root_env_; |
| 156 | std::unique_ptr<ScopedUnsetEnvironmentVariable> android_art_root_env_; |
| 157 | std::unique_ptr<ScopedUnsetEnvironmentVariable> art_apex_data_env_; |
| 158 | OdrConfig config_; |
| 159 | MockExecUtils* mock_exec_utils_; |
| 160 | std::unique_ptr<OdrMetrics> metrics_; |
| 161 | std::unique_ptr<OnDeviceRefresh> odrefresh_; |
| 162 | std::string framework_jar_; |
| 163 | std::string location_provider_jar_; |
| 164 | std::string services_jar_; |
| 165 | }; |
| 166 | |
| 167 | TEST_F(OdRefreshTest, OdrefreshArtifactDirectory) { |
| 168 | // odrefresh.h defines kOdrefreshArtifactDirectory for external callers of odrefresh. This is |
| 169 | // where compilation artifacts end up. |
| 170 | ScopedUnsetEnvironmentVariable no_env("ART_APEX_DATA"); |
| 171 | EXPECT_EQ(kOdrefreshArtifactDirectory, GetArtApexData() + "/dalvik-cache"); |
| 172 | } |
| 173 | |
| 174 | TEST_F(OdRefreshTest, CompileSetsCompilerFilter) { |
| 175 | { |
| 176 | // Defaults to "speed". |
| 177 | EXPECT_CALL( |
| 178 | *mock_exec_utils_, |
| 179 | DoExecAndReturnCode(AllOf(Contains(Concatenate({"--dex-file=", location_provider_jar_})), |
| 180 | Not(Contains(HasSubstr("--profile-file-fd="))), |
| 181 | Contains("--compiler-filter=speed")))) |
| 182 | .WillOnce(Return(0)); |
| 183 | EXPECT_CALL(*mock_exec_utils_, |
| 184 | DoExecAndReturnCode(AllOf(Contains(Concatenate({"--dex-file=", services_jar_})), |
| 185 | Not(Contains(HasSubstr("--profile-file-fd="))), |
| 186 | Contains("--compiler-filter=speed")))) |
| 187 | .WillOnce(Return(0)); |
| 188 | EXPECT_EQ(odrefresh_->Compile( |
| 189 | *metrics_, /*compile_boot_extensions=*/{}, /*compile_system_server=*/true), |
| 190 | ExitCode::kCompilationSuccess); |
| 191 | } |
| 192 | |
| 193 | { |
| 194 | auto guard = ScopedSetProperty("dalvik.vm.systemservercompilerfilter", "speed-profile"); |
| 195 | // services.jar has a profile, while location.provider.jar does not. |
| 196 | EXPECT_CALL( |
| 197 | *mock_exec_utils_, |
| 198 | DoExecAndReturnCode(AllOf(Contains(Concatenate({"--dex-file=", location_provider_jar_})), |
| 199 | Not(Contains(HasSubstr("--profile-file-fd="))), |
| 200 | Contains("--compiler-filter=speed")))) |
| 201 | .WillOnce(Return(0)); |
| 202 | EXPECT_CALL(*mock_exec_utils_, |
| 203 | DoExecAndReturnCode(AllOf(Contains(Concatenate({"--dex-file=", services_jar_})), |
| 204 | Contains(HasSubstr("--profile-file-fd=")), |
| 205 | Contains("--compiler-filter=speed-profile")))) |
| 206 | .WillOnce(Return(0)); |
| 207 | EXPECT_EQ(odrefresh_->Compile( |
| 208 | *metrics_, /*compile_boot_extensions=*/{}, /*compile_system_server=*/true), |
| 209 | ExitCode::kCompilationSuccess); |
| 210 | } |
| 211 | |
| 212 | { |
| 213 | auto guard = ScopedSetProperty("dalvik.vm.systemservercompilerfilter", "verify"); |
| 214 | EXPECT_CALL( |
| 215 | *mock_exec_utils_, |
| 216 | DoExecAndReturnCode(AllOf(Contains(Concatenate({"--dex-file=", location_provider_jar_})), |
| 217 | Not(Contains(HasSubstr("--profile-file-fd="))), |
| 218 | Contains("--compiler-filter=verify")))) |
| 219 | .WillOnce(Return(0)); |
| 220 | EXPECT_CALL(*mock_exec_utils_, |
| 221 | DoExecAndReturnCode(AllOf(Contains(Concatenate({"--dex-file=", services_jar_})), |
| 222 | Not(Contains(HasSubstr("--profile-file-fd="))), |
| 223 | Contains("--compiler-filter=verify")))) |
| 224 | .WillOnce(Return(0)); |
| 225 | EXPECT_EQ(odrefresh_->Compile( |
| 226 | *metrics_, /*compile_boot_extensions=*/{}, /*compile_system_server=*/true), |
| 227 | ExitCode::kCompilationSuccess); |
| 228 | } |
| Orion Hodson | 4c3ade6 | 2021-02-10 14:07:10 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | } // namespace odrefresh |
| 232 | } // namespace art |