blob: 59801ff648125d3d76b250dc52af39954ba2c406 [file] [log] [blame]
Andreas Gampe9b8c5882016-10-21 15:27:46 -07001/*
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 Gampe9b8c5882016-10-21 15:27:46 -070017#include <stdio.h>
18#include <string.h>
19
20#include "base/macros.h"
21#include "jni.h"
22#include "openjdkjvmti/jvmti.h"
Alex Lighte6574242016-08-17 09:56:24 -070023#include "ti-agent/common_helper.h"
Andreas Gampe9b8c5882016-10-21 15:27:46 -070024#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 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 Light41960712017-01-06 14:44:23 -080052 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err));
Andreas Gampe9b8c5882016-10-21 15:27:46 -070053 }
54}
55
56extern "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 Light41960712017-01-06 14:44:23 -080067 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err));
Andreas Gampe9b8c5882016-10-21 15:27:46 -070068 }
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 Light41960712017-01-06 14:44:23 -080077 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err));
Andreas Gampe9b8c5882016-10-21 15:27:46 -070078 }
79}
80
81extern "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
88extern "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 Gampe9b8c5882016-10-21 15:27:46 -070095} // namespace Test908GcStartFinish
96} // namespace art