1 /*
2  * Copyright 2018 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/trace.h>
19 
isEnabled(JNIEnv *,jclass)20 static jboolean isEnabled(JNIEnv*, jclass) {
21     return ATrace_isEnabled();
22 }
23 
beginEndSection(JNIEnv *,jclass)24 static void beginEndSection(JNIEnv*, jclass) {
25     ATrace_beginSection("ndk::beginEndSection");
26     ATrace_endSection();
27 }
28 
asyncBeginEndSection(JNIEnv *,jclass)29 static void asyncBeginEndSection(JNIEnv*, jclass) {
30     ATrace_beginAsyncSection("ndk::asyncBeginEndSection", 4770);
31     ATrace_endAsyncSection("ndk::asyncBeginEndSection", 4770);
32 }
33 
34 
counter(JNIEnv *,jclass)35 static void counter(JNIEnv*, jclass) {
36     ATrace_setCounter("ndk::counter", 10);
37     ATrace_setCounter("ndk::counter", 20);
38     ATrace_setCounter("ndk::counter", 30);
39     ATrace_setCounter("ndk::counter", 9223372000000005807L);
40 }
41 
42 static JNINativeMethod gMethods[] = {
43     { "isEnabled", "()Z", (void*) isEnabled },
44     { "beginEndSection", "()V", (void*) beginEndSection },
45     { "asyncBeginEndSection", "()V", (void*) asyncBeginEndSection },
46     { "counter", "()V", (void*) counter },
47 };
48 
JNI_OnLoad(JavaVM * vm,void *)49 jint JNI_OnLoad(JavaVM* vm, void* /*reserved*/) {
50     JNIEnv* env = nullptr;
51     if (vm->GetEnv((void**)&env, JNI_VERSION_1_4) != JNI_OK) {
52         return JNI_ERR;
53     }
54     jclass clazz = env->FindClass("com/android/cts/atracetestapp/AtraceNdkMethods");
55     env->RegisterNatives(clazz, gMethods, sizeof(gMethods) / sizeof(JNINativeMethod));
56     return JNI_VERSION_1_4;
57 }
58