/* * Copyright (c) 2017, Google and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package MyPackage; /** * @test * @summary Verifies the JVMTI Heap Monitor stack depth handling. * @build Frame HeapMonitor * @compile HeapMonitorStackDepthTest.java * @run main/othervm/native -agentlib:HeapMonitor MyPackage.HeapMonitorStackDepthTest */ public class HeapMonitorStackDepthTest { private native static double getAverageStackDepth(); private static double calculateErrorPercentage(double expected, double actual) { double error = expected - actual; error = error < 0 ? -error : error; return error / expected * 100; } public static void main(String[] args) { int[] depths = {10, 100, 500}; for (int depthIdx = 0; depthIdx < depths.length; depthIdx++) { int depth = depths[depthIdx]; HeapMonitor.enableSampling(); HeapMonitor.allocate(depth); HeapMonitor.disableSampling(); // baseDepth represents the helper method depth: main, finalWrapper, and helper. // To get the requested depth, remove this from the count. final int baseDepth = 3; double averageDepth = getAverageStackDepth() - baseDepth; double errorPercentage = calculateErrorPercentage(depth, averageDepth); // 1% error should be close enough. if (errorPercentage > 1) { throw new RuntimeException("Stack depth average over 5% for depth " + depth + " : " + averageDepth + " , error: " + errorPercentage); } } // Last test is 1024, which is the current maximum. HeapMonitor.enableSampling(); final int maximumDepth = 1024; HeapMonitor.allocate(1024); HeapMonitor.disableSampling(); // Because of the extra frames, we should be at (maximumDepth + a few frames). Due to the // maximum depth allowed, we hit it and so should still be at an average of 1024. double averageDepth = getAverageStackDepth(); double errorPercentage = calculateErrorPercentage(maximumDepth, averageDepth); // 1% error should be close enough. if (errorPercentage > 1) { throw new RuntimeException("Stack depth average over 5% for depth 1024 : " + averageDepth + " , error: " + errorPercentage); } } }