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 * @summary Verifies the JVMTI Heap Monitor stack depth handling. 29 * @build Frame HeapMonitor 30 * @compile HeapMonitorStackDepthTest.java 31 * @run main/othervm/native -agentlib:HeapMonitor MyPackage.HeapMonitorStackDepthTest 32 */ 33 34 public class HeapMonitorStackDepthTest { 35 private native static double getAverageStackDepth(); 36 37 private static double calculateErrorPercentage(double expected, double actual) { 38 double error = expected - actual; 39 error = error < 0 ? -error : error; 40 return error / expected * 100; 41 } 42 43 private static void runner(int max, int depth) { 44 for (int j = 0; j < max; j++) { 45 HeapMonitor.allocate(depth); 46 } 47 } 48 49 public static void main(String[] args) { 50 int[] depths = {10, 100, 500}; 51 52 for (int depthIdx = 0; depthIdx < depths.length; depthIdx++) { 53 int depth = depths[depthIdx]; 54 55 HeapMonitor.enableSampling(); 56 // Do the runner 10 times to ensure the stack is really sampled. 57 runner(10, depth); 58 59 // baseDepth represents the helper method depth: main, runner, HeapMonitor.allocate, 60 // and HeapMonitor.actuallyAllocate. 61 // To get the requested depth, remove this from the count. 62 final int baseDepth = 4; 63 double averageDepth = getAverageStackDepth() - baseDepth; 64 double errorPercentage = calculateErrorPercentage(depth, averageDepth); 65 66 // 3% error should be close enough. 67 if (errorPercentage > 3) { 68 throw new RuntimeException("Stack depth average over 5% for depth " + depth + " : " + averageDepth + " , error: " + errorPercentage); 69 } 70 71 HeapMonitor.disableSampling(); 72 } 73 74 75 // Last test is 1024, which is the current maximum. 76 HeapMonitor.enableSampling(); 77 final int maximumDepth = 1024; 78 // Do the runner 10 times to ensure the stack is really sampled. 79 runner(10, maximumDepth); 80 // Because of the extra frames, we should be at (maximumDepth + a few frames). Due to the 81 // maximum depth allowed, we hit it and so should still be at an average of 1024. 82 double averageDepth = getAverageStackDepth(); 83 double errorPercentage = calculateErrorPercentage(maximumDepth, averageDepth); 84 HeapMonitor.disableSampling(); 85 86 // 3% error should be close enough. 87 if (errorPercentage > 3) { 88 throw new RuntimeException("Stack depth average over 5% for depth 1024 : " + averageDepth + " , error: " + errorPercentage); 89 } 90 } 91 }