Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/GarbageCollectionStart/gcstart002/gcstart002.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 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);68return;69}70else {71NSK_DISPLAY1("CHECK PASSED: %s: the raw monitor entered\n",72msg);7374NSK_DISPLAY1("%s: waiting the raw monitor ...\n",75msg);76if (!NSK_JVMTI_VERIFY(jvmti_env->RawMonitorWait(_lock, (jlong)10))) {77result = STATUS_FAILED;78NSK_COMPLAIN1("TEST FAILED: %s: unable to wait the raw monitor\n\n",79msg);80}81NSK_DISPLAY1("CHECK PASSED: %s: the raw monitor waited\n",82msg);838485NSK_DISPLAY1("%s: notifying a single thread waiting on the raw monitor ...\n",86msg);87if (!NSK_JVMTI_VERIFY(jvmti_env->RawMonitorNotify(_lock))) {88result = STATUS_FAILED;89NSK_COMPLAIN1("TEST FAILED: %s: unable to notify single thread\n\n",90msg);91}92NSK_DISPLAY1("CHECK PASSED: %s: single thread notified\n",93msg);949596NSK_DISPLAY1("%s: notifying all threads waiting on the raw monitor ...\n",97msg);98if (!NSK_JVMTI_VERIFY(jvmti_env->RawMonitorNotifyAll(_lock))) {99result = STATUS_FAILED;100NSK_COMPLAIN1("TEST FAILED: %s: unable to notify all threads\n\n",101msg);102}103NSK_DISPLAY1("CHECK PASSED: %s: all threads notified\n",104msg);105106107NSK_DISPLAY1("%s: exiting the raw monitor ...\n",108msg);109if (!NSK_JVMTI_VERIFY(jvmti_env->RawMonitorExit(_lock))) {110result = STATUS_FAILED;111NSK_COMPLAIN1("TEST FAILED: %s: unable to exit the raw monitor\n\n",112msg);113}114NSK_DISPLAY1("CHECK PASSED: %s: the raw monitor exited\n",115msg);116}117118NSK_DISPLAY1("%s: destroying the raw monitor ...\n",119msg);120if (!NSK_JVMTI_VERIFY(jvmti_env->DestroyRawMonitor(_lock))) {121result = STATUS_FAILED;122NSK_COMPLAIN1("TEST FAILED: %s: unable to destroy a raw monitor\n",123msg);124return;125}126NSK_DISPLAY1("CHECK PASSED: %s: the raw monitor destroyed\n",127msg);128}129130static void memoryFunc(jvmtiEnv *jvmti_env, const char *msg) {131NSK_DISPLAY1("%s: allocating memory ...\n",132msg);133if (!NSK_JVMTI_VERIFY(jvmti_env->Allocate(MEM_SIZE, &mem))) {134result = STATUS_FAILED;135NSK_COMPLAIN1("TEST FAILED: %s: unable to allocate memory\n\n",136msg);137return;138}139else140NSK_DISPLAY1("CHECK PASSED: %s: memory has been allocated successfully\n",141msg);142143NSK_DISPLAY1("%s: deallocating memory ...\n",144msg);145if (!NSK_JVMTI_VERIFY(jvmti_env->Deallocate(mem))) {146result = STATUS_FAILED;147NSK_COMPLAIN1("TEST FAILED: %s: unable to deallocate memory\n\n",148msg);149}150else151NSK_DISPLAY1("CHECK PASSED: %s: memory has been deallocated successfully\n\n",152msg);153}154155/** callback functions **/156void JNICALL157GarbageCollectionStart(jvmtiEnv *jvmti_env) {158gcstart++;159NSK_DISPLAY1(">>>> GarbageCollectionStart event #%d received\n",160gcstart);161162rawMonitorFunc(jvmti_env, "GarbageCollectionStart");163164memoryFunc(jvmti_env, "GarbageCollectionStart");165166NSK_DISPLAY0("<<<<\n\n");167}168169void JNICALL170VMDeath(jvmtiEnv *jvmti_env, JNIEnv *env) {171NSK_DISPLAY0("VMDeath event received\n");172173if (result == STATUS_FAILED)174exit(95 + STATUS_FAILED);175}176177/************************/178179#ifdef STATIC_BUILD180JNIEXPORT jint JNICALL Agent_OnLoad_gcstart002(JavaVM *jvm, char *options, void *reserved) {181return Agent_Initialize(jvm, options, reserved);182}183JNIEXPORT jint JNICALL Agent_OnAttach_gcstart002(JavaVM *jvm, char *options, void *reserved) {184return Agent_Initialize(jvm, options, reserved);185}186JNIEXPORT jint JNI_OnLoad_gcstart002(JavaVM *jvm, char *options, void *reserved) {187return JNI_VERSION_1_8;188}189#endif190jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {191/* init framework and parse options */192if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))193return JNI_ERR;194195/* create JVMTI environment */196if (!NSK_VERIFY((jvmti =197nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))198return JNI_ERR;199200/* add capability to generate compiled method events */201memset(&caps, 0, sizeof(jvmtiCapabilities));202caps.can_generate_garbage_collection_events = 1;203if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps)))204return JNI_ERR;205206if (!NSK_JVMTI_VERIFY(jvmti->GetCapabilities(&caps)))207return JNI_ERR;208209if (!caps.can_generate_garbage_collection_events)210NSK_DISPLAY0("Warning: generation of garbage collection events is not implemented\n");211212/* set event callback */213NSK_DISPLAY0("setting event callbacks ...\n");214(void) memset(&callbacks, 0, sizeof(callbacks));215callbacks.VMDeath = &VMDeath;216callbacks.GarbageCollectionStart = &GarbageCollectionStart;217if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks))))218return JNI_ERR;219220NSK_DISPLAY0("setting event callbacks done\nenabling JVMTI events ...\n");221if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_VM_DEATH, NULL)))222return JNI_ERR;223if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_GARBAGE_COLLECTION_START, NULL)))224return JNI_ERR;225NSK_DISPLAY0("enabling the events done\n\n");226227return JNI_OK;228}229230}231232233