blob: 4c58b39319b26cea92021cd10c48d4504c0ab6c3 [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) ||
44 (m_name.compare("$noinline$inlineCache") == 0)) {
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +000045 const OatQuickMethodHeader* header =
46 Runtime::Current()->GetJit()->GetCodeCache()->LookupOsrMethodHeader(m);
47 if (header != nullptr && header == GetCurrentOatQuickMethodHeader()) {
48 in_osr_method_ = true;
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +000049 } else if (IsCurrentFrameInInterpreter()) {
50 in_interpreter_ = true;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +000051 }
52 return false;
53 }
54 return true;
55 }
56
57 bool in_osr_method_;
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +000058 bool in_interpreter_;
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +000059};
60
61extern "C" JNIEXPORT jboolean JNICALL Java_Main_ensureInOsrCode(JNIEnv*, jclass) {
62 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 }
67 ScopedObjectAccess soa(Thread::Current());
68 OsrVisitor visitor(soa.Self());
69 visitor.WalkStack();
70 return visitor.in_osr_method_;
71}
72
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +000073extern "C" JNIEXPORT jboolean JNICALL Java_Main_ensureInInterpreter(JNIEnv*, jclass) {
Nicolas Geoffray28b75742016-02-16 11:30:48 +000074 if (!Runtime::Current()->UseJit()) {
75 // The return value is irrelevant if we're not using JIT.
76 return false;
77 }
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +000078 ScopedObjectAccess soa(Thread::Current());
79 OsrVisitor visitor(soa.Self());
80 visitor.WalkStack();
81 return visitor.in_interpreter_;
82}
83
84class ProfilingInfoVisitor : public StackVisitor {
85 public:
86 explicit ProfilingInfoVisitor(Thread* thread)
87 SHARED_REQUIRES(Locks::mutator_lock_)
88 : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {}
89
90 bool VisitFrame() SHARED_REQUIRES(Locks::mutator_lock_) {
91 ArtMethod* m = GetMethod();
92 std::string m_name(m->GetName());
93
94 if (m_name.compare("$noinline$inlineCache") == 0) {
95 ProfilingInfo::Create(Thread::Current(), m, /* retry_allocation */ true);
96 return false;
97 }
98 return true;
99 }
100};
101
102extern "C" JNIEXPORT void JNICALL Java_Main_ensureHasProfilingInfo(JNIEnv*, jclass) {
Nicolas Geoffray28b75742016-02-16 11:30:48 +0000103 if (!Runtime::Current()->UseJit()) {
104 return;
105 }
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000106 ScopedObjectAccess soa(Thread::Current());
107 ProfilingInfoVisitor visitor(soa.Self());
108 visitor.WalkStack();
109}
110
111class OsrCheckVisitor : public StackVisitor {
112 public:
113 explicit OsrCheckVisitor(Thread* thread)
114 SHARED_REQUIRES(Locks::mutator_lock_)
115 : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {}
116
117 bool VisitFrame() SHARED_REQUIRES(Locks::mutator_lock_) {
118 ArtMethod* m = GetMethod();
119 std::string m_name(m->GetName());
120
121 jit::Jit* jit = Runtime::Current()->GetJit();
122 if (m_name.compare("$noinline$inlineCache") == 0 && jit != nullptr) {
123 while (jit->GetCodeCache()->LookupOsrMethodHeader(m) == nullptr) {
124 // Sleep to yield to the compiler thread.
125 sleep(0);
126 // Will either ensure it's compiled or do the compilation itself.
127 jit->CompileMethod(m, Thread::Current(), /* osr */ true);
128 }
129 return false;
130 }
131 return true;
132 }
133};
134
135extern "C" JNIEXPORT void JNICALL Java_Main_ensureHasOsrCode(JNIEnv*, jclass) {
Nicolas Geoffray28b75742016-02-16 11:30:48 +0000136 if (!Runtime::Current()->UseJit()) {
137 return;
138 }
Nicolas Geoffrayd186dd82016-02-16 10:03:44 +0000139 ScopedObjectAccess soa(Thread::Current());
140 OsrCheckVisitor visitor(soa.Self());
141 visitor.WalkStack();
142}
143
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000144} // namespace art