--- /dev/null 2017-05-01 09:42:45.355096588 -0700 +++ new/test/serviceability/jvmti/HeapMonitor/MyPackage/HeapMonitorRecentTest.java 2017-06-21 11:43:15.271528113 -0700 @@ -0,0 +1,107 @@ +/* + * 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 Checks the Recent garbage storage system. + * @build Frame + * @compile HeapMonitorTest.java + * @run main/othervm/native -agentlib:HeapMonitor MyPackage.HeapMonitorRecentTest + */ + +import java.io.PrintStream; + +public class HeapMonitorRecentTest { + + 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; + } + } + + native static int checkLiveOrRecentFrames(Frame[] frames); + native static int checkLiveAndRecentFrames(Frame[] frames); + native static int enableSampling(); + + public static int cnt; + public static int g_tmp[]; + public int array[]; + + public 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; + } + + public static void runner(int max) { + int sum = 0; + for (int j = 0; j < max; j++) { + sum += helper(); + } + System.out.println(sum); + } + + public static void main(String[] args) { + Frame[] frames = new Frame[3]; + frames[0] = new Frame("helper", "()I", "HeapMonitorRecentTest.java", 61); + frames[1] = new Frame("runner", "(I)V", "HeapMonitorRecentTest.java", 72); + frames[2] = new Frame("main", "([Ljava/lang/String;)V", "HeapMonitorRecentTest.java", 86); + + enableSampling(); + // We are testing for the recent garbage sampler: + // First run for 10000 iterations to fill up the garbage sampler. + runner(10000); + + // Now because we are in a different stack frame line here, we can just re-use the same runner. + // Run for 3, we really should not see that many of these and most should be the first type. + runner(5000); + + // We should no longer have the initial frames. + int status = checkLiveOrRecentFrames(frames); + if (status != 0) { + throw new RuntimeException("Non-zero status returned from the agent: " + status); + } + + // Change the last frame only since the rest is identical. + frames[2].lineNumber = 90; + + // We should see those new frames. + status = checkLiveAndRecentFrames(frames); + if (status == 0) { + throw new RuntimeException("Non-zero status returned from the agent: " + status); + } + } +}