author | jcbeyler |
Tue, 17 Jul 2018 19:59:38 -0700 | |
changeset 51138 | 914f305ba6fa |
parent 51136 | 6d6611346837 |
child 51175 | 65556ae796ad |
permissions | -rw-r--r-- |
50579 | 1 |
/* |
2 |
* Copyright (c) 2018, Google and/or its affiliates. All rights reserved. |
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
22 |
*/ |
|
23 |
||
24 |
#include <assert.h> |
|
25 |
#include <stdio.h> |
|
26 |
#include <stdlib.h> |
|
27 |
#include <string.h> |
|
28 |
#include "jvmti.h" |
|
29 |
||
30 |
#ifdef __cplusplus |
|
31 |
extern "C" { |
|
32 |
#endif |
|
33 |
||
34 |
#ifndef JNI_ENV_ARG |
|
35 |
||
36 |
#ifdef __cplusplus |
|
37 |
#define JNI_ENV_ARG(x, y) y |
|
38 |
#define JNI_ENV_PTR(x) x |
|
39 |
#else |
|
40 |
#define JNI_ENV_ARG(x,y) x, y |
|
41 |
#define JNI_ENV_PTR(x) (*x) |
|
42 |
#endif |
|
43 |
||
44 |
#endif |
|
45 |
||
46 |
#define TRUE 1 |
|
47 |
#define FALSE 0 |
|
51136
6d6611346837
8205541: serviceability/jvmti/HeapMonitor/MyPackage/HeapMonitorStatArrayCorrectnessTest.java fails with Should not have any events stored yet.
jcbeyler
parents:
51074
diff
changeset
|
48 |
#define PRINT_OUT 1 |
50579 | 49 |
|
50 |
static jvmtiEnv *jvmti = NULL; |
|
51 |
static jvmtiEnv *second_jvmti = NULL; |
|
52 |
||
53 |
typedef struct _ObjectTrace{ |
|
54 |
jweak object; |
|
50638 | 55 |
jlong size; |
50579 | 56 |
jvmtiFrameInfo* frames; |
57 |
size_t frame_count; |
|
58 |
jthread thread; |
|
59 |
} ObjectTrace; |
|
60 |
||
61 |
typedef struct _EventStorage { |
|
62 |
int live_object_additions; |
|
63 |
int live_object_size; |
|
64 |
int live_object_count; |
|
65 |
ObjectTrace** live_objects; |
|
66 |
||
67 |
int garbage_history_size; |
|
68 |
int garbage_history_index; |
|
69 |
ObjectTrace** garbage_collected_objects; |
|
70 |
||
71 |
// Two separate monitors to separate storage data race and the compaction field |
|
72 |
// data race. |
|
73 |
jrawMonitorID storage_monitor; |
|
74 |
||
75 |
int compaction_required; |
|
76 |
jrawMonitorID compaction_monitor; |
|
77 |
} EventStorage; |
|
78 |
||
79 |
typedef struct _ExpectedContentFrame { |
|
80 |
const char *name; |
|
81 |
const char *signature; |
|
82 |
const char *file_name; |
|
83 |
int line_number; |
|
84 |
} ExpectedContentFrame; |
|
85 |
||
86 |
static |
|
87 |
void event_storage_lock(EventStorage* storage) { |
|
88 |
(*jvmti)->RawMonitorEnter(jvmti, storage->storage_monitor); |
|
89 |
} |
|
90 |
||
91 |
static |
|
92 |
void event_storage_unlock(EventStorage* storage) { |
|
93 |
(*jvmti)->RawMonitorExit(jvmti, storage->storage_monitor); |
|
94 |
} |
|
95 |
||
96 |
static |
|
97 |
void event_storage_lock_compaction(EventStorage* storage) { |
|
98 |
(*jvmti)->RawMonitorEnter(jvmti, storage->compaction_monitor); |
|
99 |
} |
|
100 |
||
101 |
static |
|
102 |
void event_storage_unlock_compaction(EventStorage* storage) { |
|
103 |
(*jvmti)->RawMonitorExit(jvmti, storage->compaction_monitor); |
|
104 |
} |
|
105 |
||
106 |
// Given a method and a location, this method gets the line number. |
|
107 |
static |
|
108 |
jint get_line_number(jvmtiEnv* jvmti, jmethodID method, |
|
109 |
jlocation location) { |
|
110 |
// Read the line number table. |
|
111 |
jvmtiLineNumberEntry *table_ptr = 0; |
|
112 |
jint line_number_table_entries; |
|
113 |
int l; |
|
114 |
jlocation last_location; |
|
115 |
int jvmti_error = (*jvmti)->GetLineNumberTable(jvmti, method, |
|
116 |
&line_number_table_entries, |
|
117 |
&table_ptr); |
|
118 |
||
119 |
if (JVMTI_ERROR_NONE != jvmti_error) { |
|
120 |
return -1; |
|
121 |
} |
|
122 |
if (line_number_table_entries <= 0) { |
|
123 |
return -1; |
|
124 |
} |
|
125 |
if (line_number_table_entries == 1) { |
|
126 |
return table_ptr[0].line_number; |
|
127 |
} |
|
128 |
||
129 |
// Go through all the line numbers... |
|
130 |
last_location = table_ptr[0].start_location; |
|
131 |
for (l = 1; l < line_number_table_entries; l++) { |
|
132 |
// ... and if you see one that is in the right place for your |
|
133 |
// location, you've found the line number! |
|
134 |
if ((location < table_ptr[l].start_location) && |
|
135 |
(location >= last_location)) { |
|
136 |
return table_ptr[l - 1].line_number; |
|
137 |
} |
|
138 |
last_location = table_ptr[l].start_location; |
|
139 |
} |
|
140 |
||
141 |
if (location >= last_location) { |
|
142 |
return table_ptr[line_number_table_entries - 1].line_number; |
|
143 |
} else { |
|
144 |
return -1; |
|
145 |
} |
|
146 |
} |
|
147 |
||
148 |
static void print_out_frames(JNIEnv* env, ObjectTrace* trace) { |
|
149 |
jvmtiFrameInfo* frames = trace->frames; |
|
150 |
size_t i; |
|
151 |
for (i = 0; i < trace->frame_count; i++) { |
|
152 |
// Get basic information out of the trace. |
|
153 |
jlocation bci = frames[i].location; |
|
154 |
jmethodID methodid = frames[i].method; |
|
155 |
char *name = NULL, *signature = NULL, *file_name = NULL; |
|
156 |
jclass declaring_class; |
|
157 |
int line_number; |
|
158 |
jvmtiError err; |
|
159 |
||
160 |
if (bci < 0) { |
|
161 |
fprintf(stderr, "\tNative frame\n"); |
|
162 |
continue; |
|
163 |
} |
|
164 |
||
165 |
// Transform into usable information. |
|
166 |
line_number = get_line_number(jvmti, methodid, bci); |
|
167 |
if (JVMTI_ERROR_NONE != |
|
168 |
(*jvmti)->GetMethodName(jvmti, methodid, &name, &signature, 0)) { |
|
169 |
fprintf(stderr, "\tUnknown method name\n"); |
|
170 |
continue; |
|
171 |
} |
|
172 |
||
173 |
if (JVMTI_ERROR_NONE != |
|
174 |
(*jvmti)->GetMethodDeclaringClass(jvmti, methodid, &declaring_class)) { |
|
175 |
fprintf(stderr, "\tUnknown class\n"); |
|
176 |
continue; |
|
177 |
} |
|
178 |
||
179 |
err = (*jvmti)->GetSourceFileName(jvmti, declaring_class, |
|
180 |
&file_name); |
|
181 |
if (err != JVMTI_ERROR_NONE) { |
|
182 |
fprintf(stderr, "\tUnknown file\n"); |
|
183 |
continue; |
|
184 |
} |
|
185 |
||
186 |
// Compare now, none should be NULL. |
|
187 |
if (name == NULL) { |
|
188 |
fprintf(stderr, "\tUnknown name\n"); |
|
189 |
continue; |
|
190 |
} |
|
191 |
||
192 |
if (file_name == NULL) { |
|
193 |
fprintf(stderr, "\tUnknown file\n"); |
|
194 |
continue; |
|
195 |
} |
|
196 |
||
197 |
if (signature == NULL) { |
|
198 |
fprintf(stderr, "\tUnknown signature\n"); |
|
199 |
continue; |
|
200 |
} |
|
201 |
||
202 |
fprintf(stderr, "\t%s%s (%s: %d)\n", |
|
203 |
name, signature, file_name, line_number); |
|
204 |
} |
|
205 |
} |
|
206 |
||
207 |
static jboolean check_sample_content(JNIEnv* env, |
|
208 |
ObjectTrace* trace, |
|
209 |
ExpectedContentFrame *expected, |
|
210 |
size_t expected_count, |
|
51074 | 211 |
jboolean check_lines, |
50579 | 212 |
int print_out_comparisons) { |
213 |
jvmtiFrameInfo* frames; |
|
214 |
size_t i; |
|
215 |
||
216 |
if (expected_count > trace->frame_count) { |
|
217 |
return FALSE; |
|
218 |
} |
|
219 |
||
220 |
frames = trace->frames; |
|
221 |
for (i = 0; i < expected_count; i++) { |
|
222 |
// Get basic information out of the trace. |
|
223 |
jlocation bci = frames[i].location; |
|
224 |
jmethodID methodid = frames[i].method; |
|
225 |
char *name = NULL, *signature = NULL, *file_name = NULL; |
|
226 |
jclass declaring_class; |
|
227 |
int line_number; |
|
51074 | 228 |
jboolean differ; |
50579 | 229 |
jvmtiError err; |
230 |
||
231 |
if (bci < 0 && expected[i].line_number != -1) { |
|
232 |
return FALSE; |
|
233 |
} |
|
234 |
||
235 |
// Transform into usable information. |
|
236 |
line_number = get_line_number(jvmti, methodid, bci); |
|
237 |
(*jvmti)->GetMethodName(jvmti, methodid, &name, &signature, 0); |
|
238 |
||
239 |
if (JVMTI_ERROR_NONE != |
|
240 |
(*jvmti)->GetMethodDeclaringClass(jvmti, methodid, &declaring_class)) { |
|
241 |
return FALSE; |
|
242 |
} |
|
243 |
||
244 |
err = (*jvmti)->GetSourceFileName(jvmti, declaring_class, |
|
245 |
&file_name); |
|
246 |
if (err != JVMTI_ERROR_NONE) { |
|
247 |
return FALSE; |
|
248 |
} |
|
249 |
||
250 |
// Compare now, none should be NULL. |
|
251 |
if (name == NULL) { |
|
252 |
return FALSE; |
|
253 |
} |
|
254 |
||
255 |
if (file_name == NULL) { |
|
256 |
return FALSE; |
|
257 |
} |
|
258 |
||
259 |
if (signature == NULL) { |
|
260 |
return FALSE; |
|
261 |
} |
|
262 |
||
51074 | 263 |
differ = (strcmp(name, expected[i].name) || |
264 |
strcmp(signature, expected[i].signature) || |
|
265 |
strcmp(file_name, expected[i].file_name) || |
|
266 |
(check_lines && line_number != expected[i].line_number)); |
|
267 |
||
50579 | 268 |
if (print_out_comparisons) { |
51074 | 269 |
fprintf(stderr, "\tComparing: (check_lines: %d)\n", check_lines); |
50579 | 270 |
fprintf(stderr, "\t\tNames: %s and %s\n", name, expected[i].name); |
271 |
fprintf(stderr, "\t\tSignatures: %s and %s\n", signature, expected[i].signature); |
|
272 |
fprintf(stderr, "\t\tFile name: %s and %s\n", file_name, expected[i].file_name); |
|
273 |
fprintf(stderr, "\t\tLines: %d and %d\n", line_number, expected[i].line_number); |
|
51074 | 274 |
fprintf(stderr, "\t\tResult is %d\n", differ); |
50579 | 275 |
} |
276 |
||
51074 | 277 |
if (differ) { |
50579 | 278 |
return FALSE; |
279 |
} |
|
280 |
} |
|
281 |
||
282 |
return TRUE; |
|
283 |
} |
|
284 |
||
285 |
// Static native API for various tests. |
|
286 |
static void fill_native_frames(JNIEnv* env, jobjectArray frames, |
|
287 |
ExpectedContentFrame* native_frames, size_t size) { |
|
288 |
size_t i; |
|
289 |
for (i = 0; i < size; i++) { |
|
290 |
jobject obj = (*env)->GetObjectArrayElement(env, frames, (jsize) i); |
|
291 |
jclass frame_class = (*env)->GetObjectClass(env, obj); |
|
292 |
jfieldID line_number_field_id = (*env)->GetFieldID(env, frame_class, |
|
293 |
"lineNumber", "I"); |
|
294 |
int line_number = (*env)->GetIntField(env, obj, line_number_field_id); |
|
295 |
||
296 |
jfieldID string_id = (*env)->GetFieldID(env, frame_class, "method", |
|
297 |
"Ljava/lang/String;"); |
|
298 |
jstring string_object = (jstring) (*env)->GetObjectField(env, obj, |
|
299 |
string_id); |
|
300 |
const char* method = (*env)->GetStringUTFChars(env, string_object, 0); |
|
301 |
const char* file_name; |
|
302 |
const char* signature; |
|
303 |
||
304 |
string_id = (*env)->GetFieldID(env, frame_class, "fileName", |
|
305 |
"Ljava/lang/String;"); |
|
306 |
string_object = (jstring) (*env)->GetObjectField(env, obj, string_id); |
|
307 |
file_name = (*env)->GetStringUTFChars(env, string_object, 0); |
|
308 |
||
309 |
string_id = (*env)->GetFieldID(env, frame_class, "signature", |
|
310 |
"Ljava/lang/String;"); |
|
311 |
string_object = (jstring) (*env)->GetObjectField(env, obj, string_id); |
|
312 |
signature= (*env)->GetStringUTFChars(env, string_object, 0); |
|
313 |
||
314 |
native_frames[i].name = method; |
|
315 |
native_frames[i].file_name = file_name; |
|
316 |
native_frames[i].signature = signature; |
|
317 |
native_frames[i].line_number = line_number; |
|
318 |
} |
|
319 |
} |
|
320 |
||
321 |
// Internal storage system implementation. |
|
322 |
static EventStorage global_event_storage; |
|
323 |
static EventStorage second_global_event_storage; |
|
324 |
||
325 |
static void event_storage_set_compaction_required(EventStorage* storage) { |
|
326 |
event_storage_lock_compaction(storage); |
|
327 |
storage->compaction_required = 1; |
|
328 |
event_storage_unlock_compaction(storage); |
|
329 |
} |
|
330 |
||
331 |
static int event_storage_get_compaction_required(EventStorage* storage) { |
|
332 |
int result; |
|
333 |
event_storage_lock_compaction(storage); |
|
334 |
result = storage->compaction_required; |
|
335 |
event_storage_unlock_compaction(storage); |
|
336 |
return result; |
|
337 |
} |
|
338 |
||
339 |
static void event_storage_set_garbage_history(EventStorage* storage, int value) { |
|
340 |
size_t size; |
|
341 |
event_storage_lock(storage); |
|
342 |
global_event_storage.garbage_history_size = value; |
|
343 |
free(global_event_storage.garbage_collected_objects); |
|
344 |
size = sizeof(*global_event_storage.garbage_collected_objects) * value; |
|
345 |
global_event_storage.garbage_collected_objects = malloc(size); |
|
346 |
memset(global_event_storage.garbage_collected_objects, 0, size); |
|
347 |
event_storage_unlock(storage); |
|
348 |
} |
|
349 |
||
350 |
// No mutex here, it is handled by the caller. |
|
351 |
static void event_storage_add_garbage_collected_object(EventStorage* storage, |
|
352 |
ObjectTrace* object) { |
|
353 |
int idx = storage->garbage_history_index; |
|
354 |
ObjectTrace* old_object = storage->garbage_collected_objects[idx]; |
|
355 |
if (old_object != NULL) { |
|
356 |
free(old_object->frames); |
|
357 |
free(storage->garbage_collected_objects[idx]); |
|
358 |
} |
|
359 |
||
360 |
storage->garbage_collected_objects[idx] = object; |
|
361 |
storage->garbage_history_index = (idx + 1) % storage->garbage_history_size; |
|
362 |
} |
|
363 |
||
364 |
static int event_storage_get_count(EventStorage* storage) { |
|
365 |
int result; |
|
366 |
event_storage_lock(storage); |
|
367 |
result = storage->live_object_count; |
|
368 |
event_storage_unlock(storage); |
|
369 |
return result; |
|
370 |
} |
|
371 |
||
51138
914f305ba6fa
8205725: Update the JVMTI Spec for Heap Sampling
jcbeyler
parents:
51136
diff
changeset
|
372 |
static double event_storage_get_average_interval(EventStorage* storage) { |
50579 | 373 |
double accumulation = 0; |
374 |
int max_size; |
|
375 |
int i; |
|
376 |
||
377 |
event_storage_lock(storage); |
|
378 |
max_size = storage->live_object_count; |
|
379 |
||
380 |
for (i = 0; i < max_size; i++) { |
|
381 |
accumulation += storage->live_objects[i]->size; |
|
382 |
} |
|
383 |
||
384 |
event_storage_unlock(storage); |
|
385 |
return accumulation / max_size; |
|
386 |
} |
|
387 |
||
388 |
static jboolean event_storage_contains(JNIEnv* env, |
|
389 |
EventStorage* storage, |
|
390 |
ExpectedContentFrame* frames, |
|
51074 | 391 |
size_t size, |
392 |
jboolean check_lines) { |
|
50579 | 393 |
int i; |
394 |
event_storage_lock(storage); |
|
395 |
fprintf(stderr, "Checking storage count %d\n", storage->live_object_count); |
|
396 |
for (i = 0; i < storage->live_object_count; i++) { |
|
397 |
ObjectTrace* trace = storage->live_objects[i]; |
|
398 |
||
51074 | 399 |
if (check_sample_content(env, trace, frames, size, check_lines, PRINT_OUT)) { |
50579 | 400 |
event_storage_unlock(storage); |
401 |
return TRUE; |
|
402 |
} |
|
403 |
} |
|
404 |
event_storage_unlock(storage); |
|
405 |
return FALSE; |
|
406 |
} |
|
407 |
||
408 |
static jboolean event_storage_garbage_contains(JNIEnv* env, |
|
409 |
EventStorage* storage, |
|
410 |
ExpectedContentFrame* frames, |
|
51074 | 411 |
size_t size, |
412 |
jboolean check_lines) { |
|
50579 | 413 |
int i; |
414 |
event_storage_lock(storage); |
|
415 |
fprintf(stderr, "Checking garbage storage count %d\n", |
|
416 |
storage->garbage_history_size); |
|
417 |
for (i = 0; i < storage->garbage_history_size; i++) { |
|
418 |
ObjectTrace* trace = storage->garbage_collected_objects[i]; |
|
419 |
||
420 |
if (trace == NULL) { |
|
421 |
continue; |
|
422 |
} |
|
423 |
||
51074 | 424 |
if (check_sample_content(env, trace, frames, size, check_lines, PRINT_OUT)) { |
50579 | 425 |
event_storage_unlock(storage); |
426 |
return TRUE; |
|
427 |
} |
|
428 |
} |
|
429 |
event_storage_unlock(storage); |
|
430 |
return FALSE; |
|
431 |
} |
|
432 |
||
433 |
// No mutex here, handled by the caller. |
|
434 |
static void event_storage_augment_storage(EventStorage* storage) { |
|
435 |
int new_max = (storage->live_object_size * 2) + 1; |
|
436 |
ObjectTrace** new_objects = malloc(new_max * sizeof(*new_objects)); |
|
437 |
||
438 |
int current_count = storage->live_object_count; |
|
439 |
memcpy(new_objects, storage->live_objects, current_count * sizeof(*new_objects)); |
|
440 |
free(storage->live_objects); |
|
441 |
storage->live_objects = new_objects; |
|
442 |
storage->live_object_size = new_max; |
|
443 |
} |
|
444 |
||
445 |
static void event_storage_add(EventStorage* storage, |
|
446 |
JNIEnv* jni, |
|
447 |
jthread thread, |
|
448 |
jobject object, |
|
449 |
jclass klass, |
|
450 |
jlong size) { |
|
451 |
jvmtiFrameInfo frames[64]; |
|
452 |
jint count; |
|
453 |
jvmtiError err; |
|
454 |
||
455 |
err = (*jvmti)->GetStackTrace(jvmti, thread, 0, 64, frames, &count); |
|
456 |
if (err == JVMTI_ERROR_NONE && count >= 1) { |
|
457 |
ObjectTrace* live_object; |
|
458 |
jvmtiFrameInfo* allocated_frames = (jvmtiFrameInfo*) malloc(count * sizeof(*allocated_frames)); |
|
459 |
memcpy(allocated_frames, frames, count * sizeof(*allocated_frames)); |
|
460 |
||
461 |
live_object = (ObjectTrace*) malloc(sizeof(*live_object)); |
|
462 |
live_object->frames = allocated_frames; |
|
463 |
live_object->frame_count = count; |
|
464 |
live_object->size = size; |
|
465 |
live_object->thread = thread; |
|
466 |
live_object->object = (*jni)->NewWeakGlobalRef(jni, object); |
|
467 |
||
468 |
// Only now lock and get things done quickly. |
|
469 |
event_storage_lock(storage); |
|
470 |
||
471 |
storage->live_object_additions++; |
|
472 |
||
473 |
if (storage->live_object_count >= storage->live_object_size) { |
|
474 |
event_storage_augment_storage(storage); |
|
475 |
} |
|
476 |
assert(storage->live_object_count < storage->live_object_size); |
|
477 |
||
478 |
if (PRINT_OUT) { |
|
479 |
fprintf(stderr, "Adding trace for thread %p, frame_count %d, storage %p\n", |
|
480 |
thread, count, storage); |
|
481 |
print_out_frames(jni, live_object); |
|
482 |
} |
|
483 |
storage->live_objects[storage->live_object_count] = live_object; |
|
484 |
storage->live_object_count++; |
|
485 |
||
486 |
event_storage_unlock(storage); |
|
487 |
} |
|
488 |
} |
|
489 |
||
490 |
static void event_storage_compact(EventStorage* storage, JNIEnv* jni) { |
|
491 |
int max, i, dest; |
|
492 |
ObjectTrace** live_objects; |
|
493 |
||
494 |
event_storage_lock_compaction(storage); |
|
495 |
storage->compaction_required = 0; |
|
496 |
event_storage_unlock_compaction(storage); |
|
497 |
||
498 |
event_storage_lock(storage); |
|
499 |
||
500 |
max = storage->live_object_count; |
|
501 |
live_objects = storage->live_objects; |
|
502 |
||
503 |
for (i = 0, dest = 0; i < max; i++) { |
|
504 |
ObjectTrace* live_object = live_objects[i]; |
|
505 |
jweak object = live_object->object; |
|
506 |
||
507 |
if (!(*jni)->IsSameObject(jni, object, NULL)) { |
|
508 |
if (dest != i) { |
|
509 |
live_objects[dest] = live_object; |
|
510 |
dest++; |
|
511 |
} |
|
512 |
} else { |
|
513 |
(*jni)->DeleteWeakGlobalRef(jni, object); |
|
514 |
live_object->object = NULL; |
|
515 |
||
516 |
event_storage_add_garbage_collected_object(storage, live_object); |
|
517 |
} |
|
518 |
} |
|
519 |
||
520 |
storage->live_object_count = dest; |
|
521 |
event_storage_unlock(storage); |
|
522 |
} |
|
523 |
||
524 |
static void event_storage_free_objects(ObjectTrace** array, int max) { |
|
525 |
int i; |
|
526 |
for (i = 0; i < max; i++) { |
|
527 |
free(array[i]), array[i] = NULL; |
|
528 |
} |
|
529 |
} |
|
530 |
||
531 |
static void event_storage_reset(EventStorage* storage) { |
|
532 |
event_storage_lock(storage); |
|
533 |
||
534 |
// Reset everything except the mutex and the garbage collection. |
|
535 |
event_storage_free_objects(storage->live_objects, |
|
536 |
storage->live_object_count); |
|
537 |
storage->live_object_additions = 0; |
|
538 |
storage->live_object_size = 0; |
|
539 |
storage->live_object_count = 0; |
|
540 |
free(storage->live_objects), storage->live_objects = NULL; |
|
541 |
||
542 |
event_storage_free_objects(storage->garbage_collected_objects, |
|
543 |
storage->garbage_history_size); |
|
544 |
||
545 |
storage->compaction_required = 0; |
|
546 |
storage->garbage_history_index = 0; |
|
547 |
||
548 |
event_storage_unlock(storage); |
|
549 |
} |
|
550 |
||
551 |
static int event_storage_number_additions(EventStorage* storage) { |
|
552 |
int result; |
|
553 |
event_storage_lock(storage); |
|
554 |
result = storage->live_object_additions; |
|
555 |
event_storage_unlock(storage); |
|
556 |
return result; |
|
557 |
} |
|
558 |
||
559 |
// Start of the JVMTI agent code. |
|
560 |
static const char *EXC_CNAME = "java/lang/Exception"; |
|
561 |
||
562 |
static int check_error(jvmtiError err, const char *s) { |
|
563 |
if (err != JVMTI_ERROR_NONE) { |
|
564 |
printf(" ## %s error: %d\n", s, err); |
|
565 |
return 1; |
|
566 |
} |
|
567 |
return 0; |
|
568 |
} |
|
569 |
||
570 |
static int check_capability_error(jvmtiError err, const char *s) { |
|
571 |
if (err != JVMTI_ERROR_NONE) { |
|
572 |
if (err == JVMTI_ERROR_MUST_POSSESS_CAPABILITY) { |
|
573 |
return 0; |
|
574 |
} |
|
575 |
fprintf(stderr, " ## %s error: %d\n", s, err); |
|
576 |
} |
|
577 |
return 1; |
|
578 |
} |
|
579 |
||
580 |
static jint throw_exception(JNIEnv *env, char *msg) { |
|
581 |
jclass exc_class = JNI_ENV_PTR(env)->FindClass(JNI_ENV_ARG(env, EXC_CNAME)); |
|
582 |
||
583 |
if (exc_class == NULL) { |
|
584 |
fprintf(stderr, "throw_exception: Error in FindClass(env, %s)\n", |
|
585 |
EXC_CNAME); |
|
586 |
return -1; |
|
587 |
} |
|
588 |
return JNI_ENV_PTR(env)->ThrowNew(JNI_ENV_ARG(env, exc_class), msg); |
|
589 |
} |
|
590 |
||
591 |
static jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved); |
|
592 |
||
593 |
JNIEXPORT |
|
594 |
jint JNICALL Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) { |
|
595 |
return Agent_Initialize(jvm, options, reserved); |
|
596 |
} |
|
597 |
||
598 |
JNIEXPORT |
|
599 |
jint JNICALL Agent_OnAttach(JavaVM *jvm, char *options, void *reserved) { |
|
600 |
return Agent_Initialize(jvm, options, reserved); |
|
601 |
} |
|
602 |
||
603 |
JNIEXPORT |
|
604 |
jint JNICALL JNI_OnLoad(JavaVM *jvm, void *reserved) { |
|
605 |
return JNI_VERSION_1_8; |
|
606 |
} |
|
607 |
||
608 |
#define MAX_THREADS 500 |
|
609 |
||
610 |
typedef struct ThreadStats { |
|
611 |
int number_threads; |
|
612 |
int counts[MAX_THREADS]; |
|
613 |
int not_helper_counts[MAX_THREADS]; |
|
614 |
int index[MAX_THREADS]; |
|
615 |
jthread threads[MAX_THREADS]; |
|
616 |
||
617 |
int method_resolution_problem; |
|
618 |
} ThreadStats; |
|
619 |
||
620 |
static ThreadStats thread_stats; |
|
621 |
||
622 |
static void add_thread_count(jthread thread, int lock, int helper) { |
|
623 |
int i; |
|
624 |
jvmtiThreadInfo info; |
|
625 |
const char* name; |
|
626 |
char* end; |
|
627 |
int idx; |
|
628 |
int err; |
|
629 |
||
630 |
if (lock) { |
|
631 |
event_storage_lock(&global_event_storage); |
|
632 |
} |
|
633 |
||
634 |
for (i = 0; i < thread_stats.number_threads; i++) { |
|
635 |
if (thread_stats.threads[i] == thread) { |
|
636 |
if (helper) { |
|
637 |
thread_stats.counts[i]++; |
|
638 |
} else { |
|
639 |
thread_stats.not_helper_counts[i]++; |
|
640 |
} |
|
641 |
||
642 |
if (lock) { |
|
643 |
event_storage_unlock(&global_event_storage); |
|
644 |
} |
|
645 |
return; |
|
646 |
} |
|
647 |
} |
|
648 |
||
649 |
thread_stats.threads[thread_stats.number_threads] = thread; |
|
650 |
||
651 |
err = (*jvmti)->GetThreadInfo(jvmti, thread, &info); |
|
652 |
if (err != JVMTI_ERROR_NONE) { |
|
653 |
if (lock) { |
|
654 |
event_storage_unlock(&global_event_storage); |
|
655 |
} |
|
656 |
||
657 |
// Just to have it accounted as an error... |
|
658 |
info.name = "Allocator99"; |
|
659 |
} |
|
660 |
||
661 |
if (!strstr(info.name, "Allocator")) { |
|
662 |
if (lock) { |
|
663 |
event_storage_unlock(&global_event_storage); |
|
664 |
} |
|
665 |
||
666 |
// Just to have it accounted as an error... |
|
667 |
info.name = "Allocator98"; |
|
668 |
} |
|
669 |
||
670 |
name = info.name + 9; |
|
671 |
end = NULL; |
|
672 |
idx = strtol(name, &end, 0); |
|
673 |
||
674 |
if (*end == '\0') { |
|
675 |
if (helper) { |
|
676 |
thread_stats.counts[thread_stats.number_threads]++; |
|
677 |
} else { |
|
678 |
thread_stats.not_helper_counts[thread_stats.number_threads]++; |
|
679 |
} |
|
680 |
||
681 |
thread_stats.index[thread_stats.number_threads] = idx; |
|
682 |
thread_stats.number_threads++; |
|
683 |
} else { |
|
684 |
fprintf(stderr, "Problem with thread name...: %p %s\n", thread, name); |
|
685 |
} |
|
686 |
||
687 |
if (PRINT_OUT) { |
|
688 |
fprintf(stderr, "Added %s - %p - %d - lock: %d\n", info.name, thread, idx, lock); |
|
689 |
} |
|
690 |
||
691 |
if (lock) { |
|
692 |
event_storage_unlock(&global_event_storage); |
|
693 |
} |
|
694 |
} |
|
695 |
||
696 |
static void print_thread_stats() { |
|
697 |
int i; |
|
698 |
event_storage_lock(&global_event_storage); |
|
699 |
fprintf(stderr, "Method resolution problem: %d\n", thread_stats.method_resolution_problem); |
|
700 |
fprintf(stderr, "Thread count:\n"); |
|
701 |
for (i = 0; i < thread_stats.number_threads; i++) { |
|
702 |
fprintf(stderr, "\t%p: %d: %d - %d\n", thread_stats.threads[i], |
|
703 |
thread_stats.index[i], |
|
704 |
thread_stats.counts[i], |
|
705 |
thread_stats.not_helper_counts[i]); |
|
706 |
} |
|
707 |
event_storage_unlock(&global_event_storage); |
|
708 |
} |
|
709 |
||
710 |
JNIEXPORT |
|
711 |
void JNICALL SampledObjectAlloc(jvmtiEnv *jvmti_env, |
|
712 |
JNIEnv* jni_env, |
|
713 |
jthread thread, |
|
714 |
jobject object, |
|
715 |
jclass object_klass, |
|
716 |
jlong size) { |
|
717 |
add_thread_count(thread, 1, 1); |
|
718 |
||
719 |
if (event_storage_get_compaction_required(&global_event_storage)) { |
|
720 |
event_storage_compact(&global_event_storage, jni_env); |
|
721 |
} |
|
722 |
||
723 |
event_storage_add(&global_event_storage, jni_env, thread, object, |
|
724 |
object_klass, size); |
|
725 |
} |
|
726 |
||
727 |
JNIEXPORT |
|
728 |
void JNICALL VMObjectAlloc(jvmtiEnv *jvmti_env, |
|
729 |
JNIEnv* jni_env, |
|
730 |
jthread thread, |
|
731 |
jobject object, |
|
732 |
jclass object_klass, |
|
733 |
jlong size) { |
|
734 |
event_storage_add(&second_global_event_storage, jni_env, thread, object, |
|
735 |
object_klass, size); |
|
736 |
} |
|
737 |
||
738 |
JNIEXPORT |
|
739 |
void JNICALL GarbageCollectionFinish(jvmtiEnv *jvmti_env) { |
|
740 |
event_storage_set_compaction_required(&global_event_storage); |
|
741 |
} |
|
742 |
||
743 |
static int enable_notifications() { |
|
744 |
if (check_error((*jvmti)->SetEventNotificationMode( |
|
745 |
jvmti, JVMTI_ENABLE, JVMTI_EVENT_GARBAGE_COLLECTION_FINISH, NULL), |
|
746 |
"Set event notifications")) { |
|
747 |
return 1; |
|
748 |
} |
|
749 |
||
750 |
return check_error((*jvmti)->SetEventNotificationMode( |
|
751 |
jvmti, JVMTI_ENABLE, JVMTI_EVENT_SAMPLED_OBJECT_ALLOC, NULL), |
|
752 |
"Set event notifications"); |
|
753 |
} |
|
754 |
||
755 |
static int enable_notifications_for_two_threads(jthread first, jthread second) { |
|
756 |
if (check_error((*jvmti)->SetEventNotificationMode( |
|
757 |
jvmti, JVMTI_ENABLE, JVMTI_EVENT_GARBAGE_COLLECTION_FINISH, NULL), |
|
758 |
"Set event notifications")) { |
|
759 |
return 0; |
|
760 |
} |
|
761 |
||
762 |
if (check_error((*jvmti)->SetEventNotificationMode( |
|
763 |
jvmti, JVMTI_ENABLE, JVMTI_EVENT_SAMPLED_OBJECT_ALLOC, first), |
|
764 |
"Set event notifications")) { |
|
765 |
return 0; |
|
766 |
} |
|
767 |
||
768 |
// Second thread should fail. |
|
769 |
if (check_error((*jvmti)->SetEventNotificationMode( |
|
770 |
jvmti, JVMTI_ENABLE, JVMTI_EVENT_SAMPLED_OBJECT_ALLOC, second), |
|
771 |
"Set event notifications")) { |
|
772 |
return 0; |
|
773 |
} |
|
774 |
||
775 |
return 1; |
|
776 |
} |
|
777 |
||
778 |
static |
|
779 |
jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) { |
|
780 |
jint res; |
|
781 |
jvmtiEventCallbacks callbacks; |
|
782 |
jvmtiCapabilities caps; |
|
783 |
||
784 |
res = JNI_ENV_PTR(jvm)->GetEnv(JNI_ENV_ARG(jvm, (void **) &jvmti), |
|
785 |
JVMTI_VERSION_9); |
|
786 |
if (res != JNI_OK || jvmti == NULL) { |
|
787 |
fprintf(stderr, "Error: wrong result of a valid call to GetEnv!\n"); |
|
788 |
return JNI_ERR; |
|
789 |
} |
|
790 |
||
791 |
// Get second jvmti environment. |
|
792 |
res = JNI_ENV_PTR(jvm)->GetEnv(JNI_ENV_ARG(jvm, (void **) &second_jvmti), |
|
793 |
JVMTI_VERSION_9); |
|
794 |
if (res != JNI_OK || second_jvmti == NULL) { |
|
795 |
fprintf(stderr, "Error: wrong result of a valid second call to GetEnv!\n"); |
|
796 |
return JNI_ERR; |
|
797 |
} |
|
798 |
||
799 |
if (PRINT_OUT) { |
|
800 |
fprintf(stderr, "Storage is at %p, secondary is at %p\n", |
|
801 |
&global_event_storage, &second_global_event_storage); |
|
802 |
} |
|
803 |
||
804 |
(*jvmti)->CreateRawMonitor(jvmti, "storage_monitor", |
|
805 |
&global_event_storage.storage_monitor); |
|
806 |
(*jvmti)->CreateRawMonitor(jvmti, "second_storage_monitor", |
|
807 |
&second_global_event_storage.storage_monitor); |
|
808 |
||
809 |
(*jvmti)->CreateRawMonitor(jvmti, "compaction_monitor", |
|
810 |
&global_event_storage.compaction_monitor); |
|
811 |
(*jvmti)->CreateRawMonitor(jvmti, "second_compaction_monitor", |
|
812 |
&second_global_event_storage.compaction_monitor); |
|
813 |
||
814 |
event_storage_set_garbage_history(&global_event_storage, 200); |
|
815 |
event_storage_set_garbage_history(&second_global_event_storage, 200); |
|
816 |
||
817 |
memset(&callbacks, 0, sizeof(callbacks)); |
|
818 |
callbacks.SampledObjectAlloc = &SampledObjectAlloc; |
|
819 |
callbacks.VMObjectAlloc = &VMObjectAlloc; |
|
820 |
callbacks.GarbageCollectionFinish = &GarbageCollectionFinish; |
|
821 |
||
822 |
memset(&caps, 0, sizeof(caps)); |
|
823 |
// Get line numbers, sample events, filename, and gc events for the tests. |
|
824 |
caps.can_get_line_numbers = 1; |
|
825 |
caps.can_get_source_file_name = 1; |
|
826 |
caps.can_generate_garbage_collection_events = 1; |
|
827 |
caps.can_generate_sampled_object_alloc_events = 1; |
|
828 |
caps.can_generate_vm_object_alloc_events = 1; |
|
829 |
if (check_error((*jvmti)->AddCapabilities(jvmti, &caps), "Add capabilities")) { |
|
830 |
return JNI_ERR; |
|
831 |
} |
|
832 |
||
833 |
if (check_error((*jvmti)->SetEventCallbacks(jvmti, &callbacks, |
|
834 |
sizeof(jvmtiEventCallbacks)), |
|
835 |
" Set Event Callbacks")) { |
|
836 |
return JNI_ERR; |
|
837 |
} |
|
838 |
return JNI_OK; |
|
839 |
} |
|
840 |
||
841 |
JNIEXPORT void JNICALL |
|
51138
914f305ba6fa
8205725: Update the JVMTI Spec for Heap Sampling
jcbeyler
parents:
51136
diff
changeset
|
842 |
Java_MyPackage_HeapMonitor_setSamplingInterval(JNIEnv* env, jclass cls, jint value) { |
914f305ba6fa
8205725: Update the JVMTI Spec for Heap Sampling
jcbeyler
parents:
51136
diff
changeset
|
843 |
(*jvmti)->SetHeapSamplingInterval(jvmti, value); |
50579 | 844 |
} |
845 |
||
846 |
JNIEXPORT jboolean JNICALL |
|
847 |
Java_MyPackage_HeapMonitor_eventStorageIsEmpty(JNIEnv* env, jclass cls) { |
|
848 |
return event_storage_get_count(&global_event_storage) == 0; |
|
849 |
} |
|
850 |
||
851 |
JNIEXPORT jint JNICALL |
|
852 |
Java_MyPackage_HeapMonitor_getEventStorageElementCount(JNIEnv* env, jclass cls) { |
|
853 |
return event_storage_get_count(&global_event_storage); |
|
854 |
} |
|
855 |
||
856 |
JNIEXPORT void JNICALL |
|
857 |
Java_MyPackage_HeapMonitor_enableSamplingEvents(JNIEnv* env, jclass cls) { |
|
858 |
enable_notifications(); |
|
859 |
} |
|
860 |
||
861 |
JNIEXPORT jboolean JNICALL |
|
862 |
Java_MyPackage_HeapMonitor_enableSamplingEventsForTwoThreads(JNIEnv* env, |
|
863 |
jclass cls, |
|
864 |
jthread first, |
|
865 |
jthread second) { |
|
866 |
return enable_notifications_for_two_threads(first, second); |
|
867 |
} |
|
868 |
||
869 |
JNIEXPORT void JNICALL |
|
870 |
Java_MyPackage_HeapMonitor_disableSamplingEvents(JNIEnv* env, jclass cls) { |
|
871 |
check_error((*jvmti)->SetEventNotificationMode( |
|
872 |
jvmti, JVMTI_DISABLE, JVMTI_EVENT_SAMPLED_OBJECT_ALLOC, NULL), |
|
873 |
"Set event notifications"); |
|
874 |
||
875 |
check_error((*jvmti)->SetEventNotificationMode( |
|
876 |
jvmti, JVMTI_DISABLE, JVMTI_EVENT_GARBAGE_COLLECTION_FINISH, NULL), |
|
877 |
"Garbage Collection Finish"); |
|
878 |
} |
|
879 |
||
880 |
JNIEXPORT jboolean JNICALL |
|
51074 | 881 |
Java_MyPackage_HeapMonitor_obtainedEvents(JNIEnv* env, jclass cls, |
882 |
jobjectArray frames, |
|
883 |
jboolean check_lines) { |
|
50579 | 884 |
jboolean result; |
885 |
jsize size = (*env)->GetArrayLength(env, frames); |
|
886 |
ExpectedContentFrame *native_frames = malloc(size * sizeof(*native_frames)); |
|
887 |
||
888 |
if (native_frames == NULL) { |
|
889 |
return 0; |
|
890 |
} |
|
891 |
||
892 |
fill_native_frames(env, frames, native_frames, size); |
|
51074 | 893 |
result = event_storage_contains(env, &global_event_storage, native_frames, |
894 |
size, check_lines); |
|
50579 | 895 |
|
896 |
free(native_frames), native_frames = NULL; |
|
897 |
return result; |
|
898 |
} |
|
899 |
||
900 |
JNIEXPORT jboolean JNICALL |
|
51074 | 901 |
Java_MyPackage_HeapMonitor_garbageContains(JNIEnv* env, jclass cls, |
902 |
jobjectArray frames, |
|
903 |
jboolean check_lines) { |
|
50579 | 904 |
jboolean result; |
905 |
jsize size = (*env)->GetArrayLength(env, frames); |
|
906 |
ExpectedContentFrame *native_frames = malloc(size * sizeof(*native_frames)); |
|
907 |
||
908 |
if (native_frames == NULL) { |
|
909 |
return 0; |
|
910 |
} |
|
911 |
||
912 |
fill_native_frames(env, frames, native_frames, size); |
|
51074 | 913 |
result = event_storage_garbage_contains(env, &global_event_storage, |
914 |
native_frames, size, check_lines); |
|
50579 | 915 |
|
916 |
free(native_frames), native_frames = NULL; |
|
917 |
return result; |
|
918 |
} |
|
919 |
||
920 |
JNIEXPORT void JNICALL |
|
921 |
Java_MyPackage_HeapMonitor_forceGarbageCollection(JNIEnv* env, jclass cls) { |
|
922 |
check_error((*jvmti)->ForceGarbageCollection(jvmti), |
|
923 |
"Forced Garbage Collection"); |
|
924 |
} |
|
925 |
||
926 |
JNIEXPORT void JNICALL |
|
927 |
Java_MyPackage_HeapMonitor_resetEventStorage(JNIEnv* env, jclass cls) { |
|
928 |
event_storage_reset(&global_event_storage); |
|
929 |
event_storage_reset(&second_global_event_storage); |
|
930 |
} |
|
931 |
||
932 |
JNIEXPORT jboolean JNICALL |
|
933 |
Java_MyPackage_HeapMonitorNoCapabilityTest_allSamplingMethodsFail(JNIEnv *env, |
|
934 |
jclass cls) { |
|
935 |
jvmtiCapabilities caps; |
|
936 |
memset(&caps, 0, sizeof(caps)); |
|
937 |
caps.can_generate_sampled_object_alloc_events = 1; |
|
938 |
if (check_error((*jvmti)->RelinquishCapabilities(jvmti, &caps), |
|
939 |
"Add capabilities\n")){ |
|
940 |
return FALSE; |
|
941 |
} |
|
942 |
||
51138
914f305ba6fa
8205725: Update the JVMTI Spec for Heap Sampling
jcbeyler
parents:
51136
diff
changeset
|
943 |
if (check_capability_error((*jvmti)->SetHeapSamplingInterval(jvmti, 1<<19), |
914f305ba6fa
8205725: Update the JVMTI Spec for Heap Sampling
jcbeyler
parents:
51136
diff
changeset
|
944 |
"Set Heap Sampling Interval")) { |
50579 | 945 |
return FALSE; |
946 |
} |
|
947 |
return TRUE; |
|
948 |
} |
|
949 |
||
950 |
JNIEXPORT jboolean JNICALL |
|
951 |
Java_MyPackage_HeapMonitorIllegalArgumentTest_testIllegalArgument(JNIEnv *env, |
|
952 |
jclass cls) { |
|
51138
914f305ba6fa
8205725: Update the JVMTI Spec for Heap Sampling
jcbeyler
parents:
51136
diff
changeset
|
953 |
if (check_error((*jvmti)->SetHeapSamplingInterval(jvmti, 0), |
914f305ba6fa
8205725: Update the JVMTI Spec for Heap Sampling
jcbeyler
parents:
51136
diff
changeset
|
954 |
"Sampling interval 0 failed\n")){ |
50579 | 955 |
return FALSE; |
956 |
} |
|
957 |
||
51138
914f305ba6fa
8205725: Update the JVMTI Spec for Heap Sampling
jcbeyler
parents:
51136
diff
changeset
|
958 |
if (check_error((*jvmti)->SetHeapSamplingInterval(jvmti, 1024), |
914f305ba6fa
8205725: Update the JVMTI Spec for Heap Sampling
jcbeyler
parents:
51136
diff
changeset
|
959 |
"Sampling interval 1024 failed\n")){ |
50579 | 960 |
return FALSE; |
961 |
} |
|
962 |
||
51138
914f305ba6fa
8205725: Update the JVMTI Spec for Heap Sampling
jcbeyler
parents:
51136
diff
changeset
|
963 |
if (!check_error((*jvmti)->SetHeapSamplingInterval(jvmti, -1), |
914f305ba6fa
8205725: Update the JVMTI Spec for Heap Sampling
jcbeyler
parents:
51136
diff
changeset
|
964 |
"Sampling interval -1 passed\n")){ |
50579 | 965 |
return FALSE; |
966 |
} |
|
967 |
||
51138
914f305ba6fa
8205725: Update the JVMTI Spec for Heap Sampling
jcbeyler
parents:
51136
diff
changeset
|
968 |
if (!check_error((*jvmti)->SetHeapSamplingInterval(jvmti, -1024), |
914f305ba6fa
8205725: Update the JVMTI Spec for Heap Sampling
jcbeyler
parents:
51136
diff
changeset
|
969 |
"Sampling interval -1024 passed\n")){ |
50579 | 970 |
return FALSE; |
971 |
} |
|
972 |
||
973 |
return TRUE; |
|
974 |
} |
|
975 |
||
976 |
JNIEXPORT jdouble JNICALL |
|
51138
914f305ba6fa
8205725: Update the JVMTI Spec for Heap Sampling
jcbeyler
parents:
51136
diff
changeset
|
977 |
Java_MyPackage_HeapMonitorStatIntervalTest_getAverageInterval(JNIEnv *env, jclass cls) { |
914f305ba6fa
8205725: Update the JVMTI Spec for Heap Sampling
jcbeyler
parents:
51136
diff
changeset
|
978 |
return event_storage_get_average_interval(&global_event_storage); |
50579 | 979 |
} |
980 |
||
981 |
typedef struct sThreadsFound { |
|
982 |
jthread* threads; |
|
983 |
int num_threads; |
|
984 |
} ThreadsFound; |
|
985 |
||
986 |
JNIEXPORT jboolean JNICALL |
|
987 |
Java_MyPackage_HeapMonitorThreadTest_checkSamples(JNIEnv* env, jclass cls, |
|
988 |
jint num_threads) { |
|
989 |
||
990 |
print_thread_stats(); |
|
991 |
// Ensure we got stacks from at least num_threads. |
|
992 |
return thread_stats.number_threads >= num_threads; |
|
993 |
} |
|
994 |
||
995 |
JNIEXPORT |
|
996 |
void JNICALL SampledObjectAlloc2(jvmtiEnv *jvmti_env, |
|
997 |
JNIEnv* jni_env, |
|
998 |
jthread thread, |
|
999 |
jobject object, |
|
1000 |
jclass object_klass, |
|
1001 |
jlong size) { |
|
1002 |
// Nop for now, two agents are not yet implemented. |
|
1003 |
assert(0); |
|
1004 |
} |
|
1005 |
||
1006 |
JNIEXPORT jboolean JNICALL |
|
1007 |
Java_MyPackage_HeapMonitorTwoAgentsTest_enablingSamplingInSecondaryAgent( |
|
1008 |
JNIEnv* env, jclass cls) { |
|
1009 |
// Currently this method should be failing directly at the AddCapability step |
|
1010 |
// but the implementation is correct for when multi-agent support is enabled. |
|
1011 |
jvmtiCapabilities caps; |
|
1012 |
jvmtiEventCallbacks callbacks; |
|
1013 |
||
1014 |
memset(&caps, 0, sizeof(caps)); |
|
1015 |
caps.can_generate_sampled_object_alloc_events = 1; |
|
1016 |
if (check_error((*second_jvmti)->AddCapabilities(second_jvmti, &caps), |
|
1017 |
"Set the capability for second agent")) { |
|
1018 |
return FALSE; |
|
1019 |
} |
|
1020 |
||
1021 |
memset(&callbacks, 0, sizeof(callbacks)); |
|
1022 |
callbacks.SampledObjectAlloc = &SampledObjectAlloc2; |
|
1023 |
||
1024 |
if (check_error((*second_jvmti)->SetEventCallbacks(second_jvmti, &callbacks, |
|
1025 |
sizeof(jvmtiEventCallbacks)), |
|
1026 |
" Set Event Callbacks for second agent")) { |
|
1027 |
return FALSE; |
|
1028 |
} |
|
1029 |
||
1030 |
return TRUE; |
|
1031 |
} |
|
1032 |
||
1033 |
JNIEXPORT void JNICALL |
|
1034 |
Java_MyPackage_HeapMonitor_enableVMEvents(JNIEnv* env, jclass cls) { |
|
1035 |
check_error((*jvmti)->SetEventNotificationMode( |
|
1036 |
jvmti, JVMTI_ENABLE, JVMTI_EVENT_VM_OBJECT_ALLOC, NULL), |
|
1037 |
"Set vm event notifications"); |
|
1038 |
} |
|
1039 |
||
1040 |
JNIEXPORT jint JNICALL |
|
1041 |
Java_MyPackage_HeapMonitorVMEventsTest_vmEvents(JNIEnv* env, jclass cls) { |
|
1042 |
return event_storage_number_additions(&second_global_event_storage); |
|
1043 |
} |
|
1044 |
||
1045 |
JNIEXPORT jint JNICALL |
|
1046 |
Java_MyPackage_HeapMonitor_sampledEvents(JNIEnv* env, jclass cls) { |
|
1047 |
return event_storage_number_additions(&global_event_storage); |
|
1048 |
} |
|
1049 |
||
1050 |
static void allocate_object(JNIEnv* env) { |
|
1051 |
// Construct an Object. |
|
1052 |
jclass cls = (*env)->FindClass(env, "java/lang/Object"); |
|
1053 |
jmethodID constructor; |
|
1054 |
||
1055 |
if (cls == NULL) { |
|
1056 |
throw_exception(env, "Cannot find Object class."); |
|
1057 |
return; |
|
1058 |
} |
|
1059 |
||
1060 |
constructor = (*env)->GetMethodID(env, cls, "<init>", "()V"); |
|
1061 |
||
1062 |
if (constructor == NULL) { |
|
1063 |
throw_exception(env, "Cannot find Object class constructor."); |
|
1064 |
return; |
|
1065 |
} |
|
1066 |
||
1067 |
// Call back constructor to allocate a new instance, with an int argument |
|
1068 |
(*env)->NewObject(env, cls, constructor); |
|
1069 |
} |
|
1070 |
||
1071 |
// Ensure we got a callback for the test. |
|
1072 |
static int did_recursive_callback_test; |
|
1073 |
||
1074 |
JNIEXPORT |
|
1075 |
void JNICALL RecursiveSampledObjectAlloc(jvmtiEnv *jvmti_env, |
|
1076 |
JNIEnv* jni_env, |
|
1077 |
jthread thread, |
|
1078 |
jobject object, |
|
1079 |
jclass object_klass, |
|
1080 |
jlong size) { |
|
1081 |
// Basically ensure that if we were to allocate objects, we would not have an |
|
1082 |
// infinite recursion here. |
|
1083 |
int i; |
|
1084 |
for (i = 0; i < 1000; i++) { |
|
1085 |
allocate_object(jni_env); |
|
1086 |
} |
|
1087 |
||
1088 |
did_recursive_callback_test = 1; |
|
1089 |
} |
|
1090 |
||
1091 |
JNIEXPORT jboolean JNICALL |
|
1092 |
Java_MyPackage_HeapMonitorRecursiveTest_didCallback(JNIEnv* env, jclass cls) { |
|
1093 |
return did_recursive_callback_test != 0; |
|
1094 |
} |
|
1095 |
||
1096 |
JNIEXPORT void JNICALL |
|
1097 |
Java_MyPackage_HeapMonitorRecursiveTest_setCallbackToCallAllocateSomeMore(JNIEnv* env, jclass cls) { |
|
1098 |
jvmtiEventCallbacks callbacks; |
|
1099 |
||
1100 |
memset(&callbacks, 0, sizeof(callbacks)); |
|
1101 |
callbacks.SampledObjectAlloc = &RecursiveSampledObjectAlloc; |
|
1102 |
||
1103 |
if (check_error((*jvmti)->SetEventCallbacks(jvmti, &callbacks, |
|
1104 |
sizeof(jvmtiEventCallbacks)), |
|
1105 |
" Set Event Callbacks")) { |
|
1106 |
throw_exception(env, "Cannot reset the callback."); |
|
1107 |
return; |
|
1108 |
} |
|
1109 |
} |
|
1110 |
||
1111 |
#ifdef __cplusplus |
|
1112 |
} |
|
1113 |
#endif |