< prev index next >

test/hotspot/jtreg/serviceability/jvmti/HeapMonitor/libHeapMonitorTest.c

Print this page
rev 49644 : [mq]: event_rebased
rev 49645 : [mq]: heap9
rev 49646 : [mq]: heap11
rev 49648 : [mq]: heap13
rev 49649 : [mq]: heap14

@@ -59,10 +59,11 @@
   size_t frame_count;
   jthread thread;
 } ObjectTrace;
 
 typedef struct _EventStorage {
+  int live_object_additions;
   int live_object_size;
   int live_object_count;
   ObjectTrace** live_objects;
 
   int garbage_history_size;

@@ -141,11 +142,11 @@
     // Get basic information out of the trace.
     int bci = frames[i].location;
     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;
     }
 
     // Transform into usable information.
     int line_number = get_line_number(jvmti, methodid, bci);

@@ -235,10 +236,11 @@
 }
 
 // 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);
   storage->compaction_required = 1;
   pthread_mutex_unlock(&storage->compaction_mutex);

@@ -371,10 +373,12 @@
     live_object->object = (*jni)->NewWeakGlobalRef(jni, object);
 
     // 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);
     }
     assert(storage->live_object_count < storage->live_object_size);
 

@@ -425,10 +429,11 @@
   pthread_mutex_lock(&storage->storage_mutex);
 
   // 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;
 
   event_storage_free_objects(storage->garbage_collected_objects,

@@ -438,10 +443,17 @@
   storage->garbage_history_index = 0;
 
   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";
 
 static int check_error(jvmtiError err, const char *s) {

@@ -506,10 +518,21 @@
   event_storage_add(&global_event_storage, jni_env, thread, object,
                     object_klass, size);
 }
 
 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);
 }
 
 static int enable_notifications() {

@@ -568,20 +591,26 @@
 
   pthread_mutex_init(&global_event_storage.storage_mutex, 0);
   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;
   memset(&caps, 0, sizeof(caps));
   // 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")) {
     return JNI_ERR;
   }

@@ -655,11 +684,12 @@
               "Forced Garbage Collection");
 }
 
 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
 Java_MyPackage_HeapMonitorNoCapabilityTest_allSamplingMethodsFail(JNIEnv *env,
                                                                   jclass cls) {

@@ -812,12 +842,10 @@
 
   int obtained_threads = thread_data.num_threads;
   return obtained_threads == expected_num_threads;
 }
 
-static EventStorage second_global_event_storage;
-
 JNIEXPORT
 void JNICALL SampledObjectAlloc2(jvmtiEnv *jvmti_env,
                                  JNIEnv* jni_env,
                                  jthread thread,
                                  jobject object,

@@ -828,14 +856,10 @@
 }
 
 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;
   if (check_error((*second_jvmti)->AddCapabilities(second_jvmti, &caps),
                   "Set the capability for second agent")) {

@@ -869,8 +893,35 @@
       event_storage_contains(env, &second_global_event_storage, native_frames, size);
 
   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
< prev index next >