Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 "profiling_info.h" |
| 18 | |
| 19 | #include "art_method-inl.h" |
| 20 | #include "dex_instruction.h" |
| 21 | #include "jit/jit.h" |
| 22 | #include "jit/jit_code_cache.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 23 | #include "scoped_thread_state_change-inl.h" |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 24 | #include "thread.h" |
| 25 | |
| 26 | namespace art { |
| 27 | |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 28 | ProfilingInfo::ProfilingInfo(ArtMethod* method, const std::vector<uint32_t>& entries) |
| 29 | : number_of_inline_caches_(entries.size()), |
| 30 | method_(method), |
| 31 | is_method_being_compiled_(false), |
| 32 | is_osr_method_being_compiled_(false), |
| 33 | current_inline_uses_(0), |
| 34 | saved_entry_point_(nullptr) { |
| 35 | memset(&cache_, 0, number_of_inline_caches_ * sizeof(InlineCache)); |
| 36 | for (size_t i = 0; i < number_of_inline_caches_; ++i) { |
| 37 | cache_[i].dex_pc_ = entries[i]; |
| 38 | } |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 41 | bool ProfilingInfo::Create(Thread* self, ArtMethod* method, bool retry_allocation) { |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 42 | // Walk over the dex instructions of the method and keep track of |
| 43 | // instructions we are interested in profiling. |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 44 | DCHECK(!method->IsNative()); |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 45 | |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 46 | std::vector<uint32_t> entries; |
Mathieu Chartier | 1d2d4ff | 2017-09-23 16:11:06 -0700 | [diff] [blame^] | 47 | |
| 48 | IterationRange<DexInstructionIterator> instructions = method->GetCodeItem()->Instructions(); |
| 49 | for (auto inst = instructions.begin(); inst != instructions.end(); ++inst) { |
| 50 | const uint32_t dex_pc = inst.GetDexPC(instructions.begin()); |
| 51 | switch (inst->Opcode()) { |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 52 | case Instruction::INVOKE_VIRTUAL: |
| 53 | case Instruction::INVOKE_VIRTUAL_RANGE: |
| 54 | case Instruction::INVOKE_VIRTUAL_QUICK: |
| 55 | case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: |
| 56 | case Instruction::INVOKE_INTERFACE: |
| 57 | case Instruction::INVOKE_INTERFACE_RANGE: |
| 58 | entries.push_back(dex_pc); |
| 59 | break; |
| 60 | |
| 61 | default: |
| 62 | break; |
| 63 | } |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 64 | } |
| 65 | |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 66 | // We always create a `ProfilingInfo` object, even if there is no instruction we are |
| 67 | // interested in. The JIT code cache internally uses it. |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 68 | |
| 69 | // Allocate the `ProfilingInfo` object int the JIT's data space. |
| 70 | jit::JitCodeCache* code_cache = Runtime::Current()->GetJit()->GetCodeCache(); |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 71 | return code_cache->AddProfilingInfo(self, method, entries, retry_allocation) != nullptr; |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 72 | } |
| 73 | |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 74 | InlineCache* ProfilingInfo::GetInlineCache(uint32_t dex_pc) { |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 75 | // TODO: binary search if array is too long. |
| 76 | for (size_t i = 0; i < number_of_inline_caches_; ++i) { |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 77 | if (cache_[i].dex_pc_ == dex_pc) { |
Nicolas Geoffray | fdb7d63 | 2017-02-08 15:07:18 +0000 | [diff] [blame] | 78 | return &cache_[i]; |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 79 | } |
| 80 | } |
Nicolas Geoffray | fdb7d63 | 2017-02-08 15:07:18 +0000 | [diff] [blame] | 81 | LOG(FATAL) << "No inline cache found for " << ArtMethod::PrettyMethod(method_) << "@" << dex_pc; |
| 82 | UNREACHABLE(); |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 83 | } |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 84 | |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 85 | void ProfilingInfo::AddInvokeInfo(uint32_t dex_pc, mirror::Class* cls) { |
| 86 | InlineCache* cache = GetInlineCache(dex_pc); |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 87 | for (size_t i = 0; i < InlineCache::kIndividualCacheSize; ++i) { |
Nicolas Geoffray | 13056a1 | 2017-05-11 11:48:28 +0000 | [diff] [blame] | 88 | mirror::Class* existing = cache->classes_[i].Read<kWithoutReadBarrier>(); |
| 89 | mirror::Class* marked = ReadBarrier::IsMarked(existing); |
| 90 | if (marked == cls) { |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 91 | // Receiver type is already in the cache, nothing else to do. |
| 92 | return; |
Nicolas Geoffray | 13056a1 | 2017-05-11 11:48:28 +0000 | [diff] [blame] | 93 | } else if (marked == nullptr) { |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 94 | // Cache entry is empty, try to put `cls` in it. |
Nicolas Geoffray | 13056a1 | 2017-05-11 11:48:28 +0000 | [diff] [blame] | 95 | // Note: it's ok to spin on 'existing' here: if 'existing' is not null, that means |
| 96 | // it is a stalled heap address, which will only be cleared during SweepSystemWeaks, |
| 97 | // *after* this thread hits a suspend point. |
| 98 | GcRoot<mirror::Class> expected_root(existing); |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 99 | GcRoot<mirror::Class> desired_root(cls); |
| 100 | if (!reinterpret_cast<Atomic<GcRoot<mirror::Class>>*>(&cache->classes_[i])-> |
| 101 | CompareExchangeStrongSequentiallyConsistent(expected_root, desired_root)) { |
| 102 | // Some other thread put a class in the cache, continue iteration starting at this |
| 103 | // entry in case the entry contains `cls`. |
| 104 | --i; |
| 105 | } else { |
| 106 | // We successfully set `cls`, just return. |
| 107 | return; |
| 108 | } |
| 109 | } |
| 110 | } |
Nicolas Geoffray | b6e20ae | 2016-03-07 14:29:04 +0000 | [diff] [blame] | 111 | // Unsuccessfull - cache is full, making it megamorphic. We do not DCHECK it though, |
| 112 | // as the garbage collector might clear the entries concurrently. |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | } // namespace art |