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 | |
| 17 | #include "gc_callbacks.h" |
| 18 | |
| 19 | #include <stdio.h> |
| 20 | #include <string.h> |
| 21 | |
| 22 | #include "base/macros.h" |
| 23 | #include "jni.h" |
| 24 | #include "openjdkjvmti/jvmti.h" |
Alex Light | e657424 | 2016-08-17 09:56:24 -0700 | [diff] [blame] | 25 | #include "ti-agent/common_helper.h" |
Andreas Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame] | 26 | #include "ti-agent/common_load.h" |
| 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( |
| 43 | JNIEnv* env ATTRIBUTE_UNUSED, jclass klass ATTRIBUTE_UNUSED) { |
| 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)); |
| 50 | if (ret != JVMTI_ERROR_NONE) { |
| 51 | char* err; |
| 52 | jvmti_env->GetErrorName(ret, &err); |
| 53 | printf("Error setting callbacks: %s\n", err); |
Alex Light | 4196071 | 2017-01-06 14:44:23 -0800 | [diff] [blame^] | 54 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err)); |
Andreas Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame] | 55 | } |
| 56 | } |
| 57 | |
| 58 | extern "C" JNIEXPORT void JNICALL Java_Main_enableGcTracking(JNIEnv* env ATTRIBUTE_UNUSED, |
| 59 | jclass klass ATTRIBUTE_UNUSED, |
| 60 | jboolean enable) { |
| 61 | jvmtiError ret = jvmti_env->SetEventNotificationMode( |
| 62 | enable ? JVMTI_ENABLE : JVMTI_DISABLE, |
| 63 | JVMTI_EVENT_GARBAGE_COLLECTION_START, |
| 64 | nullptr); |
| 65 | if (ret != JVMTI_ERROR_NONE) { |
| 66 | char* err; |
| 67 | jvmti_env->GetErrorName(ret, &err); |
| 68 | printf("Error enabling/disabling gc callbacks: %s\n", err); |
Alex Light | 4196071 | 2017-01-06 14:44:23 -0800 | [diff] [blame^] | 69 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err)); |
Andreas Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame] | 70 | } |
| 71 | ret = jvmti_env->SetEventNotificationMode( |
| 72 | enable ? JVMTI_ENABLE : JVMTI_DISABLE, |
| 73 | JVMTI_EVENT_GARBAGE_COLLECTION_FINISH, |
| 74 | nullptr); |
| 75 | if (ret != JVMTI_ERROR_NONE) { |
| 76 | char* err; |
| 77 | jvmti_env->GetErrorName(ret, &err); |
| 78 | printf("Error enabling/disabling gc callbacks: %s\n", err); |
Alex Light | 4196071 | 2017-01-06 14:44:23 -0800 | [diff] [blame^] | 79 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err)); |
Andreas Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | |
| 83 | extern "C" JNIEXPORT jint JNICALL Java_Main_getGcStarts(JNIEnv* env ATTRIBUTE_UNUSED, |
| 84 | jclass klass ATTRIBUTE_UNUSED) { |
| 85 | jint result = static_cast<jint>(starts); |
| 86 | starts = 0; |
| 87 | return result; |
| 88 | } |
| 89 | |
| 90 | extern "C" JNIEXPORT jint JNICALL Java_Main_getGcFinishes(JNIEnv* env ATTRIBUTE_UNUSED, |
| 91 | jclass klass ATTRIBUTE_UNUSED) { |
| 92 | jint result = static_cast<jint>(finishes); |
| 93 | finishes = 0; |
| 94 | return result; |
| 95 | } |
| 96 | |
| 97 | // Don't do anything |
| 98 | jint OnLoad(JavaVM* vm, |
| 99 | char* options ATTRIBUTE_UNUSED, |
| 100 | void* reserved ATTRIBUTE_UNUSED) { |
| 101 | if (vm->GetEnv(reinterpret_cast<void**>(&jvmti_env), JVMTI_VERSION_1_0)) { |
| 102 | printf("Unable to get jvmti env!\n"); |
| 103 | return 1; |
| 104 | } |
Alex Light | e657424 | 2016-08-17 09:56:24 -0700 | [diff] [blame] | 105 | SetAllCapabilities(jvmti_env); |
Andreas Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame] | 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | } // namespace Test908GcStartFinish |
| 110 | } // namespace art |