blob: 09e97ea7515d42c1f192453bbd0656d511075b14 [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"
23#include "stack_map.h"
24
25namespace art {
26
27class OsrVisitor : public StackVisitor {
28 public:
29 explicit OsrVisitor(Thread* thread)
30 SHARED_REQUIRES(Locks::mutator_lock_)
31 : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +000032 in_osr_method_(false),
33 in_interpreter_(false) {}
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +000034
35 bool VisitFrame() SHARED_REQUIRES(Locks::mutator_lock_) {
36 ArtMethod* m = GetMethod();
37 std::string m_name(m->GetName());
38
39 if ((m_name.compare("$noinline$returnInt") == 0) ||
40 (m_name.compare("$noinline$returnFloat") == 0) ||
41 (m_name.compare("$noinline$returnDouble") == 0) ||
42 (m_name.compare("$noinline$returnLong") == 0) ||
Nicolas Geoffrayd9994f02016-02-11 17:35:55 +000043 (m_name.compare("$noinline$deopt") == 0) ||
Nicolas Geoffrayb88d59e2016-02-17 11:31:49 +000044 (m_name.compare("$noinline$inlineCache") == 0) ||
45 (m_name.compare("$noinline$stackOverflow") == 0)) {
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +000046 const OatQuickMethodHeader* header =
47 Runtime::Current()->GetJit()->GetCodeCache()->LookupOsrMethodHeader(m);
48 if (header != nullptr && header == GetCurrentOatQuickMethodHeader()) {
49 in_osr_method_ = true;
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +000050 } else if (IsCurrentFrameInInterpreter()) {
51 in_interpreter_ = true;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +000052 }
53 return false;
54 }
55 return true;
56 }
57
58 bool in_osr_method_;
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +000059 bool in_interpreter_;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +000060};
61
62extern "C" JNIEXPORT jboolean JNICALL Java_Main_ensureInOsrCode(JNIEnv*, jclass) {
63 jit::Jit* jit = Runtime::Current()->GetJit();
64 if (jit == nullptr) {
65 // Just return true for non-jit configurations to stop the infinite loop.
66 return JNI_TRUE;
67 }
68 ScopedObjectAccess soa(Thread::Current());
69 OsrVisitor visitor(soa.Self());
70 visitor.WalkStack();
71 return visitor.in_osr_method_;
72}
73
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +000074extern "C" JNIEXPORT jboolean JNICALL Java_Main_ensureInInterpreter(JNIEnv*, jclass) {
Nicolas Geoffray28b75742016-02-16 11:30:48 +000075 if (!Runtime::Current()->UseJit()) {
76 // The return value is irrelevant if we're not using JIT.
77 return false;
78 }
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +000079 ScopedObjectAccess soa(Thread::Current());
80 OsrVisitor visitor(soa.Self());
81 visitor.WalkStack();
82 return visitor.in_interpreter_;
83}
84
85class ProfilingInfoVisitor : public StackVisitor {
86 public:
87 explicit ProfilingInfoVisitor(Thread* thread)
88 SHARED_REQUIRES(Locks::mutator_lock_)
89 : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {}
90
91 bool VisitFrame() SHARED_REQUIRES(Locks::mutator_lock_) {
92 ArtMethod* m = GetMethod();
93 std::string m_name(m->GetName());
94
Nicolas Geoffrayb88d59e2016-02-17 11:31:49 +000095 if ((m_name.compare("$noinline$inlineCache") == 0) ||
96 (m_name.compare("$noinline$stackOverflow") == 0)) {
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +000097 ProfilingInfo::Create(Thread::Current(), m, /* retry_allocation */ true);
98 return false;
99 }
100 return true;
101 }
102};
103
104extern "C" JNIEXPORT void JNICALL Java_Main_ensureHasProfilingInfo(JNIEnv*, jclass) {
Nicolas Geoffray28b75742016-02-16 11:30:48 +0000105 if (!Runtime::Current()->UseJit()) {
106 return;
107 }
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000108 ScopedObjectAccess soa(Thread::Current());
109 ProfilingInfoVisitor visitor(soa.Self());
110 visitor.WalkStack();
111}
112
113class OsrCheckVisitor : public StackVisitor {
114 public:
115 explicit OsrCheckVisitor(Thread* thread)
116 SHARED_REQUIRES(Locks::mutator_lock_)
117 : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {}
118
119 bool VisitFrame() SHARED_REQUIRES(Locks::mutator_lock_) {
120 ArtMethod* m = GetMethod();
121 std::string m_name(m->GetName());
122
123 jit::Jit* jit = Runtime::Current()->GetJit();
Nicolas Geoffrayb88d59e2016-02-17 11:31:49 +0000124 if ((m_name.compare("$noinline$inlineCache") == 0) ||
125 (m_name.compare("$noinline$stackOverflow") == 0)) {
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000126 while (jit->GetCodeCache()->LookupOsrMethodHeader(m) == nullptr) {
127 // Sleep to yield to the compiler thread.
128 sleep(0);
129 // Will either ensure it's compiled or do the compilation itself.
130 jit->CompileMethod(m, Thread::Current(), /* osr */ true);
131 }
132 return false;
133 }
134 return true;
135 }
136};
137
138extern "C" JNIEXPORT void JNICALL Java_Main_ensureHasOsrCode(JNIEnv*, jclass) {
Nicolas Geoffray28b75742016-02-16 11:30:48 +0000139 if (!Runtime::Current()->UseJit()) {
140 return;
141 }
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000142 ScopedObjectAccess soa(Thread::Current());
143 OsrCheckVisitor visitor(soa.Self());
144 visitor.WalkStack();
145}
146
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000147} // namespace art