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