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