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" |
| 23 | |
| 24 | namespace art { |
| 25 | namespace jit { |
| 26 | |
Mathieu Chartier | 36c0136 | 2015-09-25 14:39:40 -0700 | [diff] [blame] | 27 | class JitCompileTask FINAL : public Task { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 28 | public: |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 29 | enum TaskKind { |
| 30 | kAllocateProfile, |
| 31 | kCompile |
| 32 | }; |
| 33 | |
| 34 | JitCompileTask(ArtMethod* method, TaskKind kind) : method_(method), kind_(kind) { |
Mathieu Chartier | a50f9cf | 2015-09-25 11:34:45 -0700 | [diff] [blame] | 35 | ScopedObjectAccess soa(Thread::Current()); |
| 36 | // Add a global ref to the class to prevent class unloading until compilation is done. |
| 37 | klass_ = soa.Vm()->AddGlobalRef(soa.Self(), method_->GetDeclaringClass()); |
| 38 | CHECK(klass_ != nullptr); |
| 39 | } |
| 40 | |
| 41 | ~JitCompileTask() { |
| 42 | ScopedObjectAccess soa(Thread::Current()); |
| 43 | soa.Vm()->DeleteGlobalRef(soa.Self(), klass_); |
| 44 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 45 | |
Mathieu Chartier | 36c0136 | 2015-09-25 14:39:40 -0700 | [diff] [blame] | 46 | void Run(Thread* self) OVERRIDE { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 47 | ScopedObjectAccess soa(self); |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 48 | if (kind_ == kCompile) { |
| 49 | VLOG(jit) << "JitCompileTask compiling method " << PrettyMethod(method_); |
| 50 | if (!Runtime::Current()->GetJit()->CompileMethod(method_, self)) { |
| 51 | VLOG(jit) << "Failed to compile method " << PrettyMethod(method_); |
| 52 | } |
| 53 | } else { |
| 54 | DCHECK(kind_ == kAllocateProfile); |
| 55 | if (ProfilingInfo::Create(self, method_, /* retry_allocation */ true)) { |
| 56 | VLOG(jit) << "Start profiling " << PrettyMethod(method_); |
| 57 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 58 | } |
| 59 | } |
| 60 | |
Mathieu Chartier | 36c0136 | 2015-09-25 14:39:40 -0700 | [diff] [blame] | 61 | void Finalize() OVERRIDE { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 62 | delete this; |
| 63 | } |
| 64 | |
| 65 | private: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 66 | ArtMethod* const method_; |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 67 | const TaskKind kind_; |
Mathieu Chartier | a50f9cf | 2015-09-25 11:34:45 -0700 | [diff] [blame] | 68 | jobject klass_; |
Mathieu Chartier | 3130cdf | 2015-05-03 15:20:23 -0700 | [diff] [blame] | 69 | |
| 70 | DISALLOW_IMPLICIT_CONSTRUCTORS(JitCompileTask); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 71 | }; |
| 72 | |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 73 | JitInstrumentationCache::JitInstrumentationCache(size_t hot_method_threshold, |
| 74 | size_t warm_method_threshold) |
| 75 | : hot_method_threshold_(hot_method_threshold), |
| 76 | warm_method_threshold_(warm_method_threshold) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | void JitInstrumentationCache::CreateThreadPool() { |
| 80 | thread_pool_.reset(new ThreadPool("Jit thread pool", 1)); |
| 81 | } |
| 82 | |
| 83 | void JitInstrumentationCache::DeleteThreadPool() { |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 84 | DCHECK(Runtime::Current()->IsShuttingDown(Thread::Current())); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 85 | thread_pool_.reset(); |
| 86 | } |
| 87 | |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 88 | void JitInstrumentationCache::AddSamples(Thread* self, ArtMethod* method, size_t) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 89 | // Since we don't have on-stack replacement, some methods can remain in the interpreter longer |
| 90 | // than we want resulting in samples even after the method is compiled. |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 91 | if (method->IsClassInitializer() || method->IsNative()) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 92 | return; |
| 93 | } |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 94 | if (thread_pool_.get() == nullptr) { |
| 95 | DCHECK(Runtime::Current()->IsShuttingDown(self)); |
| 96 | return; |
| 97 | } |
| 98 | uint16_t sample_count = method->IncrementCounter(); |
| 99 | if (sample_count == warm_method_threshold_) { |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 100 | if (ProfilingInfo::Create(self, method, /* retry_allocation */ false)) { |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 101 | VLOG(jit) << "Start profiling " << PrettyMethod(method); |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 102 | } else { |
| 103 | // We failed allocating. Instead of doing the collection on the Java thread, we push |
| 104 | // an allocation to a compiler thread, that will do the collection. |
Nicolas Geoffray | 22cf3d3 | 2015-11-02 11:57:11 +0000 | [diff] [blame] | 105 | thread_pool_->AddTask(self, new JitCompileTask(method, JitCompileTask::kAllocateProfile)); |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 106 | thread_pool_->StartWorkers(self); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 107 | } |
| 108 | } |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 109 | |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 110 | if (sample_count == hot_method_threshold_) { |
Nicolas Geoffray | 22cf3d3 | 2015-11-02 11:57:11 +0000 | [diff] [blame] | 111 | thread_pool_->AddTask(self, new JitCompileTask(method, JitCompileTask::kCompile)); |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 112 | thread_pool_->StartWorkers(self); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | |
| 116 | JitInstrumentationListener::JitInstrumentationListener(JitInstrumentationCache* cache) |
| 117 | : instrumentation_cache_(cache) { |
| 118 | CHECK(instrumentation_cache_ != nullptr); |
| 119 | } |
| 120 | |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 121 | void JitInstrumentationListener::InvokeVirtualOrInterface(Thread* thread, |
| 122 | mirror::Object* this_object, |
| 123 | ArtMethod* caller, |
| 124 | uint32_t dex_pc, |
| 125 | ArtMethod* callee ATTRIBUTE_UNUSED) { |
Nicolas Geoffray | 4e915fb | 2015-10-28 17:39:47 +0000 | [diff] [blame] | 126 | instrumentation_cache_->AddSamples(thread, caller, 1); |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 127 | // We make sure we cannot be suspended, as the profiling info can be concurrently deleted. |
| 128 | thread->StartAssertNoThreadSuspension("Instrumenting invoke"); |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 129 | DCHECK(this_object != nullptr); |
Mathieu Chartier | 1147b9b | 2015-09-14 18:50:08 -0700 | [diff] [blame] | 130 | ProfilingInfo* info = caller->GetProfilingInfo(sizeof(void*)); |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 131 | if (info != nullptr) { |
Mathieu Chartier | 9ccf051 | 2015-10-02 13:08:39 -0700 | [diff] [blame] | 132 | // Since the instrumentation is marked from the declaring class we need to mark the card so |
| 133 | // that mod-union tables and card rescanning know about the update. |
| 134 | Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(caller->GetDeclaringClass()); |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 135 | info->AddInvokeInfo(dex_pc, this_object->GetClass()); |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 136 | } |
Nicolas Geoffray | 26705e2 | 2015-10-28 12:50:11 +0000 | [diff] [blame] | 137 | thread->EndAssertNoThreadSuspension(nullptr); |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 138 | } |
| 139 | |
Mathieu Chartier | a50f9cf | 2015-09-25 11:34:45 -0700 | [diff] [blame] | 140 | void JitInstrumentationCache::WaitForCompilationToFinish(Thread* self) { |
Mathieu Chartier | c3fcd41 | 2015-09-25 16:54:59 -0700 | [diff] [blame] | 141 | thread_pool_->Wait(self, false, false); |
Mathieu Chartier | a50f9cf | 2015-09-25 11:34:45 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 144 | } // namespace jit |
| 145 | } // namespace art |