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" |
Mathieu Chartier | 5bdab12 | 2015-01-26 18:30:19 -0800 | [diff] [blame] | 22 | #include "dex/pass_manager.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 23 | #include "dex/quick_compiler_callbacks.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 24 | #include "dex/quick/dex_file_to_method_inliner_map.h" |
Mathieu Chartier | 5bdab12 | 2015-01-26 18:30:19 -0800 | [diff] [blame] | 25 | #include "dex/verification_results.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 26 | #include "driver/compiler_driver.h" |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 27 | #include "driver/compiler_options.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 28 | #include "interpreter/interpreter.h" |
| 29 | #include "mirror/art_method.h" |
| 30 | #include "mirror/dex_cache.h" |
| 31 | #include "mirror/object-inl.h" |
| 32 | #include "scoped_thread_state_change.h" |
| 33 | #include "thread-inl.h" |
| 34 | #include "utils.h" |
| 35 | |
| 36 | namespace art { |
| 37 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 38 | CommonCompilerTest::CommonCompilerTest() {} |
| 39 | CommonCompilerTest::~CommonCompilerTest() {} |
| 40 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 41 | void CommonCompilerTest::MakeExecutable(mirror::ArtMethod* method) { |
| 42 | CHECK(method != nullptr); |
| 43 | |
| 44 | const CompiledMethod* compiled_method = nullptr; |
| 45 | if (!method->IsAbstract()) { |
| 46 | mirror::DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache(); |
| 47 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
| 48 | compiled_method = |
| 49 | compiler_driver_->GetCompiledMethod(MethodReference(&dex_file, |
| 50 | method->GetDexMethodIndex())); |
| 51 | } |
| 52 | if (compiled_method != nullptr) { |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 53 | const SwapVector<uint8_t>* code = compiled_method->GetQuickCode(); |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 54 | uint32_t code_size = code->size(); |
| 55 | CHECK_NE(0u, code_size); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 56 | const SwapVector<uint8_t>* vmap_table = compiled_method->GetVmapTable(); |
| 57 | uint32_t vmap_table_offset = vmap_table->empty() ? 0u |
| 58 | : sizeof(OatQuickMethodHeader) + vmap_table->size(); |
Andreas Gampe | a2d0afc | 2014-12-22 13:43:33 -0800 | [diff] [blame] | 59 | const SwapVector<uint8_t>* mapping_table = compiled_method->GetMappingTable(); |
| 60 | bool mapping_table_used = mapping_table != nullptr && !mapping_table->empty(); |
| 61 | size_t mapping_table_size = mapping_table_used ? mapping_table->size() : 0U; |
| 62 | uint32_t mapping_table_offset = !mapping_table_used ? 0u |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 63 | : sizeof(OatQuickMethodHeader) + vmap_table->size() + mapping_table_size; |
Andreas Gampe | a2d0afc | 2014-12-22 13:43:33 -0800 | [diff] [blame] | 64 | const SwapVector<uint8_t>* gc_map = compiled_method->GetGcMap(); |
| 65 | bool gc_map_used = gc_map != nullptr && !gc_map->empty(); |
| 66 | size_t gc_map_size = gc_map_used ? gc_map->size() : 0U; |
| 67 | uint32_t gc_map_offset = !gc_map_used ? 0u |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 68 | : sizeof(OatQuickMethodHeader) + vmap_table->size() + mapping_table_size + gc_map_size; |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 69 | OatQuickMethodHeader method_header(mapping_table_offset, vmap_table_offset, gc_map_offset, |
| 70 | compiled_method->GetFrameSizeInBytes(), |
| 71 | compiled_method->GetCoreSpillMask(), |
| 72 | compiled_method->GetFpSpillMask(), code_size); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 73 | |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 74 | header_code_and_maps_chunks_.push_back(std::vector<uint8_t>()); |
| 75 | std::vector<uint8_t>* chunk = &header_code_and_maps_chunks_.back(); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 76 | size_t size = sizeof(method_header) + code_size + vmap_table->size() + mapping_table_size + |
Andreas Gampe | a2d0afc | 2014-12-22 13:43:33 -0800 | [diff] [blame] | 77 | gc_map_size; |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 78 | size_t code_offset = compiled_method->AlignCode(size - code_size); |
| 79 | size_t padding = code_offset - (size - code_size); |
| 80 | chunk->reserve(padding + size); |
| 81 | chunk->resize(sizeof(method_header)); |
| 82 | memcpy(&(*chunk)[0], &method_header, sizeof(method_header)); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 83 | chunk->insert(chunk->begin(), vmap_table->begin(), vmap_table->end()); |
Andreas Gampe | a2d0afc | 2014-12-22 13:43:33 -0800 | [diff] [blame] | 84 | if (mapping_table_used) { |
| 85 | chunk->insert(chunk->begin(), mapping_table->begin(), mapping_table->end()); |
| 86 | } |
| 87 | if (gc_map_used) { |
| 88 | chunk->insert(chunk->begin(), gc_map->begin(), gc_map->end()); |
| 89 | } |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 90 | chunk->insert(chunk->begin(), padding, 0); |
| 91 | chunk->insert(chunk->end(), code->begin(), code->end()); |
| 92 | CHECK_EQ(padding + size, chunk->size()); |
| 93 | const void* code_ptr = &(*chunk)[code_offset]; |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 94 | MakeExecutable(code_ptr, code->size()); |
| 95 | const void* method_code = CompiledMethod::CodePointer(code_ptr, |
| 96 | compiled_method->GetInstructionSet()); |
| 97 | LOG(INFO) << "MakeExecutable " << PrettyMethod(method) << " code=" << method_code; |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 98 | class_linker_->SetEntryPointsToCompiledCode(method, method_code); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 99 | } else { |
| 100 | // No code? You must mean to go into the interpreter. |
| 101 | // Or the generic JNI... |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 102 | class_linker_->SetEntryPointsToInterpreter(method); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
| 106 | void CommonCompilerTest::MakeExecutable(const void* code_start, size_t code_length) { |
| 107 | CHECK(code_start != nullptr); |
| 108 | CHECK_NE(code_length, 0U); |
| 109 | uintptr_t data = reinterpret_cast<uintptr_t>(code_start); |
| 110 | uintptr_t base = RoundDown(data, kPageSize); |
| 111 | uintptr_t limit = RoundUp(data + code_length, kPageSize); |
| 112 | uintptr_t len = limit - base; |
| 113 | int result = mprotect(reinterpret_cast<void*>(base), len, PROT_READ | PROT_WRITE | PROT_EXEC); |
| 114 | CHECK_EQ(result, 0); |
| 115 | |
| 116 | // Flush instruction cache |
| 117 | // Only uses __builtin___clear_cache if GCC >= 4.3.3 |
| 118 | #if GCC_VERSION >= 40303 |
| 119 | __builtin___clear_cache(reinterpret_cast<void*>(base), reinterpret_cast<void*>(base + len)); |
| 120 | #else |
| 121 | // Only warn if not Intel as Intel doesn't have cache flush instructions. |
| 122 | #if !defined(__i386__) && !defined(__x86_64__) |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 123 | UNIMPLEMENTED(WARNING) << "cache flush"; |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 124 | #endif |
| 125 | #endif |
| 126 | } |
| 127 | |
| 128 | void 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 | for (size_t i = 0; i < klass->NumDirectMethods(); i++) { |
| 136 | MakeExecutable(klass->GetDirectMethod(i)); |
| 137 | } |
| 138 | for (size_t i = 0; i < klass->NumVirtualMethods(); i++) { |
| 139 | MakeExecutable(klass->GetVirtualMethod(i)); |
| 140 | } |
| 141 | } |
| 142 | |
Andreas Gampe | 70bef0d | 2015-04-15 02:37:28 -0700 | [diff] [blame] | 143 | // Get the set of image classes given to the compiler-driver in SetUp. Note: the compiler |
| 144 | // driver assumes ownership of the set, so the test should properly release the set. |
| 145 | std::unordered_set<std::string>* CommonCompilerTest::GetImageClasses() { |
| 146 | // Empty set: by default no classes are retained in the image. |
| 147 | return new std::unordered_set<std::string>(); |
| 148 | } |
| 149 | |
| 150 | // Get the set of compiled classes given to the compiler-driver in SetUp. Note: the compiler |
| 151 | // driver assumes ownership of the set, so the test should properly release the set. |
| 152 | std::unordered_set<std::string>* CommonCompilerTest::GetCompiledClasses() { |
| 153 | // Null, no selection of compiled-classes. |
| 154 | return nullptr; |
| 155 | } |
| 156 | |
| 157 | // Get the set of compiled methods given to the compiler-driver in SetUp. Note: the compiler |
| 158 | // driver assumes ownership of the set, so the test should properly release the set. |
| 159 | std::unordered_set<std::string>* CommonCompilerTest::GetCompiledMethods() { |
| 160 | // Null, no selection of compiled-methods. |
| 161 | return nullptr; |
| 162 | } |
| 163 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 164 | void CommonCompilerTest::SetUp() { |
| 165 | CommonRuntimeTest::SetUp(); |
| 166 | { |
| 167 | ScopedObjectAccess soa(Thread::Current()); |
| 168 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 169 | const InstructionSet instruction_set = kRuntimeISA; |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 170 | // Take the default set of instruction features from the build. |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 171 | instruction_set_features_.reset(InstructionSetFeatures::FromCppDefines()); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 172 | |
| 173 | runtime_->SetInstructionSet(instruction_set); |
| 174 | for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) { |
| 175 | Runtime::CalleeSaveType type = Runtime::CalleeSaveType(i); |
| 176 | if (!runtime_->HasCalleeSaveMethod(type)) { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 177 | runtime_->SetCalleeSaveMethod(runtime_->CreateCalleeSaveMethod(), type); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | |
| 181 | // TODO: make selectable |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 182 | Compiler::Kind compiler_kind = Compiler::kQuick; |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 183 | timer_.reset(new CumulativeLogger("Compilation times")); |
| 184 | compiler_driver_.reset(new CompilerDriver(compiler_options_.get(), |
| 185 | verification_results_.get(), |
| 186 | method_inliner_map_.get(), |
| 187 | compiler_kind, instruction_set, |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 188 | instruction_set_features_.get(), |
Andreas Gampe | 70bef0d | 2015-04-15 02:37:28 -0700 | [diff] [blame] | 189 | true, |
| 190 | GetImageClasses(), |
| 191 | GetCompiledClasses(), |
| 192 | GetCompiledMethods(), |
David Brazdil | 866c031 | 2015-01-13 21:21:31 +0000 | [diff] [blame] | 193 | 2, true, true, "", timer_.get(), -1, "")); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 194 | } |
| 195 | // We typically don't generate an image in unit tests, disable this optimization by default. |
| 196 | compiler_driver_->SetSupportBootImageFixup(false); |
| 197 | } |
| 198 | |
| 199 | void CommonCompilerTest::SetUpRuntimeOptions(RuntimeOptions* options) { |
| 200 | CommonRuntimeTest::SetUpRuntimeOptions(options); |
| 201 | |
| 202 | compiler_options_.reset(new CompilerOptions); |
| 203 | verification_results_.reset(new VerificationResults(compiler_options_.get())); |
| 204 | method_inliner_map_.reset(new DexFileToMethodInlinerMap); |
| 205 | callbacks_.reset(new QuickCompilerCallbacks(verification_results_.get(), |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 206 | method_inliner_map_.get(), |
Andreas Gampe | 4585f87 | 2015-03-27 23:45:15 -0700 | [diff] [blame] | 207 | CompilerCallbacks::CallbackMode::kCompileApp)); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | void CommonCompilerTest::TearDown() { |
| 211 | timer_.reset(); |
| 212 | compiler_driver_.reset(); |
| 213 | callbacks_.reset(); |
| 214 | method_inliner_map_.reset(); |
| 215 | verification_results_.reset(); |
| 216 | compiler_options_.reset(); |
| 217 | |
| 218 | CommonRuntimeTest::TearDown(); |
| 219 | } |
| 220 | |
| 221 | void CommonCompilerTest::CompileClass(mirror::ClassLoader* class_loader, const char* class_name) { |
| 222 | std::string class_descriptor(DotToDescriptor(class_name)); |
| 223 | Thread* self = Thread::Current(); |
| 224 | StackHandleScope<1> hs(self); |
| 225 | Handle<mirror::ClassLoader> loader(hs.NewHandle(class_loader)); |
| 226 | mirror::Class* klass = class_linker_->FindClass(self, class_descriptor.c_str(), loader); |
| 227 | CHECK(klass != nullptr) << "Class not found " << class_name; |
| 228 | for (size_t i = 0; i < klass->NumDirectMethods(); i++) { |
| 229 | CompileMethod(klass->GetDirectMethod(i)); |
| 230 | } |
| 231 | for (size_t i = 0; i < klass->NumVirtualMethods(); i++) { |
| 232 | CompileMethod(klass->GetVirtualMethod(i)); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | void CommonCompilerTest::CompileMethod(mirror::ArtMethod* method) { |
| 237 | CHECK(method != nullptr); |
| 238 | TimingLogger timings("CommonTest::CompileMethod", false, false); |
| 239 | TimingLogger::ScopedTiming t(__FUNCTION__, &timings); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 240 | compiler_driver_->CompileOne(Thread::Current(), method, &timings); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 241 | TimingLogger::ScopedTiming t2("MakeExecutable", &timings); |
| 242 | MakeExecutable(method); |
| 243 | } |
| 244 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 245 | void CommonCompilerTest::CompileDirectMethod(Handle<mirror::ClassLoader> class_loader, |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 246 | const char* class_name, const char* method_name, |
| 247 | const char* signature) { |
| 248 | std::string class_descriptor(DotToDescriptor(class_name)); |
| 249 | Thread* self = Thread::Current(); |
| 250 | mirror::Class* klass = class_linker_->FindClass(self, class_descriptor.c_str(), class_loader); |
| 251 | CHECK(klass != nullptr) << "Class not found " << class_name; |
| 252 | mirror::ArtMethod* method = klass->FindDirectMethod(method_name, signature); |
| 253 | CHECK(method != nullptr) << "Direct method not found: " |
| 254 | << class_name << "." << method_name << signature; |
| 255 | CompileMethod(method); |
| 256 | } |
| 257 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 258 | void CommonCompilerTest::CompileVirtualMethod(Handle<mirror::ClassLoader> class_loader, |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 259 | const char* class_name, const char* method_name, |
| 260 | const char* signature) { |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 261 | std::string class_descriptor(DotToDescriptor(class_name)); |
| 262 | Thread* self = Thread::Current(); |
| 263 | mirror::Class* klass = class_linker_->FindClass(self, class_descriptor.c_str(), class_loader); |
| 264 | CHECK(klass != nullptr) << "Class not found " << class_name; |
| 265 | mirror::ArtMethod* method = klass->FindVirtualMethod(method_name, signature); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 266 | CHECK(method != nullptr) << "Virtual method not found: " |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 267 | << class_name << "." << method_name << signature; |
| 268 | CompileMethod(method); |
| 269 | } |
| 270 | |
| 271 | void CommonCompilerTest::ReserveImageSpace() { |
| 272 | // Reserve where the image will be loaded up front so that other parts of test set up don't |
| 273 | // accidentally end up colliding with the fixed memory address when we need to load the image. |
| 274 | std::string error_msg; |
Mathieu Chartier | 6e88ef6 | 2014-10-14 15:01:24 -0700 | [diff] [blame] | 275 | MemMap::Init(); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 276 | image_reservation_.reset(MemMap::MapAnonymous("image reservation", |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 277 | reinterpret_cast<uint8_t*>(ART_BASE_ADDRESS), |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 278 | (size_t)100 * 1024 * 1024, // 100MB |
| 279 | PROT_NONE, |
| 280 | false /* no need for 4gb flag with fixed mmap*/, |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 281 | false /* not reusing existing reservation */, |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 282 | &error_msg)); |
| 283 | CHECK(image_reservation_.get() != nullptr) << error_msg; |
| 284 | } |
| 285 | |
| 286 | void CommonCompilerTest::UnreserveImageSpace() { |
| 287 | image_reservation_.reset(); |
| 288 | } |
| 289 | |
| 290 | } // namespace art |