1 /*
2  * Copyright (C) 2017 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 <inttypes.h>
18 
19 #include "android-base/logging.h"
20 #include "android-base/stringprintf.h"
21 
22 #include "jni.h"
23 #include "jvmti.h"
24 
25 // Test infrastructure
26 #include "jni_helper.h"
27 #include "jvmti_helper.h"
28 #include "test_env.h"
29 #include "ti_macros.h"
30 
31 namespace art {
32 namespace Test926Timers {
33 
Java_art_Test927_getAvailableProcessors(JNIEnv * env,jclass Main_klass ATTRIBUTE_UNUSED)34 extern "C" JNIEXPORT jint JNICALL Java_art_Test927_getAvailableProcessors(
35     JNIEnv* env, jclass Main_klass ATTRIBUTE_UNUSED) {
36   jint count;
37   jvmtiError result = jvmti_env->GetAvailableProcessors(&count);
38   if (JvmtiErrorToException(env, jvmti_env, result)) {
39     return -1;
40   }
41   return count;
42 }
43 
Java_art_Test927_getTime(JNIEnv * env,jclass Main_klass ATTRIBUTE_UNUSED)44 extern "C" JNIEXPORT jlong JNICALL Java_art_Test927_getTime(
45     JNIEnv* env, jclass Main_klass ATTRIBUTE_UNUSED) {
46   jlong time;
47   jvmtiError result = jvmti_env->GetTime(&time);
48   if (JvmtiErrorToException(env, jvmti_env, result)) {
49     return -1;
50   }
51   return time;
52 }
53 
Java_art_Test927_getTimerInfo(JNIEnv * env,jclass Main_klass ATTRIBUTE_UNUSED)54 extern "C" JNIEXPORT jobjectArray JNICALL Java_art_Test927_getTimerInfo(
55     JNIEnv* env, jclass Main_klass ATTRIBUTE_UNUSED) {
56   jvmtiTimerInfo info;
57   jvmtiError result = jvmti_env->GetTimerInfo(&info);
58   if (JvmtiErrorToException(env, jvmti_env, result)) {
59     return nullptr;
60   }
61 
62   auto callback = [&](jint index) -> jobject {
63     switch (index) {
64       // Max value.
65       case 0:
66         return env->NewStringUTF(android::base::StringPrintf("%" PRId64, info.max_value).c_str());
67 
68       // Skip forward.
69       case 1:
70         return env->NewStringUTF(info.may_skip_forward == JNI_TRUE ? "true" : "false");
71       // Skip backward.
72       case 2:
73         return env->NewStringUTF(info.may_skip_forward == JNI_TRUE ? "true" : "false");
74 
75       // The kind.
76       case 3:
77         return env->NewStringUTF(
78             android::base::StringPrintf("%d", static_cast<jint>(info.kind)).c_str());
79     }
80     LOG(FATAL) << "Should not reach here";
81     UNREACHABLE();
82   };
83   return CreateObjectArray(env, 4, "java/lang/Object", callback);
84 }
85 
86 }  // namespace Test926Timers
87 }  // namespace art
88