blob: 7931306ff6eb5cdb8a495e4bd6427ab3510f0211 [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
Mathieu Chartier36c01362015-09-25 14:39:40 -070027class JitCompileTask FINAL : public Task {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080028 public:
Nicolas Geoffray26705e22015-10-28 12:50:11 +000029 enum TaskKind {
30 kAllocateProfile,
31 kCompile
32 };
33
34 JitCompileTask(ArtMethod* method, TaskKind kind) : method_(method), kind_(kind) {
Mathieu Chartiera50f9cf2015-09-25 11:34:45 -070035 ScopedObjectAccess soa(Thread::Current());
36 // Add a global ref to the class to prevent class unloading until compilation is done.
37 klass_ = soa.Vm()->AddGlobalRef(soa.Self(), method_->GetDeclaringClass());
38 CHECK(klass_ != nullptr);
39 }
40
41 ~JitCompileTask() {
42 ScopedObjectAccess soa(Thread::Current());
43 soa.Vm()->DeleteGlobalRef(soa.Self(), klass_);
44 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080045
Mathieu Chartier36c01362015-09-25 14:39:40 -070046 void Run(Thread* self) OVERRIDE {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080047 ScopedObjectAccess soa(self);
Nicolas Geoffray26705e22015-10-28 12:50:11 +000048 if (kind_ == kCompile) {
49 VLOG(jit) << "JitCompileTask compiling method " << PrettyMethod(method_);
50 if (!Runtime::Current()->GetJit()->CompileMethod(method_, self)) {
51 VLOG(jit) << "Failed to compile method " << PrettyMethod(method_);
52 }
53 } else {
54 DCHECK(kind_ == kAllocateProfile);
55 if (ProfilingInfo::Create(self, method_, /* retry_allocation */ true)) {
56 VLOG(jit) << "Start profiling " << PrettyMethod(method_);
57 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080058 }
59 }
60
Mathieu Chartier36c01362015-09-25 14:39:40 -070061 void Finalize() OVERRIDE {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080062 delete this;
63 }
64
65 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -070066 ArtMethod* const method_;
Nicolas Geoffray26705e22015-10-28 12:50:11 +000067 const TaskKind kind_;
Mathieu Chartiera50f9cf2015-09-25 11:34:45 -070068 jobject klass_;
Mathieu Chartier3130cdf2015-05-03 15:20:23 -070069
70 DISALLOW_IMPLICIT_CONSTRUCTORS(JitCompileTask);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080071};
72
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010073JitInstrumentationCache::JitInstrumentationCache(size_t hot_method_threshold,
74 size_t warm_method_threshold)
75 : hot_method_threshold_(hot_method_threshold),
76 warm_method_threshold_(warm_method_threshold) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080077}
78
79void JitInstrumentationCache::CreateThreadPool() {
80 thread_pool_.reset(new ThreadPool("Jit thread pool", 1));
81}
82
83void JitInstrumentationCache::DeleteThreadPool() {
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010084 DCHECK(Runtime::Current()->IsShuttingDown(Thread::Current()));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080085 thread_pool_.reset();
86}
87
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010088void JitInstrumentationCache::AddSamples(Thread* self, ArtMethod* method, size_t) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080089 // Since we don't have on-stack replacement, some methods can remain in the interpreter longer
90 // than we want resulting in samples even after the method is compiled.
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010091 if (method->IsClassInitializer() || method->IsNative()) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080092 return;
93 }
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010094 if (thread_pool_.get() == nullptr) {
95 DCHECK(Runtime::Current()->IsShuttingDown(self));
96 return;
97 }
98 uint16_t sample_count = method->IncrementCounter();
99 if (sample_count == warm_method_threshold_) {
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000100 if (ProfilingInfo::Create(self, method, /* retry_allocation */ false)) {
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100101 VLOG(jit) << "Start profiling " << PrettyMethod(method);
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000102 } else {
103 // We failed allocating. Instead of doing the collection on the Java thread, we push
104 // an allocation to a compiler thread, that will do the collection.
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000105 thread_pool_->AddTask(self, new JitCompileTask(method, JitCompileTask::kAllocateProfile));
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000106 thread_pool_->StartWorkers(self);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800107 }
108 }
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000109
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100110 if (sample_count == hot_method_threshold_) {
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000111 thread_pool_->AddTask(self, new JitCompileTask(method, JitCompileTask::kCompile));
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100112 thread_pool_->StartWorkers(self);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800113 }
114}
115
116JitInstrumentationListener::JitInstrumentationListener(JitInstrumentationCache* cache)
117 : instrumentation_cache_(cache) {
118 CHECK(instrumentation_cache_ != nullptr);
119}
120
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100121void JitInstrumentationListener::InvokeVirtualOrInterface(Thread* thread,
122 mirror::Object* this_object,
123 ArtMethod* caller,
124 uint32_t dex_pc,
125 ArtMethod* callee ATTRIBUTE_UNUSED) {
Nicolas Geoffray4e915fb2015-10-28 17:39:47 +0000126 instrumentation_cache_->AddSamples(thread, caller, 1);
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000127 // We make sure we cannot be suspended, as the profiling info can be concurrently deleted.
128 thread->StartAssertNoThreadSuspension("Instrumenting invoke");
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100129 DCHECK(this_object != nullptr);
Mathieu Chartier1147b9b2015-09-14 18:50:08 -0700130 ProfilingInfo* info = caller->GetProfilingInfo(sizeof(void*));
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100131 if (info != nullptr) {
Mathieu Chartier9ccf0512015-10-02 13:08:39 -0700132 // Since the instrumentation is marked from the declaring class we need to mark the card so
133 // that mod-union tables and card rescanning know about the update.
134 Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(caller->GetDeclaringClass());
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000135 info->AddInvokeInfo(dex_pc, this_object->GetClass());
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100136 }
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000137 thread->EndAssertNoThreadSuspension(nullptr);
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100138}
139
Mathieu Chartiera50f9cf2015-09-25 11:34:45 -0700140void JitInstrumentationCache::WaitForCompilationToFinish(Thread* self) {
Mathieu Chartierc3fcd412015-09-25 16:54:59 -0700141 thread_pool_->Wait(self, false, false);
Mathieu Chartiera50f9cf2015-09-25 11:34:45 -0700142}
143
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800144} // namespace jit
145} // namespace art