blob: 73c1a1edb0917cfaf86f354ab1b720762289bcbe [file] [log] [blame]
Nicolas Geoffray5550ca82015-08-21 18:38:30 +01001/*
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
25namespace art {
26
27class ArtMethod;
Nicolas Geoffray73be1e82015-09-17 15:22:56 +010028class ProfilingInfo;
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010029
Nicolas Geoffray26705e22015-10-28 12:50:11 +000030namespace jit {
31class JitCodeCache;
32}
33
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010034namespace mirror {
35class Class;
36}
37
Nicolas Geoffray73be1e82015-09-17 15:22:56 +010038// 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.
40class 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 Geoffraya42363f2015-12-17 14:57:09 +000071 mirror::Class* GetTypeAt(size_t i) const SHARED_REQUIRES(Locks::mutator_lock_) {
72 return classes_[i].Read();
73 }
74
Nicolas Geoffray73be1e82015-09-17 15:22:56 +010075 static constexpr uint16_t kIndividualCacheSize = 5;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +000076
77 private:
Nicolas Geoffray73be1e82015-09-17 15:22:56 +010078 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 Geoffray5550ca82015-08-21 18:38:30 +010086/**
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 */
90class ProfilingInfo {
91 public:
Nicolas Geoffray26705e22015-10-28 12:50:11 +000092 // 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 Geoffray5550ca82015-08-21 18:38:30 +010096
97 // Add information from an executed INVOKE instruction to the profile.
Nicolas Geoffray26705e22015-10-28 12:50:11 +000098 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 Geoffray5550ca82015-08-21 18:38:30 +0100103
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 Geoffray26705e22015-10-28 12:50:11 +0000115 ArtMethod* GetMethod() const {
116 return method_;
117 }
118
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100119 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 Geoffray35122442016-03-02 12:05:30 +0000129 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 Geoffrayb6e20ae2016-03-07 14:29:04 +0000137 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 Geoffray511e41b2016-03-02 17:09:35 +0000158 }
159
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100160 private:
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000161 ProfilingInfo(ArtMethod* method, const std::vector<uint32_t>& entries)
162 : number_of_inline_caches_(entries.size()),
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100163 method_(method),
Nicolas Geoffray35122442016-03-02 12:05:30 +0000164 is_method_being_compiled_(false),
Nicolas Geoffrayb6e20ae2016-03-07 14:29:04 +0000165 current_inline_uses_(0),
Nicolas Geoffray35122442016-03-02 12:05:30 +0000166 saved_entry_point_(nullptr) {
Nicolas Geoffrayb6e20ae2016-03-07 14:29:04 +0000167 memset(&cache_, 0, number_of_inline_caches_ * sizeof(InlineCache));
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100168 for (size_t i = 0; i < number_of_inline_caches_; ++i) {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100169 cache_[i].dex_pc_ = entries[i];
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100170 }
171 }
172
173 // Number of instructions we are profiling in the ArtMethod.
174 const uint32_t number_of_inline_caches_;
175
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000176 // Method this profiling info is for.
177 ArtMethod* const method_;
178
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100179 // 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 Geoffrayb6e20ae2016-03-07 14:29:04 +0000184 // 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 Geoffray35122442016-03-02 12:05:30 +0000188 // 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 Geoffray5550ca82015-08-21 18:38:30 +0100192 // Dynamically allocated array of size `number_of_inline_caches_`.
193 InlineCache cache_[0];
194
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000195 friend class jit::JitCodeCache;
196
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100197 DISALLOW_COPY_AND_ASSIGN(ProfilingInfo);
198};
199
200} // namespace art
201
202#endif // ART_RUNTIME_JIT_PROFILING_INFO_H_