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/dex_instruction.h"
21 #include "jit/jit.h"
22 #include "jit/jit_code_cache.h"
23 #include "scoped_thread_state_change-inl.h"
24 #include "thread.h"
25
26 namespace art HIDDEN {
27
ProfilingInfo(ArtMethod * method,const std::vector<uint32_t> & inline_cache_entries,const std::vector<uint32_t> & branch_cache_entries)28 ProfilingInfo::ProfilingInfo(ArtMethod* method,
29 const std::vector<uint32_t>& inline_cache_entries,
30 const std::vector<uint32_t>& branch_cache_entries)
31 : baseline_hotness_count_(GetOptimizeThreshold()),
32 method_(method),
33 number_of_inline_caches_(inline_cache_entries.size()),
34 number_of_branch_caches_(branch_cache_entries.size()),
35 current_inline_uses_(0) {
36 InlineCache* inline_caches = GetInlineCaches();
37 memset(inline_caches, 0, number_of_inline_caches_ * sizeof(InlineCache));
38 for (size_t i = 0; i < number_of_inline_caches_; ++i) {
39 inline_caches[i].dex_pc_ = inline_cache_entries[i];
40 }
41
42 BranchCache* branch_caches = GetBranchCaches();
43 memset(branch_caches, 0, number_of_branch_caches_ * sizeof(BranchCache));
44 for (size_t i = 0; i < number_of_branch_caches_; ++i) {
45 branch_caches[i].dex_pc_ = branch_cache_entries[i];
46 }
47 }
48
GetOptimizeThreshold()49 uint16_t ProfilingInfo::GetOptimizeThreshold() {
50 return Runtime::Current()->GetJITOptions()->GetOptimizeThreshold();
51 }
52
Create(Thread * self,ArtMethod * method,const std::vector<uint32_t> & inline_cache_entries)53 ProfilingInfo* ProfilingInfo::Create(Thread* self,
54 ArtMethod* method,
55 const std::vector<uint32_t>& inline_cache_entries) {
56 // Walk over the dex instructions of the method and keep track of
57 // instructions we are interested in profiling.
58 DCHECK(!method->IsNative());
59
60 std::vector<uint32_t> branch_cache_entries;
61 for (const DexInstructionPcPair& inst : method->DexInstructions()) {
62 switch (inst->Opcode()) {
63 case Instruction::IF_EQ:
64 case Instruction::IF_EQZ:
65 case Instruction::IF_NE:
66 case Instruction::IF_NEZ:
67 case Instruction::IF_LT:
68 case Instruction::IF_LTZ:
69 case Instruction::IF_LE:
70 case Instruction::IF_LEZ:
71 case Instruction::IF_GT:
72 case Instruction::IF_GTZ:
73 case Instruction::IF_GE:
74 case Instruction::IF_GEZ:
75 branch_cache_entries.push_back(inst.DexPc());
76 break;
77
78 default:
79 break;
80 }
81 }
82
83 // We always create a `ProfilingInfo` object, even if there is no instruction we are
84 // interested in. The JIT code cache internally uses it for hotness counter.
85
86 // Allocate the `ProfilingInfo` object int the JIT's data space.
87 jit::JitCodeCache* code_cache = Runtime::Current()->GetJit()->GetCodeCache();
88 return code_cache->AddProfilingInfo(self, method, inline_cache_entries, branch_cache_entries);
89 }
90
GetInlineCache(uint32_t dex_pc)91 InlineCache* ProfilingInfo::GetInlineCache(uint32_t dex_pc) {
92 // TODO: binary search if array is too long.
93 InlineCache* caches = GetInlineCaches();
94 for (size_t i = 0; i < number_of_inline_caches_; ++i) {
95 if (caches[i].dex_pc_ == dex_pc) {
96 return &caches[i];
97 }
98 }
99 return nullptr;
100 }
101
GetBranchCache(uint32_t dex_pc)102 BranchCache* ProfilingInfo::GetBranchCache(uint32_t dex_pc) {
103 // TODO: binary search if array is too long.
104 BranchCache* caches = GetBranchCaches();
105 for (size_t i = 0; i < number_of_branch_caches_; ++i) {
106 if (caches[i].dex_pc_ == dex_pc) {
107 return &caches[i];
108 }
109 }
110 // Currently, only if instructions are profiled. The compiler will see other
111 // branches, like switches.
112 return nullptr;
113 }
114
AddInvokeInfo(uint32_t dex_pc,mirror::Class * cls)115 void ProfilingInfo::AddInvokeInfo(uint32_t dex_pc, mirror::Class* cls) {
116 InlineCache* cache = GetInlineCache(dex_pc);
117 if (cache == nullptr) {
118 return;
119 }
120 for (size_t i = 0; i < InlineCache::kIndividualCacheSize; ++i) {
121 mirror::Class* existing = cache->classes_[i].Read<kWithoutReadBarrier>();
122 mirror::Class* marked = ReadBarrier::IsMarked(existing);
123 if (marked == cls) {
124 // Receiver type is already in the cache, nothing else to do.
125 return;
126 } else if (marked == nullptr) {
127 // Cache entry is empty, try to put `cls` in it.
128 // Note: it's ok to spin on 'existing' here: if 'existing' is not null, that means
129 // it is a stalled heap address, which will only be cleared during SweepSystemWeaks,
130 // *after* this thread hits a suspend point.
131 GcRoot<mirror::Class> expected_root(existing);
132 GcRoot<mirror::Class> desired_root(cls);
133 auto atomic_root = reinterpret_cast<Atomic<GcRoot<mirror::Class>>*>(&cache->classes_[i]);
134 if (!atomic_root->CompareAndSetStrongSequentiallyConsistent(expected_root, desired_root)) {
135 // Some other thread put a class in the cache, continue iteration starting at this
136 // entry in case the entry contains `cls`.
137 --i;
138 } else {
139 // We successfully set `cls`, just return.
140 return;
141 }
142 }
143 }
144 // Unsuccessfull - cache is full, making it megamorphic. We do not DCHECK it though,
145 // as the garbage collector might clear the entries concurrently.
146 }
147
ScopedProfilingInfoUse(jit::Jit * jit,ArtMethod * method,Thread * self)148 ScopedProfilingInfoUse::ScopedProfilingInfoUse(jit::Jit* jit, ArtMethod* method, Thread* self)
149 : jit_(jit),
150 method_(method),
151 self_(self),
152 // Fetch the profiling info ahead of using it. If it's null when fetching,
153 // we should not call JitCodeCache::DoneCompilerUse.
154 profiling_info_(jit == nullptr
155 ? nullptr
156 : jit->GetCodeCache()->NotifyCompilerUse(method, self))
157 {}
158
~ScopedProfilingInfoUse()159 ScopedProfilingInfoUse::~ScopedProfilingInfoUse() {
160 if (profiling_info_ != nullptr) {
161 jit_->GetCodeCache()->DoneCompilerUse(method_, self_);
162 }
163 }
164
EncodeDexPc(ArtMethod * method,const std::vector<uint32_t> & dex_pcs,uint32_t inline_max_code_units)165 uint32_t InlineCache::EncodeDexPc(ArtMethod* method,
166 const std::vector<uint32_t>& dex_pcs,
167 uint32_t inline_max_code_units) {
168 if (kIsDebugBuild) {
169 // Make sure `inline_max_code_units` is always the same.
170 static uint32_t global_max_code_units = inline_max_code_units;
171 CHECK_EQ(global_max_code_units, inline_max_code_units);
172 }
173 if (dex_pcs.size() - 1 > MaxDexPcEncodingDepth(method, inline_max_code_units)) {
174 return -1;
175 }
176 uint32_t size = dex_pcs.size();
177 uint32_t insns_size = method->DexInstructions().InsnsSizeInCodeUnits();
178
179 uint32_t dex_pc = dex_pcs[size - 1];
180 uint32_t shift = MinimumBitsToStore(insns_size - 1);
181 for (uint32_t i = size - 1; i > 0; --i) {
182 DCHECK_LT(shift, BitSizeOf<uint32_t>());
183 dex_pc += ((dex_pcs[i - 1] + 1) << shift);
184 shift += MinimumBitsToStore(inline_max_code_units);
185 }
186 return dex_pc;
187 }
188
MaxDexPcEncodingDepth(ArtMethod * method,uint32_t inline_max_code_units)189 uint32_t InlineCache::MaxDexPcEncodingDepth(ArtMethod* method, uint32_t inline_max_code_units) {
190 uint32_t insns_size = method->DexInstructions().InsnsSizeInCodeUnits();
191 uint32_t num_bits = MinimumBitsToStore(insns_size - 1);
192 uint32_t depth = 0;
193 do {
194 depth++;
195 num_bits += MinimumBitsToStore(inline_max_code_units);
196 } while (num_bits <= BitSizeOf<uint32_t>());
197 return depth - 1;
198 }
199
200 } // namespace art
201