blob: 771d1adae68ca146a80186e474e266cf4e0e7f0f [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"
Alex Lighte6574242016-08-17 09:56:24 -070025#include "ti-agent/common_helper.h"
Andreas Gampe9b8c5882016-10-21 15:27:46 -070026#include "ti-agent/common_load.h"
27
28namespace art {
29namespace Test908GcStartFinish {
30
31static size_t starts = 0;
32static size_t finishes = 0;
33
34static void JNICALL GarbageCollectionFinish(jvmtiEnv* ti_env ATTRIBUTE_UNUSED) {
35 finishes++;
36}
37
38static void JNICALL GarbageCollectionStart(jvmtiEnv* ti_env ATTRIBUTE_UNUSED) {
39 starts++;
40}
41
42extern "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);
54 }
55}
56
57extern "C" JNIEXPORT void JNICALL Java_Main_enableGcTracking(JNIEnv* env ATTRIBUTE_UNUSED,
58 jclass klass ATTRIBUTE_UNUSED,
59 jboolean enable) {
60 jvmtiError ret = jvmti_env->SetEventNotificationMode(
61 enable ? JVMTI_ENABLE : JVMTI_DISABLE,
62 JVMTI_EVENT_GARBAGE_COLLECTION_START,
63 nullptr);
64 if (ret != JVMTI_ERROR_NONE) {
65 char* err;
66 jvmti_env->GetErrorName(ret, &err);
67 printf("Error enabling/disabling gc callbacks: %s\n", err);
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);
77 }
78}
79
80extern "C" JNIEXPORT jint JNICALL Java_Main_getGcStarts(JNIEnv* env ATTRIBUTE_UNUSED,
81 jclass klass ATTRIBUTE_UNUSED) {
82 jint result = static_cast<jint>(starts);
83 starts = 0;
84 return result;
85}
86
87extern "C" JNIEXPORT jint JNICALL Java_Main_getGcFinishes(JNIEnv* env ATTRIBUTE_UNUSED,
88 jclass klass ATTRIBUTE_UNUSED) {
89 jint result = static_cast<jint>(finishes);
90 finishes = 0;
91 return result;
92}
93
94// Don't do anything
95jint OnLoad(JavaVM* vm,
96 char* options ATTRIBUTE_UNUSED,
97 void* reserved ATTRIBUTE_UNUSED) {
98 if (vm->GetEnv(reinterpret_cast<void**>(&jvmti_env), JVMTI_VERSION_1_0)) {
99 printf("Unable to get jvmti env!\n");
100 return 1;
101 }
Alex Lighte6574242016-08-17 09:56:24 -0700102 SetAllCapabilities(jvmti_env);
Andreas Gampe9b8c5882016-10-21 15:27:46 -0700103 return 0;
104}
105
106} // namespace Test908GcStartFinish
107} // namespace art