1 /*
2  * Copyright 2018 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 #ifndef ART_RUNTIME_JIT_JIT_INL_H_
18 #define ART_RUNTIME_JIT_JIT_INL_H_
19 
20 #include "jit/jit.h"
21 
22 #include "art_method.h"
23 #include "base/bit_utils.h"
24 #include "thread.h"
25 #include "runtime-inl.h"
26 
27 namespace art {
28 namespace jit {
29 
ShouldUsePriorityThreadWeight(Thread * self)30 inline bool Jit::ShouldUsePriorityThreadWeight(Thread* self) {
31   return self->IsJitSensitiveThread() && Runtime::Current()->InJankPerceptibleProcessState();
32 }
33 
AddSamples(Thread * self,ArtMethod * method,uint16_t samples,bool with_backedges)34 inline void Jit::AddSamples(Thread* self,
35                             ArtMethod* method,
36                             uint16_t samples,
37                             bool with_backedges) {
38   if (Jit::ShouldUsePriorityThreadWeight(self)) {
39     samples *= PriorityThreadWeight();
40   }
41   uint32_t old_count = method->GetCounter();
42   uint32_t new_count = old_count + samples;
43 
44   // The full check is fairly expensive so we just add to hotness most of the time,
45   // and we do the full check only when some of the higher bits of the count change.
46   // NB: The method needs to see the transitions of the counter past the thresholds.
47   uint32_t old_batch = RoundDown(old_count, kJitSamplesBatchSize);  // Clear lower bits.
48   uint32_t new_batch = RoundDown(new_count, kJitSamplesBatchSize);  // Clear lower bits.
49   if (UNLIKELY(kSlowMode)) {  // Check every time in slow-debug mode.
50     if (!MaybeCompileMethod(self, method, old_count, new_count, with_backedges)) {
51       // Tests may check that the counter is 0 for methods that we never compile.
52       return;  // Ignore the samples for now and retry later.
53     }
54   } else if (UNLIKELY(old_batch != new_batch)) {
55     if (!MaybeCompileMethod(self, method, old_batch, new_batch, with_backedges)) {
56       // OSR compilation will ignore the samples if they don't have backedges.
57       return;  // Ignore the samples for now and retry later.
58     }
59   }
60 
61   method->SetCounter(new_count);
62 }
63 
64 }  // namespace jit
65 }  // namespace art
66 
67 #endif  // ART_RUNTIME_JIT_JIT_INL_H_
68