--- old/test/hotspot/jtreg/serviceability/jvmti/HeapMonitor/MyPackage/HeapMonitorStatRateTest.java 2018-04-11 14:33:23.077160090 -0700 +++ new/test/hotspot/jtreg/serviceability/jvmti/HeapMonitor/MyPackage/HeapMonitorStatRateTest.java 2018-04-11 14:33:22.761161170 -0700 @@ -35,32 +35,47 @@ private native static double getAverageRate(); - public static void main(String[] args) { - int[] tab = {1024, 16384, 524288}; + private static boolean testRateOnce(int rate, boolean throwIfFailure) { + HeapMonitor.resetEventStorage(); + HeapMonitor.setSamplingRate(rate); - for (int rateIdx = 0; rateIdx < tab.length; rateIdx++) { - int rate = tab[rateIdx]; + HeapMonitor.enableSamplingEvents(); + + int allocationTotal = 512 * 1024 * 1024; + HeapMonitor.allocateSize(allocationTotal); + + HeapMonitor.disableSamplingEvents(); + + double actualCount = HeapMonitor.getEventStorageElementCount(); + double expectedCount = allocationTotal / rate; + + double error = Math.abs(actualCount - expectedCount); + double errorPercentage = error / expectedCount * 100; - HeapMonitor.resetEventStorage(); - HeapMonitor.setSamplingRate(rate); + boolean failure = (errorPercentage > 10.0); - HeapMonitor.enableSamplingEvents(); + if (failure && throwIfFailure) { + throw new RuntimeException("Rate average over 10% for rate " + rate + " -> " + actualCount + + ", " + expectedCount); + } - int allocationTotal = 512 * 1024 * 1024; - HeapMonitor.allocateSize(allocationTotal); + return failure; + } - HeapMonitor.disableSamplingEvents(); - double actualCount = HeapMonitor.getEventStorageElementCount(); - double expectedCount = allocationTotal / rate; + private static void testRate(int rate) { + // Test the rate twice, it can happen that the test is "unlucky" and the rate just goes above + // the 10% mark. So try again to squash flakiness. + if (!testRateOnce(rate, false)) { + testRateOnce(rate, true); + } + } - double error = Math.abs(actualCount - expectedCount); - double errorPercentage = error / expectedCount * 100; + public static void main(String[] args) { + int[] tab = {1024, 16384, 524288}; - if (errorPercentage > 10) { - throw new RuntimeException("Rate average over 10% for rate " + rate + " -> " + actualCount - + ", " + expectedCount); - } + for (int rateIdx = 0; rateIdx < tab.length; rateIdx++) { + testRate(tab[rateIdx]); } } }