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