common_compiler_test.cc revision 376b2bbf7c39108223a7a01568a7b4b04d84eeac
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 "class_linker.h" 21#include "compiled_method.h" 22#include "dex/quick_compiler_callbacks.h" 23#include "dex/verification_results.h" 24#include "dex/quick/dex_file_to_method_inliner_map.h" 25#include "driver/compiler_driver.h" 26#include "interpreter/interpreter.h" 27#include "mirror/art_method.h" 28#include "mirror/dex_cache.h" 29#include "mirror/object-inl.h" 30#include "scoped_thread_state_change.h" 31#include "thread-inl.h" 32#include "utils.h" 33 34namespace art { 35 36CommonCompilerTest::CommonCompilerTest() {} 37CommonCompilerTest::~CommonCompilerTest() {} 38 39void CommonCompilerTest::MakeExecutable(mirror::ArtMethod* method) { 40 CHECK(method != nullptr); 41 42 const CompiledMethod* compiled_method = nullptr; 43 if (!method->IsAbstract()) { 44 mirror::DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache(); 45 const DexFile& dex_file = *dex_cache->GetDexFile(); 46 compiled_method = 47 compiler_driver_->GetCompiledMethod(MethodReference(&dex_file, 48 method->GetDexMethodIndex())); 49 } 50 if (compiled_method != nullptr) { 51 const std::vector<uint8_t>* code = compiled_method->GetQuickCode(); 52 const void* code_ptr; 53 bool is_portable = (code == nullptr); 54 if (!is_portable) { 55 uint32_t code_size = code->size(); 56 CHECK_NE(0u, code_size); 57 const std::vector<uint8_t>& vmap_table = compiled_method->GetVmapTable(); 58 uint32_t vmap_table_offset = vmap_table.empty() ? 0u 59 : sizeof(OatQuickMethodHeader) + vmap_table.size(); 60 const std::vector<uint8_t>& mapping_table = *compiled_method->GetMappingTable(); 61 uint32_t mapping_table_offset = mapping_table.empty() ? 0u 62 : sizeof(OatQuickMethodHeader) + vmap_table.size() + mapping_table.size(); 63 const std::vector<uint8_t>& gc_map = *compiled_method->GetGcMap(); 64 uint32_t gc_map_offset = gc_map.empty() ? 0u 65 : sizeof(OatQuickMethodHeader) + vmap_table.size() + mapping_table.size() + gc_map.size(); 66 OatQuickMethodHeader method_header(mapping_table_offset, vmap_table_offset, gc_map_offset, 67 compiled_method->GetFrameSizeInBytes(), 68 compiled_method->GetCoreSpillMask(), 69 compiled_method->GetFpSpillMask(), code_size); 70 71 header_code_and_maps_chunks_.push_back(std::vector<uint8_t>()); 72 std::vector<uint8_t>* chunk = &header_code_and_maps_chunks_.back(); 73 size_t size = sizeof(method_header) + code_size + vmap_table.size() + mapping_table.size() + 74 gc_map.size(); 75 size_t code_offset = compiled_method->AlignCode(size - code_size); 76 size_t padding = code_offset - (size - code_size); 77 chunk->reserve(padding + size); 78 chunk->resize(sizeof(method_header)); 79 memcpy(&(*chunk)[0], &method_header, sizeof(method_header)); 80 chunk->insert(chunk->begin(), vmap_table.begin(), vmap_table.end()); 81 chunk->insert(chunk->begin(), mapping_table.begin(), mapping_table.end()); 82 chunk->insert(chunk->begin(), gc_map.begin(), gc_map.end()); 83 chunk->insert(chunk->begin(), padding, 0); 84 chunk->insert(chunk->end(), code->begin(), code->end()); 85 CHECK_EQ(padding + size, chunk->size()); 86 code_ptr = &(*chunk)[code_offset]; 87 } else { 88 code = compiled_method->GetPortableCode(); 89 code_ptr = &(*code)[0]; 90 } 91 MakeExecutable(code_ptr, code->size()); 92 const void* method_code = CompiledMethod::CodePointer(code_ptr, 93 compiled_method->GetInstructionSet()); 94 LOG(INFO) << "MakeExecutable " << PrettyMethod(method) << " code=" << method_code; 95 class_linker_->SetEntryPointsToCompiledCode(method, method_code, is_portable); 96 } else { 97 // No code? You must mean to go into the interpreter. 98 // Or the generic JNI... 99 class_linker_->SetEntryPointsToInterpreter(method); 100 } 101} 102 103void CommonCompilerTest::MakeExecutable(const void* code_start, size_t code_length) { 104 CHECK(code_start != nullptr); 105 CHECK_NE(code_length, 0U); 106 uintptr_t data = reinterpret_cast<uintptr_t>(code_start); 107 uintptr_t base = RoundDown(data, kPageSize); 108 uintptr_t limit = RoundUp(data + code_length, kPageSize); 109 uintptr_t len = limit - base; 110 int result = mprotect(reinterpret_cast<void*>(base), len, PROT_READ | PROT_WRITE | PROT_EXEC); 111 CHECK_EQ(result, 0); 112 113 // Flush instruction cache 114 // Only uses __builtin___clear_cache if GCC >= 4.3.3 115#if GCC_VERSION >= 40303 116 __builtin___clear_cache(reinterpret_cast<void*>(base), reinterpret_cast<void*>(base + len)); 117#else 118 // Only warn if not Intel as Intel doesn't have cache flush instructions. 119#if !defined(__i386__) && !defined(__x86_64__) 120 UNIMPLEMENTED(WARNING) << "cache flush"; 121#endif 122#endif 123} 124 125void CommonCompilerTest::MakeExecutable(mirror::ClassLoader* class_loader, const char* class_name) { 126 std::string class_descriptor(DotToDescriptor(class_name)); 127 Thread* self = Thread::Current(); 128 StackHandleScope<1> hs(self); 129 Handle<mirror::ClassLoader> loader(hs.NewHandle(class_loader)); 130 mirror::Class* klass = class_linker_->FindClass(self, class_descriptor.c_str(), loader); 131 CHECK(klass != nullptr) << "Class not found " << class_name; 132 for (size_t i = 0; i < klass->NumDirectMethods(); i++) { 133 MakeExecutable(klass->GetDirectMethod(i)); 134 } 135 for (size_t i = 0; i < klass->NumVirtualMethods(); i++) { 136 MakeExecutable(klass->GetVirtualMethod(i)); 137 } 138} 139 140void CommonCompilerTest::SetUp() { 141 CommonRuntimeTest::SetUp(); 142 { 143 ScopedObjectAccess soa(Thread::Current()); 144 145 const InstructionSet instruction_set = kRuntimeISA; 146 // Take the default set of instruction features from the build. 147 instruction_set_features_.reset(InstructionSetFeatures::FromCppDefines()); 148 149 runtime_->SetInstructionSet(instruction_set); 150 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) { 151 Runtime::CalleeSaveType type = Runtime::CalleeSaveType(i); 152 if (!runtime_->HasCalleeSaveMethod(type)) { 153 runtime_->SetCalleeSaveMethod(runtime_->CreateCalleeSaveMethod(), type); 154 } 155 } 156 157 // TODO: make selectable 158 Compiler::Kind compiler_kind = kUsePortableCompiler ? Compiler::kPortable : Compiler::kQuick; 159 timer_.reset(new CumulativeLogger("Compilation times")); 160 compiler_driver_.reset(new CompilerDriver(compiler_options_.get(), 161 verification_results_.get(), 162 method_inliner_map_.get(), 163 compiler_kind, instruction_set, 164 instruction_set_features_.get(), 165 true, new std::set<std::string>, nullptr, 166 2, true, true, timer_.get(), "")); 167 } 168 // We typically don't generate an image in unit tests, disable this optimization by default. 169 compiler_driver_->SetSupportBootImageFixup(false); 170} 171 172void CommonCompilerTest::SetUpRuntimeOptions(RuntimeOptions* options) { 173 CommonRuntimeTest::SetUpRuntimeOptions(options); 174 175 compiler_options_.reset(new CompilerOptions); 176 verification_results_.reset(new VerificationResults(compiler_options_.get())); 177 method_inliner_map_.reset(new DexFileToMethodInlinerMap); 178 callbacks_.reset(new QuickCompilerCallbacks(verification_results_.get(), 179 method_inliner_map_.get())); 180 options->push_back(std::make_pair("compilercallbacks", callbacks_.get())); 181} 182 183void CommonCompilerTest::TearDown() { 184 timer_.reset(); 185 compiler_driver_.reset(); 186 callbacks_.reset(); 187 method_inliner_map_.reset(); 188 verification_results_.reset(); 189 compiler_options_.reset(); 190 191 CommonRuntimeTest::TearDown(); 192} 193 194void CommonCompilerTest::CompileClass(mirror::ClassLoader* class_loader, const char* class_name) { 195 std::string class_descriptor(DotToDescriptor(class_name)); 196 Thread* self = Thread::Current(); 197 StackHandleScope<1> hs(self); 198 Handle<mirror::ClassLoader> loader(hs.NewHandle(class_loader)); 199 mirror::Class* klass = class_linker_->FindClass(self, class_descriptor.c_str(), loader); 200 CHECK(klass != nullptr) << "Class not found " << class_name; 201 for (size_t i = 0; i < klass->NumDirectMethods(); i++) { 202 CompileMethod(klass->GetDirectMethod(i)); 203 } 204 for (size_t i = 0; i < klass->NumVirtualMethods(); i++) { 205 CompileMethod(klass->GetVirtualMethod(i)); 206 } 207} 208 209void CommonCompilerTest::CompileMethod(mirror::ArtMethod* method) { 210 CHECK(method != nullptr); 211 TimingLogger timings("CommonTest::CompileMethod", false, false); 212 TimingLogger::ScopedTiming t(__FUNCTION__, &timings); 213 compiler_driver_->CompileOne(method, &timings); 214 TimingLogger::ScopedTiming t2("MakeExecutable", &timings); 215 MakeExecutable(method); 216} 217 218void CommonCompilerTest::CompileDirectMethod(Handle<mirror::ClassLoader> class_loader, 219 const char* class_name, const char* method_name, 220 const char* signature) { 221 std::string class_descriptor(DotToDescriptor(class_name)); 222 Thread* self = Thread::Current(); 223 mirror::Class* klass = class_linker_->FindClass(self, class_descriptor.c_str(), class_loader); 224 CHECK(klass != nullptr) << "Class not found " << class_name; 225 mirror::ArtMethod* method = klass->FindDirectMethod(method_name, signature); 226 CHECK(method != nullptr) << "Direct method not found: " 227 << class_name << "." << method_name << signature; 228 CompileMethod(method); 229} 230 231void CommonCompilerTest::CompileVirtualMethod(Handle<mirror::ClassLoader> class_loader, 232 const char* class_name, const char* method_name, 233 const char* signature) { 234 std::string class_descriptor(DotToDescriptor(class_name)); 235 Thread* self = Thread::Current(); 236 mirror::Class* klass = class_linker_->FindClass(self, class_descriptor.c_str(), class_loader); 237 CHECK(klass != nullptr) << "Class not found " << class_name; 238 mirror::ArtMethod* method = klass->FindVirtualMethod(method_name, signature); 239 CHECK(method != NULL) << "Virtual method not found: " 240 << class_name << "." << method_name << signature; 241 CompileMethod(method); 242} 243 244void CommonCompilerTest::ReserveImageSpace() { 245 // Reserve where the image will be loaded up front so that other parts of test set up don't 246 // accidentally end up colliding with the fixed memory address when we need to load the image. 247 std::string error_msg; 248 MemMap::Init(); 249 image_reservation_.reset(MemMap::MapAnonymous("image reservation", 250 reinterpret_cast<uint8_t*>(ART_BASE_ADDRESS), 251 (size_t)100 * 1024 * 1024, // 100MB 252 PROT_NONE, 253 false /* no need for 4gb flag with fixed mmap*/, 254 &error_msg)); 255 CHECK(image_reservation_.get() != nullptr) << error_msg; 256} 257 258void CommonCompilerTest::UnreserveImageSpace() { 259 image_reservation_.reset(); 260} 261 262} // namespace art 263