blob: af6aba3df593a6756af01ca0f6d5a60424973781 [file] [log] [blame]
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001/*
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 Chartiere401d142015-04-22 13:56:20 -070019#include "art_method-inl.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080020#include "jit.h"
21#include "jit_code_cache.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080022#include "scoped_thread_state_change.h"
23
24namespace art {
25namespace jit {
26
27class JitCompileTask : public Task {
28 public:
Mathieu Chartiera50f9cf2015-09-25 11:34:45 -070029 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 Chartiere5f13e52015-02-24 09:37:21 -080040
41 virtual void Run(Thread* self) OVERRIDE {
42 ScopedObjectAccess soa(self);
43 VLOG(jit) << "JitCompileTask compiling method " << PrettyMethod(method_);
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010044 if (!Runtime::Current()->GetJit()->CompileMethod(method_, self)) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080045 VLOG(jit) << "Failed to compile method " << PrettyMethod(method_);
46 }
47 }
48
49 virtual void Finalize() OVERRIDE {
50 delete this;
51 }
52
53 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -070054 ArtMethod* const method_;
Mathieu Chartiera50f9cf2015-09-25 11:34:45 -070055 jobject klass_;
Mathieu Chartier3130cdf2015-05-03 15:20:23 -070056
57 DISALLOW_IMPLICIT_CONSTRUCTORS(JitCompileTask);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080058};
59
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010060JitInstrumentationCache::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 Chartiere5f13e52015-02-24 09:37:21 -080064}
65
66void JitInstrumentationCache::CreateThreadPool() {
67 thread_pool_.reset(new ThreadPool("Jit thread pool", 1));
68}
69
70void JitInstrumentationCache::DeleteThreadPool() {
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010071 DCHECK(Runtime::Current()->IsShuttingDown(Thread::Current()));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080072 thread_pool_.reset();
73}
74
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010075void JitInstrumentationCache::AddSamples(Thread* self, ArtMethod* method, size_t) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080076 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 Chartier70f7d982015-05-08 17:05:01 -070079 if (method->IsClassInitializer() || method->IsNative() ||
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080080 Runtime::Current()->GetJit()->GetCodeCache()->ContainsMethod(method)) {
81 return;
82 }
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010083 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 Chartiere5f13e52015-02-24 09:37:21 -080092 }
93 }
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010094 if (sample_count == hot_method_threshold_) {
95 thread_pool_->AddTask(self, new JitCompileTask(
96 method->GetInterfaceMethodIfProxy(sizeof(void*))));
97 thread_pool_->StartWorkers(self);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080098 }
99}
100
101JitInstrumentationListener::JitInstrumentationListener(JitInstrumentationCache* cache)
102 : instrumentation_cache_(cache) {
103 CHECK(instrumentation_cache_ != nullptr);
104}
105
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100106void 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 Chartier1147b9b2015-09-14 18:50:08 -0700112 ProfilingInfo* info = caller->GetProfilingInfo(sizeof(void*));
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100113 if (info != nullptr) {
114 info->AddInvokeInfo(thread, dex_pc, this_object->GetClass());
115 }
116}
117
Mathieu Chartiera50f9cf2015-09-25 11:34:45 -0700118class WaitForCompilationToFinishTask : public Task {
119 public:
120 WaitForCompilationToFinishTask() : barrier_(0) {}
121
122 void Wait(Thread* self) {
123 barrier_.Increment(self, 1);
124 }
125
126 virtual void Run(Thread* self) OVERRIDE {
127 barrier_.Pass(self);
128 }
129
130 private:
131 Barrier barrier_;
132 DISALLOW_COPY_AND_ASSIGN(WaitForCompilationToFinishTask);
133};
134
135void JitInstrumentationCache::WaitForCompilationToFinish(Thread* self) {
136 std::unique_ptr<WaitForCompilationToFinishTask> task(new WaitForCompilationToFinishTask);
137 thread_pool_->AddTask(self, task.get());
138 task->Wait(self);
139}
140
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800141} // namespace jit
142} // namespace art