1 /*
2  * Copyright (C) 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 "art_method.h"
20 #include "jit/jit.h"
21 #include "jit/jit_code_cache.h"
22 #include "jni/jni_internal.h"
23 #include "mirror/class.h"
24 #include "runtime.h"
25 #include "scoped_thread_state_change-inl.h"
26 #include "thread_list.h"
27 
28 namespace art {
29 
30 extern "C" JNIEXPORT
31 jboolean
Java_JitCacheChurnTest_removeJitCompiledMethod(JNIEnv * env,jclass,jobject javaMethod,jboolean releaseMemory)32 Java_JitCacheChurnTest_removeJitCompiledMethod(JNIEnv* env,
33                                                jclass,
34                                                jobject javaMethod,
35                                                jboolean releaseMemory) {
36   if (!Runtime::Current()->UseJitCompilation()) {
37     return JNI_FALSE;
38   }
39 
40   jit::Jit* jit = Runtime::Current()->GetJit();
41   jit->WaitForCompilationToFinish(Thread::Current());
42 
43   ScopedObjectAccess soa(env);
44   ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod);
45 
46   jit::JitCodeCache* code_cache = jit->GetCodeCache();
47 
48   // Drop the shared mutator lock
49   ScopedThreadSuspension selfSuspension(Thread::Current(), art::ThreadState::kNative);
50   // Get exclusive mutator lock with suspend all.
51   ScopedSuspendAll suspend("Removing JIT compiled method", /*long_suspend*/true);
52   bool removed = code_cache->RemoveMethod(method, static_cast<bool>(releaseMemory));
53   return removed ? JNI_TRUE : JNI_FALSE;
54 }
55 
56 }  // namespace art
57