--- /dev/null 2017-09-26 09:07:01.073937575 -0700 +++ new/test/hotspot/jtreg/serviceability/jvmti/HeapMonitor/MyPackage/HeapMonitorStackDepthTest.java 2017-10-06 14:51:02.670466753 -0700 @@ -0,0 +1,123 @@ +/* + * 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. + * @compile HeapMonitorStackDepthTest.java + * @run main/othervm/native -agentlib:HeapMonitor MyPackage.HeapMonitorStackDepthTest + */ + +public class HeapMonitorStackDepthTest { + static { + try { + System.loadLibrary("HeapMonitor"); + } catch (UnsatisfiedLinkError ule) { + System.err.println("Could not load HeapMonitor library"); + System.err.println("java.library.path: " + + System.getProperty("java.library.path")); + throw ule; + } + } + + private native static void enableSampling(); + private native static void disableSampling(); + private native static double getAverageStackDepth(); + + private static int g_tmp[]; + + private static int helper() { + int sum = 0; + // Let us assume that the array is 24 bytes of memory. + for (int i = 0; i < 127000 / 6; i++) { + int tmp[] = new int[1]; + // Force it to be kept. + g_tmp = tmp; + sum += g_tmp[0]; + } + return sum; + } + + private static void finalWrapper() { + int sum = 0; + + for (int j = 0; j < 5000; j++) { + sum += helper(); + } + System.out.println(sum); + } + + private static void recursiveWrapper(int depth) { + if (depth > 1) { + recursiveWrapper(depth - 1); + } else { + finalWrapper(); + } + } + + 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]; + + enableSampling(); + recursiveWrapper(depth); + 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. + enableSampling(); + final int maximumDepth = 1024; + recursiveWrapper(maximumDepth); + 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); + } + } +}