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