blob: f42a8da8fad1ea9a1bc7dfae7834b584a2892a73 [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:
Calin Juravle940eb0c2017-01-30 19:30:44 -080042 static constexpr uint8_t kIndividualCacheSize = 5;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +000043
44 private:
Nicolas Geoffray73be1e82015-09-17 15:22:56 +010045 uint32_t dex_pc_;
46 GcRoot<mirror::Class> classes_[kIndividualCacheSize];
47
Nicolas Geoffraye51ca8b2016-11-22 14:49:31 +000048 friend class jit::JitCodeCache;
Nicolas Geoffray73be1e82015-09-17 15:22:56 +010049 friend class ProfilingInfo;
50
51 DISALLOW_COPY_AND_ASSIGN(InlineCache);
52};
53
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010054/**
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 */
58class ProfilingInfo {
59 public:
Nicolas Geoffray26705e22015-10-28 12:50:11 +000060 // 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 Gampebdf7f1c2016-08-30 16:38:47 -070063 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010064
65 // Add information from an executed INVOKE instruction to the profile.
Nicolas Geoffray26705e22015-10-28 12:50:11 +000066 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 Gampebdf7f1c2016-08-30 16:38:47 -070070 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010071
Nicolas Geoffray26705e22015-10-28 12:50:11 +000072 ArtMethod* GetMethod() const {
73 return method_;
74 }
75
Nicolas Geoffrayfdb7d632017-02-08 15:07:18 +000076 // Mutator lock only required for debugging output.
77 InlineCache* GetInlineCache(uint32_t dex_pc)
78 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +010079
buzbee454b3b62016-04-07 14:42:47 -070080 bool IsMethodBeingCompiled(bool osr) const {
81 return osr
82 ? is_osr_method_being_compiled_
83 : is_method_being_compiled_;
Nicolas Geoffray73be1e82015-09-17 15:22:56 +010084 }
85
buzbee454b3b62016-04-07 14:42:47 -070086 void SetIsMethodBeingCompiled(bool value, bool osr) {
87 if (osr) {
88 is_osr_method_being_compiled_ = value;
89 } else {
90 is_method_being_compiled_ = value;
91 }
Nicolas Geoffray73be1e82015-09-17 15:22:56 +010092 }
93
Nicolas Geoffray35122442016-03-02 12:05:30 +000094 void SetSavedEntryPoint(const void* entry_point) {
95 saved_entry_point_ = entry_point;
96 }
97
98 const void* GetSavedEntryPoint() const {
99 return saved_entry_point_;
100 }
101
Nicolas Geoffrayb6e20ae2016-03-07 14:29:04 +0000102 void ClearGcRootsInInlineCaches() {
103 for (size_t i = 0; i < number_of_inline_caches_; ++i) {
104 InlineCache* cache = &cache_[i];
105 memset(&cache->classes_[0],
106 0,
107 InlineCache::kIndividualCacheSize * sizeof(GcRoot<mirror::Class>));
108 }
109 }
110
111 void IncrementInlineUse() {
112 DCHECK_NE(current_inline_uses_, std::numeric_limits<uint16_t>::max());
113 current_inline_uses_++;
114 }
115
116 void DecrementInlineUse() {
117 DCHECK_GT(current_inline_uses_, 0);
118 current_inline_uses_--;
119 }
120
121 bool IsInUseByCompiler() const {
buzbee454b3b62016-04-07 14:42:47 -0700122 return IsMethodBeingCompiled(/*osr*/ true) || IsMethodBeingCompiled(/*osr*/ false) ||
123 (current_inline_uses_ > 0);
Nicolas Geoffray511e41b2016-03-02 17:09:35 +0000124 }
125
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100126 private:
Mathieu Chartier65975772016-08-05 10:46:36 -0700127 ProfilingInfo(ArtMethod* method, const std::vector<uint32_t>& entries);
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100128
129 // Number of instructions we are profiling in the ArtMethod.
130 const uint32_t number_of_inline_caches_;
131
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000132 // Method this profiling info is for.
Alex Lightdba61482016-12-21 08:20:29 -0800133 // Not 'const' as JVMTI introduces obsolete methods that we implement by creating new ArtMethods.
134 // See JitCodeCache::MoveObsoleteMethod.
135 ArtMethod* method_;
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000136
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100137 // Whether the ArtMethod is currently being compiled. This flag
138 // is implicitly guarded by the JIT code cache lock.
139 // TODO: Make the JIT code cache lock global.
140 bool is_method_being_compiled_;
buzbee454b3b62016-04-07 14:42:47 -0700141 bool is_osr_method_being_compiled_;
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100142
Nicolas Geoffrayb6e20ae2016-03-07 14:29:04 +0000143 // When the compiler inlines the method associated to this ProfilingInfo,
144 // it updates this counter so that the GC does not try to clear the inline caches.
145 uint16_t current_inline_uses_;
146
Nicolas Geoffray35122442016-03-02 12:05:30 +0000147 // Entry point of the corresponding ArtMethod, while the JIT code cache
148 // is poking for the liveness of compiled code.
149 const void* saved_entry_point_;
150
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100151 // Dynamically allocated array of size `number_of_inline_caches_`.
152 InlineCache cache_[0];
153
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000154 friend class jit::JitCodeCache;
155
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100156 DISALLOW_COPY_AND_ASSIGN(ProfilingInfo);
157};
158
159} // namespace art
160
161#endif // ART_RUNTIME_JIT_PROFILING_INFO_H_