Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/GarbageCollectionFinish/gcfinish001/gcfinish001.cpp
40951 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 03738#define MEM_SIZE 10243940static jvmtiEnv *jvmti = NULL;41static jvmtiEventCallbacks callbacks;42static jvmtiCapabilities caps;4344static volatile jint result = PASSED;45static volatile int gcstart = 0;46unsigned char *mem;4748static void rawMonitorFunc(jvmtiEnv *jvmti_env, const char *msg) {49jrawMonitorID _lock;5051NSK_DISPLAY1("%s: creating a raw monitor ...\n",52msg);53if (!NSK_JVMTI_VERIFY(jvmti_env->CreateRawMonitor("_lock", &_lock))) {54result = STATUS_FAILED;55NSK_COMPLAIN1("TEST FAILED: %s: unable to create a raw monitor\n\n",56msg);57return;58}59NSK_DISPLAY1("CHECK PASSED: %s: raw monitor created\n",60msg);6162NSK_DISPLAY1("%s: entering the raw monitor ...\n",63msg);64if (!NSK_JVMTI_VERIFY(jvmti_env->RawMonitorEnter(_lock))) {65result = STATUS_FAILED;66NSK_COMPLAIN1("TEST FAILED: %s: unable to enter the raw monitor\n\n",67msg);68}69else {70NSK_DISPLAY1("CHECK PASSED: %s: the raw monitor entered\n",71msg);7273NSK_DISPLAY1("%s: waiting the raw monitor ...\n",74msg);75if (!NSK_JVMTI_VERIFY(jvmti_env->RawMonitorWait(_lock, (jlong)10))) {76result = STATUS_FAILED;77NSK_COMPLAIN1("TEST FAILED: %s: unable to wait the raw monitor\n\n",78msg);79}80NSK_DISPLAY1("CHECK PASSED: %s: the raw monitor waited\n",81msg);828384NSK_DISPLAY1("%s: notifying a single thread waiting on the raw monitor ...\n",85msg);86if (!NSK_JVMTI_VERIFY(jvmti_env->RawMonitorNotify(_lock))) {87result = STATUS_FAILED;88NSK_COMPLAIN1("TEST FAILED: %s: unable to notify single thread\n\n",89msg);90}91NSK_DISPLAY1("CHECK PASSED: %s: single thread notified\n",92msg);939495NSK_DISPLAY1("%s: notifying all threads waiting on the raw monitor ...\n",96msg);97if (!NSK_JVMTI_VERIFY(jvmti_env->RawMonitorNotifyAll(_lock))) {98result = STATUS_FAILED;99NSK_COMPLAIN1("TEST FAILED: %s: unable to notify all threads\n\n",100msg);101}102NSK_DISPLAY1("CHECK PASSED: %s: all threads notified\n",103msg);104105106NSK_DISPLAY1("%s: exiting the raw monitor ...\n",107msg);108if (!NSK_JVMTI_VERIFY(jvmti_env->RawMonitorExit(_lock))) {109result = STATUS_FAILED;110NSK_COMPLAIN1("TEST FAILED: %s: unable to exit the raw monitor\n\n",111msg);112}113NSK_DISPLAY1("CHECK PASSED: %s: the raw monitor exited\n",114msg);115}116117NSK_DISPLAY1("%s: destroying the raw monitor ...\n",118msg);119if (!NSK_JVMTI_VERIFY(jvmti_env->DestroyRawMonitor(_lock))) {120result = STATUS_FAILED;121NSK_COMPLAIN1("TEST FAILED: %s: unable to destroy a raw monitor\n",122msg);123return;124}125NSK_DISPLAY1("CHECK PASSED: %s: the raw monitor destroyed\n",126msg);127}128129static void memoryFunc(jvmtiEnv *jvmti_env, const char *msg) {130NSK_DISPLAY1("%s: allocating memory ...\n",131msg);132if (!NSK_JVMTI_VERIFY(jvmti_env->Allocate(MEM_SIZE, &mem))) {133result = STATUS_FAILED;134NSK_COMPLAIN1("TEST FAILED: %s: unable to allocate memory\n\n",135msg);136return;137}138else139NSK_DISPLAY1("CHECK PASSED: %s: memory has been allocated successfully\n",140msg);141142NSK_DISPLAY1("%s: deallocating memory ...\n",143msg);144if (!NSK_JVMTI_VERIFY(jvmti_env->Deallocate(mem))) {145result = STATUS_FAILED;146NSK_COMPLAIN1("TEST FAILED: %s: unable to deallocate memory\n\n",147msg);148}149else150NSK_DISPLAY1("CHECK PASSED: %s: memory has been deallocated successfully\n\n",151msg);152}153154/** callback functions **/155void JNICALL156GarbageCollectionFinish(jvmtiEnv *jvmti_env) {157gcstart++;158NSK_DISPLAY1(">>>> GarbageCollectionFinish event #%d received\n",159gcstart);160161rawMonitorFunc(jvmti_env, "GarbageCollectionFinish");162163memoryFunc(jvmti_env, "GarbageCollectionFinish");164165NSK_DISPLAY0("<<<<\n\n");166}167168void JNICALL169VMDeath(jvmtiEnv *jvmti_env, JNIEnv *env) {170NSK_DISPLAY0("VMDeath event received\n");171172if (result == STATUS_FAILED)173exit(95 + STATUS_FAILED);174}175176/************************/177178#ifdef STATIC_BUILD179JNIEXPORT jint JNICALL Agent_OnLoad_gcfinish001(JavaVM *jvm, char *options, void *reserved) {180return Agent_Initialize(jvm, options, reserved);181}182JNIEXPORT jint JNICALL Agent_OnAttach_gcfinish001(JavaVM *jvm, char *options, void *reserved) {183return Agent_Initialize(jvm, options, reserved);184}185JNIEXPORT jint JNI_OnLoad_gcfinish001(JavaVM *jvm, char *options, void *reserved) {186return JNI_VERSION_1_8;187}188#endif189jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {190/* init framework and parse options */191if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))192return JNI_ERR;193194/* create JVMTI environment */195if (!NSK_VERIFY((jvmti =196nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))197return JNI_ERR;198199/* add capability to generate compiled method events */200memset(&caps, 0, sizeof(jvmtiCapabilities));201caps.can_generate_garbage_collection_events = 1;202if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps)))203return JNI_ERR;204205if (!NSK_JVMTI_VERIFY(jvmti->GetCapabilities(&caps)))206return JNI_ERR;207208if (!caps.can_generate_garbage_collection_events)209NSK_DISPLAY0("Warning: generation of garbage collection events is not implemented\n");210211/* set event callback */212NSK_DISPLAY0("setting event callbacks ...\n");213(void) memset(&callbacks, 0, sizeof(callbacks));214callbacks.VMDeath = &VMDeath;215callbacks.GarbageCollectionFinish = &GarbageCollectionFinish;216if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks))))217return JNI_ERR;218219NSK_DISPLAY0("setting event callbacks done\nenabling JVMTI events ...\n");220if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_VM_DEATH, NULL)))221return JNI_ERR;222if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_GARBAGE_COLLECTION_FINISH, NULL)))223return JNI_ERR;224NSK_DISPLAY0("enabling the events done\n\n");225226return JNI_OK;227}228229}230231232