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: |
Roland Levillain | f73caca | 2018-08-24 17:19:07 +0100 | [diff] [blame] | 55 | void TearDown() override {} |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 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 | |
Mathieu Chartier | f68b698 | 2019-06-27 09:40:20 -0700 | [diff] [blame] | 97 | void GenerateProfile(File* out_file, size_t method_frequency, size_t type_frequency) { |
| 98 | ProfileCompilationInfo profile; |
| 99 | VisitLibcoreDexes([&profile](MethodReference ref) { |
| 100 | uint32_t flags = ProfileCompilationInfo::MethodHotness::kFlagHot | |
| 101 | ProfileCompilationInfo::MethodHotness::kFlagStartup; |
| 102 | EXPECT_TRUE(profile.AddMethodIndex( |
| 103 | static_cast<ProfileCompilationInfo::MethodHotness::Flag>(flags), |
| 104 | ref)); |
| 105 | }, [&profile](TypeReference ref) { |
| 106 | EXPECT_TRUE(profile.AddClassForDex(ref)); |
| 107 | }, method_frequency, type_frequency); |
| 108 | ScratchFile profile_file; |
| 109 | profile.Save(out_file->Fd()); |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 110 | EXPECT_EQ(out_file->Flush(), 0); |
| 111 | } |
| 112 | |
| 113 | void GenerateMethods(File* out_file, size_t frequency = 1) { |
| 114 | VisitLibcoreDexes([out_file](MethodReference ref) { |
Mathieu Chartier | fc8b422 | 2017-09-17 13:44:24 -0700 | [diff] [blame] | 115 | WriteLine(out_file, ref.PrettyMethod()); |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 116 | }, VoidFunctor(), frequency, frequency); |
| 117 | EXPECT_EQ(out_file->Flush(), 0); |
| 118 | } |
| 119 | |
| 120 | void AddRuntimeArg(std::vector<std::string>& args, const std::string& arg) { |
| 121 | args.push_back("--runtime-arg"); |
| 122 | args.push_back(arg); |
| 123 | } |
| 124 | |
| 125 | ImageSizes CompileImageAndGetSizes(const std::vector<std::string>& extra_args) { |
| 126 | ImageSizes ret; |
| 127 | ScratchFile scratch; |
| 128 | std::string scratch_dir = scratch.GetFilename(); |
| 129 | while (!scratch_dir.empty() && scratch_dir.back() != '/') { |
| 130 | scratch_dir.pop_back(); |
| 131 | } |
| 132 | CHECK(!scratch_dir.empty()) << "No directory " << scratch.GetFilename(); |
| 133 | std::string error_msg; |
| 134 | if (!CompileBootImage(extra_args, scratch.GetFilename(), &error_msg)) { |
| 135 | LOG(ERROR) << "Failed to compile image " << scratch.GetFilename() << error_msg; |
| 136 | } |
| 137 | std::string art_file = scratch.GetFilename() + ".art"; |
| 138 | std::string oat_file = scratch.GetFilename() + ".oat"; |
| 139 | std::string vdex_file = scratch.GetFilename() + ".vdex"; |
Vladimir Marko | a5785a2 | 2018-03-08 14:25:47 +0000 | [diff] [blame] | 140 | int64_t art_size = OS::GetFileSizeBytes(art_file.c_str()); |
| 141 | int64_t oat_size = OS::GetFileSizeBytes(oat_file.c_str()); |
| 142 | int64_t vdex_size = OS::GetFileSizeBytes(vdex_file.c_str()); |
| 143 | CHECK_GT(art_size, 0u) << art_file; |
| 144 | CHECK_GT(oat_size, 0u) << oat_file; |
| 145 | CHECK_GT(vdex_size, 0u) << vdex_file; |
| 146 | ret.art_size = art_size; |
| 147 | ret.oat_size = oat_size; |
| 148 | ret.vdex_size = vdex_size; |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 149 | scratch.Close(); |
| 150 | // Clear image files since we compile the image multiple times and don't want to leave any |
| 151 | // artifacts behind. |
| 152 | ClearDirectory(scratch_dir.c_str(), /*recursive*/ false); |
| 153 | return ret; |
| 154 | } |
| 155 | |
| 156 | bool CompileBootImage(const std::vector<std::string>& extra_args, |
| 157 | const std::string& image_file_name_prefix, |
| 158 | std::string* error_msg) { |
| 159 | Runtime* const runtime = Runtime::Current(); |
| 160 | std::vector<std::string> argv; |
| 161 | argv.push_back(runtime->GetCompilerExecutable()); |
| 162 | AddRuntimeArg(argv, "-Xms64m"); |
| 163 | AddRuntimeArg(argv, "-Xmx64m"); |
| 164 | std::vector<std::string> dex_files = GetLibCoreDexFileNames(); |
| 165 | for (const std::string& dex_file : dex_files) { |
| 166 | argv.push_back("--dex-file=" + dex_file); |
| 167 | argv.push_back("--dex-location=" + dex_file); |
| 168 | } |
| 169 | if (runtime->IsJavaDebuggable()) { |
| 170 | argv.push_back("--debuggable"); |
| 171 | } |
| 172 | runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv); |
| 173 | |
| 174 | AddRuntimeArg(argv, "-Xverify:softfail"); |
| 175 | |
| 176 | if (!kIsTargetBuild) { |
| 177 | argv.push_back("--host"); |
| 178 | } |
| 179 | |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 180 | argv.push_back("--image=" + image_file_name_prefix + ".art"); |
| 181 | argv.push_back("--oat-file=" + image_file_name_prefix + ".oat"); |
| 182 | argv.push_back("--oat-location=" + image_file_name_prefix + ".oat"); |
| 183 | argv.push_back("--base=0x60000000"); |
| 184 | |
| 185 | std::vector<std::string> compiler_options = runtime->GetCompilerOptions(); |
| 186 | argv.insert(argv.end(), compiler_options.begin(), compiler_options.end()); |
| 187 | |
| 188 | // We must set --android-root. |
| 189 | const char* android_root = getenv("ANDROID_ROOT"); |
| 190 | CHECK(android_root != nullptr); |
| 191 | argv.push_back("--android-root=" + std::string(android_root)); |
| 192 | argv.insert(argv.end(), extra_args.begin(), extra_args.end()); |
| 193 | |
| 194 | return RunDex2Oat(argv, error_msg); |
| 195 | } |
| 196 | |
Andreas Gampe | 38aa0b5 | 2018-07-10 23:26:55 -0700 | [diff] [blame] | 197 | bool RunDex2Oat(const std::vector<std::string>& args, std::string* error_msg) { |
| 198 | // We only want fatal logging for the error message. |
| 199 | auto post_fork_fn = []() { return setenv("ANDROID_LOG_TAGS", "*:f", 1) == 0; }; |
| 200 | ForkAndExecResult res = ForkAndExec(args, post_fork_fn, error_msg); |
| 201 | if (res.stage != ForkAndExecResult::kFinished) { |
| 202 | *error_msg = strerror(errno); |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 203 | return false; |
| 204 | } |
Andreas Gampe | 38aa0b5 | 2018-07-10 23:26:55 -0700 | [diff] [blame] | 205 | return res.StandardSuccess(); |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 206 | } |
| 207 | }; |
| 208 | |
Vladimir Marko | a5785a2 | 2018-03-08 14:25:47 +0000 | [diff] [blame] | 209 | TEST_F(Dex2oatImageTest, TestModesAndFilters) { |
Roland Levillain | f5dd114 | 2018-07-03 13:29:18 +0100 | [diff] [blame] | 210 | // This test crashes on the gtest-heap-poisoning configuration |
| 211 | // (AddressSanitizer + CMS/RosAlloc + heap-poisoning); see b/111061592. |
| 212 | // Temporarily disable this test on this configuration to keep |
| 213 | // our automated build/testing green while we work on a fix. |
| 214 | TEST_DISABLED_FOR_MEMORY_TOOL_WITH_HEAP_POISONING_WITHOUT_READ_BARRIERS(); |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 215 | if (kIsTargetBuild) { |
| 216 | // This test is too slow for target builds. |
| 217 | return; |
| 218 | } |
| 219 | ImageSizes base_sizes = CompileImageAndGetSizes({}); |
Mathieu Chartier | f68b698 | 2019-06-27 09:40:20 -0700 | [diff] [blame] | 220 | ImageSizes everything_sizes; |
| 221 | ImageSizes filter_sizes; |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 222 | std::cout << "Base compile sizes " << base_sizes << std::endl; |
Mathieu Chartier | f68b698 | 2019-06-27 09:40:20 -0700 | [diff] [blame] | 223 | // Compile all methods and classes |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 224 | { |
Mathieu Chartier | f68b698 | 2019-06-27 09:40:20 -0700 | [diff] [blame] | 225 | ScratchFile profile_file; |
| 226 | GenerateProfile(profile_file.GetFile(), /*method_frequency=*/ 1u, /*type_frequency=*/ 1u); |
| 227 | everything_sizes = CompileImageAndGetSizes( |
| 228 | {"--profile-file=" + profile_file.GetFilename(), |
| 229 | "--compiler-filter=speed-profile"}); |
| 230 | profile_file.Close(); |
| 231 | std::cout << "All methods and classes sizes " << everything_sizes << std::endl; |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 232 | // Putting all classes as image classes should increase art size |
Mathieu Chartier | f68b698 | 2019-06-27 09:40:20 -0700 | [diff] [blame] | 233 | EXPECT_GE(everything_sizes.art_size, base_sizes.art_size); |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 234 | // Sanity check that dex is the same size. |
Mathieu Chartier | f68b698 | 2019-06-27 09:40:20 -0700 | [diff] [blame] | 235 | EXPECT_EQ(everything_sizes.vdex_size, base_sizes.vdex_size); |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 236 | } |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 237 | static size_t kMethodFrequency = 3; |
| 238 | static size_t kTypeFrequency = 4; |
| 239 | // Test compiling fewer methods and classes. |
| 240 | { |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 241 | ScratchFile profile_file; |
Mathieu Chartier | f68b698 | 2019-06-27 09:40:20 -0700 | [diff] [blame] | 242 | GenerateProfile(profile_file.GetFile(), kMethodFrequency, kTypeFrequency); |
| 243 | filter_sizes = CompileImageAndGetSizes( |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 244 | {"--profile-file=" + profile_file.GetFilename(), |
| 245 | "--compiler-filter=speed-profile"}); |
| 246 | profile_file.Close(); |
Mathieu Chartier | f68b698 | 2019-06-27 09:40:20 -0700 | [diff] [blame] | 247 | std::cout << "Fewer methods and classes sizes " << filter_sizes << std::endl; |
| 248 | EXPECT_LE(filter_sizes.art_size, everything_sizes.art_size); |
| 249 | EXPECT_LE(filter_sizes.oat_size, everything_sizes.oat_size); |
| 250 | EXPECT_LE(filter_sizes.vdex_size, everything_sizes.vdex_size); |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 251 | } |
Jeff Hao | c23b0c0 | 2017-07-27 18:19:38 -0700 | [diff] [blame] | 252 | // Test dirty image objects. |
| 253 | { |
| 254 | ScratchFile classes; |
Mathieu Chartier | f68b698 | 2019-06-27 09:40:20 -0700 | [diff] [blame] | 255 | VisitLibcoreDexes(VoidFunctor(), |
| 256 | [&](TypeReference ref) { |
| 257 | WriteLine(classes.GetFile(), ref.dex_file->PrettyType(ref.TypeIndex())); |
| 258 | }, /*method_frequency=*/ 1u, /*class_frequency=*/ 1u); |
| 259 | ImageSizes image_classes_sizes = CompileImageAndGetSizes( |
Jeff Hao | c23b0c0 | 2017-07-27 18:19:38 -0700 | [diff] [blame] | 260 | {"--dirty-image-objects=" + classes.GetFilename()}); |
| 261 | classes.Close(); |
| 262 | std::cout << "Dirty image object sizes " << image_classes_sizes << std::endl; |
| 263 | } |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | } // namespace art |