< prev index next >

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

Print this page
rev 52132 : 8201655: Add thread-enabled support for the Heap Sampling
Summary:
Reviewed-by:

@@ -699,103 +699,85 @@
 #define MAX_THREADS 500
 
 typedef struct ThreadStats {
   int number_threads;
   int counts[MAX_THREADS];
-  int not_helper_counts[MAX_THREADS];
-  int index[MAX_THREADS];
-  jthread threads[MAX_THREADS];
-
-  int method_resolution_problem;
+  char* threads[MAX_THREADS];
 } ThreadStats;
 
 static ThreadStats thread_stats;
 
-static void add_thread_count(jthread thread, int lock, int helper) {
+JNIEXPORT jboolean JNICALL
+Java_MyPackage_HeapMonitorThreadDisabledTest_checkThreadSamplesOnlyFrom(
+    JNIEnv* env, jclass cls, jthread thread) {
   int i;
   jvmtiThreadInfo info;
-  const char* name;
-  char* end;
-  int idx;
   int err;
+  char* expected_name;
+  int found_thread = FALSE;
 
-  if (lock) {
-    event_storage_lock(&global_event_storage);
+  err = (*jvmti)->GetThreadInfo(jvmti, thread, &info);
+  expected_name = info.name;
+
+  if (err != JVMTI_ERROR_NONE) {
+    fprintf(stderr, "Failed to get thread information\n");
+    return FALSE;
   }
 
   for (i = 0; i < thread_stats.number_threads; i++) {
-    if (thread_stats.threads[i] == thread) {
-      if (helper) {
-        thread_stats.counts[i]++;
+    if (strcmp(expected_name, thread_stats.threads[i])) {
+      return FALSE;
       } else {
-        thread_stats.not_helper_counts[i]++;
-      }
-
-      if (lock) {
-        event_storage_unlock(&global_event_storage);
-      }
-      return;
+      found_thread = TRUE;
     }
   }
+  return found_thread;
+}
 
-  thread_stats.threads[thread_stats.number_threads] = thread;
+static void add_thread_count(jthread thread) {
+  int i;
+  jvmtiThreadInfo info;
+  const char* name;
+  char* end;
+  int idx;
+  int err;
 
   err = (*jvmti)->GetThreadInfo(jvmti, thread, &info);
   if (err != JVMTI_ERROR_NONE) {
-    if (lock) {
-      event_storage_unlock(&global_event_storage);
-    }
-
-    // Just to have it accounted as an error...
-    info.name = "Allocator99";
+    return;
   }
 
-  if (!strstr(info.name, "Allocator")) {
-    if (lock) {
+  event_storage_lock(&global_event_storage);
+  for (i = 0; i < thread_stats.number_threads; i++) {
+    if (!strcmp(thread_stats.threads[i], info.name)) {
+      thread_stats.counts[i]++;
       event_storage_unlock(&global_event_storage);
+      return;
     }
-
-    // Just to have it accounted as an error...
-    info.name = "Allocator98";
   }
 
-  name = info.name + 9;
-  end = NULL;
-  idx = strtol(name, &end, 0);
-
-  if (*end == '\0') {
-    if (helper) {
+  thread_stats.threads[thread_stats.number_threads] = info.name;
       thread_stats.counts[thread_stats.number_threads]++;
-    } else {
-      thread_stats.not_helper_counts[thread_stats.number_threads]++;
-    }
-
-    thread_stats.index[thread_stats.number_threads] = idx;
     thread_stats.number_threads++;
-  } else {
-    fprintf(stderr, "Problem with thread name...: %p %s\n", thread, name);
-  }
-
-  if (PRINT_OUT) {
-    fprintf(stderr, "Added %s - %p - %d - lock: %d\n", info.name, thread, idx, lock);
-  }
-
-  if (lock) {
     event_storage_unlock(&global_event_storage);
-  }
+}
+
+JNIEXPORT void JNICALL
+Java_MyPackage_HeapMonitorThreadDisabledTest_enableSamplingEvents(
+    JNIEnv* env, jclass cls, jthread thread) {
+  fprintf(stderr, "Enabling for %p\n", thread);
+  check_error((*jvmti)->SetEventNotificationMode(
+      jvmti, JVMTI_ENABLE, JVMTI_EVENT_SAMPLED_OBJECT_ALLOC, thread),
+              "Set event notifications for a single thread");
 }
 
 static void print_thread_stats() {
   int i;
   event_storage_lock(&global_event_storage);
-  fprintf(stderr, "Method resolution problem: %d\n", thread_stats.method_resolution_problem);
   fprintf(stderr, "Thread count:\n");
   for (i = 0; i < thread_stats.number_threads; i++) {
-    fprintf(stderr, "\t%p: %d: %d - %d\n", thread_stats.threads[i],
-            thread_stats.index[i],
-            thread_stats.counts[i],
-            thread_stats.not_helper_counts[i]);
+    fprintf(stderr, "\t%s: %d\n", thread_stats.threads[i], thread_stats.counts[i]);
   }
   event_storage_unlock(&global_event_storage);
 }
 
 JNIEXPORT

@@ -803,11 +785,11 @@
                                 JNIEnv* jni_env,
                                 jthread thread,
                                 jobject object,
                                 jclass object_klass,
                                 jlong size) {
-  add_thread_count(thread, 1, 1);
+  add_thread_count(thread);
 
   if (event_storage_get_compaction_required(&global_event_storage)) {
     event_storage_compact(&global_event_storage, jni_env);
   }
 

@@ -841,33 +823,10 @@
   return check_error((*jvmti)->SetEventNotificationMode(
       jvmti, JVMTI_ENABLE, JVMTI_EVENT_SAMPLED_OBJECT_ALLOC, NULL),
                      "Set event notifications");
 }
 
-static int enable_notifications_for_two_threads(jthread first, jthread second) {
-  if (check_error((*jvmti)->SetEventNotificationMode(
-      jvmti, JVMTI_ENABLE, JVMTI_EVENT_GARBAGE_COLLECTION_FINISH, NULL),
-                           "Set event notifications")) {
-    return 0;
-  }
-
-  if (check_error((*jvmti)->SetEventNotificationMode(
-      jvmti, JVMTI_ENABLE, JVMTI_EVENT_SAMPLED_OBJECT_ALLOC, first),
-                  "Set event notifications")) {
-    return 0;
-  }
-
-  // Second thread should fail.
-  if (check_error((*jvmti)->SetEventNotificationMode(
-      jvmti, JVMTI_ENABLE, JVMTI_EVENT_SAMPLED_OBJECT_ALLOC, second),
-                  "Set event notifications")) {
-    return 0;
-  }
-
-  return 1;
-}
-
 static
 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
   jint res;
   jvmtiEventCallbacks callbacks;
   jvmtiCapabilities caps;

@@ -947,18 +906,10 @@
 JNIEXPORT void JNICALL
 Java_MyPackage_HeapMonitor_enableSamplingEvents(JNIEnv* env, jclass cls) {
   enable_notifications();
 }
 
-JNIEXPORT jboolean JNICALL
-Java_MyPackage_HeapMonitor_enableSamplingEventsForTwoThreads(JNIEnv* env,
-                                                             jclass cls,
-                                                             jthread first,
-                                                             jthread second) {
-  return enable_notifications_for_two_threads(first, second);
-}
-
 JNIEXPORT void JNICALL
 Java_MyPackage_HeapMonitor_disableSamplingEvents(JNIEnv* env, jclass cls) {
   check_error((*jvmti)->SetEventNotificationMode(
       jvmti, JVMTI_DISABLE, JVMTI_EVENT_SAMPLED_OBJECT_ALLOC, NULL),
               "Set event notifications");

@@ -1097,11 +1048,10 @@
 } ThreadsFound;
 
 JNIEXPORT jboolean JNICALL
 Java_MyPackage_HeapMonitorThreadTest_checkSamples(JNIEnv* env, jclass cls,
                                                   jint num_threads) {
-
   print_thread_stats();
   // Ensure we got stacks from at least num_threads.
   return thread_stats.number_threads >= num_threads;
 }
 
< prev index next >