Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 | #include "jit_instrumentation.h" |
| 18 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 19 | #include "art_method-inl.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 20 | #include "jit.h" |
| 21 | #include "jit_code_cache.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 22 | #include "scoped_thread_state_change.h" |
Nicolas Geoffray | 629e935 | 2015-11-04 17:22:16 +0000 | [diff] [blame] | 23 | #include "thread_list.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 24 | |
| 25 | namespace art { |
| 26 | namespace jit { |
| 27 | |
Andreas Gampe | 9e927f5 | 2016-02-29 20:49:38 -0800 | [diff] [blame] | 28 | // At what priority to schedule jit threads. 9 is the lowest foreground priority on device. |
| 29 | static constexpr int kJitPoolThreadPthreadPriority = 9; |
| 30 | |
Mathieu Chartier | 36c0136 | 2015-09-25 14:39:40 -0700 | [diff] [blame] | 31 | class JitCompileTask FINAL : public Task { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 32 | public: |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 33 | enum TaskKind { |
| 34 | kAllocateProfile, |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 35 | kCompile, |
| 36 | kCompileOsr |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | JitCompileTask(ArtMethod* method, TaskKind kind) : method_(method), kind_(kind) { |
Mathieu Chartier | a50f9cf | 2015-09-25 11:34:45 -0700 | [diff] [blame] | 40 | ScopedObjectAccess soa(Thread::Current()); |
| 41 | // Add a global ref to the class to prevent class unloading until compilation is done. |
| 42 | klass_ = soa.Vm()->AddGlobalRef(soa.Self(), method_->GetDeclaringClass()); |
| 43 | CHECK(klass_ != nullptr); |
| 44 | } |
| 45 | |
| 46 | ~JitCompileTask() { |
| 47 | ScopedObjectAccess soa(Thread::Current()); |
| 48 | soa.Vm()->DeleteGlobalRef(soa.Self(), klass_); |
| 49 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 50 | |
Mathieu Chartier | 36c0136 | 2015-09-25 14:39:40 -0700 | [diff] [blame] | 51 | void Run(Thread* self) OVERRIDE { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 52 | ScopedObjectAccess soa(self); |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 53 | if (kind_ == kCompile) { |
| 54 | VLOG(jit) << "JitCompileTask compiling method " << PrettyMethod(method_); |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 55 | if (!Runtime::Current()->GetJit()->CompileMethod(method_, self, /* osr */ false)) { |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 56 | VLOG(jit) << "Failed to compile method " << PrettyMethod(method_); |
| 57 | } |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 58 | } else if (kind_ == kCompileOsr) { |
| 59 | VLOG(jit) << "JitCompileTask compiling method osr " << PrettyMethod(method_); |
| 60 | if (!Runtime::Current()->GetJit()->CompileMethod(method_, self, /* osr */ true)) { |
| 61 | VLOG(jit) << "Failed to compile method osr " << PrettyMethod(method_); |
| 62 | } |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 63 | } else { |
| 64 | DCHECK(kind_ == kAllocateProfile); |
| 65 | if (ProfilingInfo::Create(self, method_, /* retry_allocation */ true)) { |
| 66 | VLOG(jit) << "Start profiling " << PrettyMethod(method_); |
| 67 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | |
Mathieu Chartier | 36c0136 | 2015-09-25 14:39:40 -0700 | [diff] [blame] | 71 | void Finalize() OVERRIDE { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 72 | delete this; |
| 73 | } |
| 74 | |
| 75 | private: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 76 | ArtMethod* const method_; |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 77 | const TaskKind kind_; |
Mathieu Chartier | a50f9cf | 2015-09-25 11:34:45 -0700 | [diff] [blame] | 78 | jobject klass_; |
Mathieu Chartier | 3130cdf | 2015-05-03 15:20:23 -0700 | [diff] [blame] | 79 | |
| 80 | DISALLOW_IMPLICIT_CONSTRUCTORS(JitCompileTask); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 81 | }; |
| 82 | |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 83 | JitInstrumentationCache::JitInstrumentationCache(size_t hot_method_threshold, |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 84 | size_t warm_method_threshold, |
| 85 | size_t osr_method_threshold) |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 86 | : hot_method_threshold_(hot_method_threshold), |
Nicolas Geoffray | 629e935 | 2015-11-04 17:22:16 +0000 | [diff] [blame] | 87 | warm_method_threshold_(warm_method_threshold), |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 88 | osr_method_threshold_(osr_method_threshold), |
Nicolas Geoffray | 629e935 | 2015-11-04 17:22:16 +0000 | [diff] [blame] | 89 | listener_(this) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | void JitInstrumentationCache::CreateThreadPool() { |
Nicolas Geoffray | 629e935 | 2015-11-04 17:22:16 +0000 | [diff] [blame] | 93 | // Create the thread pool before setting the instrumentation, so that |
| 94 | // when the threads stopped being suspended, they can use it directly. |
| 95 | // There is a DCHECK in the 'AddSamples' method to ensure the tread pool |
| 96 | // is not null when we instrument. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 97 | thread_pool_.reset(new ThreadPool("Jit thread pool", 1)); |
Andreas Gampe | 9e927f5 | 2016-02-29 20:49:38 -0800 | [diff] [blame] | 98 | thread_pool_->SetPthreadPriority(kJitPoolThreadPthreadPriority); |
Nicolas Geoffray | 629e935 | 2015-11-04 17:22:16 +0000 | [diff] [blame] | 99 | thread_pool_->StartWorkers(Thread::Current()); |
| 100 | { |
| 101 | // Add Jit interpreter instrumentation, tells the interpreter when |
| 102 | // to notify the jit to compile something. |
| 103 | ScopedSuspendAll ssa(__FUNCTION__); |
| 104 | Runtime::Current()->GetInstrumentation()->AddListener( |
| 105 | &listener_, JitInstrumentationListener::kJitEvents); |
| 106 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 107 | } |
| 108 | |
Nicolas Geoffray | 629e935 | 2015-11-04 17:22:16 +0000 | [diff] [blame] | 109 | void JitInstrumentationCache::DeleteThreadPool(Thread* self) { |
| 110 | DCHECK(Runtime::Current()->IsShuttingDown(self)); |
| 111 | if (thread_pool_ != nullptr) { |
| 112 | // First remove the listener, to avoid having mutators enter |
| 113 | // 'AddSamples'. |
| 114 | ThreadPool* cache = nullptr; |
| 115 | { |
| 116 | ScopedSuspendAll ssa(__FUNCTION__); |
| 117 | Runtime::Current()->GetInstrumentation()->RemoveListener( |
| 118 | &listener_, JitInstrumentationListener::kJitEvents); |
| 119 | // Clear thread_pool_ field while the threads are suspended. |
| 120 | // A mutator in the 'AddSamples' method will check against it. |
| 121 | cache = thread_pool_.release(); |
| 122 | } |
| 123 | cache->StopWorkers(self); |
| 124 | cache->RemoveAllTasks(self); |
| 125 | // We could just suspend all threads, but we know those threads |
| 126 | // will finish in a short period, so it's not worth adding a suspend logic |
| 127 | // here. Besides, this is only done for shutdown. |
| 128 | cache->Wait(self, false, false); |
| 129 | delete cache; |
| 130 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 131 | } |
| 132 | |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 133 | void JitInstrumentationCache::AddSamples(Thread* self, ArtMethod* method, size_t) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 134 | // Since we don't have on-stack replacement, some methods can remain in the interpreter longer |
| 135 | // than we want resulting in samples even after the method is compiled. |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 136 | if (method->IsClassInitializer() || method->IsNative()) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 137 | return; |
| 138 | } |
Nicolas Geoffray | 629e935 | 2015-11-04 17:22:16 +0000 | [diff] [blame] | 139 | DCHECK(thread_pool_ != nullptr); |
| 140 | |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 141 | uint16_t sample_count = method->IncrementCounter(); |
| 142 | if (sample_count == warm_method_threshold_) { |
Nicolas Geoffray | 629e935 | 2015-11-04 17:22:16 +0000 | [diff] [blame] | 143 | bool success = ProfilingInfo::Create(self, method, /* retry_allocation */ false); |
| 144 | if (success) { |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 145 | VLOG(jit) << "Start profiling " << PrettyMethod(method); |
Nicolas Geoffray | 629e935 | 2015-11-04 17:22:16 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | if (thread_pool_ == nullptr) { |
| 149 | // Calling ProfilingInfo::Create might put us in a suspended state, which could |
| 150 | // lead to the thread pool being deleted when we are shutting down. |
| 151 | DCHECK(Runtime::Current()->IsShuttingDown(self)); |
| 152 | return; |
| 153 | } |
| 154 | |
| 155 | if (!success) { |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 156 | // We failed allocating. Instead of doing the collection on the Java thread, we push |
| 157 | // an allocation to a compiler thread, that will do the collection. |
Nicolas Geoffray | 22cf3d3 | 2015-11-02 11:57:11 +0000 | [diff] [blame] | 158 | thread_pool_->AddTask(self, new JitCompileTask(method, JitCompileTask::kAllocateProfile)); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 159 | } |
| 160 | } |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 161 | |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 162 | if (sample_count == hot_method_threshold_) { |
Nicolas Geoffray | 629e935 | 2015-11-04 17:22:16 +0000 | [diff] [blame] | 163 | DCHECK(thread_pool_ != nullptr); |
Nicolas Geoffray | 22cf3d3 | 2015-11-02 11:57:11 +0000 | [diff] [blame] | 164 | thread_pool_->AddTask(self, new JitCompileTask(method, JitCompileTask::kCompile)); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 165 | } |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 166 | |
| 167 | if (sample_count == osr_method_threshold_) { |
| 168 | DCHECK(thread_pool_ != nullptr); |
| 169 | thread_pool_->AddTask(self, new JitCompileTask(method, JitCompileTask::kCompileOsr)); |
| 170 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | JitInstrumentationListener::JitInstrumentationListener(JitInstrumentationCache* cache) |
| 174 | : instrumentation_cache_(cache) { |
| 175 | CHECK(instrumentation_cache_ != nullptr); |
| 176 | } |
| 177 | |
Nicolas Geoffray | 629e935 | 2015-11-04 17:22:16 +0000 | [diff] [blame] | 178 | void JitInstrumentationListener::MethodEntered(Thread* thread, |
| 179 | mirror::Object* /*this_object*/, |
| 180 | ArtMethod* method, |
| 181 | uint32_t /*dex_pc*/) { |
Siva Chandra | 05d2415 | 2016-01-05 17:43:17 -0800 | [diff] [blame] | 182 | if (UNLIKELY(Runtime::Current()->GetJit()->JitAtFirstUse())) { |
| 183 | // The compiler requires a ProfilingInfo object. |
| 184 | ProfilingInfo::Create(thread, method, /* retry_allocation */ true); |
| 185 | JitCompileTask compile_task(method, JitCompileTask::kCompile); |
| 186 | compile_task.Run(thread); |
| 187 | return; |
| 188 | } |
| 189 | |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 190 | ProfilingInfo* profiling_info = method->GetProfilingInfo(sizeof(void*)); |
| 191 | // Update the entrypoint if the ProfilingInfo has one. The interpreter will call it |
| 192 | // instead of interpreting the method. |
| 193 | // We avoid doing this if exit stubs are installed to not mess with the instrumentation. |
| 194 | // TODO(ngeoffray): Clean up instrumentation and code cache interactions. |
| 195 | if ((profiling_info != nullptr) && |
| 196 | (profiling_info->GetSavedEntryPoint() != nullptr) && |
| 197 | !Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled()) { |
| 198 | method->SetEntryPointFromQuickCompiledCode(profiling_info->GetSavedEntryPoint()); |
| 199 | } else { |
| 200 | instrumentation_cache_->AddSamples(thread, method, 1); |
| 201 | } |
Nicolas Geoffray | 629e935 | 2015-11-04 17:22:16 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Nicolas Geoffray | 81f0f95 | 2016-01-20 16:25:19 +0000 | [diff] [blame] | 204 | void JitInstrumentationListener::Branch(Thread* thread, |
| 205 | ArtMethod* method, |
Nicolas Geoffray | cf22658 | 2016-01-21 15:47:37 +0000 | [diff] [blame] | 206 | uint32_t dex_pc ATTRIBUTE_UNUSED, |
Nicolas Geoffray | 81f0f95 | 2016-01-20 16:25:19 +0000 | [diff] [blame] | 207 | int32_t dex_pc_offset) { |
| 208 | if (dex_pc_offset < 0) { |
| 209 | // Increment method hotness if it is a backward branch. |
| 210 | instrumentation_cache_->AddSamples(thread, method, 1); |
| 211 | } |
Nicolas Geoffray | 629e935 | 2015-11-04 17:22:16 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 214 | void JitInstrumentationListener::InvokeVirtualOrInterface(Thread* thread, |
| 215 | mirror::Object* this_object, |
| 216 | ArtMethod* caller, |
| 217 | uint32_t dex_pc, |
| 218 | ArtMethod* callee ATTRIBUTE_UNUSED) { |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 219 | // We make sure we cannot be suspended, as the profiling info can be concurrently deleted. |
Mathieu Chartier | 3fdb3fe | 2016-01-14 10:24:28 -0800 | [diff] [blame] | 220 | instrumentation_cache_->AddSamples(thread, caller, 1); |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 221 | DCHECK(this_object != nullptr); |
Mathieu Chartier | 1147b9b | 2015-09-14 18:50:08 -0700 | [diff] [blame] | 222 | ProfilingInfo* info = caller->GetProfilingInfo(sizeof(void*)); |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 223 | if (info != nullptr) { |
Mathieu Chartier | 9ccf051 | 2015-10-02 13:08:39 -0700 | [diff] [blame] | 224 | // Since the instrumentation is marked from the declaring class we need to mark the card so |
| 225 | // that mod-union tables and card rescanning know about the update. |
| 226 | Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(caller->GetDeclaringClass()); |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 227 | info->AddInvokeInfo(dex_pc, this_object->GetClass()); |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | |
Mathieu Chartier | a50f9cf | 2015-09-25 11:34:45 -0700 | [diff] [blame] | 231 | void JitInstrumentationCache::WaitForCompilationToFinish(Thread* self) { |
Nicolas Geoffray | 629e935 | 2015-11-04 17:22:16 +0000 | [diff] [blame] | 232 | if (thread_pool_ != nullptr) { |
| 233 | thread_pool_->Wait(self, false, false); |
| 234 | } |
Mathieu Chartier | a50f9cf | 2015-09-25 11:34:45 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 237 | } // namespace jit |
| 238 | } // namespace art |