Andreas Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame] | 1 | /* |
| 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 Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame] | 17 | #include <stdio.h> |
| 18 | #include <string.h> |
| 19 | |
Andreas Gampe | 027444b | 2017-03-31 12:49:07 -0700 | [diff] [blame] | 20 | #include "android-base/macros.h" |
| 21 | |
Andreas Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame] | 22 | #include "jni.h" |
Andreas Gampe | 5e03a30 | 2017-03-13 13:10:00 -0700 | [diff] [blame] | 23 | #include "jvmti.h" |
Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame] | 24 | |
| 25 | // Test infrastructure |
| 26 | #include "jvmti_helper.h" |
| 27 | #include "test_env.h" |
Andreas Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame] | 28 | |
| 29 | namespace art { |
| 30 | namespace Test908GcStartFinish { |
| 31 | |
| 32 | static size_t starts = 0; |
| 33 | static size_t finishes = 0; |
| 34 | |
| 35 | static void JNICALL GarbageCollectionFinish(jvmtiEnv* ti_env ATTRIBUTE_UNUSED) { |
| 36 | finishes++; |
| 37 | } |
| 38 | |
| 39 | static void JNICALL GarbageCollectionStart(jvmtiEnv* ti_env ATTRIBUTE_UNUSED) { |
| 40 | starts++; |
| 41 | } |
| 42 | |
Andreas Gampe | 4665167 | 2017-04-07 09:00:04 -0700 | [diff] [blame] | 43 | extern "C" JNIEXPORT void JNICALL Java_art_Test908_setupGcCallback( |
Andreas Gampe | 4934eb1 | 2017-01-30 13:15:26 -0800 | [diff] [blame] | 44 | JNIEnv* env, jclass klass ATTRIBUTE_UNUSED) { |
Andreas Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame] | 45 | 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 Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame] | 51 | JvmtiErrorToException(env, jvmti_env, ret); |
Andreas Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Andreas Gampe | 4665167 | 2017-04-07 09:00:04 -0700 | [diff] [blame] | 54 | extern "C" JNIEXPORT void JNICALL Java_art_Test908_enableGcTracking(JNIEnv* env, |
| 55 | jclass klass ATTRIBUTE_UNUSED, |
| 56 | jboolean enable) { |
Andreas Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame] | 57 | jvmtiError ret = jvmti_env->SetEventNotificationMode( |
| 58 | enable ? JVMTI_ENABLE : JVMTI_DISABLE, |
| 59 | JVMTI_EVENT_GARBAGE_COLLECTION_START, |
| 60 | nullptr); |
Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame] | 61 | if (JvmtiErrorToException(env, jvmti_env, ret)) { |
Andreas Gampe | 4934eb1 | 2017-01-30 13:15:26 -0800 | [diff] [blame] | 62 | return; |
Andreas Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame] | 63 | } |
| 64 | ret = jvmti_env->SetEventNotificationMode( |
| 65 | enable ? JVMTI_ENABLE : JVMTI_DISABLE, |
| 66 | JVMTI_EVENT_GARBAGE_COLLECTION_FINISH, |
| 67 | nullptr); |
Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame] | 68 | if (JvmtiErrorToException(env, jvmti_env, ret)) { |
Andreas Gampe | 4934eb1 | 2017-01-30 13:15:26 -0800 | [diff] [blame] | 69 | return; |
Andreas Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | |
Andreas Gampe | 4665167 | 2017-04-07 09:00:04 -0700 | [diff] [blame] | 73 | extern "C" JNIEXPORT jint JNICALL Java_art_Test908_getGcStarts(JNIEnv* env ATTRIBUTE_UNUSED, |
| 74 | jclass klass ATTRIBUTE_UNUSED) { |
Andreas Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame] | 75 | jint result = static_cast<jint>(starts); |
| 76 | starts = 0; |
| 77 | return result; |
| 78 | } |
| 79 | |
Andreas Gampe | 4665167 | 2017-04-07 09:00:04 -0700 | [diff] [blame] | 80 | extern "C" JNIEXPORT jint JNICALL Java_art_Test908_getGcFinishes(JNIEnv* env ATTRIBUTE_UNUSED, |
| 81 | jclass klass ATTRIBUTE_UNUSED) { |
Andreas Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame] | 82 | jint result = static_cast<jint>(finishes); |
| 83 | finishes = 0; |
| 84 | return result; |
| 85 | } |
| 86 | |
Andreas Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame] | 87 | } // namespace Test908GcStartFinish |
| 88 | } // namespace art |