8207765: HeapMonitorStatIntervalTest.java fails with ZGC
authorjcbeyler
Thu, 19 Jul 2018 18:21:24 -0700
changeset 51210 e750c1a054fa
parent 51209 4630bb315ec0
child 51211 6ed2290ba410
8207765: HeapMonitorStatIntervalTest.java fails with ZGC Summary: Add a calculation of array sizes before test to satisfy ZGC support Reviewed-by: amenkov, sspitsyn Contributed-by: jcbeyler@google.com
test/hotspot/jtreg/ProblemList.txt
test/hotspot/jtreg/serviceability/jvmti/HeapMonitor/MyPackage/HeapMonitor.java
test/hotspot/jtreg/serviceability/jvmti/HeapMonitor/MyPackage/HeapMonitorStatIntervalTest.java
test/hotspot/jtreg/serviceability/jvmti/HeapMonitor/libHeapMonitorTest.c
--- a/test/hotspot/jtreg/ProblemList.txt	Fri Jul 20 08:40:14 2018 +0800
+++ b/test/hotspot/jtreg/ProblemList.txt	Thu Jul 19 18:21:24 2018 -0700
@@ -81,7 +81,6 @@
 
 serviceability/sa/TestRevPtrsForInvokeDynamic.java   8191270 generic-all
 serviceability/sa/sadebugd/SADebugDTest.java         8163805 generic-all
-serviceability/jvmti/HeapMonitor/MyPackage/HeapMonitorStatRateTest.java 8207765 generic-all
 
 #############################################################################
 
--- a/test/hotspot/jtreg/serviceability/jvmti/HeapMonitor/MyPackage/HeapMonitor.java	Fri Jul 20 08:40:14 2018 +0800
+++ b/test/hotspot/jtreg/serviceability/jvmti/HeapMonitor/MyPackage/HeapMonitor.java	Thu Jul 19 18:21:24 2018 -0700
@@ -102,13 +102,45 @@
     return sum;
   }
 
+  private static double averageOneElementSize;
+  private static native double getAverageSize();
+
+  // Calculate the size of a 1-element array in order to assess average sampling interval
+  // via the HeapMonitorStatIntervalTest. This is needed because various GCs could add
+  // extra memory to arrays.
+  // This is done by allocating a 1-element array and then looking in the heap monitoring
+  // samples for the average size of objects collected.
+  public static void calculateAverageOneElementSize() {
+    enableSamplingEvents();
+    // Assume a size of 24 for the average size.
+    averageOneElementSize = 24;
+
+    // Call allocateSize once, this allocates the internal array for the iterations.
+    int totalSize = 10 * 1024 * 1024;
+    allocateSize(totalSize);
+
+    // Reset the storage and now really track the size of the elements.
+    resetEventStorage();
+    allocateSize(totalSize);
+    disableSamplingEvents();
+
+    // Get the actual average size.
+    averageOneElementSize = getAverageSize();
+    if (averageOneElementSize == 0) {
+      throw new RuntimeException("Could not calculate the average size of a 1-element array.");
+    }
+  }
+
   public static int allocateSize(int totalSize) {
+    if (averageOneElementSize == 0) {
+      throw new RuntimeException("Average size of a 1-element array was not calculated.");
+    }
+
     int sum = 0;
 
-    // Let us assume that a 1-element array is 24 bytes.
-    int iterations = totalSize / 24;
+    int iterations = (int) (totalSize / averageOneElementSize);
 
-    if (arrays == null) {
+    if (arrays == null || arrays.length < iterations) {
       arrays = new int[iterations][];
     }
 
--- a/test/hotspot/jtreg/serviceability/jvmti/HeapMonitor/MyPackage/HeapMonitorStatIntervalTest.java	Fri Jul 20 08:40:14 2018 +0800
+++ b/test/hotspot/jtreg/serviceability/jvmti/HeapMonitor/MyPackage/HeapMonitorStatIntervalTest.java	Thu Jul 19 18:21:24 2018 -0700
@@ -33,9 +33,6 @@
  */
 
 public class HeapMonitorStatIntervalTest {
-
-  private native static double getAverageInterval();
-
   private static boolean testIntervalOnce(int interval, boolean throwIfFailure) {
     HeapMonitor.resetEventStorage();
     HeapMonitor.setSamplingInterval(interval);
@@ -84,6 +81,8 @@
   public static void main(String[] args) {
     int[] tab = {1024, 8192};
 
+    HeapMonitor.calculateAverageOneElementSize();
+
     for (int intervalIdx = 0; intervalIdx < tab.length; intervalIdx++) {
       testInterval(tab[intervalIdx]);
     }
--- a/test/hotspot/jtreg/serviceability/jvmti/HeapMonitor/libHeapMonitorTest.c	Fri Jul 20 08:40:14 2018 +0800
+++ b/test/hotspot/jtreg/serviceability/jvmti/HeapMonitor/libHeapMonitorTest.c	Thu Jul 19 18:21:24 2018 -0700
@@ -45,7 +45,7 @@
 
 #define TRUE 1
 #define FALSE 0
-#define PRINT_OUT 1
+#define PRINT_OUT 0
 
 static jvmtiEnv *jvmti = NULL;
 static jvmtiEnv *second_jvmti = NULL;
@@ -369,7 +369,7 @@
   return result;
 }
 
-static double event_storage_get_average_interval(EventStorage* storage) {
+static double event_storage_get_average_size(EventStorage* storage) {
   double accumulation = 0;
   int max_size;
   int i;
@@ -974,8 +974,8 @@
 }
 
 JNIEXPORT jdouble JNICALL
-Java_MyPackage_HeapMonitorStatIntervalTest_getAverageInterval(JNIEnv *env, jclass cls) {
-  return event_storage_get_average_interval(&global_event_storage);
+Java_MyPackage_HeapMonitor_getAverageSize(JNIEnv *env, jclass cls) {
+  return event_storage_get_average_size(&global_event_storage);
 }
 
 typedef struct sThreadsFound {