xref: /aosp_15_r20/art/test/667-jit-jni-stub/jit_jni_stub_test.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1 /*
2  * Copyright 2017 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 
19 #include "jit/jit.h"
20 #include "jit/jit_code_cache.h"
21 #include "mirror/class.h"
22 #include "mirror/string.h"
23 #include "runtime.h"
24 #include "scoped_thread_state_change-inl.h"
25 
26 namespace art {
27 
28 // Calls through to a static method with signature "()V".
29 extern "C" JNIEXPORT
Java_Main_callThrough(JNIEnv * env,jclass,jclass klass,jstring methodName)30 void Java_Main_callThrough(JNIEnv* env, jclass, jclass klass, jstring methodName) {
31   ScopedObjectAccess soa(Thread::Current());
32   std::string name = soa.Decode<mirror::String>(methodName)->ToModifiedUtf8();
33   jmethodID method = env->GetStaticMethodID(klass, name.c_str(), "()V");
34   CHECK(method != nullptr) << soa.Decode<mirror::Class>(klass)->PrettyDescriptor() << "." << name;
35   env->CallStaticVoidMethod(klass, method);
36 }
37 
38 extern "C" JNIEXPORT
Java_Main_jitGc(JNIEnv *,jclass)39 void Java_Main_jitGc(JNIEnv*, jclass) {
40   CHECK(Runtime::Current()->GetJit() != nullptr);
41   jit::JitCodeCache* cache = Runtime::Current()->GetJit()->GetCodeCache();
42   Thread* self = Thread::Current();
43   {
44     ScopedObjectAccess soa(self);
45     cache->InvalidateAllCompiledCode();
46   }
47   cache->DoCollection(self);
48   // Run a second time in case the first run was a no-op due to a concurrent JIT
49   // GC from the JIT thread.
50   cache->DoCollection(self);
51 }
52 
53 extern "C" JNIEXPORT
Java_Main_isNextJitGcFull(JNIEnv *,jclass)54 jboolean Java_Main_isNextJitGcFull(JNIEnv*, jclass) {
55   // Because we invalidate all compiled code above, we currently always do a
56   // full GC.
57   return true;
58 }
59 
60 }  // namespace art
61