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