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 | |
Mathieu Chartier | 48b2b3e | 2016-05-05 15:31:12 -0700 | [diff] [blame] | 17 | #include "art_method-inl.h" |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 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) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 31 | REQUIRES_SHARED(Locks::mutator_lock_) |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 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 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 37 | bool VisitFrame() REQUIRES_SHARED(Locks::mutator_lock_) { |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 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) { |
Calin Juravle | ffc8707 | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 78 | if (!Runtime::Current()->UseJitCompilation()) { |
Nicolas Geoffray | 28b7574 | 2016-02-16 11:30:48 +0000 | [diff] [blame] | 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: |
Vladimir Marko | b9d338b | 2016-04-19 17:26:54 +0100 | [diff] [blame] | 92 | explicit ProfilingInfoVisitor(Thread* thread, const char* method_name) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 93 | REQUIRES_SHARED(Locks::mutator_lock_) |
Vladimir Marko | b9d338b | 2016-04-19 17:26:54 +0100 | [diff] [blame] | 94 | : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
| 95 | method_name_(method_name) {} |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 96 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 97 | bool VisitFrame() REQUIRES_SHARED(Locks::mutator_lock_) { |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 98 | ArtMethod* m = GetMethod(); |
| 99 | std::string m_name(m->GetName()); |
| 100 | |
Vladimir Marko | b9d338b | 2016-04-19 17:26:54 +0100 | [diff] [blame] | 101 | if (m_name.compare(method_name_) == 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 | } |
Vladimir Marko | b9d338b | 2016-04-19 17:26:54 +0100 | [diff] [blame] | 107 | |
| 108 | const char* const method_name_; |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 109 | }; |
| 110 | |
Vladimir Marko | b9d338b | 2016-04-19 17:26:54 +0100 | [diff] [blame] | 111 | extern "C" JNIEXPORT void JNICALL Java_Main_ensureHasProfilingInfo(JNIEnv* env, |
| 112 | jclass, |
| 113 | jstring method_name) { |
Calin Juravle | ffc8707 | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 114 | if (!Runtime::Current()->UseJitCompilation()) { |
Nicolas Geoffray | 28b7574 | 2016-02-16 11:30:48 +0000 | [diff] [blame] | 115 | return; |
| 116 | } |
Vladimir Marko | b9d338b | 2016-04-19 17:26:54 +0100 | [diff] [blame] | 117 | ScopedUtfChars chars(env, method_name); |
| 118 | CHECK(chars.c_str() != nullptr); |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 119 | ScopedObjectAccess soa(Thread::Current()); |
Vladimir Marko | b9d338b | 2016-04-19 17:26:54 +0100 | [diff] [blame] | 120 | ProfilingInfoVisitor visitor(soa.Self(), chars.c_str()); |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 121 | visitor.WalkStack(); |
| 122 | } |
| 123 | |
| 124 | class OsrCheckVisitor : public StackVisitor { |
| 125 | public: |
Vladimir Marko | b9d338b | 2016-04-19 17:26:54 +0100 | [diff] [blame] | 126 | OsrCheckVisitor(Thread* thread, const char* method_name) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 127 | REQUIRES_SHARED(Locks::mutator_lock_) |
Vladimir Marko | fd66c50 | 2016-04-18 15:37:01 +0100 | [diff] [blame] | 128 | : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
| 129 | method_name_(method_name) {} |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 130 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 131 | bool VisitFrame() REQUIRES_SHARED(Locks::mutator_lock_) { |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 132 | ArtMethod* m = GetMethod(); |
| 133 | std::string m_name(m->GetName()); |
| 134 | |
| 135 | jit::Jit* jit = Runtime::Current()->GetJit(); |
Vladimir Marko | fd66c50 | 2016-04-18 15:37:01 +0100 | [diff] [blame] | 136 | if (m_name.compare(method_name_) == 0) { |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 137 | while (jit->GetCodeCache()->LookupOsrMethodHeader(m) == nullptr) { |
| 138 | // Sleep to yield to the compiler thread. |
Nicolas Geoffray | 223ac0c | 2016-06-09 09:53:55 +0100 | [diff] [blame] | 139 | usleep(1000); |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 140 | // Will either ensure it's compiled or do the compilation itself. |
| 141 | jit->CompileMethod(m, Thread::Current(), /* osr */ true); |
| 142 | } |
| 143 | return false; |
| 144 | } |
| 145 | return true; |
| 146 | } |
Vladimir Marko | fd66c50 | 2016-04-18 15:37:01 +0100 | [diff] [blame] | 147 | |
| 148 | const char* const method_name_; |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 149 | }; |
| 150 | |
Vladimir Marko | fd66c50 | 2016-04-18 15:37:01 +0100 | [diff] [blame] | 151 | extern "C" JNIEXPORT void JNICALL Java_Main_ensureHasOsrCode(JNIEnv* env, |
| 152 | jclass, |
| 153 | jstring method_name) { |
Calin Juravle | ffc8707 | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 154 | if (!Runtime::Current()->UseJitCompilation()) { |
Nicolas Geoffray | 28b7574 | 2016-02-16 11:30:48 +0000 | [diff] [blame] | 155 | return; |
| 156 | } |
Vladimir Marko | fd66c50 | 2016-04-18 15:37:01 +0100 | [diff] [blame] | 157 | ScopedUtfChars chars(env, method_name); |
| 158 | CHECK(chars.c_str() != nullptr); |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 159 | ScopedObjectAccess soa(Thread::Current()); |
Vladimir Marko | fd66c50 | 2016-04-18 15:37:01 +0100 | [diff] [blame] | 160 | OsrCheckVisitor visitor(soa.Self(), chars.c_str()); |
Nicolas Geoffray | d186dd8 | 2016-02-16 10:03:44 +0000 | [diff] [blame] | 161 | visitor.WalkStack(); |
| 162 | } |
| 163 | |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 164 | } // namespace art |