diff --git a/apct-tests/perftests/core/Android.mk b/apct-tests/perftests/core/Android.mk index 5fe2f02102260..3a7f945f55fde 100644 --- a/apct-tests/perftests/core/Android.mk +++ b/apct-tests/perftests/core/Android.mk @@ -12,8 +12,11 @@ LOCAL_STATIC_JAVA_LIBRARIES := \ LOCAL_PACKAGE_NAME := CorePerfTests +LOCAL_JNI_SHARED_LIBRARIES := libperftestscore_jni + # Use google-fonts/dancing-script for the performance metrics LOCAL_ASSET_DIR := $(TOP)/external/google-fonts/dancing-script include $(BUILD_PACKAGE) +include $(call all-makefiles-under, $(LOCAL_PATH)) diff --git a/apct-tests/perftests/core/jni/Android.mk b/apct-tests/perftests/core/jni/Android.mk new file mode 100644 index 0000000000000..d4c3f1e4a3ab0 --- /dev/null +++ b/apct-tests/perftests/core/jni/Android.mk @@ -0,0 +1,17 @@ +LOCAL_PATH := $(call my-dir) + +include $(CLEAR_VARS) +LOCAL_SDK_VERSION := 21 + +LOCAL_SRC_FILES:= \ + SystemPerfTest.cpp \ + +LOCAL_C_INCLUDES += \ + $(JNI_H_INCLUDE) + +LOCAL_MODULE := libperftestscore_jni +LOCAL_MODULE_TAGS := optional + +LOCAL_CFLAGS += -Wall -Werror -Wunused -Wunreachable-code + +include $(BUILD_SHARED_LIBRARY) diff --git a/apct-tests/perftests/core/jni/SystemPerfTest.cpp b/apct-tests/perftests/core/jni/SystemPerfTest.cpp new file mode 100644 index 0000000000000..eb5540844808e --- /dev/null +++ b/apct-tests/perftests/core/jni/SystemPerfTest.cpp @@ -0,0 +1,82 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#define NELEM(x) ((int) (sizeof(x) / sizeof((x)[0]))) + +static void jintarrayArgumentNoop(JNIEnv*, jclass, jintArray, jint) { +} + +static jint jintarrayGetLength(JNIEnv* env, jclass, jintArray jarray) { + const jsize len = env->GetArrayLength(jarray); + return static_cast(len); +} + +static jint jintarrayCriticalAccess(JNIEnv* env, jclass, jintArray jarray, jint index) { + const jsize len = env->GetArrayLength(jarray); + if (index < 0 || index >= len) { + return -1; + } + jint* data = (jint*) env->GetPrimitiveArrayCritical(jarray, 0); + jint ret = data[index]; + env->ReleasePrimitiveArrayCritical(jarray, data, 0); + return ret; +} + +static jint jintarrayBasicAccess(JNIEnv* env, jclass, jintArray jarray, jint index) { + const jsize len = env->GetArrayLength(jarray); + if (index < 0 || index >= len) { + return -1; + } + jint* data = env->GetIntArrayElements(jarray, 0); + jint ret = data[index]; + env->ReleaseIntArrayElements(jarray, data, 0); + return ret; +} + +static const JNINativeMethod sMethods[] = { + {"jintarrayArgumentNoop", "([II)V", (void *) jintarrayArgumentNoop}, + {"jintarrayGetLength", "([I)I", (void *) jintarrayGetLength}, + {"jintarrayCriticalAccess", "([II)I", (void *) jintarrayCriticalAccess}, + {"jintarrayBasicAccess", "([II)I", (void *) jintarrayBasicAccess}, +}; + +static int registerNativeMethods(JNIEnv* env, const char* className, + const JNINativeMethod* gMethods, int numMethods) { + jclass clazz = env->FindClass(className); + if (clazz == NULL) { + return JNI_FALSE; + } + if (env->RegisterNatives(clazz, gMethods, numMethods) < 0) { + return JNI_FALSE; + } + return JNI_TRUE; +} + +jint JNI_OnLoad(JavaVM* jvm, void*) { + JNIEnv *env = NULL; + if (jvm->GetEnv((void**) &env, JNI_VERSION_1_6)) { + return JNI_ERR; + } + + if (registerNativeMethods(env, "java/lang/perftests/SystemPerfTest", + sMethods, NELEM(sMethods)) == -1) { + return JNI_ERR; + } + + return JNI_VERSION_1_6; +} diff --git a/apct-tests/perftests/core/src/java/lang/perftests/SystemPerfTest.java b/apct-tests/perftests/core/src/java/lang/perftests/SystemPerfTest.java index 1e9c49cf702a7..afc9d0cf10e1a 100644 --- a/apct-tests/perftests/core/src/java/lang/perftests/SystemPerfTest.java +++ b/apct-tests/perftests/core/src/java/lang/perftests/SystemPerfTest.java @@ -20,6 +20,9 @@ import android.perftests.utils.BenchmarkState; import android.perftests.utils.PerfStatusReporter; import android.support.test.filters.LargeTest; import android.support.test.runner.AndroidJUnit4; + +import dalvik.annotation.optimization.FastNative; + import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; @@ -29,6 +32,11 @@ import java.util.concurrent.TimeUnit; @RunWith(AndroidJUnit4.class) @LargeTest public class SystemPerfTest { + + static { + System.loadLibrary("perftestscore_jni"); + } + @Rule public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter(); @@ -60,4 +68,49 @@ public class SystemPerfTest { state.resumeTiming(); } } + + @Test + public void testJniArrayNoop() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + final int[] data = new int[450]; + while (state.keepRunning()) { + jintarrayArgumentNoop(data, data.length); + } + } + + @Test + public void testJniArrayGetLength() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + final int[] data = new int[450]; + while (state.keepRunning()) { + jintarrayGetLength(data); + } + } + + @Test + public void testJniArrayCriticalAccess() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + final int[] data = new int[450]; + while (state.keepRunning()) { + jintarrayCriticalAccess(data, 50); + } + } + + @Test + public void testJniArrayBasicAccess() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + final int[] data = new int[450]; + while (state.keepRunning()) { + jintarrayBasicAccess(data, 50); + } + } + + @FastNative + private static native void jintarrayArgumentNoop(int[] array, int length); + @FastNative + private static native int jintarrayGetLength(int[] array); + @FastNative + private static native int jintarrayCriticalAccess(int[] array, int index); + @FastNative + private static native int jintarrayBasicAccess(int[] array, int index); }