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 | |
| 19 | #include "jit.h" |
| 20 | #include "jit_code_cache.h" |
| 21 | #include "mirror/art_method-inl.h" |
| 22 | #include "scoped_thread_state_change.h" |
| 23 | |
| 24 | namespace art { |
| 25 | namespace jit { |
| 26 | |
| 27 | class JitCompileTask : public Task { |
| 28 | public: |
| 29 | explicit JitCompileTask(mirror::ArtMethod* method, JitInstrumentationCache* cache) |
| 30 | : method_(method), cache_(cache) { |
| 31 | } |
| 32 | |
| 33 | virtual void Run(Thread* self) OVERRIDE { |
| 34 | ScopedObjectAccess soa(self); |
| 35 | VLOG(jit) << "JitCompileTask compiling method " << PrettyMethod(method_); |
| 36 | if (Runtime::Current()->GetJit()->CompileMethod(method_, self)) { |
| 37 | cache_->SignalCompiled(self, method_); |
| 38 | } else { |
| 39 | VLOG(jit) << "Failed to compile method " << PrettyMethod(method_); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | virtual void Finalize() OVERRIDE { |
| 44 | delete this; |
| 45 | } |
| 46 | |
| 47 | private: |
| 48 | mirror::ArtMethod* const method_; |
| 49 | JitInstrumentationCache* const cache_; |
| 50 | }; |
| 51 | |
| 52 | JitInstrumentationCache::JitInstrumentationCache(size_t hot_method_threshold) |
| 53 | : lock_("jit instrumentation lock"), hot_method_threshold_(hot_method_threshold) { |
| 54 | } |
| 55 | |
| 56 | void JitInstrumentationCache::CreateThreadPool() { |
| 57 | thread_pool_.reset(new ThreadPool("Jit thread pool", 1)); |
| 58 | } |
| 59 | |
| 60 | void JitInstrumentationCache::DeleteThreadPool() { |
| 61 | thread_pool_.reset(); |
| 62 | } |
| 63 | |
| 64 | void JitInstrumentationCache::SignalCompiled(Thread* self, mirror::ArtMethod* method) { |
| 65 | ScopedObjectAccessUnchecked soa(self); |
| 66 | jmethodID method_id = soa.EncodeMethod(method); |
| 67 | MutexLock mu(self, lock_); |
| 68 | auto it = samples_.find(method_id); |
| 69 | if (it != samples_.end()) { |
| 70 | samples_.erase(it); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | void JitInstrumentationCache::AddSamples(Thread* self, mirror::ArtMethod* method, size_t count) { |
| 75 | ScopedObjectAccessUnchecked soa(self); |
| 76 | // Since we don't have on-stack replacement, some methods can remain in the interpreter longer |
| 77 | // than we want resulting in samples even after the method is compiled. |
| 78 | if (method->IsClassInitializer() || |
| 79 | Runtime::Current()->GetJit()->GetCodeCache()->ContainsMethod(method)) { |
| 80 | return; |
| 81 | } |
| 82 | jmethodID method_id = soa.EncodeMethod(method); |
| 83 | bool is_hot = false; |
| 84 | { |
| 85 | MutexLock mu(self, lock_); |
| 86 | size_t sample_count = 0; |
| 87 | auto it = samples_.find(method_id); |
| 88 | if (it != samples_.end()) { |
| 89 | it->second += count; |
| 90 | sample_count = it->second; |
| 91 | } else { |
| 92 | sample_count = count; |
| 93 | samples_.insert(std::make_pair(method_id, count)); |
| 94 | } |
| 95 | // If we have enough samples, mark as hot and request Jit compilation. |
| 96 | if (sample_count >= hot_method_threshold_ && sample_count - count < hot_method_threshold_) { |
| 97 | is_hot = true; |
| 98 | } |
| 99 | } |
| 100 | if (is_hot) { |
| 101 | if (thread_pool_.get() != nullptr) { |
| 102 | thread_pool_->AddTask(self, new JitCompileTask(method->GetInterfaceMethodIfProxy(), this)); |
| 103 | thread_pool_->StartWorkers(self); |
| 104 | } else { |
| 105 | VLOG(jit) << "Compiling hot method " << PrettyMethod(method); |
| 106 | Runtime::Current()->GetJit()->CompileMethod(method->GetInterfaceMethodIfProxy(), self); |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | JitInstrumentationListener::JitInstrumentationListener(JitInstrumentationCache* cache) |
| 112 | : instrumentation_cache_(cache) { |
| 113 | CHECK(instrumentation_cache_ != nullptr); |
| 114 | } |
| 115 | |
| 116 | } // namespace jit |
| 117 | } // namespace art |