Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
Andreas Gampe | 7adeda8 | 2016-07-25 08:27:35 -0700 | [diff] [blame] | 17 | #include <regex> |
| 18 | #include <sstream> |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 19 | #include <string> |
| 20 | #include <vector> |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 21 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 22 | #include <sys/wait.h> |
| 23 | #include <unistd.h> |
| 24 | |
| 25 | #include "android-base/stringprintf.h" |
| 26 | |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 27 | #include "common_runtime_test.h" |
| 28 | |
| 29 | #include "base/logging.h" |
| 30 | #include "base/macros.h" |
Jeff Hao | 608f2ce | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 31 | #include "dex_file-inl.h" |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 32 | #include "dex2oat_environment_test.h" |
Calin Juravle | 33083d6 | 2017-01-18 15:29:12 -0800 | [diff] [blame] | 33 | #include "jit/profile_compilation_info.h" |
Andreas Gampe | 67f0282 | 2016-06-24 21:05:23 -0700 | [diff] [blame] | 34 | #include "oat.h" |
| 35 | #include "oat_file.h" |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 36 | #include "utils.h" |
| 37 | |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 38 | namespace art { |
| 39 | |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 40 | using android::base::StringPrintf; |
| 41 | |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 42 | class Dex2oatTest : public Dex2oatEnvironmentTest { |
| 43 | public: |
| 44 | virtual void TearDown() OVERRIDE { |
| 45 | Dex2oatEnvironmentTest::TearDown(); |
| 46 | |
| 47 | output_ = ""; |
| 48 | error_msg_ = ""; |
| 49 | success_ = false; |
| 50 | } |
| 51 | |
| 52 | protected: |
| 53 | void GenerateOdexForTest(const std::string& dex_location, |
| 54 | const std::string& odex_location, |
| 55 | CompilerFilter::Filter filter, |
| 56 | const std::vector<std::string>& extra_args = {}, |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 57 | bool expect_success = true, |
| 58 | bool use_fd = false) { |
| 59 | std::unique_ptr<File> oat_file; |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 60 | std::vector<std::string> args; |
| 61 | args.push_back("--dex-file=" + dex_location); |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 62 | if (use_fd) { |
| 63 | oat_file.reset(OS::CreateEmptyFile(odex_location.c_str())); |
| 64 | CHECK(oat_file != nullptr) << odex_location; |
| 65 | args.push_back("--oat-fd=" + std::to_string(oat_file->Fd())); |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 66 | args.push_back("--oat-location=" + odex_location); |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 67 | } else { |
| 68 | args.push_back("--oat-file=" + odex_location); |
| 69 | } |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 70 | args.push_back("--compiler-filter=" + CompilerFilter::NameOfFilter(filter)); |
| 71 | args.push_back("--runtime-arg"); |
| 72 | args.push_back("-Xnorelocate"); |
| 73 | |
| 74 | args.insert(args.end(), extra_args.begin(), extra_args.end()); |
| 75 | |
| 76 | std::string error_msg; |
| 77 | bool success = Dex2Oat(args, &error_msg); |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 78 | if (oat_file != nullptr) { |
| 79 | ASSERT_EQ(oat_file->FlushClose(), 0) << "Could not flush and close oat file"; |
| 80 | } |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 81 | |
| 82 | if (expect_success) { |
Andreas Gampe | 2e8a256 | 2017-01-18 20:39:02 -0800 | [diff] [blame] | 83 | ASSERT_TRUE(success) << error_msg << std::endl << output_; |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 84 | |
| 85 | // Verify the odex file was generated as expected. |
| 86 | std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(), |
| 87 | odex_location.c_str(), |
| 88 | nullptr, |
| 89 | nullptr, |
| 90 | false, |
| 91 | /*low_4gb*/false, |
| 92 | dex_location.c_str(), |
| 93 | &error_msg)); |
| 94 | ASSERT_TRUE(odex_file.get() != nullptr) << error_msg; |
| 95 | |
| 96 | CheckFilter(filter, odex_file->GetCompilerFilter()); |
| 97 | } else { |
| 98 | ASSERT_FALSE(success) << output_; |
| 99 | |
| 100 | error_msg_ = error_msg; |
| 101 | |
| 102 | // Verify there's no loadable odex file. |
| 103 | std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(), |
| 104 | odex_location.c_str(), |
| 105 | nullptr, |
| 106 | nullptr, |
| 107 | false, |
| 108 | /*low_4gb*/false, |
| 109 | dex_location.c_str(), |
| 110 | &error_msg)); |
| 111 | ASSERT_TRUE(odex_file.get() == nullptr); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | // Check the input compiler filter against the generated oat file's filter. Mayb be overridden |
| 116 | // in subclasses when equality is not expected. |
| 117 | virtual void CheckFilter(CompilerFilter::Filter expected, CompilerFilter::Filter actual) { |
| 118 | EXPECT_EQ(expected, actual); |
| 119 | } |
| 120 | |
| 121 | bool Dex2Oat(const std::vector<std::string>& dex2oat_args, std::string* error_msg) { |
| 122 | Runtime* runtime = Runtime::Current(); |
| 123 | |
| 124 | const std::vector<gc::space::ImageSpace*>& image_spaces = |
| 125 | runtime->GetHeap()->GetBootImageSpaces(); |
| 126 | if (image_spaces.empty()) { |
| 127 | *error_msg = "No image location found for Dex2Oat."; |
| 128 | return false; |
| 129 | } |
| 130 | std::string image_location = image_spaces[0]->GetImageLocation(); |
| 131 | |
| 132 | std::vector<std::string> argv; |
| 133 | argv.push_back(runtime->GetCompilerExecutable()); |
| 134 | argv.push_back("--runtime-arg"); |
| 135 | argv.push_back("-classpath"); |
| 136 | argv.push_back("--runtime-arg"); |
| 137 | std::string class_path = runtime->GetClassPathString(); |
| 138 | if (class_path == "") { |
| 139 | class_path = OatFile::kSpecialSharedLibrary; |
| 140 | } |
| 141 | argv.push_back(class_path); |
Nicolas Geoffray | 433b79a | 2017-01-30 20:54:45 +0000 | [diff] [blame] | 142 | if (runtime->IsJavaDebuggable()) { |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 143 | argv.push_back("--debuggable"); |
| 144 | } |
| 145 | runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv); |
| 146 | |
| 147 | if (!runtime->IsVerificationEnabled()) { |
| 148 | argv.push_back("--compiler-filter=verify-none"); |
| 149 | } |
| 150 | |
| 151 | if (runtime->MustRelocateIfPossible()) { |
| 152 | argv.push_back("--runtime-arg"); |
| 153 | argv.push_back("-Xrelocate"); |
| 154 | } else { |
| 155 | argv.push_back("--runtime-arg"); |
| 156 | argv.push_back("-Xnorelocate"); |
| 157 | } |
| 158 | |
| 159 | if (!kIsTargetBuild) { |
| 160 | argv.push_back("--host"); |
| 161 | } |
| 162 | |
| 163 | argv.push_back("--boot-image=" + image_location); |
| 164 | |
| 165 | std::vector<std::string> compiler_options = runtime->GetCompilerOptions(); |
| 166 | argv.insert(argv.end(), compiler_options.begin(), compiler_options.end()); |
| 167 | |
| 168 | argv.insert(argv.end(), dex2oat_args.begin(), dex2oat_args.end()); |
| 169 | |
| 170 | // We must set --android-root. |
| 171 | const char* android_root = getenv("ANDROID_ROOT"); |
| 172 | CHECK(android_root != nullptr); |
| 173 | argv.push_back("--android-root=" + std::string(android_root)); |
| 174 | |
Nicolas Geoffray | 56fe0f0 | 2016-06-30 15:07:46 +0100 | [diff] [blame] | 175 | int link[2]; |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 176 | |
Nicolas Geoffray | 56fe0f0 | 2016-06-30 15:07:46 +0100 | [diff] [blame] | 177 | if (pipe(link) == -1) { |
| 178 | return false; |
| 179 | } |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 180 | |
Nicolas Geoffray | 56fe0f0 | 2016-06-30 15:07:46 +0100 | [diff] [blame] | 181 | pid_t pid = fork(); |
| 182 | if (pid == -1) { |
| 183 | return false; |
| 184 | } |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 185 | |
Nicolas Geoffray | 56fe0f0 | 2016-06-30 15:07:46 +0100 | [diff] [blame] | 186 | if (pid == 0) { |
| 187 | // We need dex2oat to actually log things. |
| 188 | setenv("ANDROID_LOG_TAGS", "*:d", 1); |
| 189 | dup2(link[1], STDERR_FILENO); |
| 190 | close(link[0]); |
| 191 | close(link[1]); |
| 192 | std::vector<const char*> c_args; |
| 193 | for (const std::string& str : argv) { |
| 194 | c_args.push_back(str.c_str()); |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 195 | } |
Nicolas Geoffray | 56fe0f0 | 2016-06-30 15:07:46 +0100 | [diff] [blame] | 196 | c_args.push_back(nullptr); |
| 197 | execv(c_args[0], const_cast<char* const*>(c_args.data())); |
| 198 | exit(1); |
| 199 | } else { |
| 200 | close(link[1]); |
| 201 | char buffer[128]; |
| 202 | memset(buffer, 0, 128); |
| 203 | ssize_t bytes_read = 0; |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 204 | |
Nicolas Geoffray | 56fe0f0 | 2016-06-30 15:07:46 +0100 | [diff] [blame] | 205 | while (TEMP_FAILURE_RETRY(bytes_read = read(link[0], buffer, 128)) > 0) { |
| 206 | output_ += std::string(buffer, bytes_read); |
| 207 | } |
| 208 | close(link[0]); |
| 209 | int status = 0; |
| 210 | if (waitpid(pid, &status, 0) != -1) { |
| 211 | success_ = (status == 0); |
| 212 | } |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 213 | } |
| 214 | return success_; |
| 215 | } |
| 216 | |
| 217 | std::string output_ = ""; |
| 218 | std::string error_msg_ = ""; |
| 219 | bool success_ = false; |
| 220 | }; |
| 221 | |
| 222 | class Dex2oatSwapTest : public Dex2oatTest { |
| 223 | protected: |
| 224 | void RunTest(bool use_fd, bool expect_use, const std::vector<std::string>& extra_args = {}) { |
| 225 | std::string dex_location = GetScratchDir() + "/Dex2OatSwapTest.jar"; |
| 226 | std::string odex_location = GetOdexDir() + "/Dex2OatSwapTest.odex"; |
| 227 | |
Andreas Gampe | 7adeda8 | 2016-07-25 08:27:35 -0700 | [diff] [blame] | 228 | Copy(GetTestDexFileName(), dex_location); |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 229 | |
| 230 | std::vector<std::string> copy(extra_args); |
| 231 | |
| 232 | std::unique_ptr<ScratchFile> sf; |
| 233 | if (use_fd) { |
| 234 | sf.reset(new ScratchFile()); |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 235 | copy.push_back(android::base::StringPrintf("--swap-fd=%d", sf->GetFd())); |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 236 | } else { |
| 237 | std::string swap_location = GetOdexDir() + "/Dex2OatSwapTest.odex.swap"; |
| 238 | copy.push_back("--swap-file=" + swap_location); |
| 239 | } |
| 240 | GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed, copy); |
| 241 | |
| 242 | CheckValidity(); |
| 243 | ASSERT_TRUE(success_); |
| 244 | CheckResult(expect_use); |
| 245 | } |
| 246 | |
Andreas Gampe | 7adeda8 | 2016-07-25 08:27:35 -0700 | [diff] [blame] | 247 | virtual std::string GetTestDexFileName() { |
Vladimir Marko | 1535770 | 2017-02-09 10:37:31 +0000 | [diff] [blame] | 248 | return Dex2oatEnvironmentTest::GetTestDexFileName("VerifierDeps"); |
Andreas Gampe | 7adeda8 | 2016-07-25 08:27:35 -0700 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | virtual void CheckResult(bool expect_use) { |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 252 | if (kIsTargetBuild) { |
| 253 | CheckTargetResult(expect_use); |
| 254 | } else { |
| 255 | CheckHostResult(expect_use); |
| 256 | } |
| 257 | } |
| 258 | |
Andreas Gampe | 7adeda8 | 2016-07-25 08:27:35 -0700 | [diff] [blame] | 259 | virtual void CheckTargetResult(bool expect_use ATTRIBUTE_UNUSED) { |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 260 | // TODO: Ignore for now, as we won't capture any output (it goes to the logcat). We may do |
| 261 | // something for variants with file descriptor where we can control the lifetime of |
| 262 | // the swap file and thus take a look at it. |
| 263 | } |
| 264 | |
Andreas Gampe | 7adeda8 | 2016-07-25 08:27:35 -0700 | [diff] [blame] | 265 | virtual void CheckHostResult(bool expect_use) { |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 266 | if (!kIsTargetBuild) { |
| 267 | if (expect_use) { |
| 268 | EXPECT_NE(output_.find("Large app, accepted running with swap."), std::string::npos) |
| 269 | << output_; |
| 270 | } else { |
| 271 | EXPECT_EQ(output_.find("Large app, accepted running with swap."), std::string::npos) |
| 272 | << output_; |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | // Check whether the dex2oat run was really successful. |
Andreas Gampe | 7adeda8 | 2016-07-25 08:27:35 -0700 | [diff] [blame] | 278 | virtual void CheckValidity() { |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 279 | if (kIsTargetBuild) { |
| 280 | CheckTargetValidity(); |
| 281 | } else { |
| 282 | CheckHostValidity(); |
| 283 | } |
| 284 | } |
| 285 | |
Andreas Gampe | 7adeda8 | 2016-07-25 08:27:35 -0700 | [diff] [blame] | 286 | virtual void CheckTargetValidity() { |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 287 | // TODO: Ignore for now, as we won't capture any output (it goes to the logcat). We may do |
| 288 | // something for variants with file descriptor where we can control the lifetime of |
| 289 | // the swap file and thus take a look at it. |
| 290 | } |
| 291 | |
| 292 | // On the host, we can get the dex2oat output. Here, look for "dex2oat took." |
Andreas Gampe | 7adeda8 | 2016-07-25 08:27:35 -0700 | [diff] [blame] | 293 | virtual void CheckHostValidity() { |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 294 | EXPECT_NE(output_.find("dex2oat took"), std::string::npos) << output_; |
| 295 | } |
| 296 | }; |
| 297 | |
| 298 | TEST_F(Dex2oatSwapTest, DoNotUseSwapDefaultSingleSmall) { |
| 299 | RunTest(false /* use_fd */, false /* expect_use */); |
| 300 | RunTest(true /* use_fd */, false /* expect_use */); |
| 301 | } |
| 302 | |
| 303 | TEST_F(Dex2oatSwapTest, DoNotUseSwapSingle) { |
| 304 | RunTest(false /* use_fd */, false /* expect_use */, { "--swap-dex-size-threshold=0" }); |
| 305 | RunTest(true /* use_fd */, false /* expect_use */, { "--swap-dex-size-threshold=0" }); |
| 306 | } |
| 307 | |
| 308 | TEST_F(Dex2oatSwapTest, DoNotUseSwapSmall) { |
| 309 | RunTest(false /* use_fd */, false /* expect_use */, { "--swap-dex-count-threshold=0" }); |
| 310 | RunTest(true /* use_fd */, false /* expect_use */, { "--swap-dex-count-threshold=0" }); |
| 311 | } |
| 312 | |
| 313 | TEST_F(Dex2oatSwapTest, DoUseSwapSingleSmall) { |
| 314 | RunTest(false /* use_fd */, |
| 315 | true /* expect_use */, |
| 316 | { "--swap-dex-size-threshold=0", "--swap-dex-count-threshold=0" }); |
| 317 | RunTest(true /* use_fd */, |
| 318 | true /* expect_use */, |
| 319 | { "--swap-dex-size-threshold=0", "--swap-dex-count-threshold=0" }); |
| 320 | } |
| 321 | |
Andreas Gampe | 7adeda8 | 2016-07-25 08:27:35 -0700 | [diff] [blame] | 322 | class Dex2oatSwapUseTest : public Dex2oatSwapTest { |
| 323 | protected: |
| 324 | void CheckHostResult(bool expect_use) OVERRIDE { |
| 325 | if (!kIsTargetBuild) { |
| 326 | if (expect_use) { |
| 327 | EXPECT_NE(output_.find("Large app, accepted running with swap."), std::string::npos) |
| 328 | << output_; |
| 329 | } else { |
| 330 | EXPECT_EQ(output_.find("Large app, accepted running with swap."), std::string::npos) |
| 331 | << output_; |
| 332 | } |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | std::string GetTestDexFileName() OVERRIDE { |
| 337 | // Use Statics as it has a handful of functions. |
| 338 | return CommonRuntimeTest::GetTestDexFileName("Statics"); |
| 339 | } |
| 340 | |
| 341 | void GrabResult1() { |
| 342 | if (!kIsTargetBuild) { |
| 343 | native_alloc_1_ = ParseNativeAlloc(); |
| 344 | swap_1_ = ParseSwap(false /* expected */); |
| 345 | } else { |
| 346 | native_alloc_1_ = std::numeric_limits<size_t>::max(); |
| 347 | swap_1_ = 0; |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | void GrabResult2() { |
| 352 | if (!kIsTargetBuild) { |
| 353 | native_alloc_2_ = ParseNativeAlloc(); |
| 354 | swap_2_ = ParseSwap(true /* expected */); |
| 355 | } else { |
| 356 | native_alloc_2_ = 0; |
| 357 | swap_2_ = std::numeric_limits<size_t>::max(); |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | private: |
| 362 | size_t ParseNativeAlloc() { |
| 363 | std::regex native_alloc_regex("dex2oat took.*native alloc=[^ ]+ \\(([0-9]+)B\\)"); |
| 364 | std::smatch native_alloc_match; |
| 365 | bool found = std::regex_search(output_, native_alloc_match, native_alloc_regex); |
| 366 | if (!found) { |
| 367 | EXPECT_TRUE(found); |
| 368 | return 0; |
| 369 | } |
| 370 | if (native_alloc_match.size() != 2U) { |
| 371 | EXPECT_EQ(native_alloc_match.size(), 2U); |
| 372 | return 0; |
| 373 | } |
| 374 | |
| 375 | std::istringstream stream(native_alloc_match[1].str()); |
| 376 | size_t value; |
| 377 | stream >> value; |
| 378 | |
| 379 | return value; |
| 380 | } |
| 381 | |
| 382 | size_t ParseSwap(bool expected) { |
| 383 | std::regex swap_regex("dex2oat took[^\\n]+swap=[^ ]+ \\(([0-9]+)B\\)"); |
| 384 | std::smatch swap_match; |
| 385 | bool found = std::regex_search(output_, swap_match, swap_regex); |
| 386 | if (found != expected) { |
| 387 | EXPECT_EQ(expected, found); |
| 388 | return 0; |
| 389 | } |
| 390 | |
| 391 | if (!found) { |
| 392 | return 0; |
| 393 | } |
| 394 | |
| 395 | if (swap_match.size() != 2U) { |
| 396 | EXPECT_EQ(swap_match.size(), 2U); |
| 397 | return 0; |
| 398 | } |
| 399 | |
| 400 | std::istringstream stream(swap_match[1].str()); |
| 401 | size_t value; |
| 402 | stream >> value; |
| 403 | |
| 404 | return value; |
| 405 | } |
| 406 | |
| 407 | protected: |
| 408 | size_t native_alloc_1_; |
| 409 | size_t native_alloc_2_; |
| 410 | |
| 411 | size_t swap_1_; |
| 412 | size_t swap_2_; |
| 413 | }; |
| 414 | |
| 415 | TEST_F(Dex2oatSwapUseTest, CheckSwapUsage) { |
Vladimir Marko | 57070da | 2017-02-14 16:16:30 +0000 | [diff] [blame] | 416 | // The `native_alloc_2_ >= native_alloc_1_` assertion below may not |
Roland Levillain | 19772bf | 2017-02-16 11:28:10 +0000 | [diff] [blame] | 417 | // hold true on some x86 systems; disable this test while we |
| 418 | // investigate (b/29259363). |
| 419 | TEST_DISABLED_FOR_X86(); |
Vladimir Marko | 57070da | 2017-02-14 16:16:30 +0000 | [diff] [blame] | 420 | |
Andreas Gampe | 7adeda8 | 2016-07-25 08:27:35 -0700 | [diff] [blame] | 421 | RunTest(false /* use_fd */, |
| 422 | false /* expect_use */); |
| 423 | GrabResult1(); |
| 424 | std::string output_1 = output_; |
| 425 | |
| 426 | output_ = ""; |
| 427 | |
| 428 | RunTest(false /* use_fd */, |
| 429 | true /* expect_use */, |
| 430 | { "--swap-dex-size-threshold=0", "--swap-dex-count-threshold=0" }); |
| 431 | GrabResult2(); |
| 432 | std::string output_2 = output_; |
| 433 | |
| 434 | if (native_alloc_2_ >= native_alloc_1_ || swap_1_ >= swap_2_) { |
| 435 | EXPECT_LT(native_alloc_2_, native_alloc_1_); |
| 436 | EXPECT_LT(swap_1_, swap_2_); |
| 437 | |
| 438 | LOG(ERROR) << output_1; |
| 439 | LOG(ERROR) << output_2; |
| 440 | } |
| 441 | } |
| 442 | |
Andreas Gampe | 67f0282 | 2016-06-24 21:05:23 -0700 | [diff] [blame] | 443 | class Dex2oatVeryLargeTest : public Dex2oatTest { |
| 444 | protected: |
| 445 | void CheckFilter(CompilerFilter::Filter input ATTRIBUTE_UNUSED, |
| 446 | CompilerFilter::Filter result ATTRIBUTE_UNUSED) OVERRIDE { |
| 447 | // Ignore, we'll do our own checks. |
| 448 | } |
| 449 | |
| 450 | void RunTest(CompilerFilter::Filter filter, |
| 451 | bool expect_large, |
| 452 | const std::vector<std::string>& extra_args = {}) { |
| 453 | std::string dex_location = GetScratchDir() + "/DexNoOat.jar"; |
| 454 | std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex"; |
| 455 | |
| 456 | Copy(GetDexSrc1(), dex_location); |
| 457 | |
Andreas Gampe | ca620d7 | 2016-11-08 08:09:33 -0800 | [diff] [blame] | 458 | GenerateOdexForTest(dex_location, odex_location, filter, extra_args); |
Andreas Gampe | 67f0282 | 2016-06-24 21:05:23 -0700 | [diff] [blame] | 459 | |
| 460 | CheckValidity(); |
| 461 | ASSERT_TRUE(success_); |
| 462 | CheckResult(dex_location, odex_location, filter, expect_large); |
| 463 | } |
| 464 | |
| 465 | void CheckResult(const std::string& dex_location, |
| 466 | const std::string& odex_location, |
| 467 | CompilerFilter::Filter filter, |
| 468 | bool expect_large) { |
| 469 | // Host/target independent checks. |
| 470 | std::string error_msg; |
| 471 | std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(), |
| 472 | odex_location.c_str(), |
| 473 | nullptr, |
| 474 | nullptr, |
| 475 | false, |
| 476 | /*low_4gb*/false, |
| 477 | dex_location.c_str(), |
| 478 | &error_msg)); |
| 479 | ASSERT_TRUE(odex_file.get() != nullptr) << error_msg; |
| 480 | if (expect_large) { |
| 481 | // Note: we cannot check the following: |
| 482 | // EXPECT_TRUE(CompilerFilter::IsAsGoodAs(CompilerFilter::kVerifyAtRuntime, |
| 483 | // odex_file->GetCompilerFilter())); |
| 484 | // The reason is that the filter override currently happens when the dex files are |
| 485 | // loaded in dex2oat, which is after the oat file has been started. Thus, the header |
| 486 | // store cannot be changed, and the original filter is set in stone. |
| 487 | |
| 488 | for (const OatDexFile* oat_dex_file : odex_file->GetOatDexFiles()) { |
| 489 | std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg); |
| 490 | ASSERT_TRUE(dex_file != nullptr); |
| 491 | uint32_t class_def_count = dex_file->NumClassDefs(); |
| 492 | ASSERT_LT(class_def_count, std::numeric_limits<uint16_t>::max()); |
| 493 | for (uint16_t class_def_index = 0; class_def_index < class_def_count; ++class_def_index) { |
| 494 | OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index); |
| 495 | EXPECT_EQ(oat_class.GetType(), OatClassType::kOatClassNoneCompiled); |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | // If the input filter was "below," it should have been used. |
| 500 | if (!CompilerFilter::IsAsGoodAs(CompilerFilter::kVerifyAtRuntime, filter)) { |
| 501 | EXPECT_EQ(odex_file->GetCompilerFilter(), filter); |
| 502 | } |
| 503 | } else { |
| 504 | EXPECT_EQ(odex_file->GetCompilerFilter(), filter); |
| 505 | } |
| 506 | |
| 507 | // Host/target dependent checks. |
| 508 | if (kIsTargetBuild) { |
| 509 | CheckTargetResult(expect_large); |
| 510 | } else { |
| 511 | CheckHostResult(expect_large); |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | void CheckTargetResult(bool expect_large ATTRIBUTE_UNUSED) { |
| 516 | // TODO: Ignore for now. May do something for fd things. |
| 517 | } |
| 518 | |
| 519 | void CheckHostResult(bool expect_large) { |
| 520 | if (!kIsTargetBuild) { |
| 521 | if (expect_large) { |
| 522 | EXPECT_NE(output_.find("Very large app, downgrading to verify-at-runtime."), |
| 523 | std::string::npos) |
| 524 | << output_; |
| 525 | } else { |
| 526 | EXPECT_EQ(output_.find("Very large app, downgrading to verify-at-runtime."), |
| 527 | std::string::npos) |
| 528 | << output_; |
| 529 | } |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | // Check whether the dex2oat run was really successful. |
| 534 | void CheckValidity() { |
| 535 | if (kIsTargetBuild) { |
| 536 | CheckTargetValidity(); |
| 537 | } else { |
| 538 | CheckHostValidity(); |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | void CheckTargetValidity() { |
| 543 | // TODO: Ignore for now. |
| 544 | } |
| 545 | |
| 546 | // On the host, we can get the dex2oat output. Here, look for "dex2oat took." |
| 547 | void CheckHostValidity() { |
| 548 | EXPECT_NE(output_.find("dex2oat took"), std::string::npos) << output_; |
| 549 | } |
| 550 | }; |
| 551 | |
| 552 | TEST_F(Dex2oatVeryLargeTest, DontUseVeryLarge) { |
| 553 | RunTest(CompilerFilter::kVerifyNone, false); |
| 554 | RunTest(CompilerFilter::kVerifyAtRuntime, false); |
| 555 | RunTest(CompilerFilter::kInterpretOnly, false); |
| 556 | RunTest(CompilerFilter::kSpeed, false); |
| 557 | |
| 558 | RunTest(CompilerFilter::kVerifyNone, false, { "--very-large-app-threshold=1000000" }); |
| 559 | RunTest(CompilerFilter::kVerifyAtRuntime, false, { "--very-large-app-threshold=1000000" }); |
| 560 | RunTest(CompilerFilter::kInterpretOnly, false, { "--very-large-app-threshold=1000000" }); |
| 561 | RunTest(CompilerFilter::kSpeed, false, { "--very-large-app-threshold=1000000" }); |
| 562 | } |
| 563 | |
| 564 | TEST_F(Dex2oatVeryLargeTest, UseVeryLarge) { |
| 565 | RunTest(CompilerFilter::kVerifyNone, false, { "--very-large-app-threshold=100" }); |
| 566 | RunTest(CompilerFilter::kVerifyAtRuntime, false, { "--very-large-app-threshold=100" }); |
| 567 | RunTest(CompilerFilter::kInterpretOnly, true, { "--very-large-app-threshold=100" }); |
| 568 | RunTest(CompilerFilter::kSpeed, true, { "--very-large-app-threshold=100" }); |
| 569 | } |
| 570 | |
Mathieu Chartier | 97ab5e3 | 2017-02-22 13:35:44 -0800 | [diff] [blame] | 571 | // Regressin test for b/35665292. |
| 572 | TEST_F(Dex2oatVeryLargeTest, SpeedProfileNoProfile) { |
| 573 | // Test that dex2oat doesn't crash with speed-profile but no input profile. |
| 574 | RunTest(CompilerFilter::kSpeedProfile, false); |
| 575 | } |
| 576 | |
Jeff Hao | 608f2ce | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 577 | class Dex2oatLayoutTest : public Dex2oatTest { |
| 578 | protected: |
| 579 | void CheckFilter(CompilerFilter::Filter input ATTRIBUTE_UNUSED, |
| 580 | CompilerFilter::Filter result ATTRIBUTE_UNUSED) OVERRIDE { |
| 581 | // Ignore, we'll do our own checks. |
| 582 | } |
| 583 | |
Jeff Hao | 41fba6a | 2016-11-28 11:53:33 -0800 | [diff] [blame] | 584 | // Emits a profile with a single dex file with the given location and a single class index of 1. |
| 585 | void GenerateProfile(const std::string& test_profile, |
| 586 | const std::string& dex_location, |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 587 | size_t num_classes, |
Jeff Hao | 41fba6a | 2016-11-28 11:53:33 -0800 | [diff] [blame] | 588 | uint32_t checksum) { |
| 589 | int profile_test_fd = open(test_profile.c_str(), O_CREAT | O_TRUNC | O_WRONLY, 0644); |
| 590 | CHECK_GE(profile_test_fd, 0); |
| 591 | |
| 592 | ProfileCompilationInfo info; |
| 593 | std::string profile_key = ProfileCompilationInfo::GetProfileDexFileKey(dex_location); |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 594 | for (size_t i = 0; i < num_classes; ++i) { |
| 595 | info.AddClassIndex(profile_key, checksum, dex::TypeIndex(1 + i)); |
| 596 | } |
Jeff Hao | 41fba6a | 2016-11-28 11:53:33 -0800 | [diff] [blame] | 597 | bool result = info.Save(profile_test_fd); |
| 598 | close(profile_test_fd); |
| 599 | ASSERT_TRUE(result); |
| 600 | } |
| 601 | |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 602 | void CompileProfileOdex(const std::string& dex_location, |
| 603 | const std::string& odex_location, |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 604 | const std::string& app_image_file_name, |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 605 | bool use_fd, |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 606 | size_t num_profile_classes, |
Nicolas Geoffray | 97fa992 | 2017-03-09 13:13:25 +0000 | [diff] [blame] | 607 | const std::vector<std::string>& extra_args = {}, |
| 608 | bool expect_success = true) { |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 609 | const std::string profile_location = GetScratchDir() + "/primary.prof"; |
Jeff Hao | 41fba6a | 2016-11-28 11:53:33 -0800 | [diff] [blame] | 610 | const char* location = dex_location.c_str(); |
| 611 | std::string error_msg; |
| 612 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 613 | ASSERT_TRUE(DexFile::Open(location, location, true, &error_msg, &dex_files)); |
| 614 | EXPECT_EQ(dex_files.size(), 1U); |
| 615 | std::unique_ptr<const DexFile>& dex_file = dex_files[0]; |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 616 | GenerateProfile(profile_location, |
| 617 | dex_location, |
| 618 | num_profile_classes, |
| 619 | dex_file->GetLocationChecksum()); |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 620 | std::vector<std::string> copy(extra_args); |
| 621 | copy.push_back("--profile-file=" + profile_location); |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 622 | std::unique_ptr<File> app_image_file; |
| 623 | if (!app_image_file_name.empty()) { |
| 624 | if (use_fd) { |
| 625 | app_image_file.reset(OS::CreateEmptyFile(app_image_file_name.c_str())); |
| 626 | copy.push_back("--app-image-fd=" + std::to_string(app_image_file->Fd())); |
| 627 | } else { |
| 628 | copy.push_back("--app-image-file=" + app_image_file_name); |
| 629 | } |
| 630 | } |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 631 | GenerateOdexForTest(dex_location, |
| 632 | odex_location, |
| 633 | CompilerFilter::kSpeedProfile, |
| 634 | copy, |
Nicolas Geoffray | 97fa992 | 2017-03-09 13:13:25 +0000 | [diff] [blame] | 635 | expect_success, |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 636 | use_fd); |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 637 | if (app_image_file != nullptr) { |
| 638 | ASSERT_EQ(app_image_file->FlushCloseOrErase(), 0) << "Could not flush and close art file"; |
| 639 | } |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 640 | } |
Jeff Hao | 608f2ce | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 641 | |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 642 | uint64_t GetImageSize(const std::string& image_file_name) { |
| 643 | EXPECT_FALSE(image_file_name.empty()); |
| 644 | std::unique_ptr<File> file(OS::OpenFileForReading(image_file_name.c_str())); |
| 645 | CHECK(file != nullptr); |
| 646 | ImageHeader image_header; |
| 647 | const bool success = file->ReadFully(&image_header, sizeof(image_header)); |
| 648 | CHECK(success); |
| 649 | CHECK(image_header.IsValid()); |
| 650 | ReaderMutexLock mu(Thread::Current(), *Locks::mutator_lock_); |
| 651 | return image_header.GetImageSize(); |
| 652 | } |
| 653 | |
| 654 | void RunTest(bool app_image) { |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 655 | std::string dex_location = GetScratchDir() + "/DexNoOat.jar"; |
| 656 | std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex"; |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 657 | std::string app_image_file = app_image ? (GetOdexDir() + "/DexOdexNoOat.art"): ""; |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 658 | Copy(GetDexSrc2(), dex_location); |
| 659 | |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 660 | uint64_t image_file_empty_profile = 0; |
| 661 | if (app_image) { |
| 662 | CompileProfileOdex(dex_location, |
| 663 | odex_location, |
| 664 | app_image_file, |
| 665 | /* use_fd */ false, |
| 666 | /* num_profile_classes */ 0); |
| 667 | CheckValidity(); |
| 668 | ASSERT_TRUE(success_); |
| 669 | // Don't check the result since CheckResult relies on the class being in the profile. |
| 670 | image_file_empty_profile = GetImageSize(app_image_file); |
| 671 | EXPECT_GT(image_file_empty_profile, 0u); |
| 672 | } |
Jeff Hao | 608f2ce | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 673 | |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 674 | // Small profile. |
| 675 | CompileProfileOdex(dex_location, |
| 676 | odex_location, |
| 677 | app_image_file, |
| 678 | /* use_fd */ false, |
| 679 | /* num_profile_classes */ 1); |
Jeff Hao | 608f2ce | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 680 | CheckValidity(); |
| 681 | ASSERT_TRUE(success_); |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 682 | CheckResult(dex_location, odex_location, app_image_file); |
| 683 | |
| 684 | if (app_image) { |
| 685 | // Test that the profile made a difference by adding more classes. |
| 686 | const uint64_t image_file_small_profile = GetImageSize(app_image_file); |
| 687 | CHECK_LT(image_file_empty_profile, image_file_small_profile); |
| 688 | } |
Jeff Hao | 608f2ce | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 689 | } |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 690 | |
| 691 | void RunTestVDex() { |
| 692 | std::string dex_location = GetScratchDir() + "/DexNoOat.jar"; |
| 693 | std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex"; |
| 694 | std::string vdex_location = GetOdexDir() + "/DexOdexNoOat.vdex"; |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 695 | std::string app_image_file_name = GetOdexDir() + "/DexOdexNoOat.art"; |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 696 | Copy(GetDexSrc2(), dex_location); |
| 697 | |
| 698 | std::unique_ptr<File> vdex_file1(OS::CreateEmptyFile(vdex_location.c_str())); |
| 699 | CHECK(vdex_file1 != nullptr) << vdex_location; |
| 700 | ScratchFile vdex_file2; |
| 701 | { |
| 702 | std::string input_vdex = "--input-vdex-fd=-1"; |
| 703 | std::string output_vdex = StringPrintf("--output-vdex-fd=%d", vdex_file1->Fd()); |
| 704 | CompileProfileOdex(dex_location, |
| 705 | odex_location, |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 706 | app_image_file_name, |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 707 | /* use_fd */ true, |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 708 | /* num_profile_classes */ 1, |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 709 | { input_vdex, output_vdex }); |
| 710 | EXPECT_GT(vdex_file1->GetLength(), 0u); |
| 711 | } |
| 712 | { |
Nicolas Geoffray | 97fa992 | 2017-03-09 13:13:25 +0000 | [diff] [blame] | 713 | // Test that vdex and dexlayout fail gracefully. |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 714 | std::string input_vdex = StringPrintf("--input-vdex-fd=%d", vdex_file1->Fd()); |
| 715 | std::string output_vdex = StringPrintf("--output-vdex-fd=%d", vdex_file2.GetFd()); |
| 716 | CompileProfileOdex(dex_location, |
| 717 | odex_location, |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 718 | app_image_file_name, |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 719 | /* use_fd */ true, |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 720 | /* num_profile_classes */ 1, |
Nicolas Geoffray | 97fa992 | 2017-03-09 13:13:25 +0000 | [diff] [blame] | 721 | { input_vdex, output_vdex }, |
| 722 | /* expect_success */ false); |
| 723 | EXPECT_EQ(vdex_file2.GetFile()->GetLength(), 0u); |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 724 | } |
| 725 | ASSERT_EQ(vdex_file1->FlushCloseOrErase(), 0) << "Could not flush and close vdex file"; |
| 726 | CheckValidity(); |
Nicolas Geoffray | 97fa992 | 2017-03-09 13:13:25 +0000 | [diff] [blame] | 727 | ASSERT_FALSE(success_); |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 728 | } |
| 729 | |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 730 | void CheckResult(const std::string& dex_location, |
| 731 | const std::string& odex_location, |
| 732 | const std::string& app_image_file_name) { |
Jeff Hao | 608f2ce | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 733 | // Host/target independent checks. |
| 734 | std::string error_msg; |
| 735 | std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(), |
| 736 | odex_location.c_str(), |
| 737 | nullptr, |
| 738 | nullptr, |
| 739 | false, |
| 740 | /*low_4gb*/false, |
| 741 | dex_location.c_str(), |
| 742 | &error_msg)); |
| 743 | ASSERT_TRUE(odex_file.get() != nullptr) << error_msg; |
| 744 | |
Jeff Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 745 | const char* location = dex_location.c_str(); |
| 746 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 747 | ASSERT_TRUE(DexFile::Open(location, location, true, &error_msg, &dex_files)); |
| 748 | EXPECT_EQ(dex_files.size(), 1U); |
| 749 | std::unique_ptr<const DexFile>& old_dex_file = dex_files[0]; |
| 750 | |
Jeff Hao | 608f2ce | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 751 | for (const OatDexFile* oat_dex_file : odex_file->GetOatDexFiles()) { |
Jeff Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 752 | std::unique_ptr<const DexFile> new_dex_file = oat_dex_file->OpenDexFile(&error_msg); |
| 753 | ASSERT_TRUE(new_dex_file != nullptr); |
| 754 | uint32_t class_def_count = new_dex_file->NumClassDefs(); |
Jeff Hao | 608f2ce | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 755 | ASSERT_LT(class_def_count, std::numeric_limits<uint16_t>::max()); |
Jeff Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 756 | ASSERT_GE(class_def_count, 2U); |
| 757 | |
| 758 | // The new layout swaps the classes at indexes 0 and 1. |
| 759 | std::string old_class0 = old_dex_file->PrettyType(old_dex_file->GetClassDef(0).class_idx_); |
| 760 | std::string old_class1 = old_dex_file->PrettyType(old_dex_file->GetClassDef(1).class_idx_); |
| 761 | std::string new_class0 = new_dex_file->PrettyType(new_dex_file->GetClassDef(0).class_idx_); |
| 762 | std::string new_class1 = new_dex_file->PrettyType(new_dex_file->GetClassDef(1).class_idx_); |
| 763 | EXPECT_EQ(old_class0, new_class1); |
| 764 | EXPECT_EQ(old_class1, new_class0); |
Jeff Hao | 608f2ce | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 765 | } |
| 766 | |
Jeff Hao | c155b05 | 2017-01-17 17:43:29 -0800 | [diff] [blame] | 767 | EXPECT_EQ(odex_file->GetCompilerFilter(), CompilerFilter::kSpeedProfile); |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 768 | |
| 769 | if (!app_image_file_name.empty()) { |
| 770 | // Go peek at the image header to make sure it was large enough to contain the class. |
| 771 | std::unique_ptr<File> file(OS::OpenFileForReading(app_image_file_name.c_str())); |
| 772 | ImageHeader image_header; |
| 773 | bool success = file->ReadFully(&image_header, sizeof(image_header)); |
| 774 | ASSERT_TRUE(success); |
| 775 | ASSERT_TRUE(image_header.IsValid()); |
| 776 | EXPECT_GT(image_header.GetImageSection(ImageHeader::kSectionObjects).Size(), 0u); |
| 777 | } |
Jeff Hao | 608f2ce | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 778 | } |
| 779 | |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 780 | // Check whether the dex2oat run was really successful. |
| 781 | void CheckValidity() { |
| 782 | if (kIsTargetBuild) { |
| 783 | CheckTargetValidity(); |
| 784 | } else { |
| 785 | CheckHostValidity(); |
Jeff Hao | 608f2ce | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 786 | } |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 787 | } |
Jeff Hao | 608f2ce | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 788 | |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 789 | void CheckTargetValidity() { |
| 790 | // TODO: Ignore for now. |
| 791 | } |
Jeff Hao | 608f2ce | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 792 | |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 793 | // On the host, we can get the dex2oat output. Here, look for "dex2oat took." |
| 794 | void CheckHostValidity() { |
| 795 | EXPECT_NE(output_.find("dex2oat took"), std::string::npos) << output_; |
| 796 | } |
| 797 | }; |
Jeff Hao | 608f2ce | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 798 | |
| 799 | TEST_F(Dex2oatLayoutTest, TestLayout) { |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 800 | RunTest(/* app-image */ false); |
| 801 | } |
| 802 | |
| 803 | TEST_F(Dex2oatLayoutTest, TestLayoutAppImage) { |
| 804 | RunTest(/* app-image */ true); |
Jeff Hao | 608f2ce | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 805 | } |
| 806 | |
Mathieu Chartier | 8bc343b | 2017-03-01 15:20:30 -0800 | [diff] [blame] | 807 | TEST_F(Dex2oatLayoutTest, TestVdexLayout) { |
| 808 | RunTestVDex(); |
| 809 | } |
| 810 | |
Andreas Gampe | 2e8a256 | 2017-01-18 20:39:02 -0800 | [diff] [blame] | 811 | class Dex2oatWatchdogTest : public Dex2oatTest { |
| 812 | protected: |
| 813 | void RunTest(bool expect_success, const std::vector<std::string>& extra_args = {}) { |
| 814 | std::string dex_location = GetScratchDir() + "/Dex2OatSwapTest.jar"; |
| 815 | std::string odex_location = GetOdexDir() + "/Dex2OatSwapTest.odex"; |
| 816 | |
| 817 | Copy(GetTestDexFileName(), dex_location); |
| 818 | |
| 819 | std::vector<std::string> copy(extra_args); |
| 820 | |
| 821 | std::string swap_location = GetOdexDir() + "/Dex2OatSwapTest.odex.swap"; |
| 822 | copy.push_back("--swap-file=" + swap_location); |
| 823 | GenerateOdexForTest(dex_location, |
| 824 | odex_location, |
| 825 | CompilerFilter::kSpeed, |
| 826 | copy, |
| 827 | expect_success); |
| 828 | } |
| 829 | |
| 830 | std::string GetTestDexFileName() { |
| 831 | return GetDexSrc1(); |
| 832 | } |
| 833 | }; |
| 834 | |
| 835 | TEST_F(Dex2oatWatchdogTest, TestWatchdogOK) { |
| 836 | // Check with default. |
| 837 | RunTest(true); |
| 838 | |
| 839 | // Check with ten minutes. |
| 840 | RunTest(true, { "--watchdog-timeout=600000" }); |
| 841 | } |
| 842 | |
| 843 | TEST_F(Dex2oatWatchdogTest, TestWatchdogTrigger) { |
| 844 | // Check with ten milliseconds. |
| 845 | RunTest(false, { "--watchdog-timeout=10" }); |
| 846 | } |
| 847 | |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 848 | } // namespace art |