gc_callbacks.cc revision 4934eb1845a2b2535ebe086e906b45535a695a13
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 17#include <stdio.h> 18#include <string.h> 19 20#include "base/macros.h" 21#include "jni.h" 22#include "openjdkjvmti/jvmti.h" 23#include "ti-agent/common_helper.h" 24#include "ti-agent/common_load.h" 25 26namespace art { 27namespace Test908GcStartFinish { 28 29static size_t starts = 0; 30static size_t finishes = 0; 31 32static void JNICALL GarbageCollectionFinish(jvmtiEnv* ti_env ATTRIBUTE_UNUSED) { 33 finishes++; 34} 35 36static void JNICALL GarbageCollectionStart(jvmtiEnv* ti_env ATTRIBUTE_UNUSED) { 37 starts++; 38} 39 40extern "C" JNIEXPORT void JNICALL Java_Main_setupGcCallback( 41 JNIEnv* env, 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 JvmtiErrorToException(env, ret); 49} 50 51extern "C" JNIEXPORT void JNICALL Java_Main_enableGcTracking(JNIEnv* env, 52 jclass klass ATTRIBUTE_UNUSED, 53 jboolean enable) { 54 jvmtiError ret = jvmti_env->SetEventNotificationMode( 55 enable ? JVMTI_ENABLE : JVMTI_DISABLE, 56 JVMTI_EVENT_GARBAGE_COLLECTION_START, 57 nullptr); 58 if (JvmtiErrorToException(env, ret)) { 59 return; 60 } 61 ret = jvmti_env->SetEventNotificationMode( 62 enable ? JVMTI_ENABLE : JVMTI_DISABLE, 63 JVMTI_EVENT_GARBAGE_COLLECTION_FINISH, 64 nullptr); 65 if (JvmtiErrorToException(env, ret)) { 66 return; 67 } 68} 69 70extern "C" JNIEXPORT jint JNICALL Java_Main_getGcStarts(JNIEnv* env ATTRIBUTE_UNUSED, 71 jclass klass ATTRIBUTE_UNUSED) { 72 jint result = static_cast<jint>(starts); 73 starts = 0; 74 return result; 75} 76 77extern "C" JNIEXPORT jint JNICALL Java_Main_getGcFinishes(JNIEnv* env ATTRIBUTE_UNUSED, 78 jclass klass ATTRIBUTE_UNUSED) { 79 jint result = static_cast<jint>(finishes); 80 finishes = 0; 81 return result; 82} 83 84} // namespace Test908GcStartFinish 85} // namespace art 86