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 | |
| 29 | namespace mirror { |
| 30 | class Class; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Profiling info for a method, created and filled by the interpreter once the |
| 35 | * method is warm, and used by the compiler to drive optimizations. |
| 36 | */ |
| 37 | class ProfilingInfo { |
| 38 | public: |
| 39 | static ProfilingInfo* Create(ArtMethod* method); |
| 40 | |
| 41 | // Add information from an executed INVOKE instruction to the profile. |
| 42 | void AddInvokeInfo(Thread* self, uint32_t dex_pc, mirror::Class* cls); |
| 43 | |
| 44 | // NO_THREAD_SAFETY_ANALYSIS since we don't know what the callback requires. |
| 45 | template<typename RootVisitorType> |
| 46 | void VisitRoots(RootVisitorType& visitor) NO_THREAD_SAFETY_ANALYSIS { |
| 47 | for (size_t i = 0; i < number_of_inline_caches_; ++i) { |
| 48 | InlineCache* cache = &cache_[i]; |
| 49 | for (size_t j = 0; j < InlineCache::kIndividualCacheSize; ++j) { |
| 50 | visitor.VisitRootIfNonNull(cache->classes_[j].AddressWithoutBarrier()); |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | private: |
| 56 | // Structure to store the classes seen at runtime for a specific instruction. |
| 57 | // Once the classes_ array is full, we consider the INVOKE to be megamorphic. |
| 58 | struct InlineCache { |
| 59 | bool IsMonomorphic() const { |
| 60 | DCHECK_GE(kIndividualCacheSize, 2); |
| 61 | return !classes_[0].IsNull() && classes_[1].IsNull(); |
| 62 | } |
| 63 | |
| 64 | bool IsMegamorphic() const { |
| 65 | for (size_t i = 0; i < kIndividualCacheSize; ++i) { |
| 66 | if (classes_[i].IsNull()) { |
| 67 | return false; |
| 68 | } |
| 69 | } |
| 70 | return true; |
| 71 | } |
| 72 | |
| 73 | bool IsUnitialized() const { |
| 74 | return classes_[0].IsNull(); |
| 75 | } |
| 76 | |
| 77 | bool IsPolymorphic() const { |
| 78 | DCHECK_GE(kIndividualCacheSize, 3); |
| 79 | return !classes_[1].IsNull() && classes_[kIndividualCacheSize - 1].IsNull(); |
| 80 | } |
| 81 | |
| 82 | static constexpr uint16_t kIndividualCacheSize = 5; |
| 83 | uint32_t dex_pc; |
| 84 | GcRoot<mirror::Class> classes_[kIndividualCacheSize]; |
| 85 | }; |
| 86 | |
| 87 | explicit ProfilingInfo(const std::vector<uint32_t>& entries) |
| 88 | : number_of_inline_caches_(entries.size()) { |
| 89 | memset(&cache_, 0, number_of_inline_caches_ * sizeof(InlineCache)); |
| 90 | for (size_t i = 0; i < number_of_inline_caches_; ++i) { |
| 91 | cache_[i].dex_pc = entries[i]; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | // Number of instructions we are profiling in the ArtMethod. |
| 96 | const uint32_t number_of_inline_caches_; |
| 97 | |
| 98 | // Dynamically allocated array of size `number_of_inline_caches_`. |
| 99 | InlineCache cache_[0]; |
| 100 | |
| 101 | DISALLOW_COPY_AND_ASSIGN(ProfilingInfo); |
| 102 | }; |
| 103 | |
| 104 | } // namespace art |
| 105 | |
| 106 | #endif // ART_RUNTIME_JIT_PROFILING_INFO_H_ |