1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
/*
* Copyright 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "jit_instrumentation.h"
#include "art_method-inl.h"
#include "jit.h"
#include "jit_code_cache.h"
#include "scoped_thread_state_change.h"
namespace art {
namespace jit {
class JitCompileTask : public Task {
public:
explicit JitCompileTask(ArtMethod* method, JitInstrumentationCache* cache)
: method_(method), cache_(cache) {
}
virtual void Run(Thread* self) OVERRIDE {
ScopedObjectAccess soa(self);
VLOG(jit) << "JitCompileTask compiling method " << PrettyMethod(method_);
if (Runtime::Current()->GetJit()->CompileMethod(method_, self)) {
cache_->SignalCompiled(self, method_);
} else {
VLOG(jit) << "Failed to compile method " << PrettyMethod(method_);
}
}
virtual void Finalize() OVERRIDE {
delete this;
}
private:
ArtMethod* const method_;
JitInstrumentationCache* const cache_;
DISALLOW_IMPLICIT_CONSTRUCTORS(JitCompileTask);
};
JitInstrumentationCache::JitInstrumentationCache(size_t hot_method_threshold)
: lock_("jit instrumentation lock"), hot_method_threshold_(hot_method_threshold) {
}
void JitInstrumentationCache::CreateThreadPool() {
thread_pool_.reset(new ThreadPool("Jit thread pool", 1));
}
void JitInstrumentationCache::DeleteThreadPool() {
thread_pool_.reset();
}
void JitInstrumentationCache::SignalCompiled(Thread* self, ArtMethod* method) {
ScopedObjectAccessUnchecked soa(self);
jmethodID method_id = soa.EncodeMethod(method);
MutexLock mu(self, lock_);
auto it = samples_.find(method_id);
if (it != samples_.end()) {
samples_.erase(it);
}
}
void JitInstrumentationCache::AddSamples(Thread* self, ArtMethod* method, size_t count) {
ScopedObjectAccessUnchecked soa(self);
// Since we don't have on-stack replacement, some methods can remain in the interpreter longer
// than we want resulting in samples even after the method is compiled.
if (method->IsClassInitializer() || method->IsNative() ||
Runtime::Current()->GetJit()->GetCodeCache()->ContainsMethod(method)) {
return;
}
jmethodID method_id = soa.EncodeMethod(method);
bool is_hot = false;
{
MutexLock mu(self, lock_);
size_t sample_count = 0;
auto it = samples_.find(method_id);
if (it != samples_.end()) {
it->second += count;
sample_count = it->second;
} else {
sample_count = count;
samples_.insert(std::make_pair(method_id, count));
}
// If we have enough samples, mark as hot and request Jit compilation.
if (sample_count >= hot_method_threshold_ && sample_count - count < hot_method_threshold_) {
is_hot = true;
}
}
if (is_hot) {
if (thread_pool_.get() != nullptr) {
thread_pool_->AddTask(self, new JitCompileTask(
method->GetInterfaceMethodIfProxy(sizeof(void*)), this));
thread_pool_->StartWorkers(self);
} else {
VLOG(jit) << "Compiling hot method " << PrettyMethod(method);
Runtime::Current()->GetJit()->CompileMethod(
method->GetInterfaceMethodIfProxy(sizeof(void*)), self);
}
}
}
JitInstrumentationListener::JitInstrumentationListener(JitInstrumentationCache* cache)
: instrumentation_cache_(cache) {
CHECK(instrumentation_cache_ != nullptr);
}
} // namespace jit
} // namespace art
|