• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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 <sys/mman.h>
18 
19 #include "common_compiler_driver_test.h"
20 
21 #include "base/casts.h"
22 #include "base/timing_logger.h"
23 #include "dex/quick_compiler_callbacks.h"
24 #include "dex/verification_results.h"
25 #include "driver/compiler_driver.h"
26 #include "driver/compiler_options.h"
27 #include "utils/atomic_dex_ref_map-inl.h"
28 
29 namespace art {
30 
CommonCompilerDriverTest()31 CommonCompilerDriverTest::CommonCompilerDriverTest() {}
~CommonCompilerDriverTest()32 CommonCompilerDriverTest::~CommonCompilerDriverTest() {}
33 
CompileAll(jobject class_loader,const std::vector<const DexFile * > & dex_files,TimingLogger * timings)34 void CommonCompilerDriverTest::CompileAll(jobject class_loader,
35                                           const std::vector<const DexFile*>& dex_files,
36                                           TimingLogger* timings) {
37   TimingLogger::ScopedTiming t(__FUNCTION__, timings);
38   SetDexFilesForOatFile(dex_files);
39 
40   compiler_driver_->InitializeThreadPools();
41 
42   compiler_driver_->PreCompile(class_loader,
43                                dex_files,
44                                timings,
45                                &compiler_options_->image_classes_);
46 
47   compiler_driver_->CompileAll(class_loader, dex_files, timings);
48   compiler_driver_->FreeThreadPools();
49 }
50 
SetDexFilesForOatFile(const std::vector<const DexFile * > & dex_files)51 void CommonCompilerDriverTest::SetDexFilesForOatFile(const std::vector<const DexFile*>& dex_files) {
52   compiler_options_->dex_files_for_oat_file_ = dex_files;
53   compiler_driver_->compiled_classes_.AddDexFiles(dex_files);
54 }
55 
ReserveImageSpace()56 void CommonCompilerDriverTest::ReserveImageSpace() {
57   // Reserve where the image will be loaded up front so that other parts of test set up don't
58   // accidentally end up colliding with the fixed memory address when we need to load the image.
59   std::string error_msg;
60   MemMap::Init();
61   image_reservation_ = MemMap::MapAnonymous("image reservation",
62                                             reinterpret_cast<uint8_t*>(ART_BASE_ADDRESS),
63                                             static_cast<size_t>(120 * 1024 * 1024),  // 120MB
64                                             PROT_NONE,
65                                             false /* no need for 4gb flag with fixed mmap */,
66                                             /*reuse=*/ false,
67                                             /*reservation=*/ nullptr,
68                                             &error_msg);
69   CHECK(image_reservation_.IsValid()) << error_msg;
70 }
71 
UnreserveImageSpace()72 void CommonCompilerDriverTest::UnreserveImageSpace() {
73   image_reservation_.Reset();
74 }
75 
CreateCompilerDriver()76 void CommonCompilerDriverTest::CreateCompilerDriver() {
77   ApplyInstructionSet();
78 
79   compiler_options_->image_type_ = CompilerOptions::ImageType::kBootImage;
80   compiler_options_->compile_pic_ = false;  // Non-PIC boot image is a test configuration.
81   compiler_options_->SetCompilerFilter(GetCompilerFilter());
82   compiler_options_->image_classes_.swap(*GetImageClasses());
83   compiler_options_->profile_compilation_info_ = GetProfileCompilationInfo();
84   compiler_driver_.reset(new CompilerDriver(compiler_options_.get(),
85                                             verification_results_.get(),
86                                             number_of_threads_,
87                                             /* swap_fd= */ -1));
88 }
89 
SetUpRuntimeOptions(RuntimeOptions * options)90 void CommonCompilerDriverTest::SetUpRuntimeOptions(RuntimeOptions* options) {
91   CommonCompilerTest::SetUpRuntimeOptions(options);
92 
93   verification_results_.reset(new VerificationResults());
94   QuickCompilerCallbacks* callbacks =
95       new QuickCompilerCallbacks(CompilerCallbacks::CallbackMode::kCompileApp);
96   callbacks->SetVerificationResults(verification_results_.get());
97   callbacks_.reset(callbacks);
98 }
99 
SetUp()100 void CommonCompilerDriverTest::SetUp() {
101   CommonCompilerTest::SetUp();
102 
103   CreateCompilerDriver();
104 }
105 
TearDown()106 void CommonCompilerDriverTest::TearDown() {
107   image_reservation_.Reset();
108   compiler_driver_.reset();
109   verification_results_.reset();
110 
111   CommonCompilerTest::TearDown();
112 }
113 
114 // Get the set of image classes given to the compiler options in CreateCompilerDriver().
GetImageClasses()115 std::unique_ptr<HashSet<std::string>> CommonCompilerDriverTest::GetImageClasses() {
116   // Empty set: by default no classes are retained in the image.
117   return std::make_unique<HashSet<std::string>>();
118 }
119 
120 // Get ProfileCompilationInfo that should be passed to the driver.
GetProfileCompilationInfo()121 ProfileCompilationInfo* CommonCompilerDriverTest::GetProfileCompilationInfo() {
122   // Null, profile information will not be taken into account.
123   return nullptr;
124 }
125 
126 }  // namespace art
127