blob: b13a315d64392c1bce6ced71cd811c085f5186f5 [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;
28
Nicolas Geoffray26705e22015-10-28 12:50:11 +000029namespace jit {
30class JitCodeCache;
31}
32
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010033namespace mirror {
34class Class;
35}
36
37/**
38 * Profiling info for a method, created and filled by the interpreter once the
39 * method is warm, and used by the compiler to drive optimizations.
40 */
41class ProfilingInfo {
42 public:
Nicolas Geoffray26705e22015-10-28 12:50:11 +000043 // Create a ProfilingInfo for 'method'. Return whether it succeeded, or if it is
44 // not needed in case the method does not have virtual/interface invocations.
45 static bool Create(Thread* self, ArtMethod* method, bool retry_allocation)
46 SHARED_REQUIRES(Locks::mutator_lock_);
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010047
48 // Add information from an executed INVOKE instruction to the profile.
Nicolas Geoffray26705e22015-10-28 12:50:11 +000049 void AddInvokeInfo(uint32_t dex_pc, mirror::Class* cls)
50 // Method should not be interruptible, as it manipulates the ProfilingInfo
51 // which can be concurrently collected.
52 REQUIRES(Roles::uninterruptible_)
53 SHARED_REQUIRES(Locks::mutator_lock_);
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010054
55 // NO_THREAD_SAFETY_ANALYSIS since we don't know what the callback requires.
56 template<typename RootVisitorType>
57 void VisitRoots(RootVisitorType& visitor) NO_THREAD_SAFETY_ANALYSIS {
58 for (size_t i = 0; i < number_of_inline_caches_; ++i) {
59 InlineCache* cache = &cache_[i];
60 for (size_t j = 0; j < InlineCache::kIndividualCacheSize; ++j) {
61 visitor.VisitRootIfNonNull(cache->classes_[j].AddressWithoutBarrier());
62 }
63 }
64 }
65
Nicolas Geoffray26705e22015-10-28 12:50:11 +000066 ArtMethod* GetMethod() const {
67 return method_;
68 }
69
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010070 private:
71 // Structure to store the classes seen at runtime for a specific instruction.
72 // Once the classes_ array is full, we consider the INVOKE to be megamorphic.
73 struct InlineCache {
74 bool IsMonomorphic() const {
75 DCHECK_GE(kIndividualCacheSize, 2);
76 return !classes_[0].IsNull() && classes_[1].IsNull();
77 }
78
79 bool IsMegamorphic() const {
80 for (size_t i = 0; i < kIndividualCacheSize; ++i) {
81 if (classes_[i].IsNull()) {
82 return false;
83 }
84 }
85 return true;
86 }
87
88 bool IsUnitialized() const {
89 return classes_[0].IsNull();
90 }
91
92 bool IsPolymorphic() const {
93 DCHECK_GE(kIndividualCacheSize, 3);
94 return !classes_[1].IsNull() && classes_[kIndividualCacheSize - 1].IsNull();
95 }
96
97 static constexpr uint16_t kIndividualCacheSize = 5;
98 uint32_t dex_pc;
99 GcRoot<mirror::Class> classes_[kIndividualCacheSize];
100 };
101
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000102 ProfilingInfo(ArtMethod* method, const std::vector<uint32_t>& entries)
103 : number_of_inline_caches_(entries.size()),
104 method_(method) {
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100105 memset(&cache_, 0, number_of_inline_caches_ * sizeof(InlineCache));
106 for (size_t i = 0; i < number_of_inline_caches_; ++i) {
107 cache_[i].dex_pc = entries[i];
108 }
109 }
110
111 // Number of instructions we are profiling in the ArtMethod.
112 const uint32_t number_of_inline_caches_;
113
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000114 // Method this profiling info is for.
115 ArtMethod* const method_;
116
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100117 // Dynamically allocated array of size `number_of_inline_caches_`.
118 InlineCache cache_[0];
119
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000120 friend class jit::JitCodeCache;
121
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100122 DISALLOW_COPY_AND_ASSIGN(ProfilingInfo);
123};
124
125} // namespace art
126
127#endif // ART_RUNTIME_JIT_PROFILING_INFO_H_