--- old/test/hotspot/jtreg/serviceability/jvmti/HeapMonitor/libHeapMonitorTest.c 2018-04-11 14:33:24.077156672 -0700 +++ new/test/hotspot/jtreg/serviceability/jvmti/HeapMonitor/libHeapMonitorTest.c 2018-04-11 14:33:23.753157779 -0700 @@ -61,6 +61,7 @@ } ObjectTrace; typedef struct _EventStorage { + int live_object_additions; int live_object_size; int live_object_count; ObjectTrace** live_objects; @@ -143,7 +144,7 @@ jmethodID methodid = frames[i].method; char *name = NULL, *signature = NULL, *file_name = NULL; - if (bci < 0) { + if (bci < 0 && expected[i].line_number != -1) { return FALSE; } @@ -237,6 +238,7 @@ // Internal storage system implementation. static EventStorage global_event_storage; +static EventStorage second_global_event_storage; static void event_storage_set_compaction_required(EventStorage* storage) { pthread_mutex_lock(&storage->compaction_mutex); @@ -373,6 +375,8 @@ // Only now lock and get things done quickly. pthread_mutex_lock(&storage->storage_mutex); + storage->live_object_additions++; + if (storage->live_object_count >= storage->live_object_size) { event_storage_augment_storage(storage); } @@ -427,6 +431,7 @@ // Reset everything except the mutex and the garbage collection. event_storage_free_objects(storage->live_objects, storage->live_object_count); + storage->live_object_additions = 0; storage->live_object_size = 0; storage->live_object_count = 0; free(storage->live_objects), storage->live_objects = NULL; @@ -440,6 +445,13 @@ pthread_mutex_unlock(&storage->storage_mutex); } +static int event_storage_number_additions(EventStorage* storage) { + pthread_mutex_lock(&storage->storage_mutex); + int result = storage->live_object_additions; + pthread_mutex_unlock(&storage->storage_mutex); + return result; +} + // Start of the JVMTI agent code. static const char *EXC_CNAME = "java/lang/Exception"; @@ -508,6 +520,17 @@ } JNIEXPORT +void JNICALL VMObjectAlloc(jvmtiEnv *jvmti_env, + JNIEnv* jni_env, + jthread thread, + jobject object, + jclass object_klass, + jlong size) { + event_storage_add(&second_global_event_storage, jni_env, thread, object, + object_klass, size); +} + +JNIEXPORT void JNICALL GarbageCollectionFinish(jvmtiEnv *jvmti_env) { event_storage_set_compaction_required(&global_event_storage); } @@ -570,9 +593,14 @@ pthread_mutex_init(&global_event_storage.compaction_mutex, 0); event_storage_set_garbage_history(&global_event_storage, 200); + pthread_mutex_init(&second_global_event_storage.storage_mutex, 0); + pthread_mutex_init(&second_global_event_storage.compaction_mutex, 0); + event_storage_set_garbage_history(&second_global_event_storage, 200); + jvmtiEventCallbacks callbacks; memset(&callbacks, 0, sizeof(callbacks)); callbacks.SampledObjectAlloc = &SampledObjectAlloc; + callbacks.VMObjectAlloc = &VMObjectAlloc; callbacks.GarbageCollectionFinish = &GarbageCollectionFinish; jvmtiCapabilities caps; @@ -580,6 +608,7 @@ // Get line numbers, sample events, filename, and gc events for the tests. caps.can_get_line_numbers = 1; caps.can_generate_sampled_alloc_events = 1; + caps.can_generate_vm_object_alloc_events = 1; caps.can_get_source_file_name = 1; caps.can_generate_garbage_collection_events = 1; if (check_error((*jvmti)->AddCapabilities(jvmti, &caps), "Add capabilities")) { @@ -657,7 +686,8 @@ JNIEXPORT void JNICALL Java_MyPackage_HeapMonitor_resetEventStorage(JNIEnv* env, jclass cls) { - return event_storage_reset(&global_event_storage); + event_storage_reset(&global_event_storage); + event_storage_reset(&second_global_event_storage); } JNIEXPORT jboolean JNICALL @@ -814,8 +844,6 @@ return obtained_threads == expected_num_threads; } -static EventStorage second_global_event_storage; - JNIEXPORT void JNICALL SampledObjectAlloc2(jvmtiEnv *jvmti_env, JNIEnv* jni_env, @@ -830,10 +858,6 @@ JNIEXPORT jboolean JNICALL Java_MyPackage_HeapMonitorTwoAgentsTest_enablingSamplingInSecondaryAgent( JNIEnv* env, jclass cls) { - pthread_mutex_init(&second_global_event_storage.storage_mutex, 0); - pthread_mutex_init(&second_global_event_storage.compaction_mutex, 0); - event_storage_set_garbage_history(&second_global_event_storage, 200); - jvmtiCapabilities caps; memset(&caps, 0, sizeof(caps)); caps.can_generate_sampled_alloc_events = 1; @@ -871,6 +895,33 @@ return first_storage_contained_events && second_storage_contained_events; } +JNIEXPORT void JNICALL +Java_MyPackage_HeapMonitorVMEventsTest_enableVMEvents(JNIEnv* env, jclass cls) { + check_error((*jvmti)->SetEventNotificationMode( + jvmti, JVMTI_ENABLE, JVMTI_EVENT_VM_OBJECT_ALLOC, NULL), + "Set vm event notifications"); +} + +JNIEXPORT jboolean JNICALL +Java_MyPackage_HeapMonitorVMEventsTest_vmStorageContained( + JNIEnv* env, jclass cls, jobjectArray frames) { + jsize size = (*env)->GetArrayLength(env, frames); + ExpectedContentFrame native_frames[size]; + fill_native_frames(env, frames, native_frames, size); + + return event_storage_contains(env, &second_global_event_storage, native_frames, size); +} + +JNIEXPORT jint JNICALL +Java_MyPackage_HeapMonitorVMEventsTest_vmEvents(JNIEnv* env, jclass cls) { + return event_storage_number_additions(&second_global_event_storage); +} + +JNIEXPORT jint JNICALL +Java_MyPackage_HeapMonitorVMEventsTest_sampledEvents(JNIEnv* env, jclass cls) { + return event_storage_number_additions(&global_event_storage); +} + #ifdef __cplusplus } #endif