blob: ac2e0ce77de50224c25a4714eebc63db98939087 [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
17#include "art_method.h"
18#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)
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +000031 SHARED_REQUIRES(Locks::mutator_lock_)
32 : 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
37 bool VisitFrame() SHARED_REQUIRES(Locks::mutator_lock_) {
38 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) {
Nicolas Geoffray28b75742016-02-16 11:30:48 +000078 if (!Runtime::Current()->UseJit()) {
79 // 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:
92 explicit ProfilingInfoVisitor(Thread* thread)
93 SHARED_REQUIRES(Locks::mutator_lock_)
94 : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {}
95
96 bool VisitFrame() SHARED_REQUIRES(Locks::mutator_lock_) {
97 ArtMethod* m = GetMethod();
98 std::string m_name(m->GetName());
99
Nicolas Geoffrayb88d59e2016-02-17 11:31:49 +0000100 if ((m_name.compare("$noinline$inlineCache") == 0) ||
101 (m_name.compare("$noinline$stackOverflow") == 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 }
107};
108
109extern "C" JNIEXPORT void JNICALL Java_Main_ensureHasProfilingInfo(JNIEnv*, jclass) {
Nicolas Geoffray28b75742016-02-16 11:30:48 +0000110 if (!Runtime::Current()->UseJit()) {
111 return;
112 }
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000113 ScopedObjectAccess soa(Thread::Current());
114 ProfilingInfoVisitor visitor(soa.Self());
115 visitor.WalkStack();
116}
117
118class OsrCheckVisitor : public StackVisitor {
119 public:
Vladimir Markofd66c502016-04-18 15:37:01 +0100120 OsrCheckVisitor(Thread* thread, const char* const method_name)
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000121 SHARED_REQUIRES(Locks::mutator_lock_)
Vladimir Markofd66c502016-04-18 15:37:01 +0100122 : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
123 method_name_(method_name) {}
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000124
125 bool VisitFrame() SHARED_REQUIRES(Locks::mutator_lock_) {
126 ArtMethod* m = GetMethod();
127 std::string m_name(m->GetName());
128
129 jit::Jit* jit = Runtime::Current()->GetJit();
Vladimir Markofd66c502016-04-18 15:37:01 +0100130 if (m_name.compare(method_name_) == 0) {
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000131 while (jit->GetCodeCache()->LookupOsrMethodHeader(m) == nullptr) {
132 // Sleep to yield to the compiler thread.
133 sleep(0);
134 // Will either ensure it's compiled or do the compilation itself.
135 jit->CompileMethod(m, Thread::Current(), /* osr */ true);
136 }
137 return false;
138 }
139 return true;
140 }
Vladimir Markofd66c502016-04-18 15:37:01 +0100141
142 const char* const method_name_;
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000143};
144
Vladimir Markofd66c502016-04-18 15:37:01 +0100145extern "C" JNIEXPORT void JNICALL Java_Main_ensureHasOsrCode(JNIEnv* env,
146 jclass,
147 jstring method_name) {
Nicolas Geoffray28b75742016-02-16 11:30:48 +0000148 if (!Runtime::Current()->UseJit()) {
149 return;
150 }
Vladimir Markofd66c502016-04-18 15:37:01 +0100151 ScopedUtfChars chars(env, method_name);
152 CHECK(chars.c_str() != nullptr);
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000153 ScopedObjectAccess soa(Thread::Current());
Vladimir Markofd66c502016-04-18 15:37:01 +0100154 OsrCheckVisitor visitor(soa.Self(), chars.c_str());
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000155 visitor.WalkStack();
156}
157
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000158} // namespace art