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; |
| 28 | |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame^] | 29 | namespace jit { |
| 30 | class JitCodeCache; |
| 31 | } |
| 32 | |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 33 | namespace mirror { |
| 34 | class Class; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Profiling info for a method, created and filled by the interpreter once the |
| 39 | * method is warm, and used by the compiler to drive optimizations. |
| 40 | */ |
| 41 | class ProfilingInfo { |
| 42 | public: |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame^] | 43 | // Create a ProfilingInfo for 'method'. Return whether it succeeded, or if it is |
| 44 | // not needed in case the method does not have virtual/interface invocations. |
| 45 | static bool Create(Thread* self, ArtMethod* method, bool retry_allocation) |
| 46 | SHARED_REQUIRES(Locks::mutator_lock_); |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 47 | |
| 48 | // Add information from an executed INVOKE instruction to the profile. |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame^] | 49 | void AddInvokeInfo(uint32_t dex_pc, mirror::Class* cls) |
| 50 | // Method should not be interruptible, as it manipulates the ProfilingInfo |
| 51 | // which can be concurrently collected. |
| 52 | REQUIRES(Roles::uninterruptible_) |
| 53 | SHARED_REQUIRES(Locks::mutator_lock_); |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 54 | |
| 55 | // NO_THREAD_SAFETY_ANALYSIS since we don't know what the callback requires. |
| 56 | template<typename RootVisitorType> |
| 57 | void VisitRoots(RootVisitorType& visitor) NO_THREAD_SAFETY_ANALYSIS { |
| 58 | for (size_t i = 0; i < number_of_inline_caches_; ++i) { |
| 59 | InlineCache* cache = &cache_[i]; |
| 60 | for (size_t j = 0; j < InlineCache::kIndividualCacheSize; ++j) { |
| 61 | visitor.VisitRootIfNonNull(cache->classes_[j].AddressWithoutBarrier()); |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame^] | 66 | ArtMethod* GetMethod() const { |
| 67 | return method_; |
| 68 | } |
| 69 | |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 70 | private: |
| 71 | // Structure to store the classes seen at runtime for a specific instruction. |
| 72 | // Once the classes_ array is full, we consider the INVOKE to be megamorphic. |
| 73 | struct InlineCache { |
| 74 | bool IsMonomorphic() const { |
| 75 | DCHECK_GE(kIndividualCacheSize, 2); |
| 76 | return !classes_[0].IsNull() && classes_[1].IsNull(); |
| 77 | } |
| 78 | |
| 79 | bool IsMegamorphic() const { |
| 80 | for (size_t i = 0; i < kIndividualCacheSize; ++i) { |
| 81 | if (classes_[i].IsNull()) { |
| 82 | return false; |
| 83 | } |
| 84 | } |
| 85 | return true; |
| 86 | } |
| 87 | |
| 88 | bool IsUnitialized() const { |
| 89 | return classes_[0].IsNull(); |
| 90 | } |
| 91 | |
| 92 | bool IsPolymorphic() const { |
| 93 | DCHECK_GE(kIndividualCacheSize, 3); |
| 94 | return !classes_[1].IsNull() && classes_[kIndividualCacheSize - 1].IsNull(); |
| 95 | } |
| 96 | |
| 97 | static constexpr uint16_t kIndividualCacheSize = 5; |
| 98 | uint32_t dex_pc; |
| 99 | GcRoot<mirror::Class> classes_[kIndividualCacheSize]; |
| 100 | }; |
| 101 | |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame^] | 102 | ProfilingInfo(ArtMethod* method, const std::vector<uint32_t>& entries) |
| 103 | : number_of_inline_caches_(entries.size()), |
| 104 | method_(method) { |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 105 | memset(&cache_, 0, number_of_inline_caches_ * sizeof(InlineCache)); |
| 106 | for (size_t i = 0; i < number_of_inline_caches_; ++i) { |
| 107 | cache_[i].dex_pc = entries[i]; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | // Number of instructions we are profiling in the ArtMethod. |
| 112 | const uint32_t number_of_inline_caches_; |
| 113 | |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame^] | 114 | // Method this profiling info is for. |
| 115 | ArtMethod* const method_; |
| 116 | |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 117 | // Dynamically allocated array of size `number_of_inline_caches_`. |
| 118 | InlineCache cache_[0]; |
| 119 | |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame^] | 120 | friend class jit::JitCodeCache; |
| 121 | |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 122 | DISALLOW_COPY_AND_ASSIGN(ProfilingInfo); |
| 123 | }; |
| 124 | |
| 125 | } // namespace art |
| 126 | |
| 127 | #endif // ART_RUNTIME_JIT_PROFILING_INFO_H_ |