Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 1 | /* |
| 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 Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 20 | #include "jit/profiling_info.h" |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 21 | #include "oat_quick_method_header.h" |
| 22 | #include "scoped_thread_state_change.h" |
| 23 | #include "stack_map.h" |
| 24 | |
| 25 | namespace art { |
| 26 | |
| 27 | class OsrVisitor : public StackVisitor { |
| 28 | public: |
| 29 | explicit OsrVisitor(Thread* thread) |
| 30 | SHARED_REQUIRES(Locks::mutator_lock_) |
| 31 | : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 32 | in_osr_method_(false), |
| 33 | in_interpreter_(false) {} |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 34 | |
| 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 Geoffray | d9994f0 | 2016-02-11 17:35:55 +0000 | [diff] [blame] | 43 | (m_name.compare("$noinline$deopt") == 0) || |
| 44 | (m_name.compare("$noinline$inlineCache") == 0)) { |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 45 | const OatQuickMethodHeader* header = |
| 46 | Runtime::Current()->GetJit()->GetCodeCache()->LookupOsrMethodHeader(m); |
| 47 | if (header != nullptr && header == GetCurrentOatQuickMethodHeader()) { |
| 48 | in_osr_method_ = true; |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 49 | } else if (IsCurrentFrameInInterpreter()) { |
| 50 | in_interpreter_ = true; |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 51 | } |
| 52 | return false; |
| 53 | } |
| 54 | return true; |
| 55 | } |
| 56 | |
| 57 | bool in_osr_method_; |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 58 | bool in_interpreter_; |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | extern "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 Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 73 | extern "C" JNIEXPORT jboolean JNICALL Java_Main_ensureInInterpreter(JNIEnv*, jclass) { |
Nicolas Geoffray | 28b7574 | 2016-02-16 11:30:48 +0000 | [diff] [blame^] | 74 | if (!Runtime::Current()->UseJit()) { |
| 75 | // The return value is irrelevant if we're not using JIT. |
| 76 | return false; |
| 77 | } |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 78 | ScopedObjectAccess soa(Thread::Current()); |
| 79 | OsrVisitor visitor(soa.Self()); |
| 80 | visitor.WalkStack(); |
| 81 | return visitor.in_interpreter_; |
| 82 | } |
| 83 | |
| 84 | class 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 | |
| 102 | extern "C" JNIEXPORT void JNICALL Java_Main_ensureHasProfilingInfo(JNIEnv*, jclass) { |
Nicolas Geoffray | 28b7574 | 2016-02-16 11:30:48 +0000 | [diff] [blame^] | 103 | if (!Runtime::Current()->UseJit()) { |
| 104 | return; |
| 105 | } |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 106 | ScopedObjectAccess soa(Thread::Current()); |
| 107 | ProfilingInfoVisitor visitor(soa.Self()); |
| 108 | visitor.WalkStack(); |
| 109 | } |
| 110 | |
| 111 | class 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 | |
| 135 | extern "C" JNIEXPORT void JNICALL Java_Main_ensureHasOsrCode(JNIEnv*, jclass) { |
Nicolas Geoffray | 28b7574 | 2016-02-16 11:30:48 +0000 | [diff] [blame^] | 136 | if (!Runtime::Current()->UseJit()) { |
| 137 | return; |
| 138 | } |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 139 | ScopedObjectAccess soa(Thread::Current()); |
| 140 | OsrCheckVisitor visitor(soa.Self()); |
| 141 | visitor.WalkStack(); |
| 142 | } |
| 143 | |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 144 | } // namespace art |