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"
23 #include "scoped_thread_state_change.h"
24 #include "thread.h"
25
26 namespace art {
27
ProfilingInfo(ArtMethod * method,const std::vector<uint32_t> & entries)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 }
39 if (method->IsCopied()) {
40 // GetHoldingClassOfCopiedMethod is expensive, but creating a profiling info for a copied method
41 // appears to happen very rarely in practice.
42 holding_class_ = GcRoot<mirror::Class>(
43 Runtime::Current()->GetClassLinker()->GetHoldingClassOfCopiedMethod(method));
44 } else {
45 holding_class_ = GcRoot<mirror::Class>(method->GetDeclaringClass());
46 }
47 DCHECK(!holding_class_.IsNull());
48 }
49
Create(Thread * self,ArtMethod * method,bool retry_allocation)50 bool ProfilingInfo::Create(Thread* self, ArtMethod* method, bool retry_allocation) {
51 // Walk over the dex instructions of the method and keep track of
52 // instructions we are interested in profiling.
53 DCHECK(!method->IsNative());
54
55 const DexFile::CodeItem& code_item = *method->GetCodeItem();
56 const uint16_t* code_ptr = code_item.insns_;
57 const uint16_t* code_end = code_item.insns_ + code_item.insns_size_in_code_units_;
58
59 uint32_t dex_pc = 0;
60 std::vector<uint32_t> entries;
61 while (code_ptr < code_end) {
62 const Instruction& instruction = *Instruction::At(code_ptr);
63 switch (instruction.Opcode()) {
64 case Instruction::INVOKE_VIRTUAL:
65 case Instruction::INVOKE_VIRTUAL_RANGE:
66 case Instruction::INVOKE_VIRTUAL_QUICK:
67 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK:
68 case Instruction::INVOKE_INTERFACE:
69 case Instruction::INVOKE_INTERFACE_RANGE:
70 entries.push_back(dex_pc);
71 break;
72
73 default:
74 break;
75 }
76 dex_pc += instruction.SizeInCodeUnits();
77 code_ptr += instruction.SizeInCodeUnits();
78 }
79
80 // We always create a `ProfilingInfo` object, even if there is no instruction we are
81 // interested in. The JIT code cache internally uses it.
82
83 // Allocate the `ProfilingInfo` object int the JIT's data space.
84 jit::JitCodeCache* code_cache = Runtime::Current()->GetJit()->GetCodeCache();
85 return code_cache->AddProfilingInfo(self, method, entries, retry_allocation) != nullptr;
86 }
87
GetInlineCache(uint32_t dex_pc)88 InlineCache* ProfilingInfo::GetInlineCache(uint32_t dex_pc) {
89 InlineCache* cache = nullptr;
90 // TODO: binary search if array is too long.
91 for (size_t i = 0; i < number_of_inline_caches_; ++i) {
92 if (cache_[i].dex_pc_ == dex_pc) {
93 cache = &cache_[i];
94 break;
95 }
96 }
97 return cache;
98 }
99
AddInvokeInfo(uint32_t dex_pc,mirror::Class * cls)100 void ProfilingInfo::AddInvokeInfo(uint32_t dex_pc, mirror::Class* cls) {
101 InlineCache* cache = GetInlineCache(dex_pc);
102 CHECK(cache != nullptr) << PrettyMethod(method_) << "@" << dex_pc;
103 for (size_t i = 0; i < InlineCache::kIndividualCacheSize; ++i) {
104 mirror::Class* existing = cache->classes_[i].Read();
105 if (existing == cls) {
106 // Receiver type is already in the cache, nothing else to do.
107 return;
108 } else if (existing == nullptr) {
109 // Cache entry is empty, try to put `cls` in it.
110 GcRoot<mirror::Class> expected_root(nullptr);
111 GcRoot<mirror::Class> desired_root(cls);
112 if (!reinterpret_cast<Atomic<GcRoot<mirror::Class>>*>(&cache->classes_[i])->
113 CompareExchangeStrongSequentiallyConsistent(expected_root, desired_root)) {
114 // Some other thread put a class in the cache, continue iteration starting at this
115 // entry in case the entry contains `cls`.
116 --i;
117 } else {
118 // We successfully set `cls`, just return.
119 // Since the instrumentation is marked from the declaring class we need to mark the card so
120 // that mod-union tables and card rescanning know about the update.
121 // Note that the declaring class is not necessarily the holding class if the method is
122 // copied. We need the card mark to be in the holding class since that is from where we
123 // will visit the profiling info.
124 if (!holding_class_.IsNull()) {
125 Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(holding_class_.Read());
126 }
127 return;
128 }
129 }
130 }
131 // Unsuccessfull - cache is full, making it megamorphic. We do not DCHECK it though,
132 // as the garbage collector might clear the entries concurrently.
133 }
134
135 } // namespace art
136