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 // Local class declared as a friend of JitCodeCache so that we can access its internals.
29 class JitJniStubTestHelper {
30  public:
isNextJitGcFull(Thread * self)31   static bool isNextJitGcFull(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_) {
32     CHECK(Runtime::Current()->GetJit() != nullptr);
33     jit::JitCodeCache* cache = Runtime::Current()->GetJit()->GetCodeCache();
34     MutexLock mu(self, *Locks::jit_lock_);
35     return cache->ShouldDoFullCollection();
36   }
37 };
38 
39 // Calls through to a static method with signature "()V".
40 extern "C" JNIEXPORT
Java_Main_callThrough(JNIEnv * env,jclass,jclass klass,jstring methodName)41 void Java_Main_callThrough(JNIEnv* env, jclass, jclass klass, jstring methodName) {
42   ScopedObjectAccess soa(Thread::Current());
43   std::string name = soa.Decode<mirror::String>(methodName)->ToModifiedUtf8();
44   jmethodID method = env->GetStaticMethodID(klass, name.c_str(), "()V");
45   CHECK(method != nullptr) << soa.Decode<mirror::Class>(klass)->PrettyDescriptor() << "." << name;
46   env->CallStaticVoidMethod(klass, method);
47 }
48 
49 extern "C" JNIEXPORT
Java_Main_jitGc(JNIEnv *,jclass)50 void Java_Main_jitGc(JNIEnv*, jclass) {
51   CHECK(Runtime::Current()->GetJit() != nullptr);
52   jit::JitCodeCache* cache = Runtime::Current()->GetJit()->GetCodeCache();
53   ScopedObjectAccess soa(Thread::Current());
54   cache->GarbageCollectCache(Thread::Current());
55 }
56 
57 extern "C" JNIEXPORT
Java_Main_isNextJitGcFull(JNIEnv *,jclass)58 jboolean Java_Main_isNextJitGcFull(JNIEnv*, jclass) {
59   ScopedObjectAccess soa(Thread::Current());
60   return JitJniStubTestHelper::isNextJitGcFull(soa.Self());
61 }
62 
63 }  // namespace art
64