blob: 7451cf97de347bf0d305fce11c038532a337a654 [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"
Alex Lightdba61482016-12-21 08:20:29 -080022#include "instrumentation.h"
Nicolas Geoffray491617a2016-07-19 17:06:23 +010023#include "jit/jit.h"
24#include "jit/jit_code_cache.h"
Andreas Gampe89df7bf2015-09-30 13:13:21 -070025#include "mirror/class-inl.h"
Nicolas Geoffray491617a2016-07-19 17:06:23 +010026#include "oat_quick_method_header.h"
Andreas Gampe89df7bf2015-09-30 13:13:21 -070027#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070028#include "scoped_thread_state_change-inl.h"
Nicolas Geoffray491617a2016-07-19 17:06:23 +010029#include "ScopedUtfChars.h"
Andreas Gampe89df7bf2015-09-30 13:13:21 -070030#include "thread-inl.h"
31
32namespace art {
33
Alex Lightdba61482016-12-21 08:20:29 -080034// public static native boolean hasJit();
35
36extern "C" JNIEXPORT jboolean JNICALL Java_Main_hasJit(JNIEnv*, jclass) {
37 Runtime* runtime = Runtime::Current();
38 return runtime != nullptr
39 && runtime->GetJit() != nullptr
40 && runtime->GetInstrumentation()->GetCurrentInstrumentationLevel() !=
41 instrumentation::Instrumentation::InstrumentationLevel::kInstrumentWithInterpreter;
42}
43
Andreas Gampe89df7bf2015-09-30 13:13:21 -070044// public static native boolean hasOatFile();
45
46extern "C" JNIEXPORT jboolean JNICALL Java_Main_hasOatFile(JNIEnv* env, jclass cls) {
47 ScopedObjectAccess soa(env);
48
Mathieu Chartier0795f232016-09-27 18:43:30 -070049 ObjPtr<mirror::Class> klass = soa.Decode<mirror::Class>(cls);
Andreas Gampe89df7bf2015-09-30 13:13:21 -070050 const DexFile& dex_file = klass->GetDexFile();
51 const OatFile::OatDexFile* oat_dex_file = dex_file.GetOatDexFile();
52 return (oat_dex_file != nullptr) ? JNI_TRUE : JNI_FALSE;
53}
54
55// public static native boolean runtimeIsSoftFail();
56
57extern "C" JNIEXPORT jboolean JNICALL Java_Main_runtimeIsSoftFail(JNIEnv* env ATTRIBUTE_UNUSED,
58 jclass cls ATTRIBUTE_UNUSED) {
59 return Runtime::Current()->IsVerificationSoftFail() ? JNI_TRUE : JNI_FALSE;
60}
61
62// public static native boolean isDex2OatEnabled();
63
64extern "C" JNIEXPORT jboolean JNICALL Java_Main_isDex2OatEnabled(JNIEnv* env ATTRIBUTE_UNUSED,
65 jclass cls ATTRIBUTE_UNUSED) {
66 return Runtime::Current()->IsDex2OatEnabled();
67}
68
69// public static native boolean hasImage();
70
71extern "C" JNIEXPORT jboolean JNICALL Java_Main_hasImage(JNIEnv* env ATTRIBUTE_UNUSED,
72 jclass cls ATTRIBUTE_UNUSED) {
Jeff Haodcdc85b2015-12-04 14:06:18 -080073 return Runtime::Current()->GetHeap()->HasBootImageSpace();
Andreas Gampe89df7bf2015-09-30 13:13:21 -070074}
75
76// public static native boolean isImageDex2OatEnabled();
77
78extern "C" JNIEXPORT jboolean JNICALL Java_Main_isImageDex2OatEnabled(JNIEnv* env ATTRIBUTE_UNUSED,
79 jclass cls ATTRIBUTE_UNUSED) {
80 return Runtime::Current()->IsImageDex2OatEnabled();
81}
82
Andreas Gampe0dfc9bc2015-09-30 17:13:59 -070083// public static native boolean compiledWithOptimizing();
84// Did we use the optimizing compiler to compile this?
85
86extern "C" JNIEXPORT jboolean JNICALL Java_Main_compiledWithOptimizing(JNIEnv* env, jclass cls) {
87 ScopedObjectAccess soa(env);
88
Mathieu Chartier0795f232016-09-27 18:43:30 -070089 ObjPtr<mirror::Class> klass = soa.Decode<mirror::Class>(cls);
Andreas Gampe0dfc9bc2015-09-30 17:13:59 -070090 const DexFile& dex_file = klass->GetDexFile();
91 const OatFile::OatDexFile* oat_dex_file = dex_file.GetOatDexFile();
92 if (oat_dex_file == nullptr) {
93 // Could be JIT, which also uses optimizing, but conservatively say no.
94 return JNI_FALSE;
95 }
96 const OatFile* oat_file = oat_dex_file->GetOatFile();
97 CHECK(oat_file != nullptr);
98
99 const char* cmd_line = oat_file->GetOatHeader().GetStoreValueByKey(OatHeader::kDex2OatCmdLineKey);
100 CHECK(cmd_line != nullptr); // Huh? This should not happen.
101
102 // Check the backend.
103 constexpr const char* kCompilerBackend = "--compiler-backend=";
104 const char* backend = strstr(cmd_line, kCompilerBackend);
105 if (backend != nullptr) {
106 // If it's set, make sure it's optimizing.
107 backend += strlen(kCompilerBackend);
108 if (strncmp(backend, "Optimizing", strlen("Optimizing")) != 0) {
109 return JNI_FALSE;
110 }
111 }
112
113 // Check the filter.
114 constexpr const char* kCompilerFilter = "--compiler-filter=";
115 const char* filter = strstr(cmd_line, kCompilerFilter);
116 if (filter != nullptr) {
117 // If it's set, make sure it's not interpret-only|verify-none|verify-at-runtime.
118 // Note: The space filter might have an impact on the test, but ignore that for now.
119 filter += strlen(kCompilerFilter);
120 constexpr const char* kInterpretOnly = "interpret-only";
121 constexpr const char* kVerifyNone = "verify-none";
122 constexpr const char* kVerifyAtRuntime = "verify-at-runtime";
123 if (strncmp(filter, kInterpretOnly, strlen(kInterpretOnly)) == 0 ||
124 strncmp(filter, kVerifyNone, strlen(kVerifyNone)) == 0 ||
125 strncmp(filter, kVerifyAtRuntime, strlen(kVerifyAtRuntime)) == 0) {
126 return JNI_FALSE;
127 }
128 }
129
130 return JNI_TRUE;
131}
132
Nicolas Geoffray51c17fa2016-11-25 15:56:12 +0000133extern "C" JNIEXPORT jboolean JNICALL Java_Main_isAotCompiled(JNIEnv* env,
134 jclass,
135 jclass cls,
136 jstring method_name) {
137 Thread* self = Thread::Current();
138 ScopedObjectAccess soa(self);
139 ScopedUtfChars chars(env, method_name);
140 CHECK(chars.c_str() != nullptr);
141 ArtMethod* method = soa.Decode<mirror::Class>(cls)->FindDeclaredDirectMethodByName(
142 chars.c_str(), kRuntimePointerSize);
143 const void* code = method->GetOatMethodQuickCode(kRuntimePointerSize);
144 jit::Jit* jit = Runtime::Current()->GetJit();
145 if (jit != nullptr && jit->GetCodeCache()->ContainsPc(code)) {
146 return true;
147 }
148 return code != nullptr;
149}
150
Nicolas Geoffray491617a2016-07-19 17:06:23 +0100151extern "C" JNIEXPORT void JNICALL Java_Main_ensureJitCompiled(JNIEnv* env,
152 jclass,
153 jclass cls,
154 jstring method_name) {
Alex Lightca4feac2017-01-13 22:47:50 +0000155 jit::Jit* jit = Runtime::Current()->GetJit();
Nicolas Geoffray491617a2016-07-19 17:06:23 +0100156 if (jit == nullptr) {
157 return;
158 }
159
Nicolas Geoffray44cea192016-08-03 20:48:13 +0100160 Thread* self = Thread::Current();
Nicolas Geoffrayd55f4ab2016-08-02 18:49:25 +0100161 ArtMethod* method = nullptr;
162 {
Nicolas Geoffray44cea192016-08-03 20:48:13 +0100163 ScopedObjectAccess soa(self);
Nicolas Geoffray491617a2016-07-19 17:06:23 +0100164
Nicolas Geoffrayd55f4ab2016-08-02 18:49:25 +0100165 ScopedUtfChars chars(env, method_name);
166 CHECK(chars.c_str() != nullptr);
Mathieu Chartier0795f232016-09-27 18:43:30 -0700167 method = soa.Decode<mirror::Class>(cls)->FindDeclaredDirectMethodByName(
Nicolas Geoffrayd55f4ab2016-08-02 18:49:25 +0100168 chars.c_str(), kRuntimePointerSize);
169 }
Nicolas Geoffray491617a2016-07-19 17:06:23 +0100170
171 jit::JitCodeCache* code_cache = jit->GetCodeCache();
172 OatQuickMethodHeader* header = nullptr;
Nicolas Geoffray491617a2016-07-19 17:06:23 +0100173 while (true) {
174 header = OatQuickMethodHeader::FromEntryPoint(method->GetEntryPointFromQuickCompiledCode());
175 if (code_cache->ContainsPc(header->GetCode())) {
176 break;
177 } else {
178 // Sleep to yield to the compiler thread.
179 usleep(1000);
Nicolas Geoffray44cea192016-08-03 20:48:13 +0100180 ScopedObjectAccess soa(self);
Nicolas Geoffrayd55f4ab2016-08-02 18:49:25 +0100181 // Make sure there is a profiling info, required by the compiler.
Nicolas Geoffray44cea192016-08-03 20:48:13 +0100182 ProfilingInfo::Create(self, method, /* retry_allocation */ true);
Nicolas Geoffray491617a2016-07-19 17:06:23 +0100183 // Will either ensure it's compiled or do the compilation itself.
Nicolas Geoffray44cea192016-08-03 20:48:13 +0100184 jit->CompileMethod(method, self, /* osr */ false);
Nicolas Geoffray491617a2016-07-19 17:06:23 +0100185 }
186 }
187}
188
Mingyao Yang063fc772016-08-02 11:02:54 -0700189extern "C" JNIEXPORT jboolean JNICALL Java_Main_hasSingleImplementation(JNIEnv* env,
190 jclass,
191 jclass cls,
192 jstring method_name) {
193 ArtMethod* method = nullptr;
194 ScopedObjectAccess soa(Thread::Current());
195 ScopedUtfChars chars(env, method_name);
196 CHECK(chars.c_str() != nullptr);
197 method = soa.Decode<mirror::Class>(cls)->FindDeclaredVirtualMethodByName(
198 chars.c_str(), kRuntimePointerSize);
199 return method->HasSingleImplementation();
200}
201
Calin Juravle857f0582016-12-20 14:36:59 +0000202extern "C" JNIEXPORT int JNICALL Java_Main_getHotnessCounter(JNIEnv* env,
203 jclass,
204 jclass cls,
205 jstring method_name) {
206 jit::Jit* jit = Runtime::Current()->GetJit();
207 if (jit == nullptr) {
208 // The hotness counter is valid only under JIT.
209 // If we don't JIT return 0 to match test expectations.
210 return 0;
211 }
212
213 ArtMethod* method = nullptr;
214 {
215 ScopedObjectAccess soa(Thread::Current());
216
217 ScopedUtfChars chars(env, method_name);
218 CHECK(chars.c_str() != nullptr);
219 method = soa.Decode<mirror::Class>(cls)->FindDeclaredDirectMethodByName(
220 chars.c_str(), kRuntimePointerSize);
221 }
222
223 return method->GetCounter();
224}
225
Andreas Gampe89df7bf2015-09-30 13:13:21 -0700226} // namespace art