Path: blob/master/test/hotspot/jtreg/serviceability/jvmti/CompiledMethodLoad/libCompiledZombie.cpp
41153 views
/*1* Copyright (c) 2019, 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>2627#include "jvmti.h"28#include "jni.h"2930#ifdef __cplusplus31extern "C" {32#endif3334static int events;35static int total_events = 0;3637void JNICALL CompiledMethodLoad(jvmtiEnv* jvmti, jmethodID method,38jint code_size, const void* code_addr,39jint map_length, const jvmtiAddrLocationMap* map,40const void* compile_info) {41events++;42total_events++;43}4445// Continuously generate CompiledMethodLoad events for all currently compiled methods46void JNICALL GenerateEventsThread(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) {47jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_COMPILED_METHOD_LOAD, NULL);48int count = 0;4950while (true) {51events = 0;52jvmti->GenerateEvents(JVMTI_EVENT_COMPILED_METHOD_LOAD);53if (events != 0 && ++count == 200) {54printf("Generated %d events\n", events);55count = 0;56}57}58}5960// As soon as VM starts, run a separate Agent thread that will generate CompiledMethodLoad events61void JNICALL VMInit(jvmtiEnv* jvmti, JNIEnv* jni, jthread thread) {62jclass thread_class = jni->FindClass("java/lang/Thread");63jmethodID thread_constructor = jni->GetMethodID(thread_class, "<init>", "()V");64jthread agent_thread = jni->NewObject(thread_class, thread_constructor);6566jvmti->RunAgentThread(agent_thread, GenerateEventsThread, NULL, JVMTI_THREAD_NORM_PRIORITY);67}6869JNIEXPORT70jint JNICALL Agent_OnLoad(JavaVM* vm, char* options, void* reserved) {71jvmtiEnv* jvmti;72vm->GetEnv((void**)&jvmti, JVMTI_VERSION_1_0);7374jvmtiCapabilities capabilities;75memset(&capabilities, 0, sizeof(capabilities));7677capabilities.can_generate_compiled_method_load_events = 1;78jvmti->AddCapabilities(&capabilities);7980jvmtiEventCallbacks callbacks;81memset(&callbacks, 0, sizeof(callbacks));82callbacks.VMInit = VMInit;83callbacks.CompiledMethodLoad = CompiledMethodLoad;84jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks));85jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_VM_INIT, NULL);8687return 0;88}8990#ifdef __cplusplus91}92#endif93949596