1 /*
2  * Copyright 2023 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 #include <android/log.h>
19 
20 #define LOG(MSG) __android_log_write(ANDROID_LOG_DEBUG, "BENCHMARK", MSG)
21 
22 extern "C" {
23 
consumeByte(__unused jbyte value)24 static void JNICALL consumeByte(__unused jbyte value) {}
consumeShort(__unused jshort value)25 static void JNICALL consumeShort(__unused jshort value) {}
consumeInt(__unused jint value)26 static void JNICALL consumeInt(__unused jint value) {}
consumeLong(__unused jlong value)27 static void JNICALL consumeLong(__unused jlong value) {}
consumeFloat(__unused jfloat value)28 static void JNICALL consumeFloat(__unused jfloat value) {}
consumeDouble(__unused jdouble value)29 static void JNICALL consumeDouble(__unused jdouble value) {}
consumeBoolean(__unused jboolean value)30 static void JNICALL consumeBoolean(__unused jboolean value) {}
consumeChar(__unused jchar value)31 static void JNICALL consumeChar(__unused jchar value) {}
consumeObject(JNIEnv * env,__unused jclass clazz,__unused jobject value)32 static void JNICALL consumeObject(
33         JNIEnv *env,
34         __unused jclass clazz,
35         __unused jobject value
36 ) {}
37 
38 }
39 
40 static JNINativeMethod sMethods[] = {
41         {"consume",
42                 "(B)V", // byte
43                 reinterpret_cast<void *>(consumeByte)
44         },
45         {"consume",
46                 "(S)V", // short
47                 reinterpret_cast<void *>(consumeShort)
48         },
49         {"consume",
50                 "(I)V", // int
51                 reinterpret_cast<void *>(consumeInt)
52         },
53         {"consume",
54                 "(J)V", // long
55                 reinterpret_cast<void *>(consumeLong)
56         },
57         {"consume",
58                 "(F)V", // float
59                 reinterpret_cast<void *>(consumeFloat)
60         },
61         {"consume",
62                 "(D)V", // double
63                 reinterpret_cast<void *>(consumeDouble)
64         },
65         {"consume",
66                 "(Z)V", // boolean
67                 reinterpret_cast<void *>(consumeBoolean)
68         },
69         {"consume",
70                 "(C)V", // char
71                 reinterpret_cast<void *>(consumeChar)
72         },
73         {"consume",
74                 "(Ljava/lang/Object;)V",
75                 reinterpret_cast<void *>(consumeObject)
76         },
77 };
78 
JNI_OnLoad(JavaVM * vm,void *)79 jint JNI_OnLoad(JavaVM *vm, void * /* reserved */) {
80     JNIEnv *env = nullptr;
81     if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6)) {
82         LOG("JNI_OnLoad failure when trying to register native methods for BlackHole.");
83         return JNI_ERR;
84     }
85     jclass clazz = env->FindClass("androidx/benchmark/BlackHole");
86     if (clazz == nullptr) {
87         LOG("Cannot find BlackHole class when trying to register native methods.");
88         return JNI_ERR;
89     }
90 
91     const int methodCount = sizeof(sMethods) / sizeof(sMethods[0]);
92     int result = env->RegisterNatives(clazz, sMethods, methodCount);
93     env->DeleteLocalRef(clazz);
94 
95     if (result != 0) {
96         LOG("Failure when trying to call RegisterNatives to register native BlackHole methods.");
97         return JNI_ERR;
98     }
99 
100     return JNI_VERSION_1_6;
101 }