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 #ifndef ART_RUNTIME_JIT_PROFILING_INFO_H_
18 #define ART_RUNTIME_JIT_PROFILING_INFO_H_
19 
20 #include <vector>
21 
22 #include "base/macros.h"
23 #include "gc_root.h"
24 
25 namespace art {
26 
27 class ArtMethod;
28 class ProfilingInfo;
29 
30 namespace jit {
31 class JitCodeCache;
32 }
33 
34 namespace mirror {
35 class Class;
36 }
37 
38 // Structure to store the classes seen at runtime for a specific instruction.
39 // Once the classes_ array is full, we consider the INVOKE to be megamorphic.
40 class InlineCache {
41  public:
42   static constexpr uint8_t kIndividualCacheSize = 5;
43 
44  private:
45   uint32_t dex_pc_;
46   GcRoot<mirror::Class> classes_[kIndividualCacheSize];
47 
48   friend class jit::JitCodeCache;
49   friend class ProfilingInfo;
50 
51   DISALLOW_COPY_AND_ASSIGN(InlineCache);
52 };
53 
54 /**
55  * Profiling info for a method, created and filled by the interpreter once the
56  * method is warm, and used by the compiler to drive optimizations.
57  */
58 class ProfilingInfo {
59  public:
60   // Create a ProfilingInfo for 'method'. Return whether it succeeded, or if it is
61   // not needed in case the method does not have virtual/interface invocations.
62   static bool Create(Thread* self, ArtMethod* method, bool retry_allocation)
63       REQUIRES_SHARED(Locks::mutator_lock_);
64 
65   // Add information from an executed INVOKE instruction to the profile.
66   void AddInvokeInfo(uint32_t dex_pc, mirror::Class* cls)
67       // Method should not be interruptible, as it manipulates the ProfilingInfo
68       // which can be concurrently collected.
69       REQUIRES(Roles::uninterruptible_)
70       REQUIRES_SHARED(Locks::mutator_lock_);
71 
GetMethod()72   ArtMethod* GetMethod() const {
73     return method_;
74   }
75 
76   // Mutator lock only required for debugging output.
77   InlineCache* GetInlineCache(uint32_t dex_pc)
78       REQUIRES_SHARED(Locks::mutator_lock_);
79 
IsMethodBeingCompiled(bool osr)80   bool IsMethodBeingCompiled(bool osr) const {
81     return osr
82         ? is_osr_method_being_compiled_
83         : is_method_being_compiled_;
84   }
85 
SetIsMethodBeingCompiled(bool value,bool osr)86   void SetIsMethodBeingCompiled(bool value, bool osr) {
87     if (osr) {
88       is_osr_method_being_compiled_ = value;
89     } else {
90       is_method_being_compiled_ = value;
91     }
92   }
93 
SetSavedEntryPoint(const void * entry_point)94   void SetSavedEntryPoint(const void* entry_point) {
95     saved_entry_point_ = entry_point;
96   }
97 
GetSavedEntryPoint()98   const void* GetSavedEntryPoint() const {
99     return saved_entry_point_;
100   }
101 
ClearGcRootsInInlineCaches()102   void ClearGcRootsInInlineCaches() {
103     for (size_t i = 0; i < number_of_inline_caches_; ++i) {
104       InlineCache* cache = &cache_[i];
105       memset(&cache->classes_[0],
106              0,
107              InlineCache::kIndividualCacheSize * sizeof(GcRoot<mirror::Class>));
108     }
109   }
110 
111   // Increments the number of times this method is currently being inlined.
112   // Returns whether it was successful, that is it could increment without
113   // overflowing.
IncrementInlineUse()114   bool IncrementInlineUse() {
115     if (current_inline_uses_ == std::numeric_limits<uint16_t>::max()) {
116       return false;
117     }
118     current_inline_uses_++;
119     return true;
120   }
121 
DecrementInlineUse()122   void DecrementInlineUse() {
123     DCHECK_GT(current_inline_uses_, 0);
124     current_inline_uses_--;
125   }
126 
IsInUseByCompiler()127   bool IsInUseByCompiler() const {
128     return IsMethodBeingCompiled(/*osr*/ true) || IsMethodBeingCompiled(/*osr*/ false) ||
129         (current_inline_uses_ > 0);
130   }
131 
132  private:
133   ProfilingInfo(ArtMethod* method, const std::vector<uint32_t>& entries);
134 
135   // Number of instructions we are profiling in the ArtMethod.
136   const uint32_t number_of_inline_caches_;
137 
138   // Method this profiling info is for.
139   // Not 'const' as JVMTI introduces obsolete methods that we implement by creating new ArtMethods.
140   // See JitCodeCache::MoveObsoleteMethod.
141   ArtMethod* method_;
142 
143   // Whether the ArtMethod is currently being compiled. This flag
144   // is implicitly guarded by the JIT code cache lock.
145   // TODO: Make the JIT code cache lock global.
146   bool is_method_being_compiled_;
147   bool is_osr_method_being_compiled_;
148 
149   // When the compiler inlines the method associated to this ProfilingInfo,
150   // it updates this counter so that the GC does not try to clear the inline caches.
151   uint16_t current_inline_uses_;
152 
153   // Entry point of the corresponding ArtMethod, while the JIT code cache
154   // is poking for the liveness of compiled code.
155   const void* saved_entry_point_;
156 
157   // Dynamically allocated array of size `number_of_inline_caches_`.
158   InlineCache cache_[0];
159 
160   friend class jit::JitCodeCache;
161 
162   DISALLOW_COPY_AND_ASSIGN(ProfilingInfo);
163 };
164 
165 }  // namespace art
166 
167 #endif  // ART_RUNTIME_JIT_PROFILING_INFO_H_
168