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 public static void main(String[] args) { 44 int[] depths = {10, 100, 500}; 45 46 for (int depthIdx = 0; depthIdx < depths.length; depthIdx++) { 47 int depth = depths[depthIdx]; 48 49 HeapMonitor.enableSampling(); 50 HeapMonitor.allocate(depth); 51 52 // baseDepth represents the helper method depth: main, finalWrapper, and helper. 53 // To get the requested depth, remove this from the count. 54 final int baseDepth = 3; 55 double averageDepth = getAverageStackDepth() - baseDepth; 56 double errorPercentage = calculateErrorPercentage(depth, averageDepth); 57 58 // 1% error should be close enough. 59 if (errorPercentage > 1) { 60 throw new RuntimeException("Stack depth average over 5% for depth " + depth + " : " + averageDepth + " , error: " + errorPercentage); 61 } 62 } 63 64 HeapMonitor.disableSampling(); 65 66 // Last test is 1024, which is the current maximum. 67 HeapMonitor.enableSampling(); 68 final int maximumDepth = 1024; 69 HeapMonitor.allocate(1024); 70 // Because of the extra frames, we should be at (maximumDepth + a few frames). Due to the 71 // maximum depth allowed, we hit it and so should still be at an average of 1024. 72 double averageDepth = getAverageStackDepth(); 73 double errorPercentage = calculateErrorPercentage(maximumDepth, averageDepth); 74 HeapMonitor.disableSampling(); 75 76 // 1% error should be close enough. 77 if (errorPercentage > 1) { 78 throw new RuntimeException("Stack depth average over 5% for depth 1024 : " + averageDepth + " , error: " + errorPercentage); 79 } 80 } 81 }