blob: 4b9a23cedea68524f7ec556c143059c5200ae692 [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
20#include "base/macros.h"
21#include "jni.h"
Andreas Gampe5e03a302017-03-13 13:10:00 -070022#include "jvmti.h"
Andreas Gampe3f46c962017-03-30 10:26:59 -070023
24// Test infrastructure
25#include "jvmti_helper.h"
26#include "test_env.h"
Andreas Gampe9b8c5882016-10-21 15:27:46 -070027
28namespace art {
29namespace Test908GcStartFinish {
30
31static size_t starts = 0;
32static size_t finishes = 0;
33
34static void JNICALL GarbageCollectionFinish(jvmtiEnv* ti_env ATTRIBUTE_UNUSED) {
35 finishes++;
36}
37
38static void JNICALL GarbageCollectionStart(jvmtiEnv* ti_env ATTRIBUTE_UNUSED) {
39 starts++;
40}
41
42extern "C" JNIEXPORT void JNICALL Java_Main_setupGcCallback(
Andreas Gampe4934eb12017-01-30 13:15:26 -080043 JNIEnv* env, jclass klass ATTRIBUTE_UNUSED) {
Andreas Gampe9b8c5882016-10-21 15:27:46 -070044 jvmtiEventCallbacks callbacks;
45 memset(&callbacks, 0, sizeof(jvmtiEventCallbacks));
46 callbacks.GarbageCollectionFinish = GarbageCollectionFinish;
47 callbacks.GarbageCollectionStart = GarbageCollectionStart;
48
49 jvmtiError ret = jvmti_env->SetEventCallbacks(&callbacks, sizeof(callbacks));
Andreas Gampe3f46c962017-03-30 10:26:59 -070050 JvmtiErrorToException(env, jvmti_env, ret);
Andreas Gampe9b8c5882016-10-21 15:27:46 -070051}
52
Andreas Gampe4934eb12017-01-30 13:15:26 -080053extern "C" JNIEXPORT void JNICALL Java_Main_enableGcTracking(JNIEnv* env,
Andreas Gampe9b8c5882016-10-21 15:27:46 -070054 jclass klass ATTRIBUTE_UNUSED,
55 jboolean enable) {
56 jvmtiError ret = jvmti_env->SetEventNotificationMode(
57 enable ? JVMTI_ENABLE : JVMTI_DISABLE,
58 JVMTI_EVENT_GARBAGE_COLLECTION_START,
59 nullptr);
Andreas Gampe3f46c962017-03-30 10:26:59 -070060 if (JvmtiErrorToException(env, jvmti_env, ret)) {
Andreas Gampe4934eb12017-01-30 13:15:26 -080061 return;
Andreas Gampe9b8c5882016-10-21 15:27:46 -070062 }
63 ret = jvmti_env->SetEventNotificationMode(
64 enable ? JVMTI_ENABLE : JVMTI_DISABLE,
65 JVMTI_EVENT_GARBAGE_COLLECTION_FINISH,
66 nullptr);
Andreas Gampe3f46c962017-03-30 10:26:59 -070067 if (JvmtiErrorToException(env, jvmti_env, ret)) {
Andreas Gampe4934eb12017-01-30 13:15:26 -080068 return;
Andreas Gampe9b8c5882016-10-21 15:27:46 -070069 }
70}
71
72extern "C" JNIEXPORT jint JNICALL Java_Main_getGcStarts(JNIEnv* env ATTRIBUTE_UNUSED,
73 jclass klass ATTRIBUTE_UNUSED) {
74 jint result = static_cast<jint>(starts);
75 starts = 0;
76 return result;
77}
78
79extern "C" JNIEXPORT jint JNICALL Java_Main_getGcFinishes(JNIEnv* env ATTRIBUTE_UNUSED,
80 jclass klass ATTRIBUTE_UNUSED) {
81 jint result = static_cast<jint>(finishes);
82 finishes = 0;
83 return result;
84}
85
Andreas Gampe9b8c5882016-10-21 15:27:46 -070086} // namespace Test908GcStartFinish
87} // namespace art