blob: 9cfa3247c825cefcff73d5be95fb9c74019f2824 [file] [log] [blame]
Andreas Gampe89df7bf2015-09-30 13:13:21 -07001/*
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 Gampe542451c2016-07-26 09:02:02 -070019#include "base/enums.h"
Andreas Gampe89df7bf2015-09-30 13:13:21 -070020#include "base/logging.h"
21#include "dex_file-inl.h"
Nicolas Geoffray491617a2016-07-19 17:06:23 +010022#include "jit/jit.h"
23#include "jit/jit_code_cache.h"
Andreas Gampe89df7bf2015-09-30 13:13:21 -070024#include "mirror/class-inl.h"
Nicolas Geoffray491617a2016-07-19 17:06:23 +010025#include "oat_quick_method_header.h"
Andreas Gampe89df7bf2015-09-30 13:13:21 -070026#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070027#include "scoped_thread_state_change-inl.h"
Nicolas Geoffray491617a2016-07-19 17:06:23 +010028#include "ScopedUtfChars.h"
Andreas Gampe89df7bf2015-09-30 13:13:21 -070029#include "thread-inl.h"
30
31namespace art {
32
33// public static native boolean hasOatFile();
34
35extern "C" JNIEXPORT jboolean JNICALL Java_Main_hasOatFile(JNIEnv* env, jclass cls) {
36 ScopedObjectAccess soa(env);
37
Mathieu Chartier0795f232016-09-27 18:43:30 -070038 ObjPtr<mirror::Class> klass = soa.Decode<mirror::Class>(cls);
Andreas Gampe89df7bf2015-09-30 13:13:21 -070039 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
46extern "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
53extern "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
60extern "C" JNIEXPORT jboolean JNICALL Java_Main_hasImage(JNIEnv* env ATTRIBUTE_UNUSED,
61 jclass cls ATTRIBUTE_UNUSED) {
Jeff Haodcdc85b2015-12-04 14:06:18 -080062 return Runtime::Current()->GetHeap()->HasBootImageSpace();
Andreas Gampe89df7bf2015-09-30 13:13:21 -070063}
64
65// public static native boolean isImageDex2OatEnabled();
66
67extern "C" JNIEXPORT jboolean JNICALL Java_Main_isImageDex2OatEnabled(JNIEnv* env ATTRIBUTE_UNUSED,
68 jclass cls ATTRIBUTE_UNUSED) {
69 return Runtime::Current()->IsImageDex2OatEnabled();
70}
71
Andreas Gampe0dfc9bc2015-09-30 17:13:59 -070072// public static native boolean compiledWithOptimizing();
73// Did we use the optimizing compiler to compile this?
74
75extern "C" JNIEXPORT jboolean JNICALL Java_Main_compiledWithOptimizing(JNIEnv* env, jclass cls) {
76 ScopedObjectAccess soa(env);
77
Mathieu Chartier0795f232016-09-27 18:43:30 -070078 ObjPtr<mirror::Class> klass = soa.Decode<mirror::Class>(cls);
Andreas Gampe0dfc9bc2015-09-30 17:13:59 -070079 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 Geoffray491617a2016-07-19 17:06:23 +0100122extern "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 Geoffray44cea192016-08-03 20:48:13 +0100131 Thread* self = Thread::Current();
Nicolas Geoffrayd55f4ab2016-08-02 18:49:25 +0100132 ArtMethod* method = nullptr;
133 {
Nicolas Geoffray44cea192016-08-03 20:48:13 +0100134 ScopedObjectAccess soa(self);
Nicolas Geoffray491617a2016-07-19 17:06:23 +0100135
Nicolas Geoffrayd55f4ab2016-08-02 18:49:25 +0100136 ScopedUtfChars chars(env, method_name);
137 CHECK(chars.c_str() != nullptr);
Mathieu Chartier0795f232016-09-27 18:43:30 -0700138 method = soa.Decode<mirror::Class>(cls)->FindDeclaredDirectMethodByName(
Nicolas Geoffrayd55f4ab2016-08-02 18:49:25 +0100139 chars.c_str(), kRuntimePointerSize);
140 }
Nicolas Geoffray491617a2016-07-19 17:06:23 +0100141
142 jit::JitCodeCache* code_cache = jit->GetCodeCache();
143 OatQuickMethodHeader* header = nullptr;
Nicolas Geoffray491617a2016-07-19 17:06:23 +0100144 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 Geoffray44cea192016-08-03 20:48:13 +0100151 ScopedObjectAccess soa(self);
Nicolas Geoffrayd55f4ab2016-08-02 18:49:25 +0100152 // Make sure there is a profiling info, required by the compiler.
Nicolas Geoffray44cea192016-08-03 20:48:13 +0100153 ProfilingInfo::Create(self, method, /* retry_allocation */ true);
Nicolas Geoffray491617a2016-07-19 17:06:23 +0100154 // Will either ensure it's compiled or do the compilation itself.
Nicolas Geoffray44cea192016-08-03 20:48:13 +0100155 jit->CompileMethod(method, self, /* osr */ false);
Nicolas Geoffray491617a2016-07-19 17:06:23 +0100156 }
157 }
158}
159
Mingyao Yang063fc772016-08-02 11:02:54 -0700160extern "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 Gampe89df7bf2015-09-30 13:13:21 -0700173} // namespace art