Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | |
Igor Murashkin | 5573c37 | 2017-11-16 13:34:30 -0800 | [diff] [blame] | 17 | #include <regex> |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 18 | #include <sstream> |
| 19 | #include <string> |
| 20 | #include <vector> |
| 21 | |
| 22 | #include <sys/wait.h> |
| 23 | #include <unistd.h> |
| 24 | |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 25 | #include <android-base/logging.h> |
| 26 | |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 27 | #include "common_runtime_test.h" |
| 28 | |
David Sehr | 891a50e | 2017-10-27 17:01:07 -0700 | [diff] [blame] | 29 | #include "base/file_utils.h" |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 30 | #include "base/macros.h" |
| 31 | #include "base/unix_file/fd_file.h" |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 32 | #include "base/utils.h" |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 33 | #include "dex/art_dex_file_loader.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 34 | #include "dex/dex_file-inl.h" |
| 35 | #include "dex/dex_file_loader.h" |
David Sehr | 312f3b2 | 2018-03-19 08:39:26 -0700 | [diff] [blame^] | 36 | #include "dex/method_reference.h" |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 37 | #include "jit/profile_compilation_info.h" |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 38 | #include "runtime.h" |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 39 | |
| 40 | namespace art { |
| 41 | |
| 42 | struct ImageSizes { |
| 43 | size_t art_size = 0; |
| 44 | size_t oat_size = 0; |
| 45 | size_t vdex_size = 0; |
| 46 | }; |
| 47 | |
| 48 | std::ostream& operator<<(std::ostream& os, const ImageSizes& sizes) { |
| 49 | os << "art=" << sizes.art_size << " oat=" << sizes.oat_size << " vdex=" << sizes.vdex_size; |
| 50 | return os; |
| 51 | } |
| 52 | |
| 53 | class Dex2oatImageTest : public CommonRuntimeTest { |
| 54 | public: |
| 55 | virtual void TearDown() OVERRIDE {} |
| 56 | |
| 57 | protected: |
| 58 | // Visitors take method and type references |
| 59 | template <typename MethodVisitor, typename ClassVisitor> |
| 60 | void VisitLibcoreDexes(const MethodVisitor& method_visitor, |
| 61 | const ClassVisitor& class_visitor, |
| 62 | size_t method_frequency = 1, |
| 63 | size_t class_frequency = 1) { |
| 64 | size_t method_counter = 0; |
| 65 | size_t class_counter = 0; |
Andreas Gampe | 641a473 | 2017-08-24 13:21:35 -0700 | [diff] [blame] | 66 | for (const std::string& dex : GetLibCoreDexFileNames()) { |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 67 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 68 | std::string error_msg; |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 69 | const ArtDexFileLoader dex_file_loader; |
| 70 | CHECK(dex_file_loader.Open(dex.c_str(), |
| 71 | dex, |
| 72 | /*verify*/ true, |
| 73 | /*verify_checksum*/ false, |
| 74 | &error_msg, |
| 75 | &dex_files)) |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 76 | << error_msg; |
| 77 | for (const std::unique_ptr<const DexFile>& dex_file : dex_files) { |
| 78 | for (size_t i = 0; i < dex_file->NumMethodIds(); ++i) { |
| 79 | if (++method_counter % method_frequency == 0) { |
| 80 | method_visitor(MethodReference(dex_file.get(), i)); |
| 81 | } |
| 82 | } |
| 83 | for (size_t i = 0; i < dex_file->NumTypeIds(); ++i) { |
| 84 | if (++class_counter % class_frequency == 0) { |
| 85 | class_visitor(TypeReference(dex_file.get(), dex::TypeIndex(i))); |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | static void WriteLine(File* file, std::string line) { |
| 93 | line += '\n'; |
| 94 | EXPECT_TRUE(file->WriteFully(&line[0], line.length())); |
| 95 | } |
| 96 | |
| 97 | void GenerateClasses(File* out_file, size_t frequency = 1) { |
| 98 | VisitLibcoreDexes(VoidFunctor(), |
| 99 | [out_file](TypeReference ref) { |
Mathieu Chartier | fc8b422 | 2017-09-17 13:44:24 -0700 | [diff] [blame] | 100 | WriteLine(out_file, ref.dex_file->PrettyType(ref.TypeIndex())); |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 101 | }, frequency, frequency); |
| 102 | EXPECT_EQ(out_file->Flush(), 0); |
| 103 | } |
| 104 | |
| 105 | void GenerateMethods(File* out_file, size_t frequency = 1) { |
| 106 | VisitLibcoreDexes([out_file](MethodReference ref) { |
Mathieu Chartier | fc8b422 | 2017-09-17 13:44:24 -0700 | [diff] [blame] | 107 | WriteLine(out_file, ref.PrettyMethod()); |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 108 | }, VoidFunctor(), frequency, frequency); |
| 109 | EXPECT_EQ(out_file->Flush(), 0); |
| 110 | } |
| 111 | |
| 112 | void AddRuntimeArg(std::vector<std::string>& args, const std::string& arg) { |
| 113 | args.push_back("--runtime-arg"); |
| 114 | args.push_back(arg); |
| 115 | } |
| 116 | |
| 117 | ImageSizes CompileImageAndGetSizes(const std::vector<std::string>& extra_args) { |
| 118 | ImageSizes ret; |
| 119 | ScratchFile scratch; |
| 120 | std::string scratch_dir = scratch.GetFilename(); |
| 121 | while (!scratch_dir.empty() && scratch_dir.back() != '/') { |
| 122 | scratch_dir.pop_back(); |
| 123 | } |
| 124 | CHECK(!scratch_dir.empty()) << "No directory " << scratch.GetFilename(); |
| 125 | std::string error_msg; |
| 126 | if (!CompileBootImage(extra_args, scratch.GetFilename(), &error_msg)) { |
| 127 | LOG(ERROR) << "Failed to compile image " << scratch.GetFilename() << error_msg; |
| 128 | } |
| 129 | std::string art_file = scratch.GetFilename() + ".art"; |
| 130 | std::string oat_file = scratch.GetFilename() + ".oat"; |
| 131 | std::string vdex_file = scratch.GetFilename() + ".vdex"; |
Vladimir Marko | a5785a2 | 2018-03-08 14:25:47 +0000 | [diff] [blame] | 132 | int64_t art_size = OS::GetFileSizeBytes(art_file.c_str()); |
| 133 | int64_t oat_size = OS::GetFileSizeBytes(oat_file.c_str()); |
| 134 | int64_t vdex_size = OS::GetFileSizeBytes(vdex_file.c_str()); |
| 135 | CHECK_GT(art_size, 0u) << art_file; |
| 136 | CHECK_GT(oat_size, 0u) << oat_file; |
| 137 | CHECK_GT(vdex_size, 0u) << vdex_file; |
| 138 | ret.art_size = art_size; |
| 139 | ret.oat_size = oat_size; |
| 140 | ret.vdex_size = vdex_size; |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 141 | scratch.Close(); |
| 142 | // Clear image files since we compile the image multiple times and don't want to leave any |
| 143 | // artifacts behind. |
| 144 | ClearDirectory(scratch_dir.c_str(), /*recursive*/ false); |
| 145 | return ret; |
| 146 | } |
| 147 | |
| 148 | bool CompileBootImage(const std::vector<std::string>& extra_args, |
| 149 | const std::string& image_file_name_prefix, |
| 150 | std::string* error_msg) { |
| 151 | Runtime* const runtime = Runtime::Current(); |
| 152 | std::vector<std::string> argv; |
| 153 | argv.push_back(runtime->GetCompilerExecutable()); |
| 154 | AddRuntimeArg(argv, "-Xms64m"); |
| 155 | AddRuntimeArg(argv, "-Xmx64m"); |
| 156 | std::vector<std::string> dex_files = GetLibCoreDexFileNames(); |
| 157 | for (const std::string& dex_file : dex_files) { |
| 158 | argv.push_back("--dex-file=" + dex_file); |
| 159 | argv.push_back("--dex-location=" + dex_file); |
| 160 | } |
| 161 | if (runtime->IsJavaDebuggable()) { |
| 162 | argv.push_back("--debuggable"); |
| 163 | } |
| 164 | runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv); |
| 165 | |
| 166 | AddRuntimeArg(argv, "-Xverify:softfail"); |
| 167 | |
| 168 | if (!kIsTargetBuild) { |
| 169 | argv.push_back("--host"); |
| 170 | } |
| 171 | |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 172 | argv.push_back("--image=" + image_file_name_prefix + ".art"); |
| 173 | argv.push_back("--oat-file=" + image_file_name_prefix + ".oat"); |
| 174 | argv.push_back("--oat-location=" + image_file_name_prefix + ".oat"); |
| 175 | argv.push_back("--base=0x60000000"); |
| 176 | |
| 177 | std::vector<std::string> compiler_options = runtime->GetCompilerOptions(); |
| 178 | argv.insert(argv.end(), compiler_options.begin(), compiler_options.end()); |
| 179 | |
| 180 | // We must set --android-root. |
| 181 | const char* android_root = getenv("ANDROID_ROOT"); |
| 182 | CHECK(android_root != nullptr); |
| 183 | argv.push_back("--android-root=" + std::string(android_root)); |
| 184 | argv.insert(argv.end(), extra_args.begin(), extra_args.end()); |
| 185 | |
| 186 | return RunDex2Oat(argv, error_msg); |
| 187 | } |
| 188 | |
| 189 | int RunDex2Oat(const std::vector<std::string>& args, std::string* error_msg) { |
| 190 | int link[2]; |
| 191 | |
| 192 | if (pipe(link) == -1) { |
| 193 | return false; |
| 194 | } |
| 195 | |
| 196 | pid_t pid = fork(); |
| 197 | if (pid == -1) { |
| 198 | return false; |
| 199 | } |
| 200 | |
| 201 | if (pid == 0) { |
| 202 | // We need dex2oat to actually log things. |
| 203 | setenv("ANDROID_LOG_TAGS", "*:f", 1); |
| 204 | dup2(link[1], STDERR_FILENO); |
| 205 | close(link[0]); |
| 206 | close(link[1]); |
| 207 | std::vector<const char*> c_args; |
| 208 | for (const std::string& str : args) { |
| 209 | c_args.push_back(str.c_str()); |
| 210 | } |
| 211 | c_args.push_back(nullptr); |
| 212 | execv(c_args[0], const_cast<char* const*>(c_args.data())); |
| 213 | exit(1); |
| 214 | UNREACHABLE(); |
| 215 | } else { |
| 216 | close(link[1]); |
| 217 | char buffer[128]; |
| 218 | memset(buffer, 0, 128); |
| 219 | ssize_t bytes_read = 0; |
| 220 | |
| 221 | while (TEMP_FAILURE_RETRY(bytes_read = read(link[0], buffer, 128)) > 0) { |
| 222 | *error_msg += std::string(buffer, bytes_read); |
| 223 | } |
| 224 | close(link[0]); |
| 225 | int status = -1; |
| 226 | if (waitpid(pid, &status, 0) != -1) { |
| 227 | return (status == 0); |
| 228 | } |
| 229 | return false; |
| 230 | } |
| 231 | } |
| 232 | }; |
| 233 | |
Vladimir Marko | a5785a2 | 2018-03-08 14:25:47 +0000 | [diff] [blame] | 234 | TEST_F(Dex2oatImageTest, TestModesAndFilters) { |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 235 | if (kIsTargetBuild) { |
| 236 | // This test is too slow for target builds. |
| 237 | return; |
| 238 | } |
| 239 | ImageSizes base_sizes = CompileImageAndGetSizes({}); |
| 240 | ImageSizes image_classes_sizes; |
| 241 | ImageSizes compiled_classes_sizes; |
| 242 | ImageSizes compiled_all_classes_sizes; |
| 243 | ImageSizes compiled_methods_sizes; |
| 244 | ImageSizes compiled_all_methods_sizes; |
| 245 | ImageSizes profile_sizes; |
| 246 | std::cout << "Base compile sizes " << base_sizes << std::endl; |
| 247 | // Test image classes |
| 248 | { |
| 249 | ScratchFile classes; |
| 250 | GenerateClasses(classes.GetFile(), /*frequency*/ 1u); |
| 251 | image_classes_sizes = CompileImageAndGetSizes( |
| 252 | {"--image-classes=" + classes.GetFilename()}); |
| 253 | classes.Close(); |
| 254 | std::cout << "Image classes sizes " << image_classes_sizes << std::endl; |
| 255 | // Putting all classes as image classes should increase art size |
| 256 | EXPECT_GE(image_classes_sizes.art_size, base_sizes.art_size); |
| 257 | // Sanity check that dex is the same size. |
| 258 | EXPECT_EQ(image_classes_sizes.vdex_size, base_sizes.vdex_size); |
| 259 | } |
| 260 | // Test compiled classes with all the classes. |
| 261 | { |
| 262 | ScratchFile classes; |
| 263 | // Only compile every even class. |
| 264 | GenerateClasses(classes.GetFile(), /*frequency*/ 1u); |
| 265 | compiled_all_classes_sizes = CompileImageAndGetSizes( |
| 266 | {"--compiled-classes=" + classes.GetFilename()}); |
| 267 | classes.Close(); |
| 268 | std::cout << "Compiled all classes sizes " << compiled_all_classes_sizes << std::endl; |
| 269 | // Check that oat size is smaller since we didn't compile everything. |
| 270 | EXPECT_EQ(compiled_all_classes_sizes.art_size, base_sizes.art_size); |
Nicolas Geoffray | 6e5c044 | 2017-07-17 11:29:41 +0100 | [diff] [blame] | 271 | // TODO(mathieuc): Find a reliable way to check compiled code. |
| 272 | // EXPECT_EQ(compiled_all_classes_sizes.oat_size, base_sizes.oat_size); |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 273 | EXPECT_EQ(compiled_all_classes_sizes.vdex_size, base_sizes.vdex_size); |
| 274 | } |
| 275 | // Test compiled classes. |
| 276 | { |
| 277 | ScratchFile classes; |
| 278 | // Only compile every even class. |
| 279 | GenerateClasses(classes.GetFile(), /*frequency*/ 2u); |
| 280 | compiled_classes_sizes = CompileImageAndGetSizes( |
| 281 | {"--image-classes=" + classes.GetFilename(), |
| 282 | "--compiled-classes=" + classes.GetFilename()}); |
| 283 | classes.Close(); |
| 284 | std::cout << "Compiled classes sizes " << compiled_classes_sizes << std::endl; |
| 285 | // Check that oat size is smaller since we didn't compile everything. |
Nicolas Geoffray | 6e5c044 | 2017-07-17 11:29:41 +0100 | [diff] [blame] | 286 | // TODO(mathieuc): Find a reliable way to check compiled code. |
| 287 | // EXPECT_LT(compiled_classes_sizes.oat_size, base_sizes.oat_size); |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 288 | // Art file should be smaller than image classes version since we included fewer classes in the |
| 289 | // list. |
| 290 | EXPECT_LT(compiled_classes_sizes.art_size, image_classes_sizes.art_size); |
| 291 | } |
| 292 | // Test compiled methods. |
| 293 | { |
| 294 | ScratchFile methods; |
| 295 | // Only compile every even class. |
| 296 | GenerateMethods(methods.GetFile(), /*frequency*/ 1u); |
| 297 | compiled_all_methods_sizes = CompileImageAndGetSizes( |
| 298 | {"--compiled-methods=" + methods.GetFilename()}); |
| 299 | methods.Close(); |
| 300 | std::cout << "Compiled all methods sizes " << compiled_all_methods_sizes << std::endl; |
| 301 | EXPECT_EQ(compiled_all_classes_sizes.art_size, base_sizes.art_size); |
Nicolas Geoffray | 6e5c044 | 2017-07-17 11:29:41 +0100 | [diff] [blame] | 302 | // TODO(mathieuc): Find a reliable way to check compiled code. b/63746626 |
| 303 | // EXPECT_EQ(compiled_all_classes_sizes.oat_size, base_sizes.oat_size); |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 304 | EXPECT_EQ(compiled_all_classes_sizes.vdex_size, base_sizes.vdex_size); |
| 305 | } |
| 306 | static size_t kMethodFrequency = 3; |
| 307 | static size_t kTypeFrequency = 4; |
| 308 | // Test compiling fewer methods and classes. |
| 309 | { |
| 310 | ScratchFile methods; |
| 311 | ScratchFile classes; |
| 312 | // Only compile every even class. |
| 313 | GenerateMethods(methods.GetFile(), kMethodFrequency); |
| 314 | GenerateClasses(classes.GetFile(), kTypeFrequency); |
| 315 | compiled_methods_sizes = CompileImageAndGetSizes( |
| 316 | {"--image-classes=" + classes.GetFilename(), |
| 317 | "--compiled-methods=" + methods.GetFilename()}); |
| 318 | methods.Close(); |
| 319 | classes.Close(); |
| 320 | std::cout << "Compiled fewer methods sizes " << compiled_methods_sizes << std::endl; |
| 321 | } |
| 322 | // Cross verify profile based image against image-classes and compiled-methods to make sure it |
| 323 | // matches. |
| 324 | { |
| 325 | ProfileCompilationInfo profile; |
| 326 | VisitLibcoreDexes([&profile](MethodReference ref) { |
Mathieu Chartier | c46cf80 | 2017-09-28 11:52:19 -0700 | [diff] [blame] | 327 | uint32_t flags = ProfileCompilationInfo::MethodHotness::kFlagHot | |
| 328 | ProfileCompilationInfo::MethodHotness::kFlagStartup; |
| 329 | EXPECT_TRUE(profile.AddMethodIndex( |
| 330 | static_cast<ProfileCompilationInfo::MethodHotness::Flag>(flags), |
| 331 | ref)); |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 332 | }, [&profile](TypeReference ref) { |
Mathieu Chartier | fc8b422 | 2017-09-17 13:44:24 -0700 | [diff] [blame] | 333 | EXPECT_TRUE(profile.AddClassForDex(ref)); |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 334 | }, kMethodFrequency, kTypeFrequency); |
| 335 | ScratchFile profile_file; |
| 336 | profile.Save(profile_file.GetFile()->Fd()); |
| 337 | EXPECT_EQ(profile_file.GetFile()->Flush(), 0); |
| 338 | profile_sizes = CompileImageAndGetSizes( |
| 339 | {"--profile-file=" + profile_file.GetFilename(), |
| 340 | "--compiler-filter=speed-profile"}); |
| 341 | profile_file.Close(); |
| 342 | std::cout << "Profile sizes " << profile_sizes << std::endl; |
| 343 | // Since there is some difference between profile vs image + methods due to layout, check that |
Mathieu Chartier | 44e9bac | 2017-10-06 09:50:18 -0700 | [diff] [blame] | 344 | // the range is within expected margins (+-10%). |
| 345 | const double kRatio = 0.90; |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 346 | EXPECT_LE(profile_sizes.art_size * kRatio, compiled_methods_sizes.art_size); |
Nicolas Geoffray | 6e5c044 | 2017-07-17 11:29:41 +0100 | [diff] [blame] | 347 | // TODO(mathieuc): Find a reliable way to check compiled code. b/63746626 |
| 348 | // EXPECT_LE(profile_sizes.oat_size * kRatio, compiled_methods_sizes.oat_size); |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 349 | EXPECT_LE(profile_sizes.vdex_size * kRatio, compiled_methods_sizes.vdex_size); |
| 350 | EXPECT_GE(profile_sizes.art_size / kRatio, compiled_methods_sizes.art_size); |
Nicolas Geoffray | 6e5c044 | 2017-07-17 11:29:41 +0100 | [diff] [blame] | 351 | // TODO(mathieuc): Find a reliable way to check compiled code. b/63746626 |
| 352 | // EXPECT_GE(profile_sizes.oat_size / kRatio, compiled_methods_sizes.oat_size); |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 353 | EXPECT_GE(profile_sizes.vdex_size / kRatio, compiled_methods_sizes.vdex_size); |
| 354 | } |
Jeff Hao | c23b0c0 | 2017-07-27 18:19:38 -0700 | [diff] [blame] | 355 | // Test dirty image objects. |
| 356 | { |
| 357 | ScratchFile classes; |
| 358 | GenerateClasses(classes.GetFile(), /*frequency*/ 1u); |
| 359 | image_classes_sizes = CompileImageAndGetSizes( |
| 360 | {"--dirty-image-objects=" + classes.GetFilename()}); |
| 361 | classes.Close(); |
| 362 | std::cout << "Dirty image object sizes " << image_classes_sizes << std::endl; |
| 363 | } |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | } // namespace art |