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