1 /* 2 * Copyright (c) 2017, 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 * @build Frame HeapMonitor 29 * @summary Verifies the JVMTI Heap Monitor Statistics 30 * @compile HeapMonitorStatCorrectnessTest.java 31 * @run main/othervm/native -agentlib:HeapMonitor MyPackage.HeapMonitorStatCorrectnessTest 32 */ 33 34 public class HeapMonitorStatCorrectnessTest { 35 36 // Do 100000 iterations and expect maxIteration / multiplier samples. 37 private static final int maxIteration = 100000; 38 private static int array[]; 39 40 private native static boolean statsHaveExpectedNumberSamples(int expected, int percentError); 41 42 private static void allocate(int size) { 43 for (int j = 0; j < maxIteration; j++) { 44 array = new int[size]; 45 } 46 } 47 48 public static void main(String[] args) { 49 int sizes[] = {1000, 10000, 100000}; 50 51 for (int currentSize : sizes) { 52 System.out.println("Testing size " + currentSize); 53 54 // 111 is as good a number as any. 55 final int samplingMultiplier = 111; 56 HeapMonitor.enableSampling(samplingMultiplier * currentSize); 57 58 if (!HeapMonitor.areSamplingStatisticsZero()) { 59 throw new RuntimeException("Statistics should be null to begin with."); 60 } 61 62 allocate(currentSize); 63 64 // For simplifications, we ignore the array memory usage for array internals (with the array 65 // sizes requested, it should be a negligible oversight). 66 // 67 // That means that with maxIterations, the loop in the method allocate requests: 68 // maxIterations * currentSize * 4 bytes (4 for integers) 69 // 70 // Via the enable sampling, the code requests a sample every samplingMultiplier * currentSize bytes. 71 // 72 // Therefore, the expected sample number is: 73 // (maxIterations * currentSize * 4) / (samplingMultiplier * currentSize); 74 double expected = maxIteration; 75 expected *= 4; 76 expected /= samplingMultiplier; 77 78 // 10% error ensures a sanity test without becoming flaky. 79 if (!statsHaveExpectedNumberSamples((int) expected, 10)) { 80 throw new RuntimeException("Statistics should show about " + expected + " samples."); 81 } 82 83 HeapMonitor.disableSampling(); 84 } 85 } 86 }