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: |
Mathieu Chartier | a50f9cf | 2015-09-25 11:34:45 -0700 | [diff] [blame] | 29 | explicit JitCompileTask(ArtMethod* method) : method_(method) { |
| 30 | ScopedObjectAccess soa(Thread::Current()); |
| 31 | // Add a global ref to the class to prevent class unloading until compilation is done. |
| 32 | klass_ = soa.Vm()->AddGlobalRef(soa.Self(), method_->GetDeclaringClass()); |
| 33 | CHECK(klass_ != nullptr); |
| 34 | } |
| 35 | |
| 36 | ~JitCompileTask() { |
| 37 | ScopedObjectAccess soa(Thread::Current()); |
| 38 | soa.Vm()->DeleteGlobalRef(soa.Self(), klass_); |
| 39 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 40 | |
Mathieu Chartier | 36c0136 | 2015-09-25 14:39:40 -0700 | [diff] [blame] | 41 | void Run(Thread* self) OVERRIDE { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 42 | ScopedObjectAccess soa(self); |
| 43 | VLOG(jit) << "JitCompileTask compiling method " << PrettyMethod(method_); |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 44 | if (!Runtime::Current()->GetJit()->CompileMethod(method_, self)) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 45 | VLOG(jit) << "Failed to compile method " << PrettyMethod(method_); |
| 46 | } |
| 47 | } |
| 48 | |
Mathieu Chartier | 36c0136 | 2015-09-25 14:39:40 -0700 | [diff] [blame] | 49 | void Finalize() OVERRIDE { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 50 | delete this; |
| 51 | } |
| 52 | |
| 53 | private: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 54 | ArtMethod* const method_; |
Mathieu Chartier | a50f9cf | 2015-09-25 11:34:45 -0700 | [diff] [blame] | 55 | jobject klass_; |
Mathieu Chartier | 3130cdf | 2015-05-03 15:20:23 -0700 | [diff] [blame] | 56 | |
| 57 | DISALLOW_IMPLICIT_CONSTRUCTORS(JitCompileTask); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 58 | }; |
| 59 | |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 60 | JitInstrumentationCache::JitInstrumentationCache(size_t hot_method_threshold, |
| 61 | size_t warm_method_threshold) |
| 62 | : hot_method_threshold_(hot_method_threshold), |
| 63 | warm_method_threshold_(warm_method_threshold) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | void JitInstrumentationCache::CreateThreadPool() { |
| 67 | thread_pool_.reset(new ThreadPool("Jit thread pool", 1)); |
| 68 | } |
| 69 | |
| 70 | void JitInstrumentationCache::DeleteThreadPool() { |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 71 | DCHECK(Runtime::Current()->IsShuttingDown(Thread::Current())); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 72 | thread_pool_.reset(); |
| 73 | } |
| 74 | |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 75 | void JitInstrumentationCache::AddSamples(Thread* self, ArtMethod* method, size_t) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 76 | ScopedObjectAccessUnchecked soa(self); |
| 77 | // Since we don't have on-stack replacement, some methods can remain in the interpreter longer |
| 78 | // than we want resulting in samples even after the method is compiled. |
Mathieu Chartier | 70f7d98 | 2015-05-08 17:05:01 -0700 | [diff] [blame] | 79 | if (method->IsClassInitializer() || method->IsNative() || |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 80 | Runtime::Current()->GetJit()->GetCodeCache()->ContainsMethod(method)) { |
| 81 | return; |
| 82 | } |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 83 | if (thread_pool_.get() == nullptr) { |
| 84 | DCHECK(Runtime::Current()->IsShuttingDown(self)); |
| 85 | return; |
| 86 | } |
| 87 | uint16_t sample_count = method->IncrementCounter(); |
| 88 | if (sample_count == warm_method_threshold_) { |
| 89 | ProfilingInfo* info = method->CreateProfilingInfo(); |
| 90 | if (info != nullptr) { |
| 91 | VLOG(jit) << "Start profiling " << PrettyMethod(method); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 92 | } |
| 93 | } |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 94 | if (sample_count == hot_method_threshold_) { |
| 95 | thread_pool_->AddTask(self, new JitCompileTask( |
| 96 | method->GetInterfaceMethodIfProxy(sizeof(void*)))); |
| 97 | thread_pool_->StartWorkers(self); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
| 101 | JitInstrumentationListener::JitInstrumentationListener(JitInstrumentationCache* cache) |
| 102 | : instrumentation_cache_(cache) { |
| 103 | CHECK(instrumentation_cache_ != nullptr); |
| 104 | } |
| 105 | |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 106 | void JitInstrumentationListener::InvokeVirtualOrInterface(Thread* thread, |
| 107 | mirror::Object* this_object, |
| 108 | ArtMethod* caller, |
| 109 | uint32_t dex_pc, |
| 110 | ArtMethod* callee ATTRIBUTE_UNUSED) { |
| 111 | DCHECK(this_object != nullptr); |
Mathieu Chartier | 1147b9b | 2015-09-14 18:50:08 -0700 | [diff] [blame] | 112 | ProfilingInfo* info = caller->GetProfilingInfo(sizeof(void*)); |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 113 | if (info != nullptr) { |
Mathieu Chartier | 9ccf051 | 2015-10-02 13:08:39 -0700 | [diff] [blame^] | 114 | // Since the instrumentation is marked from the declaring class we need to mark the card so |
| 115 | // that mod-union tables and card rescanning know about the update. |
| 116 | Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(caller->GetDeclaringClass()); |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 117 | info->AddInvokeInfo(thread, dex_pc, this_object->GetClass()); |
| 118 | } |
| 119 | } |
| 120 | |
Mathieu Chartier | a50f9cf | 2015-09-25 11:34:45 -0700 | [diff] [blame] | 121 | void JitInstrumentationCache::WaitForCompilationToFinish(Thread* self) { |
Mathieu Chartier | c3fcd41 | 2015-09-25 16:54:59 -0700 | [diff] [blame] | 122 | thread_pool_->Wait(self, false, false); |
Mathieu Chartier | a50f9cf | 2015-09-25 11:34:45 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 125 | } // namespace jit |
| 126 | } // namespace art |