• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  
17  #include <regex>
18  #include <sstream>
19  #include <string>
20  #include <vector>
21  
22  #include <sys/wait.h>
23  #include <unistd.h>
24  
25  #include <android-base/logging.h>
26  
27  #include "common_runtime_test.h"
28  
29  #include "base/file_utils.h"
30  #include "base/macros.h"
31  #include "base/unix_file/fd_file.h"
32  #include "base/utils.h"
33  #include "dex/art_dex_file_loader.h"
34  #include "dex/dex_file-inl.h"
35  #include "dex/dex_file_loader.h"
36  #include "dex/method_reference.h"
37  #include "profile/profile_compilation_info.h"
38  #include "runtime.h"
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  
operator <<(std::ostream & os,const ImageSizes & sizes)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:
TearDown()55    void TearDown() override {}
56  
57   protected:
58    // Visitors take method and type references
59    template <typename MethodVisitor, typename ClassVisitor>
VisitLibcoreDexes(const MethodVisitor & method_visitor,const ClassVisitor & class_visitor,size_t method_frequency=1,size_t class_frequency=1)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;
66      for (const std::string& dex : GetLibCoreDexFileNames()) {
67        std::vector<std::unique_ptr<const DexFile>> dex_files;
68        std::string error_msg;
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))
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  
WriteLine(File * file,std::string line)92    static void WriteLine(File* file, std::string line) {
93      line += '\n';
94      EXPECT_TRUE(file->WriteFully(&line[0], line.length()));
95    }
96  
GenerateClasses(File * out_file,size_t frequency=1)97    void GenerateClasses(File* out_file, size_t frequency = 1) {
98      VisitLibcoreDexes(VoidFunctor(),
99                        [out_file](TypeReference ref) {
100        WriteLine(out_file, ref.dex_file->PrettyType(ref.TypeIndex()));
101      }, frequency, frequency);
102      EXPECT_EQ(out_file->Flush(), 0);
103    }
104  
GenerateMethods(File * out_file,size_t frequency=1)105    void GenerateMethods(File* out_file, size_t frequency = 1) {
106      VisitLibcoreDexes([out_file](MethodReference ref) {
107        WriteLine(out_file, ref.PrettyMethod());
108      }, VoidFunctor(), frequency, frequency);
109      EXPECT_EQ(out_file->Flush(), 0);
110    }
111  
AddRuntimeArg(std::vector<std::string> & args,const std::string & arg)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  
CompileImageAndGetSizes(const std::vector<std::string> & extra_args)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";
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;
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  
CompileBootImage(const std::vector<std::string> & extra_args,const std::string & image_file_name_prefix,std::string * error_msg)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  
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  
RunDex2Oat(const std::vector<std::string> & args,std::string * error_msg)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);
195        return false;
196      }
197      return res.StandardSuccess();
198    }
199  };
200  
TEST_F(Dex2oatImageTest,TestModesAndFilters)201  TEST_F(Dex2oatImageTest, TestModesAndFilters) {
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();
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;
214    ImageSizes compiled_methods_sizes;
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    }
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(
236          {"--image-classes=" + classes.GetFilename()});
237      classes.Close();
238      std::cout << "Compiled classes sizes " << compiled_classes_sizes << std::endl;
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    }
243    static size_t kMethodFrequency = 3;
244    static size_t kTypeFrequency = 4;
245    // Test compiling fewer methods and classes.
246    {
247      ScratchFile classes;
248      // Only compile every even class.
249      GenerateClasses(classes.GetFile(), kTypeFrequency);
250      compiled_methods_sizes = CompileImageAndGetSizes(
251          {"--image-classes=" + classes.GetFilename()});
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) {
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));
265      }, [&profile](TypeReference ref) {
266        EXPECT_TRUE(profile.AddClassForDex(ref));
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
277      // the range is within expected margins (+-10%).
278      const double kRatio = 0.90;
279      EXPECT_LE(profile_sizes.art_size * kRatio, compiled_methods_sizes.art_size);
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);
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);
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);
286      EXPECT_GE(profile_sizes.vdex_size / kRatio, compiled_methods_sizes.vdex_size);
287    }
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    }
297  }
298  
299  }  // namespace art
300