Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 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; |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 28 | class ProfilingInfo; |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 29 | |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 30 | namespace jit { |
| 31 | class JitCodeCache; |
| 32 | } |
| 33 | |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 34 | namespace mirror { |
| 35 | class Class; |
| 36 | } |
| 37 | |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 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 | bool IsMonomorphic() const { |
| 43 | DCHECK_GE(kIndividualCacheSize, 2); |
| 44 | return !classes_[0].IsNull() && classes_[1].IsNull(); |
| 45 | } |
| 46 | |
| 47 | bool IsMegamorphic() const { |
| 48 | for (size_t i = 0; i < kIndividualCacheSize; ++i) { |
| 49 | if (classes_[i].IsNull()) { |
| 50 | return false; |
| 51 | } |
| 52 | } |
| 53 | return true; |
| 54 | } |
| 55 | |
| 56 | mirror::Class* GetMonomorphicType() const SHARED_REQUIRES(Locks::mutator_lock_) { |
| 57 | // Note that we cannot ensure the inline cache is actually monomorphic |
| 58 | // at this point, as other threads may have updated it. |
| 59 | return classes_[0].Read(); |
| 60 | } |
| 61 | |
| 62 | bool IsUnitialized() const { |
| 63 | return classes_[0].IsNull(); |
| 64 | } |
| 65 | |
| 66 | bool IsPolymorphic() const { |
| 67 | DCHECK_GE(kIndividualCacheSize, 3); |
| 68 | return !classes_[1].IsNull() && classes_[kIndividualCacheSize - 1].IsNull(); |
| 69 | } |
| 70 | |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 71 | mirror::Class* GetTypeAt(size_t i) const SHARED_REQUIRES(Locks::mutator_lock_) { |
| 72 | return classes_[i].Read(); |
| 73 | } |
| 74 | |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 75 | static constexpr uint16_t kIndividualCacheSize = 5; |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 76 | |
| 77 | private: |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 78 | uint32_t dex_pc_; |
| 79 | GcRoot<mirror::Class> classes_[kIndividualCacheSize]; |
| 80 | |
| 81 | friend class ProfilingInfo; |
| 82 | |
| 83 | DISALLOW_COPY_AND_ASSIGN(InlineCache); |
| 84 | }; |
| 85 | |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 86 | /** |
| 87 | * Profiling info for a method, created and filled by the interpreter once the |
| 88 | * method is warm, and used by the compiler to drive optimizations. |
| 89 | */ |
| 90 | class ProfilingInfo { |
| 91 | public: |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 92 | // Create a ProfilingInfo for 'method'. Return whether it succeeded, or if it is |
| 93 | // not needed in case the method does not have virtual/interface invocations. |
| 94 | static bool Create(Thread* self, ArtMethod* method, bool retry_allocation) |
| 95 | SHARED_REQUIRES(Locks::mutator_lock_); |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 96 | |
| 97 | // Add information from an executed INVOKE instruction to the profile. |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 98 | void AddInvokeInfo(uint32_t dex_pc, mirror::Class* cls) |
| 99 | // Method should not be interruptible, as it manipulates the ProfilingInfo |
| 100 | // which can be concurrently collected. |
| 101 | REQUIRES(Roles::uninterruptible_) |
| 102 | SHARED_REQUIRES(Locks::mutator_lock_); |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 103 | |
| 104 | // NO_THREAD_SAFETY_ANALYSIS since we don't know what the callback requires. |
| 105 | template<typename RootVisitorType> |
| 106 | void VisitRoots(RootVisitorType& visitor) NO_THREAD_SAFETY_ANALYSIS { |
| 107 | for (size_t i = 0; i < number_of_inline_caches_; ++i) { |
| 108 | InlineCache* cache = &cache_[i]; |
| 109 | for (size_t j = 0; j < InlineCache::kIndividualCacheSize; ++j) { |
| 110 | visitor.VisitRootIfNonNull(cache->classes_[j].AddressWithoutBarrier()); |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 115 | ArtMethod* GetMethod() const { |
| 116 | return method_; |
| 117 | } |
| 118 | |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 119 | InlineCache* GetInlineCache(uint32_t dex_pc); |
| 120 | |
| 121 | bool IsMethodBeingCompiled() const { |
| 122 | return is_method_being_compiled_; |
| 123 | } |
| 124 | |
| 125 | void SetIsMethodBeingCompiled(bool value) { |
| 126 | is_method_being_compiled_ = value; |
| 127 | } |
| 128 | |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 129 | void SetSavedEntryPoint(const void* entry_point) { |
| 130 | saved_entry_point_ = entry_point; |
| 131 | } |
| 132 | |
| 133 | const void* GetSavedEntryPoint() const { |
| 134 | return saved_entry_point_; |
| 135 | } |
| 136 | |
Nicolas Geoffray | b6e20ae | 2016-03-07 14:29:04 +0000 | [diff] [blame] | 137 | void ClearGcRootsInInlineCaches() { |
| 138 | for (size_t i = 0; i < number_of_inline_caches_; ++i) { |
| 139 | InlineCache* cache = &cache_[i]; |
| 140 | memset(&cache->classes_[0], |
| 141 | 0, |
| 142 | InlineCache::kIndividualCacheSize * sizeof(GcRoot<mirror::Class>)); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | void IncrementInlineUse() { |
| 147 | DCHECK_NE(current_inline_uses_, std::numeric_limits<uint16_t>::max()); |
| 148 | current_inline_uses_++; |
| 149 | } |
| 150 | |
| 151 | void DecrementInlineUse() { |
| 152 | DCHECK_GT(current_inline_uses_, 0); |
| 153 | current_inline_uses_--; |
| 154 | } |
| 155 | |
| 156 | bool IsInUseByCompiler() const { |
| 157 | return IsMethodBeingCompiled() || (current_inline_uses_ > 0); |
Nicolas Geoffray | 511e41b | 2016-03-02 17:09:35 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 160 | private: |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 161 | ProfilingInfo(ArtMethod* method, const std::vector<uint32_t>& entries) |
| 162 | : number_of_inline_caches_(entries.size()), |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 163 | method_(method), |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 164 | is_method_being_compiled_(false), |
Nicolas Geoffray | b6e20ae | 2016-03-07 14:29:04 +0000 | [diff] [blame] | 165 | current_inline_uses_(0), |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 166 | saved_entry_point_(nullptr) { |
Nicolas Geoffray | b6e20ae | 2016-03-07 14:29:04 +0000 | [diff] [blame] | 167 | memset(&cache_, 0, number_of_inline_caches_ * sizeof(InlineCache)); |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 168 | for (size_t i = 0; i < number_of_inline_caches_; ++i) { |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 169 | cache_[i].dex_pc_ = entries[i]; |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | |
| 173 | // Number of instructions we are profiling in the ArtMethod. |
| 174 | const uint32_t number_of_inline_caches_; |
| 175 | |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 176 | // Method this profiling info is for. |
| 177 | ArtMethod* const method_; |
| 178 | |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 179 | // Whether the ArtMethod is currently being compiled. This flag |
| 180 | // is implicitly guarded by the JIT code cache lock. |
| 181 | // TODO: Make the JIT code cache lock global. |
| 182 | bool is_method_being_compiled_; |
| 183 | |
Nicolas Geoffray | b6e20ae | 2016-03-07 14:29:04 +0000 | [diff] [blame] | 184 | // When the compiler inlines the method associated to this ProfilingInfo, |
| 185 | // it updates this counter so that the GC does not try to clear the inline caches. |
| 186 | uint16_t current_inline_uses_; |
| 187 | |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 188 | // Entry point of the corresponding ArtMethod, while the JIT code cache |
| 189 | // is poking for the liveness of compiled code. |
| 190 | const void* saved_entry_point_; |
| 191 | |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 192 | // Dynamically allocated array of size `number_of_inline_caches_`. |
| 193 | InlineCache cache_[0]; |
| 194 | |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 195 | friend class jit::JitCodeCache; |
| 196 | |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 197 | DISALLOW_COPY_AND_ASSIGN(ProfilingInfo); |
| 198 | }; |
| 199 | |
| 200 | } // namespace art |
| 201 | |
| 202 | #endif // ART_RUNTIME_JIT_PROFILING_INFO_H_ |