Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/GarbageCollectionStart/gcstart001/gcstart001.cpp
40952 views
/*1* Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223#include <stdio.h>24#include <stdlib.h>25#include <string.h>26#include <jvmti.h>27#include "agent_common.h"2829#include "nsk_tools.h"30#include "JVMTITools.h"31#include "jvmti_tools.h"3233extern "C" {3435#define STATUS_FAILED 236#define PASSED 03738static volatile jint result = PASSED;39static volatile int gcstart = 0;40static volatile int gcfinish = 0;41static jvmtiEnv *jvmti = NULL;42static jvmtiEventCallbacks callbacks;43static jvmtiCapabilities caps;4445/** callback functions **/46void JNICALL47GarbageCollectionStart(jvmtiEnv *jvmti_env) {48gcstart++;49NSK_DISPLAY1("GarbageCollectionStart event #%d received\n",50gcstart);5152if (gcstart != (gcfinish+1)) {53result = STATUS_FAILED;54NSK_COMPLAIN2(55"TEST FAILED: GarbageCollectionStart event has no a matched pair GarbageCollectionFinish:\n"56"\t%d GarbageCollectionStart events\t%d GarbageCollectionFinish events\n\n",57gcstart, gcfinish);58}59else60NSK_DISPLAY0("CHECK PASSED: GarbageCollectionStart event has a matched pair GarbageCollectionFinish as expected\n\n");61}6263void JNICALL64GarbageCollectionFinish(jvmtiEnv *jvmti_env) {65gcfinish++;66NSK_DISPLAY1("GarbageCollectionFinish event #%d received\n",67gcfinish);6869if (gcstart != gcfinish) {70result = STATUS_FAILED;71NSK_COMPLAIN2(72"TEST FAILED: GarbageCollectionFinish event has no a matched pair GarbageCollectionStart:\n"73"\t%d GarbageCollectionStart events\t%d GarbageCollectionFinish events\n\n",74gcstart, gcfinish);75}76else77NSK_DISPLAY0("CHECK PASSED: GarbageCollectionFinish event has a matched pair GarbageCollectionStart as expected\n\n");78}7980void JNICALL81VMDeath(jvmtiEnv *jvmti_env, JNIEnv *env) {82NSK_DISPLAY0("VMDeath event received\n");8384if (gcstart != gcfinish || result == STATUS_FAILED) {85NSK_COMPLAIN2(86"TEST FAILED: some GarbageCollectionFinish events have no a matched pair GarbageCollectionStart:\n"87"\t%d GarbageCollectionStart events\t%d GarbageCollectionFinish events\n\n",88gcstart, gcfinish);8990exit(95 + STATUS_FAILED);91}92else93NSK_DISPLAY0("CHECK PASSED: all GarbageCollectionStart/GarbageCollectionFinish events have a matched pair as expected\n\n");94}9596/************************/9798#ifdef STATIC_BUILD99JNIEXPORT jint JNICALL Agent_OnLoad_gcstart001(JavaVM *jvm, char *options, void *reserved) {100return Agent_Initialize(jvm, options, reserved);101}102JNIEXPORT jint JNICALL Agent_OnAttach_gcstart001(JavaVM *jvm, char *options, void *reserved) {103return Agent_Initialize(jvm, options, reserved);104}105JNIEXPORT jint JNI_OnLoad_gcstart001(JavaVM *jvm, char *options, void *reserved) {106return JNI_VERSION_1_8;107}108#endif109jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {110/* init framework and parse options */111if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))112return JNI_ERR;113114/* create JVMTI environment */115if (!NSK_VERIFY((jvmti =116nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))117return JNI_ERR;118119/* add capability to generate compiled method events */120memset(&caps, 0, sizeof(jvmtiCapabilities));121caps.can_generate_garbage_collection_events = 1;122if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps)))123return JNI_ERR;124125if (!NSK_JVMTI_VERIFY(jvmti->GetCapabilities(&caps)))126return JNI_ERR;127128if (!caps.can_generate_garbage_collection_events)129NSK_DISPLAY0("Warning: generation of garbage collection events is not implemented\n");130131/* set event callback */132NSK_DISPLAY0("setting event callbacks ...\n");133(void) memset(&callbacks, 0, sizeof(callbacks));134callbacks.VMDeath = &VMDeath;135callbacks.GarbageCollectionStart = &GarbageCollectionStart;136callbacks.GarbageCollectionFinish = &GarbageCollectionFinish;137if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks))))138return JNI_ERR;139140NSK_DISPLAY0("setting event callbacks done\nenabling JVMTI events ...\n");141if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_VM_DEATH, NULL)))142return JNI_ERR;143if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_GARBAGE_COLLECTION_START, NULL)))144return JNI_ERR;145if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_GARBAGE_COLLECTION_FINISH, NULL)))146return JNI_ERR;147NSK_DISPLAY0("enabling the events done\n\n");148149return JNI_OK;150}151152}153154155