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(old_batch == 0)) {
50     // For low sample counts, we check every time (which is important for tests).
51     if (!MaybeCompileMethod(self, method, old_count, new_count, with_backedges)) {
52       // Tests may check that the counter is 0 for methods that we never compile.
53       return;  // Ignore the samples for now and retry later.
54     }
55   } else if (UNLIKELY(old_batch != new_batch)) {
56     // For high sample counts, we check only when we move past the batch boundary.
57     if (!MaybeCompileMethod(self, method, old_batch, new_batch, with_backedges)) {
58       // OSR compilation will ignore the samples if they don't have backedges.
59       return;  // Ignore the samples for now and retry later.
60     }
61   }
62 
63   method->SetCounter(new_count);
64 }
65 
66 }  // namespace jit
67 }  // namespace art
68 
69 #endif  // ART_RUNTIME_JIT_JIT_INL_H_
70