Andreas Gampe | 89df7bf | 2015-09-30 13:13:21 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 "jni.h" |
| 18 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 19 | #include "base/enums.h" |
Andreas Gampe | 89df7bf | 2015-09-30 13:13:21 -0700 | [diff] [blame] | 20 | #include "base/logging.h" |
| 21 | #include "dex_file-inl.h" |
Nicolas Geoffray | 491617a | 2016-07-19 17:06:23 +0100 | [diff] [blame] | 22 | #include "jit/jit.h" |
| 23 | #include "jit/jit_code_cache.h" |
Andreas Gampe | 89df7bf | 2015-09-30 13:13:21 -0700 | [diff] [blame] | 24 | #include "mirror/class-inl.h" |
Nicolas Geoffray | 491617a | 2016-07-19 17:06:23 +0100 | [diff] [blame] | 25 | #include "oat_quick_method_header.h" |
Andreas Gampe | 89df7bf | 2015-09-30 13:13:21 -0700 | [diff] [blame] | 26 | #include "runtime.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 27 | #include "scoped_thread_state_change-inl.h" |
Nicolas Geoffray | 491617a | 2016-07-19 17:06:23 +0100 | [diff] [blame] | 28 | #include "ScopedUtfChars.h" |
Andreas Gampe | 89df7bf | 2015-09-30 13:13:21 -0700 | [diff] [blame] | 29 | #include "thread-inl.h" |
| 30 | |
| 31 | namespace art { |
| 32 | |
| 33 | // public static native boolean hasOatFile(); |
| 34 | |
| 35 | extern "C" JNIEXPORT jboolean JNICALL Java_Main_hasOatFile(JNIEnv* env, jclass cls) { |
| 36 | ScopedObjectAccess soa(env); |
| 37 | |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 38 | ObjPtr<mirror::Class> klass = soa.Decode<mirror::Class>(cls); |
Andreas Gampe | 89df7bf | 2015-09-30 13:13:21 -0700 | [diff] [blame] | 39 | const DexFile& dex_file = klass->GetDexFile(); |
| 40 | const OatFile::OatDexFile* oat_dex_file = dex_file.GetOatDexFile(); |
| 41 | return (oat_dex_file != nullptr) ? JNI_TRUE : JNI_FALSE; |
| 42 | } |
| 43 | |
| 44 | // public static native boolean runtimeIsSoftFail(); |
| 45 | |
| 46 | extern "C" JNIEXPORT jboolean JNICALL Java_Main_runtimeIsSoftFail(JNIEnv* env ATTRIBUTE_UNUSED, |
| 47 | jclass cls ATTRIBUTE_UNUSED) { |
| 48 | return Runtime::Current()->IsVerificationSoftFail() ? JNI_TRUE : JNI_FALSE; |
| 49 | } |
| 50 | |
| 51 | // public static native boolean isDex2OatEnabled(); |
| 52 | |
| 53 | extern "C" JNIEXPORT jboolean JNICALL Java_Main_isDex2OatEnabled(JNIEnv* env ATTRIBUTE_UNUSED, |
| 54 | jclass cls ATTRIBUTE_UNUSED) { |
| 55 | return Runtime::Current()->IsDex2OatEnabled(); |
| 56 | } |
| 57 | |
| 58 | // public static native boolean hasImage(); |
| 59 | |
| 60 | extern "C" JNIEXPORT jboolean JNICALL Java_Main_hasImage(JNIEnv* env ATTRIBUTE_UNUSED, |
| 61 | jclass cls ATTRIBUTE_UNUSED) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 62 | return Runtime::Current()->GetHeap()->HasBootImageSpace(); |
Andreas Gampe | 89df7bf | 2015-09-30 13:13:21 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | // public static native boolean isImageDex2OatEnabled(); |
| 66 | |
| 67 | extern "C" JNIEXPORT jboolean JNICALL Java_Main_isImageDex2OatEnabled(JNIEnv* env ATTRIBUTE_UNUSED, |
| 68 | jclass cls ATTRIBUTE_UNUSED) { |
| 69 | return Runtime::Current()->IsImageDex2OatEnabled(); |
| 70 | } |
| 71 | |
Andreas Gampe | 0dfc9bc | 2015-09-30 17:13:59 -0700 | [diff] [blame] | 72 | // public static native boolean compiledWithOptimizing(); |
| 73 | // Did we use the optimizing compiler to compile this? |
| 74 | |
| 75 | extern "C" JNIEXPORT jboolean JNICALL Java_Main_compiledWithOptimizing(JNIEnv* env, jclass cls) { |
| 76 | ScopedObjectAccess soa(env); |
| 77 | |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 78 | ObjPtr<mirror::Class> klass = soa.Decode<mirror::Class>(cls); |
Andreas Gampe | 0dfc9bc | 2015-09-30 17:13:59 -0700 | [diff] [blame] | 79 | const DexFile& dex_file = klass->GetDexFile(); |
| 80 | const OatFile::OatDexFile* oat_dex_file = dex_file.GetOatDexFile(); |
| 81 | if (oat_dex_file == nullptr) { |
| 82 | // Could be JIT, which also uses optimizing, but conservatively say no. |
| 83 | return JNI_FALSE; |
| 84 | } |
| 85 | const OatFile* oat_file = oat_dex_file->GetOatFile(); |
| 86 | CHECK(oat_file != nullptr); |
| 87 | |
| 88 | const char* cmd_line = oat_file->GetOatHeader().GetStoreValueByKey(OatHeader::kDex2OatCmdLineKey); |
| 89 | CHECK(cmd_line != nullptr); // Huh? This should not happen. |
| 90 | |
| 91 | // Check the backend. |
| 92 | constexpr const char* kCompilerBackend = "--compiler-backend="; |
| 93 | const char* backend = strstr(cmd_line, kCompilerBackend); |
| 94 | if (backend != nullptr) { |
| 95 | // If it's set, make sure it's optimizing. |
| 96 | backend += strlen(kCompilerBackend); |
| 97 | if (strncmp(backend, "Optimizing", strlen("Optimizing")) != 0) { |
| 98 | return JNI_FALSE; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | // Check the filter. |
| 103 | constexpr const char* kCompilerFilter = "--compiler-filter="; |
| 104 | const char* filter = strstr(cmd_line, kCompilerFilter); |
| 105 | if (filter != nullptr) { |
| 106 | // If it's set, make sure it's not interpret-only|verify-none|verify-at-runtime. |
| 107 | // Note: The space filter might have an impact on the test, but ignore that for now. |
| 108 | filter += strlen(kCompilerFilter); |
| 109 | constexpr const char* kInterpretOnly = "interpret-only"; |
| 110 | constexpr const char* kVerifyNone = "verify-none"; |
| 111 | constexpr const char* kVerifyAtRuntime = "verify-at-runtime"; |
| 112 | if (strncmp(filter, kInterpretOnly, strlen(kInterpretOnly)) == 0 || |
| 113 | strncmp(filter, kVerifyNone, strlen(kVerifyNone)) == 0 || |
| 114 | strncmp(filter, kVerifyAtRuntime, strlen(kVerifyAtRuntime)) == 0) { |
| 115 | return JNI_FALSE; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | return JNI_TRUE; |
| 120 | } |
| 121 | |
Nicolas Geoffray | 491617a | 2016-07-19 17:06:23 +0100 | [diff] [blame] | 122 | extern "C" JNIEXPORT void JNICALL Java_Main_ensureJitCompiled(JNIEnv* env, |
| 123 | jclass, |
| 124 | jclass cls, |
| 125 | jstring method_name) { |
| 126 | jit::Jit* jit = Runtime::Current()->GetJit(); |
| 127 | if (jit == nullptr) { |
| 128 | return; |
| 129 | } |
| 130 | |
Nicolas Geoffray | 44cea19 | 2016-08-03 20:48:13 +0100 | [diff] [blame] | 131 | Thread* self = Thread::Current(); |
Nicolas Geoffray | d55f4ab | 2016-08-02 18:49:25 +0100 | [diff] [blame] | 132 | ArtMethod* method = nullptr; |
| 133 | { |
Nicolas Geoffray | 44cea19 | 2016-08-03 20:48:13 +0100 | [diff] [blame] | 134 | ScopedObjectAccess soa(self); |
Nicolas Geoffray | 491617a | 2016-07-19 17:06:23 +0100 | [diff] [blame] | 135 | |
Nicolas Geoffray | d55f4ab | 2016-08-02 18:49:25 +0100 | [diff] [blame] | 136 | ScopedUtfChars chars(env, method_name); |
| 137 | CHECK(chars.c_str() != nullptr); |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 138 | method = soa.Decode<mirror::Class>(cls)->FindDeclaredDirectMethodByName( |
Nicolas Geoffray | d55f4ab | 2016-08-02 18:49:25 +0100 | [diff] [blame] | 139 | chars.c_str(), kRuntimePointerSize); |
| 140 | } |
Nicolas Geoffray | 491617a | 2016-07-19 17:06:23 +0100 | [diff] [blame] | 141 | |
| 142 | jit::JitCodeCache* code_cache = jit->GetCodeCache(); |
| 143 | OatQuickMethodHeader* header = nullptr; |
Nicolas Geoffray | 491617a | 2016-07-19 17:06:23 +0100 | [diff] [blame] | 144 | while (true) { |
| 145 | header = OatQuickMethodHeader::FromEntryPoint(method->GetEntryPointFromQuickCompiledCode()); |
| 146 | if (code_cache->ContainsPc(header->GetCode())) { |
| 147 | break; |
| 148 | } else { |
| 149 | // Sleep to yield to the compiler thread. |
| 150 | usleep(1000); |
Nicolas Geoffray | 44cea19 | 2016-08-03 20:48:13 +0100 | [diff] [blame] | 151 | ScopedObjectAccess soa(self); |
Nicolas Geoffray | d55f4ab | 2016-08-02 18:49:25 +0100 | [diff] [blame] | 152 | // Make sure there is a profiling info, required by the compiler. |
Nicolas Geoffray | 44cea19 | 2016-08-03 20:48:13 +0100 | [diff] [blame] | 153 | ProfilingInfo::Create(self, method, /* retry_allocation */ true); |
Nicolas Geoffray | 491617a | 2016-07-19 17:06:23 +0100 | [diff] [blame] | 154 | // Will either ensure it's compiled or do the compilation itself. |
Nicolas Geoffray | 44cea19 | 2016-08-03 20:48:13 +0100 | [diff] [blame] | 155 | jit->CompileMethod(method, self, /* osr */ false); |
Nicolas Geoffray | 491617a | 2016-07-19 17:06:23 +0100 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | } |
| 159 | |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame^] | 160 | extern "C" JNIEXPORT jboolean JNICALL Java_Main_hasSingleImplementation(JNIEnv* env, |
| 161 | jclass, |
| 162 | jclass cls, |
| 163 | jstring method_name) { |
| 164 | ArtMethod* method = nullptr; |
| 165 | ScopedObjectAccess soa(Thread::Current()); |
| 166 | ScopedUtfChars chars(env, method_name); |
| 167 | CHECK(chars.c_str() != nullptr); |
| 168 | method = soa.Decode<mirror::Class>(cls)->FindDeclaredVirtualMethodByName( |
| 169 | chars.c_str(), kRuntimePointerSize); |
| 170 | return method->HasSingleImplementation(); |
| 171 | } |
| 172 | |
Andreas Gampe | 89df7bf | 2015-09-30 13:13:21 -0700 | [diff] [blame] | 173 | } // namespace art |