Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/demo/jvmti/gctest/gctest.c
38829 views
/*1* Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved.2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions5* are met:6*7* - Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9*10* - Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* - Neither the name of Oracle nor the names of its15* contributors may be used to endorse or promote products derived16* from this software without specific prior written permission.17*18* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS19* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,20* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR21* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR22* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,23* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,24* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR25* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF26* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING27* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS28* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.29*/3031/*32* This source code is provided to illustrate the usage of a given feature33* or technique and has been deliberately simplified. Additional steps34* required for a production-quality application, such as security checks,35* input validation and proper error handling, might not be present in36* this sample code.37*/383940/* Example of using JVMTI_EVENT_GARBAGE_COLLECTION_START and41* JVMTI_EVENT_GARBAGE_COLLECTION_FINISH events.42*/4344#include <stdio.h>45#include <stdlib.h>46#include <string.h>4748#include "jni.h"49#include "jvmti.h"5051/* For stdout_message(), fatal_error(), and check_jvmti_error() */52#include "agent_util.h"5354/* Global static data */55static jvmtiEnv *jvmti;56static int gc_count;57static jrawMonitorID lock;5859/* Worker thread that waits for garbage collections */60static void JNICALL61worker(jvmtiEnv* jvmti, JNIEnv* jni, void *p)62{63jvmtiError err;6465stdout_message("GC worker started...\n");6667for (;;) {68err = (*jvmti)->RawMonitorEnter(jvmti, lock);69check_jvmti_error(jvmti, err, "raw monitor enter");70while (gc_count == 0) {71err = (*jvmti)->RawMonitorWait(jvmti, lock, 0);72if (err != JVMTI_ERROR_NONE) {73err = (*jvmti)->RawMonitorExit(jvmti, lock);74check_jvmti_error(jvmti, err, "raw monitor wait");75return;76}77}78gc_count = 0;7980err = (*jvmti)->RawMonitorExit(jvmti, lock);81check_jvmti_error(jvmti, err, "raw monitor exit");8283/* Perform arbitrary JVMTI/JNI work here to do post-GC cleanup */84stdout_message("post-GarbageCollectionFinish actions...\n");85}86}8788/* Creates a new jthread */89static jthread90alloc_thread(JNIEnv *env)91{92jclass thrClass;93jmethodID cid;94jthread res;9596thrClass = (*env)->FindClass(env, "java/lang/Thread");97if ( thrClass == NULL ) {98fatal_error("Cannot find Thread class\n");99}100cid = (*env)->GetMethodID(env, thrClass, "<init>", "()V");101if ( cid == NULL ) {102fatal_error("Cannot find Thread constructor method\n");103}104res = (*env)->NewObject(env, thrClass, cid);105if ( res == NULL ) {106fatal_error("Cannot create new Thread object\n");107}108return res;109}110111/* Callback for JVMTI_EVENT_VM_INIT */112static void JNICALL113vm_init(jvmtiEnv *jvmti, JNIEnv *env, jthread thread)114{115jvmtiError err;116117stdout_message("VMInit...\n");118119err = (*jvmti)->RunAgentThread(jvmti, alloc_thread(env), &worker, NULL,120JVMTI_THREAD_MAX_PRIORITY);121check_jvmti_error(jvmti, err, "running agent thread");122}123124/* Callback for JVMTI_EVENT_GARBAGE_COLLECTION_START */125static void JNICALL126gc_start(jvmtiEnv* jvmti_env)127{128stdout_message("GarbageCollectionStart...\n");129}130131/* Callback for JVMTI_EVENT_GARBAGE_COLLECTION_FINISH */132static void JNICALL133gc_finish(jvmtiEnv* jvmti_env)134{135jvmtiError err;136137stdout_message("GarbageCollectionFinish...\n");138139err = (*jvmti)->RawMonitorEnter(jvmti, lock);140check_jvmti_error(jvmti, err, "raw monitor enter");141gc_count++;142err = (*jvmti)->RawMonitorNotify(jvmti, lock);143check_jvmti_error(jvmti, err, "raw monitor notify");144err = (*jvmti)->RawMonitorExit(jvmti, lock);145check_jvmti_error(jvmti, err, "raw monitor exit");146}147148/* Agent_OnLoad() is called first, we prepare for a VM_INIT event here. */149JNIEXPORT jint JNICALL150Agent_OnLoad(JavaVM *vm, char *options, void *reserved)151{152jint rc;153jvmtiError err;154jvmtiCapabilities capabilities;155jvmtiEventCallbacks callbacks;156157/* Get JVMTI environment */158rc = (*vm)->GetEnv(vm, (void **)&jvmti, JVMTI_VERSION);159if (rc != JNI_OK) {160fatal_error("ERROR: Unable to create jvmtiEnv, rc=%d\n", rc);161return -1;162}163164/* Get/Add JVMTI capabilities */165(void)memset(&capabilities, 0, sizeof(capabilities));166capabilities.can_generate_garbage_collection_events = 1;167err = (*jvmti)->AddCapabilities(jvmti, &capabilities);168check_jvmti_error(jvmti, err, "add capabilities");169170/* Set callbacks and enable event notifications */171memset(&callbacks, 0, sizeof(callbacks));172callbacks.VMInit = &vm_init;173callbacks.GarbageCollectionStart = &gc_start;174callbacks.GarbageCollectionFinish = &gc_finish;175err = (*jvmti)->SetEventCallbacks(jvmti, &callbacks, sizeof(callbacks));176check_jvmti_error(jvmti, err, "set event callbacks");177err = (*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE,178JVMTI_EVENT_VM_INIT, NULL);179check_jvmti_error(jvmti, err, "set event notification");180err = (*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE,181JVMTI_EVENT_GARBAGE_COLLECTION_START, NULL);182check_jvmti_error(jvmti, err, "set event notification");183err = (*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE,184JVMTI_EVENT_GARBAGE_COLLECTION_FINISH, NULL);185check_jvmti_error(jvmti, err, "set event notification");186187/* Create the necessary raw monitor */188err = (*jvmti)->CreateRawMonitor(jvmti, "lock", &lock);189check_jvmti_error(jvmti, err, "create raw monitor");190return 0;191}192193/* Agent_OnUnload() is called last */194JNIEXPORT void JNICALL195Agent_OnUnload(JavaVM *vm)196{197}198199200