blob: a8c056c7c93f89445ca8dd3effb47592256c93a9 [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 Geoffray511e41b2016-03-02 17:09:35 +0000137 void ClearInlineCaches() {
138 memset(&cache_, 0, number_of_inline_caches_ * sizeof(InlineCache));
139 }
140
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100141 private:
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000142 ProfilingInfo(ArtMethod* method, const std::vector<uint32_t>& entries)
143 : number_of_inline_caches_(entries.size()),
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100144 method_(method),
Nicolas Geoffray35122442016-03-02 12:05:30 +0000145 is_method_being_compiled_(false),
146 saved_entry_point_(nullptr) {
Nicolas Geoffray511e41b2016-03-02 17:09:35 +0000147 ClearInlineCaches();
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100148 for (size_t i = 0; i < number_of_inline_caches_; ++i) {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100149 cache_[i].dex_pc_ = entries[i];
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100150 }
151 }
152
153 // Number of instructions we are profiling in the ArtMethod.
154 const uint32_t number_of_inline_caches_;
155
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000156 // Method this profiling info is for.
157 ArtMethod* const method_;
158
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100159 // Whether the ArtMethod is currently being compiled. This flag
160 // is implicitly guarded by the JIT code cache lock.
161 // TODO: Make the JIT code cache lock global.
162 bool is_method_being_compiled_;
163
Nicolas Geoffray35122442016-03-02 12:05:30 +0000164 // Entry point of the corresponding ArtMethod, while the JIT code cache
165 // is poking for the liveness of compiled code.
166 const void* saved_entry_point_;
167
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100168 // Dynamically allocated array of size `number_of_inline_caches_`.
169 InlineCache cache_[0];
170
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000171 friend class jit::JitCodeCache;
172
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100173 DISALLOW_COPY_AND_ASSIGN(ProfilingInfo);
174};
175
176} // namespace art
177
178#endif // ART_RUNTIME_JIT_PROFILING_INFO_H_