Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/runtime/gc_tests/hooktests/gc_hooktests.c
6000 views
1
/*******************************************************************************
2
* Copyright (c) 2001, 2017 IBM Corp. and others
3
*
4
* This program and the accompanying materials are made available under
5
* the terms of the Eclipse Public License 2.0 which accompanies this
6
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
7
* or the Apache License, Version 2.0 which accompanies this distribution and
8
* is available at https://www.apache.org/licenses/LICENSE-2.0.
9
*
10
* This Source Code may also be made available under the following
11
* Secondary Licenses when the conditions for such availability set
12
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
13
* General Public License, version 2 with the GNU Classpath
14
* Exception [1] and GNU General Public License, version 2 with the
15
* OpenJDK Assembly Exception [2].
16
*
17
* [1] https://www.gnu.org/software/classpath/license.html
18
* [2] http://openjdk.java.net/legal/assembly-exception.html
19
*
20
* 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-exception
21
*******************************************************************************/
22
#include "j9port.h"
23
#include "j9.h"
24
#include "omr.h"
25
#include "j9protos.h"
26
#include "j9comp.h"
27
#include "j9consts.h"
28
#include "gc_hooktests.h"
29
#include "mmhook.h"
30
31
static void allocationThresholdHandler(J9HookInterface** hook, UDATA eventNum, void* eventData, void* userData);
32
static void vmShutdownHandler(J9HookInterface** hook, UDATA eventNum, void* eventData, void* userData);
33
static void commonAllocHandler(J9HookInterface** hook, UDATA eventNum, void* eventData, void* userData);
34
static UDATA numAllocs;
35
36
jint JNICALL
37
JVM_OnLoad( JavaVM *jvm, char* options, void *reserved )
38
{
39
J9JavaVM *javaVM = (J9JavaVM*)jvm;
40
J9HookInterface **vmHooks = javaVM->internalVMFunctions->getVMHookInterface(javaVM);
41
PORT_ACCESS_FROM_JAVAVM(javaVM);
42
43
if (0 != (*vmHooks)->J9HookRegisterWithCallSite(vmHooks, J9HOOK_VM_OBJECT_ALLOCATE_WITHIN_THRESHOLD, allocationThresholdHandler, OMR_GET_CALLSITE(),NULL)) {
44
j9tty_printf(PORTLIB,"Unable to register for hook\n");
45
return JNI_ERR;
46
} else {
47
j9tty_printf(PORTLIB,"Registered for J9HOOK_MM_ALLOCATION_THRESHOLD hook\n");
48
}
49
50
if (0 != (*vmHooks)->J9HookRegisterWithCallSite(vmHooks, J9HOOK_VM_OBJECT_ALLOCATE, commonAllocHandler, OMR_GET_CALLSITE(), NULL)) {
51
j9tty_printf(PORTLIB,"Unable to register for hook\n");
52
return JNI_ERR;
53
}
54
55
/*if (0 != (*vmHooks)->J9HookRegisterWithCallSite(vmHooks, J9HOOK_VM_OBJECT_ALLOCATE_INSTRUMENTABLE, commonAllocHandler, OMR_GET_CALLSITE(), NULL)) {
56
j9tty_printf(PORTLIB,"Unable to register for hook\n");
57
return JNI_ERR;
58
}*/
59
60
if (0 != (*vmHooks)->J9HookRegisterWithCallSite(vmHooks, J9HOOK_VM_SHUTTING_DOWN, vmShutdownHandler, OMR_GET_CALLSITE(), NULL)) {
61
j9tty_printf(PORTLIB,"Unable to register shutdown listener.\n");
62
return JNI_ERR;
63
}
64
65
numAllocs = 0;
66
67
javaVM->memoryManagerFunctions->j9gc_set_allocation_threshold(javaVM->mainThread, 1024*64, 128*1024);
68
69
return JNI_OK;
70
}
71
72
static void
73
commonAllocHandler(J9HookInterface** hook, UDATA eventNum, void* eventData, void* userData)
74
{
75
J9VMObjectAllocateEvent * acEvent = (J9VMObjectAllocateEvent*)eventData;
76
J9VMThread * vmThread = acEvent->currentThread;
77
J9JavaVM * vm = vmThread->javaVM;
78
static UDATA state = 0;
79
PORT_ACCESS_FROM_JAVAVM(vm);
80
numAllocs++;
81
82
if ( numAllocs >= 100 && state == 0) {
83
state = 1;
84
}
85
if ( numAllocs >= 200 && state == 2) {
86
state = 3;
87
}
88
if ( numAllocs >= 300 && state == 4) {
89
state = 5;
90
}
91
if ( numAllocs >= 400 && state == 6) {
92
state = 7;
93
}
94
95
switch(state) {
96
case 1:
97
j9tty_printf(PORTLIB,"100 allocations, setting threshold lower\n");
98
vm->memoryManagerFunctions->j9gc_set_allocation_threshold(vmThread, 1024*32, 64*1024);
99
state = 2;
100
break;
101
case 3:
102
j9tty_printf(PORTLIB,"200 allocations, setting threshold lower\n");
103
vm->memoryManagerFunctions->j9gc_set_allocation_threshold(vmThread, 32, 64);
104
state = 4;
105
break;
106
case 5:
107
j9tty_printf(PORTLIB,"500 allocations increasing threshold\n");
108
vm->memoryManagerFunctions->j9gc_set_allocation_threshold(vmThread, 1024*32, 64*1024);
109
state = 6;
110
break;
111
case 7:
112
j9tty_printf(PORTLIB,"Disabling allocation threshold\n");
113
vm->memoryManagerFunctions->j9gc_set_allocation_threshold(vmThread, UDATA_MAX, UDATA_MAX);
114
state = 8;
115
break;
116
}
117
}
118
119
static void
120
allocationThresholdHandler(J9HookInterface** hook, UDATA eventNum, void* eventData, void* userData)
121
{
122
#if 0
123
J9VMAllocationThresholdEvent* atEvent = (J9VMAllocationThresholdEvent*)eventData;
124
J9VMThread *vmThread = atEvent->currentThread;
125
J9JavaVM *vm = vmThread->javaVM;
126
PORT_ACCESS_FROM_JAVAVM(vm);
127
128
/*j9tty_printf(PORTLIB,"Object allocated of size %lu (low:%lu high:%lu)\n",atEvent->size,atEvent->lowThreshold,atEvent->highThreshold);*/
129
130
/*j9tty_printf(PORTLIB,"Number of allocates: %lu\n",numAllocs);*/
131
#endif
132
}
133
134
static void
135
vmShutdownHandler(J9HookInterface** hook, UDATA eventNum, void* eventData, void* userData)
136
{
137
J9VMShutdownEvent* event = eventData;
138
J9VMThread *vmThread = event->vmThread;
139
J9HookInterface **vmHooks = vmThread->javaVM->internalVMFunctions->getVMHookInterface(vmThread->javaVM);
140
PORT_ACCESS_FROM_VMC(vmThread);
141
142
(*vmHooks)->J9HookUnregister(vmHooks,J9HOOK_VM_OBJECT_ALLOCATE_WITHIN_THRESHOLD,allocationThresholdHandler,NULL);
143
(*vmHooks)->J9HookUnregister(vmHooks,J9HOOK_VM_OBJECT_ALLOCATE,commonAllocHandler,NULL);
144
/*(*vmHooks)->J9HookUnregister(vmHooks,J9HOOK_VM_OBJECT_ALLOCATE_INSTRUMENTABLE,commonAllocHandler,NULL);*/
145
j9tty_printf(PORTLIB,"Unregistering hooks\n");
146
j9tty_printf(PORTLIB,"Total number of allocates: %lu\n",numAllocs);
147
}
148
149