135bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe/*
235bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe * Copyright (C) 2017 The Android Open Source Project
335bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe *
435bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe * Licensed under the Apache License, Version 2.0 (the "License");
535bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe * you may not use this file except in compliance with the License.
635bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe * You may obtain a copy of the License at
735bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe *
835bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe *      http://www.apache.org/licenses/LICENSE-2.0
935bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe *
1035bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe * Unless required by applicable law or agreed to in writing, software
1135bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe * distributed under the License is distributed on an "AS IS" BASIS,
1235bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1335bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe * See the License for the specific language governing permissions and
1435bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe * limitations under the License.
1535bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe */
1635bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe
1735bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe#include <inttypes.h>
1835bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe
19027444b64dd52e1d2beea7aa525fbb8146a516bcAndreas Gampe#include "android-base/logging.h"
2035bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe#include "android-base/stringprintf.h"
21027444b64dd52e1d2beea7aa525fbb8146a516bcAndreas Gampe
2235bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe#include "jni.h"
235e03a305edafb49a34b436d9a858281e53d390f2Andreas Gampe#include "jvmti.h"
2435bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe
253f46c96568bef650ba6d9ce6ac8835d30877f243Andreas Gampe// Test infrastructure
263f46c96568bef650ba6d9ce6ac8835d30877f243Andreas Gampe#include "jni_helper.h"
273f46c96568bef650ba6d9ce6ac8835d30877f243Andreas Gampe#include "jvmti_helper.h"
283f46c96568bef650ba6d9ce6ac8835d30877f243Andreas Gampe#include "test_env.h"
29027444b64dd52e1d2beea7aa525fbb8146a516bcAndreas Gampe#include "ti_macros.h"
3035bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe
3135bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampenamespace art {
3235bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampenamespace Test926Timers {
3335bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe
344665167ddc34008dfa78a2873685fe7a98772eabAndreas Gampeextern "C" JNIEXPORT jint JNICALL Java_art_Test927_getAvailableProcessors(
3535bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe    JNIEnv* env, jclass Main_klass ATTRIBUTE_UNUSED) {
3635bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe  jint count;
3735bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe  jvmtiError result = jvmti_env->GetAvailableProcessors(&count);
383f46c96568bef650ba6d9ce6ac8835d30877f243Andreas Gampe  if (JvmtiErrorToException(env, jvmti_env, result)) {
3935bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe    return -1;
4035bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe  }
4135bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe  return count;
4235bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe}
4335bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe
444665167ddc34008dfa78a2873685fe7a98772eabAndreas Gampeextern "C" JNIEXPORT jlong JNICALL Java_art_Test927_getTime(
4535bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe    JNIEnv* env, jclass Main_klass ATTRIBUTE_UNUSED) {
4635bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe  jlong time;
4735bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe  jvmtiError result = jvmti_env->GetTime(&time);
483f46c96568bef650ba6d9ce6ac8835d30877f243Andreas Gampe  if (JvmtiErrorToException(env, jvmti_env, result)) {
4935bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe    return -1;
5035bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe  }
5135bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe  return time;
5235bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe}
5335bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe
544665167ddc34008dfa78a2873685fe7a98772eabAndreas Gampeextern "C" JNIEXPORT jobjectArray JNICALL Java_art_Test927_getTimerInfo(
5535bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe    JNIEnv* env, jclass Main_klass ATTRIBUTE_UNUSED) {
5635bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe  jvmtiTimerInfo info;
5735bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe  jvmtiError result = jvmti_env->GetTimerInfo(&info);
583f46c96568bef650ba6d9ce6ac8835d30877f243Andreas Gampe  if (JvmtiErrorToException(env, jvmti_env, result)) {
5935bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe    return nullptr;
6035bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe  }
6135bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe
6235bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe  auto callback = [&](jint index) -> jobject {
6335bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe    switch (index) {
6435bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe      // Max value.
6535bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe      case 0:
6635bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe        return env->NewStringUTF(android::base::StringPrintf("%" PRId64, info.max_value).c_str());
6735bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe
6835bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe      // Skip forward.
6935bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe      case 1:
7035bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe        return env->NewStringUTF(info.may_skip_forward == JNI_TRUE ? "true" : "false");
7135bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe      // Skip backward.
7235bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe      case 2:
7335bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe        return env->NewStringUTF(info.may_skip_forward == JNI_TRUE ? "true" : "false");
7435bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe
7535bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe      // The kind.
7635bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe      case 3:
7735bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe        return env->NewStringUTF(
7835bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe            android::base::StringPrintf("%d", static_cast<jint>(info.kind)).c_str());
7935bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe    }
8035bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe    LOG(FATAL) << "Should not reach here";
8135bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe    UNREACHABLE();
8235bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe  };
8335bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe  return CreateObjectArray(env, 4, "java/lang/Object", callback);
8435bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe}
8535bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe
8635bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe}  // namespace Test926Timers
8735bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe}  // namespace art
88