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