blob: ddaf02fdf5f0e8d015bbefb7a1e68cfabece4444 [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
71 private:
72 static constexpr uint16_t kIndividualCacheSize = 5;
73 uint32_t dex_pc_;
74 GcRoot<mirror::Class> classes_[kIndividualCacheSize];
75
76 friend class ProfilingInfo;
77
78 DISALLOW_COPY_AND_ASSIGN(InlineCache);
79};
80
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010081/**
82 * Profiling info for a method, created and filled by the interpreter once the
83 * method is warm, and used by the compiler to drive optimizations.
84 */
85class ProfilingInfo {
86 public:
Nicolas Geoffray26705e22015-10-28 12:50:11 +000087 // Create a ProfilingInfo for 'method'. Return whether it succeeded, or if it is
88 // not needed in case the method does not have virtual/interface invocations.
89 static bool Create(Thread* self, ArtMethod* method, bool retry_allocation)
90 SHARED_REQUIRES(Locks::mutator_lock_);
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010091
92 // Add information from an executed INVOKE instruction to the profile.
Nicolas Geoffray26705e22015-10-28 12:50:11 +000093 void AddInvokeInfo(uint32_t dex_pc, mirror::Class* cls)
94 // Method should not be interruptible, as it manipulates the ProfilingInfo
95 // which can be concurrently collected.
96 REQUIRES(Roles::uninterruptible_)
97 SHARED_REQUIRES(Locks::mutator_lock_);
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010098
99 // NO_THREAD_SAFETY_ANALYSIS since we don't know what the callback requires.
100 template<typename RootVisitorType>
101 void VisitRoots(RootVisitorType& visitor) NO_THREAD_SAFETY_ANALYSIS {
102 for (size_t i = 0; i < number_of_inline_caches_; ++i) {
103 InlineCache* cache = &cache_[i];
104 for (size_t j = 0; j < InlineCache::kIndividualCacheSize; ++j) {
105 visitor.VisitRootIfNonNull(cache->classes_[j].AddressWithoutBarrier());
106 }
107 }
108 }
109
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000110 ArtMethod* GetMethod() const {
111 return method_;
112 }
113
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100114 InlineCache* GetInlineCache(uint32_t dex_pc);
115
116 bool IsMethodBeingCompiled() const {
117 return is_method_being_compiled_;
118 }
119
120 void SetIsMethodBeingCompiled(bool value) {
121 is_method_being_compiled_ = value;
122 }
123
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100124 private:
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000125 ProfilingInfo(ArtMethod* method, const std::vector<uint32_t>& entries)
126 : number_of_inline_caches_(entries.size()),
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100127 method_(method),
128 is_method_being_compiled_(false) {
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100129 memset(&cache_, 0, number_of_inline_caches_ * sizeof(InlineCache));
130 for (size_t i = 0; i < number_of_inline_caches_; ++i) {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100131 cache_[i].dex_pc_ = entries[i];
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100132 }
133 }
134
135 // Number of instructions we are profiling in the ArtMethod.
136 const uint32_t number_of_inline_caches_;
137
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000138 // Method this profiling info is for.
139 ArtMethod* const method_;
140
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100141 // Whether the ArtMethod is currently being compiled. This flag
142 // is implicitly guarded by the JIT code cache lock.
143 // TODO: Make the JIT code cache lock global.
144 bool is_method_being_compiled_;
145
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100146 // Dynamically allocated array of size `number_of_inline_caches_`.
147 InlineCache cache_[0];
148
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000149 friend class jit::JitCodeCache;
150
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100151 DISALLOW_COPY_AND_ASSIGN(ProfilingInfo);
152};
153
154} // namespace art
155
156#endif // ART_RUNTIME_JIT_PROFILING_INFO_H_