Path: blob/master/runtime/gc_tests/hooktests/gc_hooktests.c
6000 views
/*******************************************************************************1* Copyright (c) 2001, 2017 IBM Corp. and others2*3* This program and the accompanying materials are made available under4* the terms of the Eclipse Public License 2.0 which accompanies this5* distribution and is available at https://www.eclipse.org/legal/epl-2.0/6* or the Apache License, Version 2.0 which accompanies this distribution and7* is available at https://www.apache.org/licenses/LICENSE-2.0.8*9* This Source Code may also be made available under the following10* Secondary Licenses when the conditions for such availability set11* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU12* General Public License, version 2 with the GNU Classpath13* Exception [1] and GNU General Public License, version 2 with the14* OpenJDK Assembly Exception [2].15*16* [1] https://www.gnu.org/software/classpath/license.html17* [2] http://openjdk.java.net/legal/assembly-exception.html18*19* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception20*******************************************************************************/21#include "j9port.h"22#include "j9.h"23#include "omr.h"24#include "j9protos.h"25#include "j9comp.h"26#include "j9consts.h"27#include "gc_hooktests.h"28#include "mmhook.h"2930static void allocationThresholdHandler(J9HookInterface** hook, UDATA eventNum, void* eventData, void* userData);31static void vmShutdownHandler(J9HookInterface** hook, UDATA eventNum, void* eventData, void* userData);32static void commonAllocHandler(J9HookInterface** hook, UDATA eventNum, void* eventData, void* userData);33static UDATA numAllocs;3435jint JNICALL36JVM_OnLoad( JavaVM *jvm, char* options, void *reserved )37{38J9JavaVM *javaVM = (J9JavaVM*)jvm;39J9HookInterface **vmHooks = javaVM->internalVMFunctions->getVMHookInterface(javaVM);40PORT_ACCESS_FROM_JAVAVM(javaVM);4142if (0 != (*vmHooks)->J9HookRegisterWithCallSite(vmHooks, J9HOOK_VM_OBJECT_ALLOCATE_WITHIN_THRESHOLD, allocationThresholdHandler, OMR_GET_CALLSITE(),NULL)) {43j9tty_printf(PORTLIB,"Unable to register for hook\n");44return JNI_ERR;45} else {46j9tty_printf(PORTLIB,"Registered for J9HOOK_MM_ALLOCATION_THRESHOLD hook\n");47}4849if (0 != (*vmHooks)->J9HookRegisterWithCallSite(vmHooks, J9HOOK_VM_OBJECT_ALLOCATE, commonAllocHandler, OMR_GET_CALLSITE(), NULL)) {50j9tty_printf(PORTLIB,"Unable to register for hook\n");51return JNI_ERR;52}5354/*if (0 != (*vmHooks)->J9HookRegisterWithCallSite(vmHooks, J9HOOK_VM_OBJECT_ALLOCATE_INSTRUMENTABLE, commonAllocHandler, OMR_GET_CALLSITE(), NULL)) {55j9tty_printf(PORTLIB,"Unable to register for hook\n");56return JNI_ERR;57}*/5859if (0 != (*vmHooks)->J9HookRegisterWithCallSite(vmHooks, J9HOOK_VM_SHUTTING_DOWN, vmShutdownHandler, OMR_GET_CALLSITE(), NULL)) {60j9tty_printf(PORTLIB,"Unable to register shutdown listener.\n");61return JNI_ERR;62}6364numAllocs = 0;6566javaVM->memoryManagerFunctions->j9gc_set_allocation_threshold(javaVM->mainThread, 1024*64, 128*1024);6768return JNI_OK;69}7071static void72commonAllocHandler(J9HookInterface** hook, UDATA eventNum, void* eventData, void* userData)73{74J9VMObjectAllocateEvent * acEvent = (J9VMObjectAllocateEvent*)eventData;75J9VMThread * vmThread = acEvent->currentThread;76J9JavaVM * vm = vmThread->javaVM;77static UDATA state = 0;78PORT_ACCESS_FROM_JAVAVM(vm);79numAllocs++;8081if ( numAllocs >= 100 && state == 0) {82state = 1;83}84if ( numAllocs >= 200 && state == 2) {85state = 3;86}87if ( numAllocs >= 300 && state == 4) {88state = 5;89}90if ( numAllocs >= 400 && state == 6) {91state = 7;92}9394switch(state) {95case 1:96j9tty_printf(PORTLIB,"100 allocations, setting threshold lower\n");97vm->memoryManagerFunctions->j9gc_set_allocation_threshold(vmThread, 1024*32, 64*1024);98state = 2;99break;100case 3:101j9tty_printf(PORTLIB,"200 allocations, setting threshold lower\n");102vm->memoryManagerFunctions->j9gc_set_allocation_threshold(vmThread, 32, 64);103state = 4;104break;105case 5:106j9tty_printf(PORTLIB,"500 allocations increasing threshold\n");107vm->memoryManagerFunctions->j9gc_set_allocation_threshold(vmThread, 1024*32, 64*1024);108state = 6;109break;110case 7:111j9tty_printf(PORTLIB,"Disabling allocation threshold\n");112vm->memoryManagerFunctions->j9gc_set_allocation_threshold(vmThread, UDATA_MAX, UDATA_MAX);113state = 8;114break;115}116}117118static void119allocationThresholdHandler(J9HookInterface** hook, UDATA eventNum, void* eventData, void* userData)120{121#if 0122J9VMAllocationThresholdEvent* atEvent = (J9VMAllocationThresholdEvent*)eventData;123J9VMThread *vmThread = atEvent->currentThread;124J9JavaVM *vm = vmThread->javaVM;125PORT_ACCESS_FROM_JAVAVM(vm);126127/*j9tty_printf(PORTLIB,"Object allocated of size %lu (low:%lu high:%lu)\n",atEvent->size,atEvent->lowThreshold,atEvent->highThreshold);*/128129/*j9tty_printf(PORTLIB,"Number of allocates: %lu\n",numAllocs);*/130#endif131}132133static void134vmShutdownHandler(J9HookInterface** hook, UDATA eventNum, void* eventData, void* userData)135{136J9VMShutdownEvent* event = eventData;137J9VMThread *vmThread = event->vmThread;138J9HookInterface **vmHooks = vmThread->javaVM->internalVMFunctions->getVMHookInterface(vmThread->javaVM);139PORT_ACCESS_FROM_VMC(vmThread);140141(*vmHooks)->J9HookUnregister(vmHooks,J9HOOK_VM_OBJECT_ALLOCATE_WITHIN_THRESHOLD,allocationThresholdHandler,NULL);142(*vmHooks)->J9HookUnregister(vmHooks,J9HOOK_VM_OBJECT_ALLOCATE,commonAllocHandler,NULL);143/*(*vmHooks)->J9HookUnregister(vmHooks,J9HOOK_VM_OBJECT_ALLOCATE_INSTRUMENTABLE,commonAllocHandler,NULL);*/144j9tty_printf(PORTLIB,"Unregistering hooks\n");145j9tty_printf(PORTLIB,"Total number of allocates: %lu\n",numAllocs);146}147148149