blob: 1fab79dcb113b95ac2d07bf7db00f38535c9f1fb [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);
Alex Light41960712017-01-06 14:44:23 -080054 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err));
Andreas Gampe9b8c5882016-10-21 15:27:46 -070055 }
56}
57
58extern "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 Light41960712017-01-06 14:44:23 -080069 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err));
Andreas Gampe9b8c5882016-10-21 15:27:46 -070070 }
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 Light41960712017-01-06 14:44:23 -080079 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err));
Andreas Gampe9b8c5882016-10-21 15:27:46 -070080 }
81}
82
83extern "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
90extern "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
98jint 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 Lighte6574242016-08-17 09:56:24 -0700105 SetAllCapabilities(jvmti_env);
Andreas Gampe9b8c5882016-10-21 15:27:46 -0700106 return 0;
107}
108
109} // namespace Test908GcStartFinish
110} // namespace art