blob: adda3ccb5bbf2b05fffb48a3bdc362c902bf439d [file] [log] [blame]
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00001/*
2 * Copyright (C) 2016 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
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -070017#include "art_method-inl.h"
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +000018#include "jit/jit.h"
19#include "jit/jit_code_cache.h"
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +000020#include "jit/profiling_info.h"
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +000021#include "oat_quick_method_header.h"
22#include "scoped_thread_state_change.h"
Vladimir Markofd66c502016-04-18 15:37:01 +010023#include "ScopedUtfChars.h"
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +000024#include "stack_map.h"
25
26namespace art {
27
28class OsrVisitor : public StackVisitor {
29 public:
Vladimir Markofd66c502016-04-18 15:37:01 +010030 explicit OsrVisitor(Thread* thread, const char* method_name)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070031 REQUIRES_SHARED(Locks::mutator_lock_)
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +000032 : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
Vladimir Markofd66c502016-04-18 15:37:01 +010033 method_name_(method_name),
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +000034 in_osr_method_(false),
35 in_interpreter_(false) {}
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +000036
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070037 bool VisitFrame() REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +000038 ArtMethod* m = GetMethod();
39 std::string m_name(m->GetName());
40
Vladimir Markofd66c502016-04-18 15:37:01 +010041 if (m_name.compare(method_name_) == 0) {
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +000042 const OatQuickMethodHeader* header =
43 Runtime::Current()->GetJit()->GetCodeCache()->LookupOsrMethodHeader(m);
44 if (header != nullptr && header == GetCurrentOatQuickMethodHeader()) {
45 in_osr_method_ = true;
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +000046 } else if (IsCurrentFrameInInterpreter()) {
47 in_interpreter_ = true;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +000048 }
49 return false;
50 }
51 return true;
52 }
53
Vladimir Markofd66c502016-04-18 15:37:01 +010054 const char* const method_name_;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +000055 bool in_osr_method_;
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +000056 bool in_interpreter_;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +000057};
58
Vladimir Markofd66c502016-04-18 15:37:01 +010059extern "C" JNIEXPORT jboolean JNICALL Java_Main_isInOsrCode(JNIEnv* env,
60 jclass,
61 jstring method_name) {
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +000062 jit::Jit* jit = Runtime::Current()->GetJit();
63 if (jit == nullptr) {
64 // Just return true for non-jit configurations to stop the infinite loop.
65 return JNI_TRUE;
66 }
Vladimir Markofd66c502016-04-18 15:37:01 +010067 ScopedUtfChars chars(env, method_name);
68 CHECK(chars.c_str() != nullptr);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +000069 ScopedObjectAccess soa(Thread::Current());
Vladimir Markofd66c502016-04-18 15:37:01 +010070 OsrVisitor visitor(soa.Self(), chars.c_str());
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +000071 visitor.WalkStack();
72 return visitor.in_osr_method_;
73}
74
Vladimir Markofd66c502016-04-18 15:37:01 +010075extern "C" JNIEXPORT jboolean JNICALL Java_Main_isInInterpreter(JNIEnv* env,
76 jclass,
77 jstring method_name) {
Calin Juravleffc87072016-04-20 14:22:09 +010078 if (!Runtime::Current()->UseJitCompilation()) {
Nicolas Geoffray28b75742016-02-16 11:30:48 +000079 // The return value is irrelevant if we're not using JIT.
80 return false;
81 }
Vladimir Markofd66c502016-04-18 15:37:01 +010082 ScopedUtfChars chars(env, method_name);
83 CHECK(chars.c_str() != nullptr);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +000084 ScopedObjectAccess soa(Thread::Current());
Vladimir Markofd66c502016-04-18 15:37:01 +010085 OsrVisitor visitor(soa.Self(), chars.c_str());
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +000086 visitor.WalkStack();
87 return visitor.in_interpreter_;
88}
89
90class ProfilingInfoVisitor : public StackVisitor {
91 public:
Vladimir Markob9d338b2016-04-19 17:26:54 +010092 explicit ProfilingInfoVisitor(Thread* thread, const char* method_name)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070093 REQUIRES_SHARED(Locks::mutator_lock_)
Vladimir Markob9d338b2016-04-19 17:26:54 +010094 : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
95 method_name_(method_name) {}
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +000096
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070097 bool VisitFrame() REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +000098 ArtMethod* m = GetMethod();
99 std::string m_name(m->GetName());
100
Vladimir Markob9d338b2016-04-19 17:26:54 +0100101 if (m_name.compare(method_name_) == 0) {
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000102 ProfilingInfo::Create(Thread::Current(), m, /* retry_allocation */ true);
103 return false;
104 }
105 return true;
106 }
Vladimir Markob9d338b2016-04-19 17:26:54 +0100107
108 const char* const method_name_;
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000109};
110
Vladimir Markob9d338b2016-04-19 17:26:54 +0100111extern "C" JNIEXPORT void JNICALL Java_Main_ensureHasProfilingInfo(JNIEnv* env,
112 jclass,
113 jstring method_name) {
Calin Juravleffc87072016-04-20 14:22:09 +0100114 if (!Runtime::Current()->UseJitCompilation()) {
Nicolas Geoffray28b75742016-02-16 11:30:48 +0000115 return;
116 }
Vladimir Markob9d338b2016-04-19 17:26:54 +0100117 ScopedUtfChars chars(env, method_name);
118 CHECK(chars.c_str() != nullptr);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000119 ScopedObjectAccess soa(Thread::Current());
Vladimir Markob9d338b2016-04-19 17:26:54 +0100120 ProfilingInfoVisitor visitor(soa.Self(), chars.c_str());
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000121 visitor.WalkStack();
122}
123
124class OsrCheckVisitor : public StackVisitor {
125 public:
Vladimir Markob9d338b2016-04-19 17:26:54 +0100126 OsrCheckVisitor(Thread* thread, const char* method_name)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700127 REQUIRES_SHARED(Locks::mutator_lock_)
Vladimir Markofd66c502016-04-18 15:37:01 +0100128 : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
129 method_name_(method_name) {}
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000130
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700131 bool VisitFrame() REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000132 ArtMethod* m = GetMethod();
133 std::string m_name(m->GetName());
134
135 jit::Jit* jit = Runtime::Current()->GetJit();
Vladimir Markofd66c502016-04-18 15:37:01 +0100136 if (m_name.compare(method_name_) == 0) {
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000137 while (jit->GetCodeCache()->LookupOsrMethodHeader(m) == nullptr) {
138 // Sleep to yield to the compiler thread.
Nicolas Geoffray223ac0c2016-06-09 09:53:55 +0100139 usleep(1000);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000140 // Will either ensure it's compiled or do the compilation itself.
141 jit->CompileMethod(m, Thread::Current(), /* osr */ true);
142 }
143 return false;
144 }
145 return true;
146 }
Vladimir Markofd66c502016-04-18 15:37:01 +0100147
148 const char* const method_name_;
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000149};
150
Vladimir Markofd66c502016-04-18 15:37:01 +0100151extern "C" JNIEXPORT void JNICALL Java_Main_ensureHasOsrCode(JNIEnv* env,
152 jclass,
153 jstring method_name) {
Calin Juravleffc87072016-04-20 14:22:09 +0100154 if (!Runtime::Current()->UseJitCompilation()) {
Nicolas Geoffray28b75742016-02-16 11:30:48 +0000155 return;
156 }
Vladimir Markofd66c502016-04-18 15:37:01 +0100157 ScopedUtfChars chars(env, method_name);
158 CHECK(chars.c_str() != nullptr);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000159 ScopedObjectAccess soa(Thread::Current());
Vladimir Markofd66c502016-04-18 15:37:01 +0100160 OsrCheckVisitor visitor(soa.Self(), chars.c_str());
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000161 visitor.WalkStack();
162}
163
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000164} // namespace art