/*
* 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;
import java.util.List;
/**
* @test
* @summary Checks the frequent garbage storage system.
* @build Frame HeapMonitor
* @compile HeapMonitorFrequentTest.java
* @run main/othervm/native -agentlib:HeapMonitor MyPackage.HeapMonitorFrequentTest
*/
public class HeapMonitorFrequentTest {
private native static boolean framesExistInFrequent(Frame[] frames);
private static List runner(int max) {
List frameList = null;
for (int j = 0; j < max; j++) {
frameList = HeapMonitor.allocate();
}
frameList.add(new Frame("runner", "(I)Ljava/util/List;", "HeapMonitorFrequentTest.java", 43));
return frameList;
}
public static void main(String[] args) {
HeapMonitor.enableSampling();
// We are testing for the recent garbage sampler:
// First run for 10 iterations to fill up the garbage sampler.
List firstFrameList = runner(10);
firstFrameList.add(new Frame("main", "([Ljava/lang/String;)V", "HeapMonitorFrequentTest.java",
53));
// Now because we are in a different stack frame line here, we can just re-use the same runner.
// Run for 10, this would normally replace the garbage history but frequent has a back-off
// mechanism making it less and less likely to populate the history buffer.
// See the HeapMonitorRecentTest to understand the Frequent variation.
List secondFrameList = runner(10);
secondFrameList.add(new Frame("main", "([Ljava/lang/String;)V", "HeapMonitorFrequentTest.java",
61));
// Both types should exist in frequent since it was frequent enough.
boolean status = framesExistInFrequent(firstFrameList.toArray(new Frame[0]));
if (!status) {
throw new RuntimeException("Old frames no longer exist in the frequent list.");
}
status = framesExistInFrequent(secondFrameList.toArray(new Frame[0]));
if (!status) {
throw new RuntimeException("New frames no longer exist in the frequent list");
}
}
}