1 /*
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 
19 #include "jni/java_vm_ext.h"
20 #include "mirror/class-inl.h"
21 #include "scoped_thread_state_change-inl.h"
22 
23 namespace art {
24 namespace {
25 
Java_JObjectBenchmark_timeAddRemoveLocal(JNIEnv * env,jobject jobj,jint reps)26 extern "C" JNIEXPORT void JNICALL Java_JObjectBenchmark_timeAddRemoveLocal(
27     JNIEnv* env, jobject jobj, jint reps) {
28   ScopedObjectAccess soa(env);
29   ObjPtr<mirror::Object> obj = soa.Decode<mirror::Object>(jobj);
30   CHECK(obj != nullptr);
31   for (jint i = 0; i < reps; ++i) {
32     jobject ref = soa.Env()->AddLocalReference<jobject>(obj);
33     soa.Env()->DeleteLocalRef(ref);
34   }
35 }
36 
Java_JObjectBenchmark_timeDecodeLocal(JNIEnv * env,jobject jobj,jint reps)37 extern "C" JNIEXPORT void JNICALL Java_JObjectBenchmark_timeDecodeLocal(
38     JNIEnv* env, jobject jobj, jint reps) {
39   ScopedObjectAccess soa(env);
40   ObjPtr<mirror::Object> obj = soa.Decode<mirror::Object>(jobj);
41   CHECK(obj != nullptr);
42   jobject ref = soa.Env()->AddLocalReference<jobject>(obj);
43   for (jint i = 0; i < reps; ++i) {
44     CHECK_EQ(soa.Decode<mirror::Object>(ref), obj);
45   }
46   soa.Env()->DeleteLocalRef(ref);
47 }
48 
Java_JObjectBenchmark_timeAddRemoveGlobal(JNIEnv * env,jobject jobj,jint reps)49 extern "C" JNIEXPORT void JNICALL Java_JObjectBenchmark_timeAddRemoveGlobal(
50     JNIEnv* env, jobject jobj, jint reps) {
51   ScopedObjectAccess soa(env);
52   ObjPtr<mirror::Object> obj = soa.Decode<mirror::Object>(jobj);
53   CHECK(obj != nullptr);
54   for (jint i = 0; i < reps; ++i) {
55     jobject ref = soa.Vm()->AddGlobalRef(soa.Self(), obj);
56     soa.Vm()->DeleteGlobalRef(soa.Self(), ref);
57   }
58 }
59 
Java_JObjectBenchmark_timeDecodeGlobal(JNIEnv * env,jobject jobj,jint reps)60 extern "C" JNIEXPORT void JNICALL Java_JObjectBenchmark_timeDecodeGlobal(
61     JNIEnv* env, jobject jobj, jint reps) {
62   ScopedObjectAccess soa(env);
63   ObjPtr<mirror::Object> obj = soa.Decode<mirror::Object>(jobj);
64   CHECK(obj != nullptr);
65   jobject ref = soa.Vm()->AddGlobalRef(soa.Self(), obj);
66   for (jint i = 0; i < reps; ++i) {
67     CHECK_EQ(soa.Decode<mirror::Object>(ref), obj);
68   }
69   soa.Vm()->DeleteGlobalRef(soa.Self(), ref);
70 }
71 
Java_JObjectBenchmark_timeAddRemoveWeakGlobal(JNIEnv * env,jobject jobj,jint reps)72 extern "C" JNIEXPORT void JNICALL Java_JObjectBenchmark_timeAddRemoveWeakGlobal(
73     JNIEnv* env, jobject jobj, jint reps) {
74   ScopedObjectAccess soa(env);
75   ObjPtr<mirror::Object> obj = soa.Decode<mirror::Object>(jobj);
76   CHECK(obj != nullptr);
77   for (jint i = 0; i < reps; ++i) {
78     jobject ref = soa.Vm()->AddWeakGlobalRef(soa.Self(), obj);
79     soa.Vm()->DeleteWeakGlobalRef(soa.Self(), ref);
80   }
81 }
82 
Java_JObjectBenchmark_timeDecodeWeakGlobal(JNIEnv * env,jobject jobj,jint reps)83 extern "C" JNIEXPORT void JNICALL Java_JObjectBenchmark_timeDecodeWeakGlobal(
84     JNIEnv* env, jobject jobj, jint reps) {
85   ScopedObjectAccess soa(env);
86   ObjPtr<mirror::Object> obj = soa.Decode<mirror::Object>(jobj);
87   CHECK(obj != nullptr);
88   jobject ref = soa.Vm()->AddWeakGlobalRef(soa.Self(), obj);
89   for (jint i = 0; i < reps; ++i) {
90     CHECK_EQ(soa.Decode<mirror::Object>(ref), obj);
91   }
92   soa.Vm()->DeleteWeakGlobalRef(soa.Self(), ref);
93 }
94 
Java_JObjectBenchmark_timeDecodeHandleScopeRef(JNIEnv * env,jobject jobj,jint reps)95 extern "C" JNIEXPORT void JNICALL Java_JObjectBenchmark_timeDecodeHandleScopeRef(
96     JNIEnv* env, jobject jobj, jint reps) {
97   ScopedObjectAccess soa(env);
98   for (jint i = 0; i < reps; ++i) {
99     soa.Decode<mirror::Object>(jobj);
100   }
101 }
102 
103 }  // namespace
104 }  // namespace art
105