blob: d54651324d8bc1c15db441e3d0ee69865f2a4b31 [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
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"
25#include "ti-agent/common_load.h"
26
27namespace art {
28namespace Test908GcStartFinish {
29
30static size_t starts = 0;
31static size_t finishes = 0;
32
33static void JNICALL GarbageCollectionFinish(jvmtiEnv* ti_env ATTRIBUTE_UNUSED) {
34 finishes++;
35}
36
37static void JNICALL GarbageCollectionStart(jvmtiEnv* ti_env ATTRIBUTE_UNUSED) {
38 starts++;
39}
40
41extern "C" JNIEXPORT void JNICALL Java_Main_setupGcCallback(
42 JNIEnv* env ATTRIBUTE_UNUSED, jclass klass ATTRIBUTE_UNUSED) {
43 jvmtiEventCallbacks callbacks;
44 memset(&callbacks, 0, sizeof(jvmtiEventCallbacks));
45 callbacks.GarbageCollectionFinish = GarbageCollectionFinish;
46 callbacks.GarbageCollectionStart = GarbageCollectionStart;
47
48 jvmtiError ret = jvmti_env->SetEventCallbacks(&callbacks, sizeof(callbacks));
49 if (ret != JVMTI_ERROR_NONE) {
50 char* err;
51 jvmti_env->GetErrorName(ret, &err);
52 printf("Error setting callbacks: %s\n", err);
53 }
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);
67 }
68 ret = jvmti_env->SetEventNotificationMode(
69 enable ? JVMTI_ENABLE : JVMTI_DISABLE,
70 JVMTI_EVENT_GARBAGE_COLLECTION_FINISH,
71 nullptr);
72 if (ret != JVMTI_ERROR_NONE) {
73 char* err;
74 jvmti_env->GetErrorName(ret, &err);
75 printf("Error enabling/disabling gc callbacks: %s\n", err);
76 }
77}
78
79extern "C" JNIEXPORT jint JNICALL Java_Main_getGcStarts(JNIEnv* env ATTRIBUTE_UNUSED,
80 jclass klass ATTRIBUTE_UNUSED) {
81 jint result = static_cast<jint>(starts);
82 starts = 0;
83 return result;
84}
85
86extern "C" JNIEXPORT jint JNICALL Java_Main_getGcFinishes(JNIEnv* env ATTRIBUTE_UNUSED,
87 jclass klass ATTRIBUTE_UNUSED) {
88 jint result = static_cast<jint>(finishes);
89 finishes = 0;
90 return result;
91}
92
93// Don't do anything
94jint OnLoad(JavaVM* vm,
95 char* options ATTRIBUTE_UNUSED,
96 void* reserved ATTRIBUTE_UNUSED) {
97 if (vm->GetEnv(reinterpret_cast<void**>(&jvmti_env), JVMTI_VERSION_1_0)) {
98 printf("Unable to get jvmti env!\n");
99 return 1;
100 }
101 return 0;
102}
103
104} // namespace Test908GcStartFinish
105} // namespace art