common_compiler_test.cc revision 1d6fdaf2ed078c774e2c88c5132b23ab35259308
1/* 2 * Copyright (C) 2011 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 "common_compiler_test.h" 18 19#include "arch/instruction_set_features.h" 20#include "art_field-inl.h" 21#include "art_method.h" 22#include "class_linker.h" 23#include "compiled_method.h" 24#include "dex/quick_compiler_callbacks.h" 25#include "dex/quick/dex_file_to_method_inliner_map.h" 26#include "dex/verification_results.h" 27#include "driver/compiler_driver.h" 28#include "driver/compiler_options.h" 29#include "interpreter/interpreter.h" 30#include "mirror/class_loader.h" 31#include "mirror/class-inl.h" 32#include "mirror/dex_cache.h" 33#include "mirror/object-inl.h" 34#include "oat_quick_method_header.h" 35#include "scoped_thread_state_change.h" 36#include "thread-inl.h" 37#include "utils.h" 38 39namespace art { 40 41CommonCompilerTest::CommonCompilerTest() {} 42CommonCompilerTest::~CommonCompilerTest() {} 43 44void CommonCompilerTest::MakeExecutable(ArtMethod* method) { 45 CHECK(method != nullptr); 46 47 const CompiledMethod* compiled_method = nullptr; 48 if (!method->IsAbstract()) { 49 mirror::DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache(); 50 const DexFile& dex_file = *dex_cache->GetDexFile(); 51 compiled_method = 52 compiler_driver_->GetCompiledMethod(MethodReference(&dex_file, 53 method->GetDexMethodIndex())); 54 } 55 if (compiled_method != nullptr) { 56 ArrayRef<const uint8_t> code = compiled_method->GetQuickCode(); 57 uint32_t code_size = code.size(); 58 CHECK_NE(0u, code_size); 59 ArrayRef<const uint8_t> vmap_table = compiled_method->GetVmapTable(); 60 uint32_t vmap_table_offset = vmap_table.empty() ? 0u 61 : sizeof(OatQuickMethodHeader) + vmap_table.size(); 62 OatQuickMethodHeader method_header(vmap_table_offset, 63 compiled_method->GetFrameSizeInBytes(), 64 compiled_method->GetCoreSpillMask(), 65 compiled_method->GetFpSpillMask(), 66 code_size); 67 68 header_code_and_maps_chunks_.push_back(std::vector<uint8_t>()); 69 std::vector<uint8_t>* chunk = &header_code_and_maps_chunks_.back(); 70 const size_t max_padding = GetInstructionSetAlignment(compiled_method->GetInstructionSet()); 71 const size_t size = vmap_table.size() + sizeof(method_header) + code_size; 72 chunk->reserve(size + max_padding); 73 chunk->resize(sizeof(method_header)); 74 memcpy(&(*chunk)[0], &method_header, sizeof(method_header)); 75 chunk->insert(chunk->begin(), vmap_table.begin(), vmap_table.end()); 76 chunk->insert(chunk->end(), code.begin(), code.end()); 77 CHECK_EQ(chunk->size(), size); 78 const void* unaligned_code_ptr = chunk->data() + (size - code_size); 79 size_t offset = dchecked_integral_cast<size_t>(reinterpret_cast<uintptr_t>(unaligned_code_ptr)); 80 size_t padding = compiled_method->AlignCode(offset) - offset; 81 // Make sure no resizing takes place. 82 CHECK_GE(chunk->capacity(), chunk->size() + padding); 83 chunk->insert(chunk->begin(), padding, 0); 84 const void* code_ptr = reinterpret_cast<const uint8_t*>(unaligned_code_ptr) + padding; 85 CHECK_EQ(code_ptr, static_cast<const void*>(chunk->data() + (chunk->size() - code_size))); 86 MakeExecutable(code_ptr, code.size()); 87 const void* method_code = CompiledMethod::CodePointer(code_ptr, 88 compiled_method->GetInstructionSet()); 89 LOG(INFO) << "MakeExecutable " << PrettyMethod(method) << " code=" << method_code; 90 class_linker_->SetEntryPointsToCompiledCode(method, method_code); 91 } else { 92 // No code? You must mean to go into the interpreter. 93 // Or the generic JNI... 94 class_linker_->SetEntryPointsToInterpreter(method); 95 } 96} 97 98void CommonCompilerTest::MakeExecutable(const void* code_start, size_t code_length) { 99 CHECK(code_start != nullptr); 100 CHECK_NE(code_length, 0U); 101 uintptr_t data = reinterpret_cast<uintptr_t>(code_start); 102 uintptr_t base = RoundDown(data, kPageSize); 103 uintptr_t limit = RoundUp(data + code_length, kPageSize); 104 uintptr_t len = limit - base; 105 int result = mprotect(reinterpret_cast<void*>(base), len, PROT_READ | PROT_WRITE | PROT_EXEC); 106 CHECK_EQ(result, 0); 107 108 FlushInstructionCache(reinterpret_cast<char*>(base), reinterpret_cast<char*>(base + len)); 109} 110 111void CommonCompilerTest::MakeExecutable(mirror::ClassLoader* class_loader, const char* class_name) { 112 std::string class_descriptor(DotToDescriptor(class_name)); 113 Thread* self = Thread::Current(); 114 StackHandleScope<1> hs(self); 115 Handle<mirror::ClassLoader> loader(hs.NewHandle(class_loader)); 116 mirror::Class* klass = class_linker_->FindClass(self, class_descriptor.c_str(), loader); 117 CHECK(klass != nullptr) << "Class not found " << class_name; 118 size_t pointer_size = class_linker_->GetImagePointerSize(); 119 for (auto& m : klass->GetMethods(pointer_size)) { 120 MakeExecutable(&m); 121 } 122} 123 124// Get the set of image classes given to the compiler-driver in SetUp. Note: the compiler 125// driver assumes ownership of the set, so the test should properly release the set. 126std::unordered_set<std::string>* CommonCompilerTest::GetImageClasses() { 127 // Empty set: by default no classes are retained in the image. 128 return new std::unordered_set<std::string>(); 129} 130 131// Get the set of compiled classes given to the compiler-driver in SetUp. Note: the compiler 132// driver assumes ownership of the set, so the test should properly release the set. 133std::unordered_set<std::string>* CommonCompilerTest::GetCompiledClasses() { 134 // Null, no selection of compiled-classes. 135 return nullptr; 136} 137 138// Get the set of compiled methods given to the compiler-driver in SetUp. Note: the compiler 139// driver assumes ownership of the set, so the test should properly release the set. 140std::unordered_set<std::string>* CommonCompilerTest::GetCompiledMethods() { 141 // Null, no selection of compiled-methods. 142 return nullptr; 143} 144 145// Get ProfileCompilationInfo that should be passed to the driver. 146ProfileCompilationInfo* CommonCompilerTest::GetProfileCompilationInfo() { 147 // Null, profile information will not be taken into account. 148 return nullptr; 149} 150 151void CommonCompilerTest::SetUp() { 152 CommonRuntimeTest::SetUp(); 153 { 154 ScopedObjectAccess soa(Thread::Current()); 155 156 const InstructionSet instruction_set = kRuntimeISA; 157 // Take the default set of instruction features from the build. 158 instruction_set_features_.reset(InstructionSetFeatures::FromCppDefines()); 159 160 runtime_->SetInstructionSet(instruction_set); 161 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) { 162 Runtime::CalleeSaveType type = Runtime::CalleeSaveType(i); 163 if (!runtime_->HasCalleeSaveMethod(type)) { 164 runtime_->SetCalleeSaveMethod(runtime_->CreateCalleeSaveMethod(), type); 165 } 166 } 167 168 timer_.reset(new CumulativeLogger("Compilation times")); 169 CreateCompilerDriver(compiler_kind_, instruction_set); 170 } 171} 172 173void CommonCompilerTest::CreateCompilerDriver(Compiler::Kind kind, 174 InstructionSet isa, 175 size_t number_of_threads) { 176 compiler_driver_.reset(new CompilerDriver(compiler_options_.get(), 177 verification_results_.get(), 178 method_inliner_map_.get(), 179 kind, 180 isa, 181 instruction_set_features_.get(), 182 /* boot_image */ true, 183 GetImageClasses(), 184 GetCompiledClasses(), 185 GetCompiledMethods(), 186 number_of_threads, 187 /* dump_stats */ true, 188 /* dump_passes */ true, 189 timer_.get(), 190 /* swap_fd */ -1, 191 GetProfileCompilationInfo())); 192 // We typically don't generate an image in unit tests, disable this optimization by default. 193 compiler_driver_->SetSupportBootImageFixup(false); 194} 195 196void CommonCompilerTest::SetUpRuntimeOptions(RuntimeOptions* options) { 197 CommonRuntimeTest::SetUpRuntimeOptions(options); 198 199 compiler_options_.reset(new CompilerOptions); 200 verification_results_.reset(new VerificationResults(compiler_options_.get())); 201 method_inliner_map_.reset(new DexFileToMethodInlinerMap); 202 callbacks_.reset(new QuickCompilerCallbacks(verification_results_.get(), 203 method_inliner_map_.get(), 204 CompilerCallbacks::CallbackMode::kCompileApp)); 205} 206 207Compiler::Kind CommonCompilerTest::GetCompilerKind() const { 208 return compiler_kind_; 209} 210 211void CommonCompilerTest::SetCompilerKind(Compiler::Kind compiler_kind) { 212 compiler_kind_ = compiler_kind; 213} 214 215InstructionSet CommonCompilerTest::GetInstructionSet() const { 216 DCHECK(compiler_driver_.get() != nullptr); 217 return compiler_driver_->GetInstructionSet(); 218} 219 220void CommonCompilerTest::TearDown() { 221 timer_.reset(); 222 compiler_driver_.reset(); 223 callbacks_.reset(); 224 method_inliner_map_.reset(); 225 verification_results_.reset(); 226 compiler_options_.reset(); 227 228 CommonRuntimeTest::TearDown(); 229} 230 231void CommonCompilerTest::CompileClass(mirror::ClassLoader* class_loader, const char* class_name) { 232 std::string class_descriptor(DotToDescriptor(class_name)); 233 Thread* self = Thread::Current(); 234 StackHandleScope<1> hs(self); 235 Handle<mirror::ClassLoader> loader(hs.NewHandle(class_loader)); 236 mirror::Class* klass = class_linker_->FindClass(self, class_descriptor.c_str(), loader); 237 CHECK(klass != nullptr) << "Class not found " << class_name; 238 auto pointer_size = class_linker_->GetImagePointerSize(); 239 for (auto& m : klass->GetMethods(pointer_size)) { 240 CompileMethod(&m); 241 } 242} 243 244void CommonCompilerTest::CompileMethod(ArtMethod* method) { 245 CHECK(method != nullptr); 246 TimingLogger timings("CommonTest::CompileMethod", false, false); 247 TimingLogger::ScopedTiming t(__FUNCTION__, &timings); 248 compiler_driver_->CompileOne(Thread::Current(), method, &timings); 249 TimingLogger::ScopedTiming t2("MakeExecutable", &timings); 250 MakeExecutable(method); 251} 252 253void CommonCompilerTest::CompileDirectMethod(Handle<mirror::ClassLoader> class_loader, 254 const char* class_name, const char* method_name, 255 const char* signature) { 256 std::string class_descriptor(DotToDescriptor(class_name)); 257 Thread* self = Thread::Current(); 258 mirror::Class* klass = class_linker_->FindClass(self, class_descriptor.c_str(), class_loader); 259 CHECK(klass != nullptr) << "Class not found " << class_name; 260 auto pointer_size = class_linker_->GetImagePointerSize(); 261 ArtMethod* method = klass->FindDirectMethod(method_name, signature, pointer_size); 262 CHECK(method != nullptr) << "Direct method not found: " 263 << class_name << "." << method_name << signature; 264 CompileMethod(method); 265} 266 267void CommonCompilerTest::CompileVirtualMethod(Handle<mirror::ClassLoader> class_loader, 268 const char* class_name, const char* method_name, 269 const char* signature) { 270 std::string class_descriptor(DotToDescriptor(class_name)); 271 Thread* self = Thread::Current(); 272 mirror::Class* klass = class_linker_->FindClass(self, class_descriptor.c_str(), class_loader); 273 CHECK(klass != nullptr) << "Class not found " << class_name; 274 auto pointer_size = class_linker_->GetImagePointerSize(); 275 ArtMethod* method = klass->FindVirtualMethod(method_name, signature, pointer_size); 276 CHECK(method != nullptr) << "Virtual method not found: " 277 << class_name << "." << method_name << signature; 278 CompileMethod(method); 279} 280 281void CommonCompilerTest::ReserveImageSpace() { 282 // Reserve where the image will be loaded up front so that other parts of test set up don't 283 // accidentally end up colliding with the fixed memory address when we need to load the image. 284 std::string error_msg; 285 MemMap::Init(); 286 image_reservation_.reset(MemMap::MapAnonymous("image reservation", 287 reinterpret_cast<uint8_t*>(ART_BASE_ADDRESS), 288 (size_t)120 * 1024 * 1024, // 120MB 289 PROT_NONE, 290 false /* no need for 4gb flag with fixed mmap*/, 291 false /* not reusing existing reservation */, 292 &error_msg)); 293 CHECK(image_reservation_.get() != nullptr) << error_msg; 294} 295 296void CommonCompilerTest::UnreserveImageSpace() { 297 image_reservation_.reset(); 298} 299 300} // namespace art 301