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 | |
| 20 | #include "base/macros.h" |
| 21 | #include "jni.h" |
Andreas Gampe | 5e03a30 | 2017-03-13 13:10:00 -0700 | [diff] [blame] | 22 | #include "jvmti.h" |
Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame^] | 23 | |
| 24 | // Test infrastructure |
| 25 | #include "jvmti_helper.h" |
| 26 | #include "test_env.h" |
Andreas Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame] | 27 | |
| 28 | namespace art { |
| 29 | namespace Test908GcStartFinish { |
| 30 | |
| 31 | static size_t starts = 0; |
| 32 | static size_t finishes = 0; |
| 33 | |
| 34 | static void JNICALL GarbageCollectionFinish(jvmtiEnv* ti_env ATTRIBUTE_UNUSED) { |
| 35 | finishes++; |
| 36 | } |
| 37 | |
| 38 | static void JNICALL GarbageCollectionStart(jvmtiEnv* ti_env ATTRIBUTE_UNUSED) { |
| 39 | starts++; |
| 40 | } |
| 41 | |
| 42 | extern "C" JNIEXPORT void JNICALL Java_Main_setupGcCallback( |
Andreas Gampe | 4934eb1 | 2017-01-30 13:15:26 -0800 | [diff] [blame] | 43 | JNIEnv* env, jclass klass ATTRIBUTE_UNUSED) { |
Andreas Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame] | 44 | 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 Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame^] | 50 | JvmtiErrorToException(env, jvmti_env, ret); |
Andreas Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Andreas Gampe | 4934eb1 | 2017-01-30 13:15:26 -0800 | [diff] [blame] | 53 | extern "C" JNIEXPORT void JNICALL Java_Main_enableGcTracking(JNIEnv* env, |
Andreas Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame] | 54 | 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 Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame^] | 60 | if (JvmtiErrorToException(env, jvmti_env, ret)) { |
Andreas Gampe | 4934eb1 | 2017-01-30 13:15:26 -0800 | [diff] [blame] | 61 | return; |
Andreas Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame] | 62 | } |
| 63 | ret = jvmti_env->SetEventNotificationMode( |
| 64 | enable ? JVMTI_ENABLE : JVMTI_DISABLE, |
| 65 | JVMTI_EVENT_GARBAGE_COLLECTION_FINISH, |
| 66 | nullptr); |
Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame^] | 67 | if (JvmtiErrorToException(env, jvmti_env, ret)) { |
Andreas Gampe | 4934eb1 | 2017-01-30 13:15:26 -0800 | [diff] [blame] | 68 | return; |
Andreas Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | |
| 72 | extern "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 | |
| 79 | extern "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 Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame] | 86 | } // namespace Test908GcStartFinish |
| 87 | } // namespace art |