blob: 2dd953bb94dd2c44e1e38e7f40956cad0a574aed [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 ScopedObjectAccessUnchecked soa(self);
90 // Since we don't have on-stack replacement, some methods can remain in the interpreter longer
91 // than we want resulting in samples even after the method is compiled.
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010092 if (method->IsClassInitializer() || method->IsNative()) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080093 return;
94 }
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010095 if (thread_pool_.get() == nullptr) {
96 DCHECK(Runtime::Current()->IsShuttingDown(self));
97 return;
98 }
99 uint16_t sample_count = method->IncrementCounter();
100 if (sample_count == warm_method_threshold_) {
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000101 if (ProfilingInfo::Create(self, method, /* retry_allocation */ false)) {
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100102 VLOG(jit) << "Start profiling " << PrettyMethod(method);
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000103 } else {
104 // We failed allocating. Instead of doing the collection on the Java thread, we push
105 // an allocation to a compiler thread, that will do the collection.
106 thread_pool_->AddTask(self, new JitCompileTask(
107 method->GetInterfaceMethodIfProxy(sizeof(void*)), JitCompileTask::kAllocateProfile));
108 thread_pool_->StartWorkers(self);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800109 }
110 }
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000111
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100112 if (sample_count == hot_method_threshold_) {
113 thread_pool_->AddTask(self, new JitCompileTask(
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000114 method->GetInterfaceMethodIfProxy(sizeof(void*)), JitCompileTask::kCompile));
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100115 thread_pool_->StartWorkers(self);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800116 }
117}
118
119JitInstrumentationListener::JitInstrumentationListener(JitInstrumentationCache* cache)
120 : instrumentation_cache_(cache) {
121 CHECK(instrumentation_cache_ != nullptr);
122}
123
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100124void JitInstrumentationListener::InvokeVirtualOrInterface(Thread* thread,
125 mirror::Object* this_object,
126 ArtMethod* caller,
127 uint32_t dex_pc,
128 ArtMethod* callee ATTRIBUTE_UNUSED) {
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000129 // We make sure we cannot be suspended, as the profiling info can be concurrently deleted.
130 thread->StartAssertNoThreadSuspension("Instrumenting invoke");
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100131 DCHECK(this_object != nullptr);
Mathieu Chartier1147b9b2015-09-14 18:50:08 -0700132 ProfilingInfo* info = caller->GetProfilingInfo(sizeof(void*));
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100133 if (info != nullptr) {
Mathieu Chartier9ccf0512015-10-02 13:08:39 -0700134 // Since the instrumentation is marked from the declaring class we need to mark the card so
135 // that mod-union tables and card rescanning know about the update.
136 Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(caller->GetDeclaringClass());
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000137 info->AddInvokeInfo(dex_pc, this_object->GetClass());
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100138 }
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000139 thread->EndAssertNoThreadSuspension(nullptr);
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100140}
141
Mathieu Chartiera50f9cf2015-09-25 11:34:45 -0700142void JitInstrumentationCache::WaitForCompilationToFinish(Thread* self) {
Mathieu Chartierc3fcd412015-09-25 16:54:59 -0700143 thread_pool_->Wait(self, false, false);
Mathieu Chartiera50f9cf2015-09-25 11:34:45 -0700144}
145
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800146} // namespace jit
147} // namespace art