1 /* 2 * Copyright (c) 2018, Google and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 package MyPackage; 25 26 /** 27 * @test 28 * @summary Verifies the JVMTI Heap Monitor sampling interval average. 29 * @build Frame HeapMonitor 30 * @compile HeapMonitorStatIntervalTest.java 31 * @requires vm.compMode != "Xcomp" 32 * @run main/othervm/native -agentlib:HeapMonitorTest MyPackage.HeapMonitorStatIntervalTest 33 */ 34 35 public class HeapMonitorStatIntervalTest { testIntervalOnce(int interval, boolean throwIfFailure)36 private static boolean testIntervalOnce(int interval, boolean throwIfFailure) { 37 HeapMonitor.resetEventStorage(); 38 HeapMonitor.setSamplingInterval(interval); 39 40 HeapMonitor.enableSamplingEvents(); 41 42 int allocationTotal = 10 * 1024 * 1024; 43 int allocationIterations = 10; 44 45 double actualCount = 0; 46 for (int i = 0; i < allocationIterations; i++) { 47 HeapMonitor.resetEventStorage(); 48 HeapMonitor.allocateSize(allocationTotal); 49 actualCount += HeapMonitor.getEventStorageElementCount(); 50 } 51 52 HeapMonitor.disableSamplingEvents(); 53 54 double expectedCount = allocationTotal * allocationIterations / interval; 55 56 double error = Math.abs(actualCount - expectedCount); 57 double errorPercentage = error / expectedCount * 100; 58 59 boolean success = (errorPercentage < 10.0); 60 61 if (!success && throwIfFailure) { 62 throw new RuntimeException("Interval average over 10% for interval " + interval + " -> " 63 + actualCount + ", " + expectedCount); 64 } 65 66 return success; 67 } 68 69 testInterval(int interval)70 private static void testInterval(int interval) { 71 // Test the interval twice, it can happen that the test is "unlucky" and the interval just goes above 72 // the 10% mark. So try again to squash flakiness. 73 // Flakiness is due to the fact that this test is dependent on the sampling interval, which is a 74 // statistical geometric variable around the sampling interval. This means that the test could be 75 // unlucky and not achieve the mean average fast enough for the test case. 76 if (!testIntervalOnce(interval, false)) { 77 testIntervalOnce(interval, true); 78 } 79 } 80 main(String[] args)81 public static void main(String[] args) { 82 int[] tab = {1024, 8192}; 83 84 HeapMonitor.calculateAverageOneElementSize(); 85 86 for (int intervalIdx = 0; intervalIdx < tab.length; intervalIdx++) { 87 testInterval(tab[intervalIdx]); 88 } 89 } 90 } 91