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