blob: ddd2ba7734c6b7cbc6a6631dfd3e2772b697ec9f [file] [log] [blame]
Andreas Gampe9b8c5882016-10-21 15:27:46 -07001/*
2 * Copyright (C) 2013 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
Andreas Gampe9b8c5882016-10-21 15:27:46 -070017#include <stdio.h>
18#include <string.h>
19
Andreas Gampe027444b2017-03-31 12:49:07 -070020#include "android-base/macros.h"
21
Andreas Gampe9b8c5882016-10-21 15:27:46 -070022#include "jni.h"
Andreas Gampe5e03a302017-03-13 13:10:00 -070023#include "jvmti.h"
Andreas Gampe3f46c962017-03-30 10:26:59 -070024
25// Test infrastructure
26#include "jvmti_helper.h"
27#include "test_env.h"
Andreas Gampe9b8c5882016-10-21 15:27:46 -070028
29namespace art {
30namespace Test908GcStartFinish {
31
32static size_t starts = 0;
33static size_t finishes = 0;
34
35static void JNICALL GarbageCollectionFinish(jvmtiEnv* ti_env ATTRIBUTE_UNUSED) {
36 finishes++;
37}
38
39static void JNICALL GarbageCollectionStart(jvmtiEnv* ti_env ATTRIBUTE_UNUSED) {
40 starts++;
41}
42
Andreas Gampe46651672017-04-07 09:00:04 -070043extern "C" JNIEXPORT void JNICALL Java_art_Test908_setupGcCallback(
Andreas Gampe4934eb12017-01-30 13:15:26 -080044 JNIEnv* env, jclass klass ATTRIBUTE_UNUSED) {
Andreas Gampe9b8c5882016-10-21 15:27:46 -070045 jvmtiEventCallbacks callbacks;
46 memset(&callbacks, 0, sizeof(jvmtiEventCallbacks));
47 callbacks.GarbageCollectionFinish = GarbageCollectionFinish;
48 callbacks.GarbageCollectionStart = GarbageCollectionStart;
49
50 jvmtiError ret = jvmti_env->SetEventCallbacks(&callbacks, sizeof(callbacks));
Andreas Gampe3f46c962017-03-30 10:26:59 -070051 JvmtiErrorToException(env, jvmti_env, ret);
Andreas Gampe9b8c5882016-10-21 15:27:46 -070052}
53
Andreas Gampe46651672017-04-07 09:00:04 -070054extern "C" JNIEXPORT void JNICALL Java_art_Test908_enableGcTracking(JNIEnv* env,
55 jclass klass ATTRIBUTE_UNUSED,
56 jboolean enable) {
Andreas Gampe9b8c5882016-10-21 15:27:46 -070057 jvmtiError ret = jvmti_env->SetEventNotificationMode(
58 enable ? JVMTI_ENABLE : JVMTI_DISABLE,
59 JVMTI_EVENT_GARBAGE_COLLECTION_START,
60 nullptr);
Andreas Gampe3f46c962017-03-30 10:26:59 -070061 if (JvmtiErrorToException(env, jvmti_env, ret)) {
Andreas Gampe4934eb12017-01-30 13:15:26 -080062 return;
Andreas Gampe9b8c5882016-10-21 15:27:46 -070063 }
64 ret = jvmti_env->SetEventNotificationMode(
65 enable ? JVMTI_ENABLE : JVMTI_DISABLE,
66 JVMTI_EVENT_GARBAGE_COLLECTION_FINISH,
67 nullptr);
Andreas Gampe3f46c962017-03-30 10:26:59 -070068 if (JvmtiErrorToException(env, jvmti_env, ret)) {
Andreas Gampe4934eb12017-01-30 13:15:26 -080069 return;
Andreas Gampe9b8c5882016-10-21 15:27:46 -070070 }
71}
72
Andreas Gampe46651672017-04-07 09:00:04 -070073extern "C" JNIEXPORT jint JNICALL Java_art_Test908_getGcStarts(JNIEnv* env ATTRIBUTE_UNUSED,
74 jclass klass ATTRIBUTE_UNUSED) {
Andreas Gampe9b8c5882016-10-21 15:27:46 -070075 jint result = static_cast<jint>(starts);
76 starts = 0;
77 return result;
78}
79
Andreas Gampe46651672017-04-07 09:00:04 -070080extern "C" JNIEXPORT jint JNICALL Java_art_Test908_getGcFinishes(JNIEnv* env ATTRIBUTE_UNUSED,
81 jclass klass ATTRIBUTE_UNUSED) {
Andreas Gampe9b8c5882016-10-21 15:27:46 -070082 jint result = static_cast<jint>(finishes);
83 finishes = 0;
84 return result;
85}
86
Andreas Gampe9b8c5882016-10-21 15:27:46 -070087} // namespace Test908GcStartFinish
88} // namespace art