gc_callbacks.cc revision 027444b64dd52e1d2beea7aa525fbb8146a516bc
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 <stdio.h>
18#include <string.h>
19
20#include "android-base/macros.h"
21
22#include "jni.h"
23#include "jvmti.h"
24
25// Test infrastructure
26#include "jvmti_helper.h"
27#include "test_env.h"
28
29namespace art {
30namespace Test908GcStartFinish {
31
32static size_t starts = 0;
33static size_t finishes = 0;
34
35static void JNICALL GarbageCollectionFinish(jvmtiEnv* ti_env ATTRIBUTE_UNUSED) {
36  finishes++;
37}
38
39static void JNICALL GarbageCollectionStart(jvmtiEnv* ti_env ATTRIBUTE_UNUSED) {
40  starts++;
41}
42
43extern "C" JNIEXPORT void JNICALL Java_Main_setupGcCallback(
44    JNIEnv* env, jclass klass ATTRIBUTE_UNUSED) {
45  jvmtiEventCallbacks callbacks;
46  memset(&callbacks, 0, sizeof(jvmtiEventCallbacks));
47  callbacks.GarbageCollectionFinish = GarbageCollectionFinish;
48  callbacks.GarbageCollectionStart = GarbageCollectionStart;
49
50  jvmtiError ret = jvmti_env->SetEventCallbacks(&callbacks, sizeof(callbacks));
51  JvmtiErrorToException(env, jvmti_env, ret);
52}
53
54extern "C" JNIEXPORT void JNICALL Java_Main_enableGcTracking(JNIEnv* env,
55                                                             jclass klass ATTRIBUTE_UNUSED,
56                                                             jboolean enable) {
57  jvmtiError ret = jvmti_env->SetEventNotificationMode(
58      enable ? JVMTI_ENABLE : JVMTI_DISABLE,
59      JVMTI_EVENT_GARBAGE_COLLECTION_START,
60      nullptr);
61  if (JvmtiErrorToException(env, jvmti_env, ret)) {
62    return;
63  }
64  ret = jvmti_env->SetEventNotificationMode(
65      enable ? JVMTI_ENABLE : JVMTI_DISABLE,
66      JVMTI_EVENT_GARBAGE_COLLECTION_FINISH,
67      nullptr);
68  if (JvmtiErrorToException(env, jvmti_env, ret)) {
69    return;
70  }
71}
72
73extern "C" JNIEXPORT jint JNICALL Java_Main_getGcStarts(JNIEnv* env ATTRIBUTE_UNUSED,
74                                                        jclass klass ATTRIBUTE_UNUSED) {
75  jint result = static_cast<jint>(starts);
76  starts = 0;
77  return result;
78}
79
80extern "C" JNIEXPORT jint JNICALL Java_Main_getGcFinishes(JNIEnv* env ATTRIBUTE_UNUSED,
81                                                          jclass klass ATTRIBUTE_UNUSED) {
82  jint result = static_cast<jint>(finishes);
83  finishes = 0;
84  return result;
85}
86
87}  // namespace Test908GcStartFinish
88}  // namespace art
89