xref: /aosp_15_r20/art/dex2oat/common_compiler_driver_test.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2019 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #include <sys/mman.h>
18*795d594fSAndroid Build Coastguard Worker 
19*795d594fSAndroid Build Coastguard Worker #include "common_compiler_driver_test.h"
20*795d594fSAndroid Build Coastguard Worker 
21*795d594fSAndroid Build Coastguard Worker #include "base/casts.h"
22*795d594fSAndroid Build Coastguard Worker #include "base/timing_logger.h"
23*795d594fSAndroid Build Coastguard Worker #include "dex/quick_compiler_callbacks.h"
24*795d594fSAndroid Build Coastguard Worker #include "dex/verification_results.h"
25*795d594fSAndroid Build Coastguard Worker #include "driver/compiler_driver.h"
26*795d594fSAndroid Build Coastguard Worker #include "driver/compiler_options.h"
27*795d594fSAndroid Build Coastguard Worker #include "utils/atomic_dex_ref_map-inl.h"
28*795d594fSAndroid Build Coastguard Worker 
29*795d594fSAndroid Build Coastguard Worker namespace art {
30*795d594fSAndroid Build Coastguard Worker 
CommonCompilerDriverTest()31*795d594fSAndroid Build Coastguard Worker CommonCompilerDriverTest::CommonCompilerDriverTest() : inaccessible_page_(nullptr) {}
~CommonCompilerDriverTest()32*795d594fSAndroid Build Coastguard Worker CommonCompilerDriverTest::~CommonCompilerDriverTest() {}
33*795d594fSAndroid Build Coastguard Worker 
CompileAll(jobject class_loader,const std::vector<const DexFile * > & dex_files,TimingLogger * timings)34*795d594fSAndroid Build Coastguard Worker void CommonCompilerDriverTest::CompileAll(jobject class_loader,
35*795d594fSAndroid Build Coastguard Worker                                           const std::vector<const DexFile*>& dex_files,
36*795d594fSAndroid Build Coastguard Worker                                           TimingLogger* timings) {
37*795d594fSAndroid Build Coastguard Worker   TimingLogger::ScopedTiming t(__FUNCTION__, timings);
38*795d594fSAndroid Build Coastguard Worker   SetDexFilesForOatFile(dex_files);
39*795d594fSAndroid Build Coastguard Worker 
40*795d594fSAndroid Build Coastguard Worker   compiler_driver_->InitializeThreadPools();
41*795d594fSAndroid Build Coastguard Worker 
42*795d594fSAndroid Build Coastguard Worker   compiler_driver_->PreCompile(class_loader,
43*795d594fSAndroid Build Coastguard Worker                                dex_files,
44*795d594fSAndroid Build Coastguard Worker                                timings,
45*795d594fSAndroid Build Coastguard Worker                                &compiler_options_->image_classes_);
46*795d594fSAndroid Build Coastguard Worker 
47*795d594fSAndroid Build Coastguard Worker   // Verification results in the `callback_` should not be used during compilation.
48*795d594fSAndroid Build Coastguard Worker   down_cast<QuickCompilerCallbacks*>(callbacks_.get())->SetVerificationResults(
49*795d594fSAndroid Build Coastguard Worker       reinterpret_cast<VerificationResults*>(inaccessible_page_));
50*795d594fSAndroid Build Coastguard Worker   compiler_driver_->CompileAll(class_loader, dex_files, timings);
51*795d594fSAndroid Build Coastguard Worker   down_cast<QuickCompilerCallbacks*>(callbacks_.get())->SetVerificationResults(
52*795d594fSAndroid Build Coastguard Worker       verification_results_.get());
53*795d594fSAndroid Build Coastguard Worker 
54*795d594fSAndroid Build Coastguard Worker   compiler_driver_->FreeThreadPools();
55*795d594fSAndroid Build Coastguard Worker }
56*795d594fSAndroid Build Coastguard Worker 
SetDexFilesForOatFile(const std::vector<const DexFile * > & dex_files)57*795d594fSAndroid Build Coastguard Worker void CommonCompilerDriverTest::SetDexFilesForOatFile(const std::vector<const DexFile*>& dex_files) {
58*795d594fSAndroid Build Coastguard Worker   compiler_options_->dex_files_for_oat_file_ = dex_files;
59*795d594fSAndroid Build Coastguard Worker   compiler_driver_->compiled_classes_.AddDexFiles(dex_files);
60*795d594fSAndroid Build Coastguard Worker }
61*795d594fSAndroid Build Coastguard Worker 
ReserveImageSpace()62*795d594fSAndroid Build Coastguard Worker void CommonCompilerDriverTest::ReserveImageSpace() {
63*795d594fSAndroid Build Coastguard Worker   // Reserve where the image will be loaded up front so that other parts of test set up don't
64*795d594fSAndroid Build Coastguard Worker   // accidentally end up colliding with the fixed memory address when we need to load the image.
65*795d594fSAndroid Build Coastguard Worker   std::string error_msg;
66*795d594fSAndroid Build Coastguard Worker   MemMap::Init();
67*795d594fSAndroid Build Coastguard Worker   image_reservation_ = MemMap::MapAnonymous("image reservation",
68*795d594fSAndroid Build Coastguard Worker                                             reinterpret_cast<uint8_t*>(ART_BASE_ADDRESS),
69*795d594fSAndroid Build Coastguard Worker                                             static_cast<size_t>(120 * 1024 * 1024),  // 120MB
70*795d594fSAndroid Build Coastguard Worker                                             PROT_NONE,
71*795d594fSAndroid Build Coastguard Worker                                             false /* no need for 4gb flag with fixed mmap */,
72*795d594fSAndroid Build Coastguard Worker                                             /*reuse=*/ false,
73*795d594fSAndroid Build Coastguard Worker                                             /*reservation=*/ nullptr,
74*795d594fSAndroid Build Coastguard Worker                                             &error_msg);
75*795d594fSAndroid Build Coastguard Worker   CHECK(image_reservation_.IsValid()) << error_msg;
76*795d594fSAndroid Build Coastguard Worker }
77*795d594fSAndroid Build Coastguard Worker 
UnreserveImageSpace()78*795d594fSAndroid Build Coastguard Worker void CommonCompilerDriverTest::UnreserveImageSpace() {
79*795d594fSAndroid Build Coastguard Worker   image_reservation_.Reset();
80*795d594fSAndroid Build Coastguard Worker }
81*795d594fSAndroid Build Coastguard Worker 
CreateCompilerDriver()82*795d594fSAndroid Build Coastguard Worker void CommonCompilerDriverTest::CreateCompilerDriver() {
83*795d594fSAndroid Build Coastguard Worker   ApplyInstructionSet();
84*795d594fSAndroid Build Coastguard Worker 
85*795d594fSAndroid Build Coastguard Worker   compiler_options_->image_type_ = CompilerOptions::ImageType::kBootImage;
86*795d594fSAndroid Build Coastguard Worker   compiler_options_->compile_pic_ = false;  // Non-PIC boot image is a test configuration.
87*795d594fSAndroid Build Coastguard Worker   compiler_options_->SetCompilerFilter(GetCompilerFilter());
88*795d594fSAndroid Build Coastguard Worker   compiler_options_->image_classes_.swap(*GetImageClasses());
89*795d594fSAndroid Build Coastguard Worker   compiler_options_->profile_compilation_info_ = GetProfileCompilationInfo();
90*795d594fSAndroid Build Coastguard Worker   compiler_driver_.reset(new CompilerDriver(compiler_options_.get(),
91*795d594fSAndroid Build Coastguard Worker                                             verification_results_.get(),
92*795d594fSAndroid Build Coastguard Worker                                             number_of_threads_,
93*795d594fSAndroid Build Coastguard Worker                                             /* swap_fd= */ -1));
94*795d594fSAndroid Build Coastguard Worker }
95*795d594fSAndroid Build Coastguard Worker 
SetUpRuntimeOptions(RuntimeOptions * options)96*795d594fSAndroid Build Coastguard Worker void CommonCompilerDriverTest::SetUpRuntimeOptions(RuntimeOptions* options) {
97*795d594fSAndroid Build Coastguard Worker   CommonCompilerTest::SetUpRuntimeOptions(options);
98*795d594fSAndroid Build Coastguard Worker 
99*795d594fSAndroid Build Coastguard Worker   verification_results_.reset(new VerificationResults());
100*795d594fSAndroid Build Coastguard Worker   QuickCompilerCallbacks* callbacks =
101*795d594fSAndroid Build Coastguard Worker       new QuickCompilerCallbacks(CompilerCallbacks::CallbackMode::kCompileApp);
102*795d594fSAndroid Build Coastguard Worker   callbacks->SetVerificationResults(verification_results_.get());
103*795d594fSAndroid Build Coastguard Worker   callbacks_.reset(callbacks);
104*795d594fSAndroid Build Coastguard Worker }
105*795d594fSAndroid Build Coastguard Worker 
SetUp()106*795d594fSAndroid Build Coastguard Worker void CommonCompilerDriverTest::SetUp() {
107*795d594fSAndroid Build Coastguard Worker   CommonCompilerTest::SetUp();
108*795d594fSAndroid Build Coastguard Worker 
109*795d594fSAndroid Build Coastguard Worker   CreateCompilerDriver();
110*795d594fSAndroid Build Coastguard Worker 
111*795d594fSAndroid Build Coastguard Worker   // Note: We cannot use MemMap because some tests tear down the Runtime and destroy
112*795d594fSAndroid Build Coastguard Worker   // the gMaps, so when destroying the MemMap, the test would crash.
113*795d594fSAndroid Build Coastguard Worker   const size_t page_size = MemMap::GetPageSize();
114*795d594fSAndroid Build Coastguard Worker   inaccessible_page_ = mmap(nullptr, page_size, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
115*795d594fSAndroid Build Coastguard Worker   CHECK(inaccessible_page_ != MAP_FAILED) << strerror(errno);
116*795d594fSAndroid Build Coastguard Worker }
117*795d594fSAndroid Build Coastguard Worker 
TearDown()118*795d594fSAndroid Build Coastguard Worker void CommonCompilerDriverTest::TearDown() {
119*795d594fSAndroid Build Coastguard Worker   if (inaccessible_page_ != nullptr) {
120*795d594fSAndroid Build Coastguard Worker     munmap(inaccessible_page_, MemMap::GetPageSize());
121*795d594fSAndroid Build Coastguard Worker     inaccessible_page_ = nullptr;
122*795d594fSAndroid Build Coastguard Worker   }
123*795d594fSAndroid Build Coastguard Worker   image_reservation_.Reset();
124*795d594fSAndroid Build Coastguard Worker   compiler_driver_.reset();
125*795d594fSAndroid Build Coastguard Worker   verification_results_.reset();
126*795d594fSAndroid Build Coastguard Worker 
127*795d594fSAndroid Build Coastguard Worker   CommonCompilerTest::TearDown();
128*795d594fSAndroid Build Coastguard Worker }
129*795d594fSAndroid Build Coastguard Worker 
130*795d594fSAndroid Build Coastguard Worker // Get the set of image classes given to the compiler options in CreateCompilerDriver().
GetImageClasses()131*795d594fSAndroid Build Coastguard Worker std::unique_ptr<HashSet<std::string>> CommonCompilerDriverTest::GetImageClasses() {
132*795d594fSAndroid Build Coastguard Worker   // Empty set: by default no classes are retained in the image.
133*795d594fSAndroid Build Coastguard Worker   return std::make_unique<HashSet<std::string>>();
134*795d594fSAndroid Build Coastguard Worker }
135*795d594fSAndroid Build Coastguard Worker 
136*795d594fSAndroid Build Coastguard Worker // Get ProfileCompilationInfo that should be passed to the driver.
GetProfileCompilationInfo()137*795d594fSAndroid Build Coastguard Worker ProfileCompilationInfo* CommonCompilerDriverTest::GetProfileCompilationInfo() {
138*795d594fSAndroid Build Coastguard Worker   // Null, profile information will not be taken into account.
139*795d594fSAndroid Build Coastguard Worker   return nullptr;
140*795d594fSAndroid Build Coastguard Worker }
141*795d594fSAndroid Build Coastguard Worker 
142*795d594fSAndroid Build Coastguard Worker }  // namespace art
143