-
Notifications
You must be signed in to change notification settings - Fork 751
/
Copy pathjvmtiObject.c
247 lines (196 loc) · 7.44 KB
/
jvmtiObject.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*******************************************************************************
* Copyright (c) 1991, 2018 IBM Corp. and others
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
* or the Apache License, Version 2.0 which accompanies this distribution and
* is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* This Source Code may also be made available under the following
* Secondary Licenses when the conditions for such availability set
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
* General Public License, version 2 with the GNU Classpath
* Exception [1] and GNU General Public License, version 2 with the
* OpenJDK Assembly Exception [2].
*
* [1] https://www.gnu.org/software/classpath/license.html
* [2] http://openjdk.java.net/legal/assembly-exception.html
*
* 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
*******************************************************************************/
#include "jvmtiHelpers.h"
#include "jvmti_internal.h"
typedef struct J9JVMTIMonitorStats {
J9JavaVM * vm;
J9VMThread * currentThread;
j9object_t lockObject;
UDATA numWaiting;
UDATA waitingCntr;
UDATA numBlocked;
UDATA blockedCntr;
jthread * waiting;
jthread * blocked;
} J9JVMTIMonitorStats;
static void findMonitorThreads(J9VMThread * vmThread, J9JVMTIMonitorStats * pStats);
jvmtiError JNICALL
jvmtiGetObjectSize(jvmtiEnv* env,
jobject object,
jlong* size_ptr)
{
J9JavaVM * vm = JAVAVM_FROM_ENV(env);
jvmtiError rc;
J9VMThread * currentThread;
Trc_JVMTI_jvmtiGetObjectSize2_Entry(env, object, size_ptr);
rc = getCurrentVMThread(vm, ¤tThread);
if (rc == JVMTI_ERROR_NONE) {
vm->internalVMFunctions->internalEnterVMFromJNI(currentThread);
ENSURE_PHASE_START_OR_LIVE(env);
ENSURE_JOBJECT_NON_NULL(object);
ENSURE_NON_NULL(size_ptr);
*size_ptr = getObjectSize(vm, *(j9object_t*)object);
done:
vm->internalVMFunctions->internalExitVMToJNI(currentThread);
}
TRACE_ONE_JVMTI_RETURN(jvmtiGetObjectSize2, *size_ptr);
}
jvmtiError JNICALL
jvmtiGetObjectHashCode(jvmtiEnv* env,
jobject object,
jint* hash_code_ptr)
{
J9JavaVM * vm = JAVAVM_FROM_ENV(env);
jvmtiError rc;
J9VMThread * currentThread;
Trc_JVMTI_jvmtiGetObjectHashCode2_Entry(env, object, hash_code_ptr);
rc = getCurrentVMThread(vm, ¤tThread);
if (rc == JVMTI_ERROR_NONE) {
j9object_t obj;
vm->internalVMFunctions->internalEnterVMFromJNI(currentThread);
ENSURE_PHASE_START_OR_LIVE(env);
ENSURE_JOBJECT_NON_NULL(object);
ENSURE_NON_NULL(hash_code_ptr);
obj = *((j9object_t*) object);
*hash_code_ptr = (jint)objectHashCode(vm, obj);
done:
vm->internalVMFunctions->internalExitVMToJNI(currentThread);
}
TRACE_ONE_JVMTI_RETURN(jvmtiGetObjectHashCode2, *hash_code_ptr);
}
jvmtiError JNICALL
jvmtiGetObjectMonitorUsage(jvmtiEnv* env,
jobject object,
jvmtiMonitorUsage* info_ptr)
{
J9JavaVM * vm = JAVAVM_FROM_ENV(env);
jvmtiError rc;
J9VMThread * currentThread;
PORT_ACCESS_FROM_JAVAVM(vm);
Trc_JVMTI_jvmtiGetObjectMonitorUsage2_Entry(env, object, info_ptr);
rc = getCurrentVMThread(vm, ¤tThread);
if (rc == JVMTI_ERROR_NONE) {
J9VMThread * owner;
J9VMThread * walkThread;
UDATA count = 0;
J9JVMTIMonitorStats stats;
vm->internalVMFunctions->internalEnterVMFromJNI(currentThread);
ENSURE_PHASE_LIVE(env);
ENSURE_CAPABILITY(env, can_get_monitor_info);
ENSURE_JOBJECT_NON_NULL(object);
ENSURE_NON_NULL(info_ptr);
vm->internalVMFunctions->acquireExclusiveVMAccess(currentThread);
/*
* getObjectMonitorOwner requires a J9VMThread solely for multi-tenant support,
* however, the multi-tenant JVM does not support can_get_monitor_info (see ENSURE_CAPABILITY check above)
* and therefore will not use jvmtiGetObjectMonitorUsage: so just pass in NULL as the vm thread
*/
owner = getObjectMonitorOwner(vm, NULL, *((j9object_t*) object), &count);
memset(info_ptr, 0, sizeof(jvmtiMonitorUsage));
if (owner && owner->threadObject) {
j9object_t target = (j9object_t)owner->threadObject;
info_ptr->owner = (jthread) vm->internalVMFunctions->j9jni_createLocalRef((JNIEnv *) currentThread, target);
info_ptr->entry_count = (jint)count;
}
memset(&stats, 0, sizeof(J9JVMTIMonitorStats));
stats.vm = vm;
stats.currentThread = currentThread;
stats.lockObject = *((j9object_t*) object);
/* Search for blocked/waiting threads (wind up counts) */
walkThread = J9_LINKED_LIST_START_DO(vm->mainThread);
while (walkThread != NULL) {
findMonitorThreads(walkThread, &stats);
walkThread = J9_LINKED_LIST_NEXT_DO(vm->mainThread, walkThread);
}
stats.waiting = j9mem_allocate_memory(sizeof(jthread) * stats.numWaiting, J9MEM_CATEGORY_JVMTI_ALLOCATE);
if (stats.waiting == NULL) {
rc = JVMTI_ERROR_OUT_OF_MEMORY;
} else {
stats.blocked = j9mem_allocate_memory(sizeof(jthread) * stats.numBlocked, J9MEM_CATEGORY_JVMTI_ALLOCATE);
if (stats.blocked == NULL) {
j9mem_free_memory(stats.waiting);
rc = JVMTI_ERROR_OUT_OF_MEMORY;
} else {
info_ptr->notify_waiters = stats.waiting;
info_ptr->waiters = stats.blocked;
/* Record blocked/waiting threads (wind down counts) */
walkThread = J9_LINKED_LIST_START_DO(vm->mainThread);
while (walkThread != NULL) {
findMonitorThreads(walkThread, &stats);
walkThread = J9_LINKED_LIST_NEXT_DO(vm->mainThread, walkThread);
}
/* adjust count after filling the output buffer */
info_ptr->notify_waiter_count = (jint)stats.waitingCntr;
info_ptr->waiter_count = (jint)stats.blockedCntr;
}
}
vm->internalVMFunctions->releaseExclusiveVMAccess(currentThread);
done:
vm->internalVMFunctions->internalExitVMToJNI(currentThread);
}
TRACE_FOUR_JVMTI_RETURN(jvmtiGetObjectMonitorUsage2, info_ptr->owner, info_ptr->entry_count, info_ptr->notify_waiter_count, info_ptr->waiter_count);
}
static void
findMonitorThreads(J9VMThread * vmThread, J9JVMTIMonitorStats * pStats)
{
j9object_t lockObject;
UDATA threadState = getVMThreadObjectStatesAll(vmThread, &lockObject, NULL, NULL);
/* Stopped on the same monitor object? */
if (lockObject == pStats->lockObject) {
j9object_t target = (j9object_t)vmThread->threadObject;
if (target) {
J9JavaVM * vm = pStats->vm;
JNIEnv * jniEnv = (JNIEnv *)pStats->currentThread;
/* CMVC 87023 - the spec is unclear, but from experimentation it appears that 'waiting to be notified' threads
* should appear in both lists
*/
threadState &= ~(J9VMTHREAD_STATE_SUSPENDED | J9VMTHREAD_STATE_INTERRUPTED);
switch (threadState) {
case J9VMTHREAD_STATE_WAITING:
case J9VMTHREAD_STATE_WAITING_TIMED:
if (pStats->waiting == NULL) {
(pStats->numWaiting)++;
} else {
if (pStats->waitingCntr < pStats->numWaiting) {
pStats->waiting[pStats->waitingCntr] =
(jthread) vm->internalVMFunctions->j9jni_createLocalRef(jniEnv, target);
++(pStats->waitingCntr);
}
}
/* FALL THROUGH */
case J9VMTHREAD_STATE_BLOCKED:
if (pStats->blocked == NULL) {
(pStats->numBlocked)++;
} else {
if (pStats->blockedCntr < pStats->numBlocked) {
pStats->blocked[pStats->blockedCntr] =
(jthread) vm->internalVMFunctions->j9jni_createLocalRef(jniEnv, target);
++(pStats->blockedCntr);
}
}
break;
default:
break;
}
}
}
}