SystemPerfTest.cpp revision b2264c2bc5af19b53edc4708b5b86743f91ae8aa
1/* 2 * Copyright (C) 2016 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#define NELEM(x) ((int) (sizeof(x) / sizeof((x)[0]))) 20 21static void jintarrayArgumentNoop(JNIEnv*, jclass, jintArray, jint) { 22} 23 24static jint jintarrayGetLength(JNIEnv* env, jclass, jintArray jarray) { 25 const jsize len = env->GetArrayLength(jarray); 26 return static_cast<jint>(len); 27} 28 29static jint jintarrayCriticalAccess(JNIEnv* env, jclass, jintArray jarray, jint index) { 30 const jsize len = env->GetArrayLength(jarray); 31 if (index < 0 || index >= len) { 32 return -1; 33 } 34 jint* data = (jint*) env->GetPrimitiveArrayCritical(jarray, 0); 35 jint ret = data[index]; 36 env->ReleasePrimitiveArrayCritical(jarray, data, 0); 37 return ret; 38} 39 40static jint jintarrayBasicAccess(JNIEnv* env, jclass, jintArray jarray, jint index) { 41 const jsize len = env->GetArrayLength(jarray); 42 if (index < 0 || index >= len) { 43 return -1; 44 } 45 jint* data = env->GetIntArrayElements(jarray, 0); 46 jint ret = data[index]; 47 env->ReleaseIntArrayElements(jarray, data, 0); 48 return ret; 49} 50 51static const JNINativeMethod sMethods[] = { 52 {"jintarrayArgumentNoop", "([II)V", (void *) jintarrayArgumentNoop}, 53 {"jintarrayGetLength", "([I)I", (void *) jintarrayGetLength}, 54 {"jintarrayCriticalAccess", "([II)I", (void *) jintarrayCriticalAccess}, 55 {"jintarrayBasicAccess", "([II)I", (void *) jintarrayBasicAccess}, 56}; 57 58static int registerNativeMethods(JNIEnv* env, const char* className, 59 const JNINativeMethod* gMethods, int numMethods) { 60 jclass clazz = env->FindClass(className); 61 if (clazz == NULL) { 62 return JNI_FALSE; 63 } 64 if (env->RegisterNatives(clazz, gMethods, numMethods) < 0) { 65 return JNI_FALSE; 66 } 67 return JNI_TRUE; 68} 69 70jint JNI_OnLoad(JavaVM* jvm, void*) { 71 JNIEnv *env = NULL; 72 if (jvm->GetEnv((void**) &env, JNI_VERSION_1_6)) { 73 return JNI_ERR; 74 } 75 76 if (registerNativeMethods(env, "java/lang/perftests/SystemPerfTest", 77 sMethods, NELEM(sMethods)) == -1) { 78 return JNI_ERR; 79 } 80 81 return JNI_VERSION_1_6; 82} 83