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" |
David Sehr | 82d046e | 2018-04-23 08:14:19 -0700 | [diff] [blame] | 37 | #include "profile/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 | |
Andreas Gampe | 38aa0b5 | 2018-07-10 23:26:55 -0700 | [diff] [blame] | 189 | bool RunDex2Oat(const std::vector<std::string>& args, std::string* error_msg) { |
| 190 | // We only want fatal logging for the error message. |
| 191 | auto post_fork_fn = []() { return setenv("ANDROID_LOG_TAGS", "*:f", 1) == 0; }; |
| 192 | ForkAndExecResult res = ForkAndExec(args, post_fork_fn, error_msg); |
| 193 | if (res.stage != ForkAndExecResult::kFinished) { |
| 194 | *error_msg = strerror(errno); |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 195 | return false; |
| 196 | } |
Andreas Gampe | 38aa0b5 | 2018-07-10 23:26:55 -0700 | [diff] [blame] | 197 | return res.StandardSuccess(); |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 198 | } |
| 199 | }; |
| 200 | |
Vladimir Marko | a5785a2 | 2018-03-08 14:25:47 +0000 | [diff] [blame] | 201 | TEST_F(Dex2oatImageTest, TestModesAndFilters) { |
Roland Levillain | f5dd114 | 2018-07-03 13:29:18 +0100 | [diff] [blame] | 202 | // This test crashes on the gtest-heap-poisoning configuration |
| 203 | // (AddressSanitizer + CMS/RosAlloc + heap-poisoning); see b/111061592. |
| 204 | // Temporarily disable this test on this configuration to keep |
| 205 | // our automated build/testing green while we work on a fix. |
| 206 | TEST_DISABLED_FOR_MEMORY_TOOL_WITH_HEAP_POISONING_WITHOUT_READ_BARRIERS(); |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 207 | if (kIsTargetBuild) { |
| 208 | // This test is too slow for target builds. |
| 209 | return; |
| 210 | } |
| 211 | ImageSizes base_sizes = CompileImageAndGetSizes({}); |
| 212 | ImageSizes image_classes_sizes; |
| 213 | ImageSizes compiled_classes_sizes; |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 214 | ImageSizes compiled_methods_sizes; |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 215 | ImageSizes profile_sizes; |
| 216 | std::cout << "Base compile sizes " << base_sizes << std::endl; |
| 217 | // Test image classes |
| 218 | { |
| 219 | ScratchFile classes; |
| 220 | GenerateClasses(classes.GetFile(), /*frequency*/ 1u); |
| 221 | image_classes_sizes = CompileImageAndGetSizes( |
| 222 | {"--image-classes=" + classes.GetFilename()}); |
| 223 | classes.Close(); |
| 224 | std::cout << "Image classes sizes " << image_classes_sizes << std::endl; |
| 225 | // Putting all classes as image classes should increase art size |
| 226 | EXPECT_GE(image_classes_sizes.art_size, base_sizes.art_size); |
| 227 | // Sanity check that dex is the same size. |
| 228 | EXPECT_EQ(image_classes_sizes.vdex_size, base_sizes.vdex_size); |
| 229 | } |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 230 | // Test compiled classes. |
| 231 | { |
| 232 | ScratchFile classes; |
| 233 | // Only compile every even class. |
| 234 | GenerateClasses(classes.GetFile(), /*frequency*/ 2u); |
| 235 | compiled_classes_sizes = CompileImageAndGetSizes( |
Andreas Gampe | bd600e3 | 2018-04-12 14:25:39 -0700 | [diff] [blame] | 236 | {"--image-classes=" + classes.GetFilename()}); |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 237 | classes.Close(); |
| 238 | std::cout << "Compiled classes sizes " << compiled_classes_sizes << std::endl; |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 239 | // Art file should be smaller than image classes version since we included fewer classes in the |
| 240 | // list. |
| 241 | EXPECT_LT(compiled_classes_sizes.art_size, image_classes_sizes.art_size); |
| 242 | } |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 243 | static size_t kMethodFrequency = 3; |
| 244 | static size_t kTypeFrequency = 4; |
| 245 | // Test compiling fewer methods and classes. |
| 246 | { |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 247 | ScratchFile classes; |
| 248 | // Only compile every even class. |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 249 | GenerateClasses(classes.GetFile(), kTypeFrequency); |
| 250 | compiled_methods_sizes = CompileImageAndGetSizes( |
Andreas Gampe | bd600e3 | 2018-04-12 14:25:39 -0700 | [diff] [blame] | 251 | {"--image-classes=" + classes.GetFilename()}); |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 252 | classes.Close(); |
| 253 | std::cout << "Compiled fewer methods sizes " << compiled_methods_sizes << std::endl; |
| 254 | } |
| 255 | // Cross verify profile based image against image-classes and compiled-methods to make sure it |
| 256 | // matches. |
| 257 | { |
| 258 | ProfileCompilationInfo profile; |
| 259 | VisitLibcoreDexes([&profile](MethodReference ref) { |
Mathieu Chartier | c46cf80 | 2017-09-28 11:52:19 -0700 | [diff] [blame] | 260 | uint32_t flags = ProfileCompilationInfo::MethodHotness::kFlagHot | |
| 261 | ProfileCompilationInfo::MethodHotness::kFlagStartup; |
| 262 | EXPECT_TRUE(profile.AddMethodIndex( |
| 263 | static_cast<ProfileCompilationInfo::MethodHotness::Flag>(flags), |
| 264 | ref)); |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 265 | }, [&profile](TypeReference ref) { |
Mathieu Chartier | fc8b422 | 2017-09-17 13:44:24 -0700 | [diff] [blame] | 266 | EXPECT_TRUE(profile.AddClassForDex(ref)); |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 267 | }, kMethodFrequency, kTypeFrequency); |
| 268 | ScratchFile profile_file; |
| 269 | profile.Save(profile_file.GetFile()->Fd()); |
| 270 | EXPECT_EQ(profile_file.GetFile()->Flush(), 0); |
| 271 | profile_sizes = CompileImageAndGetSizes( |
| 272 | {"--profile-file=" + profile_file.GetFilename(), |
| 273 | "--compiler-filter=speed-profile"}); |
| 274 | profile_file.Close(); |
| 275 | std::cout << "Profile sizes " << profile_sizes << std::endl; |
| 276 | // 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] | 277 | // the range is within expected margins (+-10%). |
| 278 | const double kRatio = 0.90; |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 279 | EXPECT_LE(profile_sizes.art_size * kRatio, compiled_methods_sizes.art_size); |
Nicolas Geoffray | 6e5c044 | 2017-07-17 11:29:41 +0100 | [diff] [blame] | 280 | // TODO(mathieuc): Find a reliable way to check compiled code. b/63746626 |
| 281 | // EXPECT_LE(profile_sizes.oat_size * kRatio, compiled_methods_sizes.oat_size); |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 282 | EXPECT_LE(profile_sizes.vdex_size * kRatio, compiled_methods_sizes.vdex_size); |
| 283 | EXPECT_GE(profile_sizes.art_size / kRatio, compiled_methods_sizes.art_size); |
Nicolas Geoffray | 6e5c044 | 2017-07-17 11:29:41 +0100 | [diff] [blame] | 284 | // TODO(mathieuc): Find a reliable way to check compiled code. b/63746626 |
| 285 | // EXPECT_GE(profile_sizes.oat_size / kRatio, compiled_methods_sizes.oat_size); |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 286 | EXPECT_GE(profile_sizes.vdex_size / kRatio, compiled_methods_sizes.vdex_size); |
| 287 | } |
Jeff Hao | c23b0c0 | 2017-07-27 18:19:38 -0700 | [diff] [blame] | 288 | // Test dirty image objects. |
| 289 | { |
| 290 | ScratchFile classes; |
| 291 | GenerateClasses(classes.GetFile(), /*frequency*/ 1u); |
| 292 | image_classes_sizes = CompileImageAndGetSizes( |
| 293 | {"--dirty-image-objects=" + classes.GetFilename()}); |
| 294 | classes.Close(); |
| 295 | std::cout << "Dirty image object sizes " << image_classes_sizes << std::endl; |
| 296 | } |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | } // namespace art |