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" |
| 22 | #include "openjdkjvmti/jvmti.h" |
Alex Light | e657424 | 2016-08-17 09:56:24 -0700 | [diff] [blame] | 23 | #include "ti-agent/common_helper.h" |
Andreas Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame] | 24 | #include "ti-agent/common_load.h" |
| 25 | |
| 26 | namespace art { |
| 27 | namespace Test908GcStartFinish { |
| 28 | |
| 29 | static size_t starts = 0; |
| 30 | static size_t finishes = 0; |
| 31 | |
| 32 | static void JNICALL GarbageCollectionFinish(jvmtiEnv* ti_env ATTRIBUTE_UNUSED) { |
| 33 | finishes++; |
| 34 | } |
| 35 | |
| 36 | static void JNICALL GarbageCollectionStart(jvmtiEnv* ti_env ATTRIBUTE_UNUSED) { |
| 37 | starts++; |
| 38 | } |
| 39 | |
| 40 | extern "C" JNIEXPORT void JNICALL Java_Main_setupGcCallback( |
| 41 | JNIEnv* env ATTRIBUTE_UNUSED, jclass klass ATTRIBUTE_UNUSED) { |
| 42 | jvmtiEventCallbacks callbacks; |
| 43 | memset(&callbacks, 0, sizeof(jvmtiEventCallbacks)); |
| 44 | callbacks.GarbageCollectionFinish = GarbageCollectionFinish; |
| 45 | callbacks.GarbageCollectionStart = GarbageCollectionStart; |
| 46 | |
| 47 | jvmtiError ret = jvmti_env->SetEventCallbacks(&callbacks, sizeof(callbacks)); |
| 48 | if (ret != JVMTI_ERROR_NONE) { |
| 49 | char* err; |
| 50 | jvmti_env->GetErrorName(ret, &err); |
| 51 | printf("Error setting callbacks: %s\n", err); |
Alex Light | 4196071 | 2017-01-06 14:44:23 -0800 | [diff] [blame] | 52 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err)); |
Andreas Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame] | 53 | } |
| 54 | } |
| 55 | |
| 56 | extern "C" JNIEXPORT void JNICALL Java_Main_enableGcTracking(JNIEnv* env ATTRIBUTE_UNUSED, |
| 57 | jclass klass ATTRIBUTE_UNUSED, |
| 58 | jboolean enable) { |
| 59 | jvmtiError ret = jvmti_env->SetEventNotificationMode( |
| 60 | enable ? JVMTI_ENABLE : JVMTI_DISABLE, |
| 61 | JVMTI_EVENT_GARBAGE_COLLECTION_START, |
| 62 | nullptr); |
| 63 | if (ret != JVMTI_ERROR_NONE) { |
| 64 | char* err; |
| 65 | jvmti_env->GetErrorName(ret, &err); |
| 66 | printf("Error enabling/disabling gc callbacks: %s\n", err); |
Alex Light | 4196071 | 2017-01-06 14:44:23 -0800 | [diff] [blame] | 67 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err)); |
Andreas Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame] | 68 | } |
| 69 | ret = jvmti_env->SetEventNotificationMode( |
| 70 | enable ? JVMTI_ENABLE : JVMTI_DISABLE, |
| 71 | JVMTI_EVENT_GARBAGE_COLLECTION_FINISH, |
| 72 | nullptr); |
| 73 | if (ret != JVMTI_ERROR_NONE) { |
| 74 | char* err; |
| 75 | jvmti_env->GetErrorName(ret, &err); |
| 76 | printf("Error enabling/disabling gc callbacks: %s\n", err); |
Alex Light | 4196071 | 2017-01-06 14:44:23 -0800 | [diff] [blame] | 77 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err)); |
Andreas Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
| 81 | extern "C" JNIEXPORT jint JNICALL Java_Main_getGcStarts(JNIEnv* env ATTRIBUTE_UNUSED, |
| 82 | jclass klass ATTRIBUTE_UNUSED) { |
| 83 | jint result = static_cast<jint>(starts); |
| 84 | starts = 0; |
| 85 | return result; |
| 86 | } |
| 87 | |
| 88 | extern "C" JNIEXPORT jint JNICALL Java_Main_getGcFinishes(JNIEnv* env ATTRIBUTE_UNUSED, |
| 89 | jclass klass ATTRIBUTE_UNUSED) { |
| 90 | jint result = static_cast<jint>(finishes); |
| 91 | finishes = 0; |
| 92 | return result; |
| 93 | } |
| 94 | |
Andreas Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame] | 95 | } // namespace Test908GcStartFinish |
| 96 | } // namespace art |