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" |
Vladimir Marko | fd66c50 | 2016-04-18 15:37:01 +0100 | [diff] [blame^] | 23 | #include "ScopedUtfChars.h" |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 24 | #include "stack_map.h" |
| 25 | |
| 26 | namespace art { |
| 27 | |
| 28 | class OsrVisitor : public StackVisitor { |
| 29 | public: |
Vladimir Marko | fd66c50 | 2016-04-18 15:37:01 +0100 | [diff] [blame^] | 30 | explicit OsrVisitor(Thread* thread, const char* method_name) |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 31 | SHARED_REQUIRES(Locks::mutator_lock_) |
| 32 | : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
Vladimir Marko | fd66c50 | 2016-04-18 15:37:01 +0100 | [diff] [blame^] | 33 | method_name_(method_name), |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 34 | in_osr_method_(false), |
| 35 | in_interpreter_(false) {} |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 36 | |
| 37 | bool VisitFrame() SHARED_REQUIRES(Locks::mutator_lock_) { |
| 38 | ArtMethod* m = GetMethod(); |
| 39 | std::string m_name(m->GetName()); |
| 40 | |
Vladimir Marko | fd66c50 | 2016-04-18 15:37:01 +0100 | [diff] [blame^] | 41 | if (m_name.compare(method_name_) == 0) { |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 42 | const OatQuickMethodHeader* header = |
| 43 | Runtime::Current()->GetJit()->GetCodeCache()->LookupOsrMethodHeader(m); |
| 44 | if (header != nullptr && header == GetCurrentOatQuickMethodHeader()) { |
| 45 | in_osr_method_ = true; |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 46 | } else if (IsCurrentFrameInInterpreter()) { |
| 47 | in_interpreter_ = true; |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 48 | } |
| 49 | return false; |
| 50 | } |
| 51 | return true; |
| 52 | } |
| 53 | |
Vladimir Marko | fd66c50 | 2016-04-18 15:37:01 +0100 | [diff] [blame^] | 54 | const char* const method_name_; |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 55 | bool in_osr_method_; |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 56 | bool in_interpreter_; |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 57 | }; |
| 58 | |
Vladimir Marko | fd66c50 | 2016-04-18 15:37:01 +0100 | [diff] [blame^] | 59 | extern "C" JNIEXPORT jboolean JNICALL Java_Main_isInOsrCode(JNIEnv* env, |
| 60 | jclass, |
| 61 | jstring method_name) { |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 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 | } |
Vladimir Marko | fd66c50 | 2016-04-18 15:37:01 +0100 | [diff] [blame^] | 67 | ScopedUtfChars chars(env, method_name); |
| 68 | CHECK(chars.c_str() != nullptr); |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 69 | ScopedObjectAccess soa(Thread::Current()); |
Vladimir Marko | fd66c50 | 2016-04-18 15:37:01 +0100 | [diff] [blame^] | 70 | OsrVisitor visitor(soa.Self(), chars.c_str()); |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 71 | visitor.WalkStack(); |
| 72 | return visitor.in_osr_method_; |
| 73 | } |
| 74 | |
Vladimir Marko | fd66c50 | 2016-04-18 15:37:01 +0100 | [diff] [blame^] | 75 | extern "C" JNIEXPORT jboolean JNICALL Java_Main_isInInterpreter(JNIEnv* env, |
| 76 | jclass, |
| 77 | jstring method_name) { |
Nicolas Geoffray | 28b7574 | 2016-02-16 11:30:48 +0000 | [diff] [blame] | 78 | if (!Runtime::Current()->UseJit()) { |
| 79 | // The return value is irrelevant if we're not using JIT. |
| 80 | return false; |
| 81 | } |
Vladimir Marko | fd66c50 | 2016-04-18 15:37:01 +0100 | [diff] [blame^] | 82 | ScopedUtfChars chars(env, method_name); |
| 83 | CHECK(chars.c_str() != nullptr); |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 84 | ScopedObjectAccess soa(Thread::Current()); |
Vladimir Marko | fd66c50 | 2016-04-18 15:37:01 +0100 | [diff] [blame^] | 85 | OsrVisitor visitor(soa.Self(), chars.c_str()); |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 86 | visitor.WalkStack(); |
| 87 | return visitor.in_interpreter_; |
| 88 | } |
| 89 | |
| 90 | class 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 Geoffray | b88d59e | 2016-02-17 11:31:49 +0000 | [diff] [blame] | 100 | if ((m_name.compare("$noinline$inlineCache") == 0) || |
| 101 | (m_name.compare("$noinline$stackOverflow") == 0)) { |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 102 | ProfilingInfo::Create(Thread::Current(), m, /* retry_allocation */ true); |
| 103 | return false; |
| 104 | } |
| 105 | return true; |
| 106 | } |
| 107 | }; |
| 108 | |
| 109 | extern "C" JNIEXPORT void JNICALL Java_Main_ensureHasProfilingInfo(JNIEnv*, jclass) { |
Nicolas Geoffray | 28b7574 | 2016-02-16 11:30:48 +0000 | [diff] [blame] | 110 | if (!Runtime::Current()->UseJit()) { |
| 111 | return; |
| 112 | } |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 113 | ScopedObjectAccess soa(Thread::Current()); |
| 114 | ProfilingInfoVisitor visitor(soa.Self()); |
| 115 | visitor.WalkStack(); |
| 116 | } |
| 117 | |
| 118 | class OsrCheckVisitor : public StackVisitor { |
| 119 | public: |
Vladimir Marko | fd66c50 | 2016-04-18 15:37:01 +0100 | [diff] [blame^] | 120 | OsrCheckVisitor(Thread* thread, const char* const method_name) |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 121 | SHARED_REQUIRES(Locks::mutator_lock_) |
Vladimir Marko | fd66c50 | 2016-04-18 15:37:01 +0100 | [diff] [blame^] | 122 | : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
| 123 | method_name_(method_name) {} |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 124 | |
| 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 Marko | fd66c50 | 2016-04-18 15:37:01 +0100 | [diff] [blame^] | 130 | if (m_name.compare(method_name_) == 0) { |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 131 | 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 Marko | fd66c50 | 2016-04-18 15:37:01 +0100 | [diff] [blame^] | 141 | |
| 142 | const char* const method_name_; |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 143 | }; |
| 144 | |
Vladimir Marko | fd66c50 | 2016-04-18 15:37:01 +0100 | [diff] [blame^] | 145 | extern "C" JNIEXPORT void JNICALL Java_Main_ensureHasOsrCode(JNIEnv* env, |
| 146 | jclass, |
| 147 | jstring method_name) { |
Nicolas Geoffray | 28b7574 | 2016-02-16 11:30:48 +0000 | [diff] [blame] | 148 | if (!Runtime::Current()->UseJit()) { |
| 149 | return; |
| 150 | } |
Vladimir Marko | fd66c50 | 2016-04-18 15:37:01 +0100 | [diff] [blame^] | 151 | ScopedUtfChars chars(env, method_name); |
| 152 | CHECK(chars.c_str() != nullptr); |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 153 | ScopedObjectAccess soa(Thread::Current()); |
Vladimir Marko | fd66c50 | 2016-04-18 15:37:01 +0100 | [diff] [blame^] | 154 | OsrCheckVisitor visitor(soa.Self(), chars.c_str()); |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 155 | visitor.WalkStack(); |
| 156 | } |
| 157 | |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 158 | } // namespace art |