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: |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 42 | static constexpr uint16_t kIndividualCacheSize = 5; |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 43 | |
| 44 | private: |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 45 | uint32_t dex_pc_; |
| 46 | GcRoot<mirror::Class> classes_[kIndividualCacheSize]; |
| 47 | |
Nicolas Geoffray | e51ca8b | 2016-11-22 14:49:31 +0000 | [diff] [blame^] | 48 | friend class jit::JitCodeCache; |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 49 | friend class ProfilingInfo; |
| 50 | |
| 51 | DISALLOW_COPY_AND_ASSIGN(InlineCache); |
| 52 | }; |
| 53 | |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 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: |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 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) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 63 | REQUIRES_SHARED(Locks::mutator_lock_); |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 64 | |
| 65 | // Add information from an executed INVOKE instruction to the profile. |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 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_) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 70 | REQUIRES_SHARED(Locks::mutator_lock_); |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 71 | |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 72 | ArtMethod* GetMethod() const { |
| 73 | return method_; |
| 74 | } |
| 75 | |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 76 | InlineCache* GetInlineCache(uint32_t dex_pc); |
| 77 | |
buzbee | 454b3b6 | 2016-04-07 14:42:47 -0700 | [diff] [blame] | 78 | bool IsMethodBeingCompiled(bool osr) const { |
| 79 | return osr |
| 80 | ? is_osr_method_being_compiled_ |
| 81 | : is_method_being_compiled_; |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 82 | } |
| 83 | |
buzbee | 454b3b6 | 2016-04-07 14:42:47 -0700 | [diff] [blame] | 84 | void SetIsMethodBeingCompiled(bool value, bool osr) { |
| 85 | if (osr) { |
| 86 | is_osr_method_being_compiled_ = value; |
| 87 | } else { |
| 88 | is_method_being_compiled_ = value; |
| 89 | } |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 90 | } |
| 91 | |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 92 | void SetSavedEntryPoint(const void* entry_point) { |
| 93 | saved_entry_point_ = entry_point; |
| 94 | } |
| 95 | |
| 96 | const void* GetSavedEntryPoint() const { |
| 97 | return saved_entry_point_; |
| 98 | } |
| 99 | |
Nicolas Geoffray | b6e20ae | 2016-03-07 14:29:04 +0000 | [diff] [blame] | 100 | void ClearGcRootsInInlineCaches() { |
| 101 | for (size_t i = 0; i < number_of_inline_caches_; ++i) { |
| 102 | InlineCache* cache = &cache_[i]; |
| 103 | memset(&cache->classes_[0], |
| 104 | 0, |
| 105 | InlineCache::kIndividualCacheSize * sizeof(GcRoot<mirror::Class>)); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | void IncrementInlineUse() { |
| 110 | DCHECK_NE(current_inline_uses_, std::numeric_limits<uint16_t>::max()); |
| 111 | current_inline_uses_++; |
| 112 | } |
| 113 | |
| 114 | void DecrementInlineUse() { |
| 115 | DCHECK_GT(current_inline_uses_, 0); |
| 116 | current_inline_uses_--; |
| 117 | } |
| 118 | |
| 119 | bool IsInUseByCompiler() const { |
buzbee | 454b3b6 | 2016-04-07 14:42:47 -0700 | [diff] [blame] | 120 | return IsMethodBeingCompiled(/*osr*/ true) || IsMethodBeingCompiled(/*osr*/ false) || |
| 121 | (current_inline_uses_ > 0); |
Nicolas Geoffray | 511e41b | 2016-03-02 17:09:35 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 124 | private: |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 125 | ProfilingInfo(ArtMethod* method, const std::vector<uint32_t>& entries); |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 126 | |
| 127 | // Number of instructions we are profiling in the ArtMethod. |
| 128 | const uint32_t number_of_inline_caches_; |
| 129 | |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 130 | // Method this profiling info is for. |
| 131 | ArtMethod* const method_; |
| 132 | |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 133 | // Whether the ArtMethod is currently being compiled. This flag |
| 134 | // is implicitly guarded by the JIT code cache lock. |
| 135 | // TODO: Make the JIT code cache lock global. |
| 136 | bool is_method_being_compiled_; |
buzbee | 454b3b6 | 2016-04-07 14:42:47 -0700 | [diff] [blame] | 137 | bool is_osr_method_being_compiled_; |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 138 | |
Nicolas Geoffray | b6e20ae | 2016-03-07 14:29:04 +0000 | [diff] [blame] | 139 | // When the compiler inlines the method associated to this ProfilingInfo, |
| 140 | // it updates this counter so that the GC does not try to clear the inline caches. |
| 141 | uint16_t current_inline_uses_; |
| 142 | |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 143 | // Entry point of the corresponding ArtMethod, while the JIT code cache |
| 144 | // is poking for the liveness of compiled code. |
| 145 | const void* saved_entry_point_; |
| 146 | |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 147 | // Dynamically allocated array of size `number_of_inline_caches_`. |
| 148 | InlineCache cache_[0]; |
| 149 | |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 150 | friend class jit::JitCodeCache; |
| 151 | |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 152 | DISALLOW_COPY_AND_ASSIGN(ProfilingInfo); |
| 153 | }; |
| 154 | |
| 155 | } // namespace art |
| 156 | |
| 157 | #endif // ART_RUNTIME_JIT_PROFILING_INFO_H_ |