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