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 import java.util.List; 27 28 /** 29 * @test 30 * @summary Checks the frequent garbage storage system. 31 * @build Frame HeapMonitor 32 * @compile HeapMonitorFrequentTest.java 33 * @run main/othervm/native -agentlib:HeapMonitor MyPackage.HeapMonitorFrequentTest 34 */ 35 36 public class HeapMonitorFrequentTest { 37 38 private native static boolean framesExistInFrequent(Frame[] frames); 39 40 private static List<Frame> runner(int max) { 41 List<Frame> frameList = null; 42 for (int j = 0; j < max; j++) { 43 frameList = HeapMonitor.allocate(); 44 } 45 frameList.add(new Frame("runner", "(I)Ljava/util/List;", "HeapMonitorFrequentTest.java", 43)); 46 return frameList; 47 } 48 49 public static void main(String[] args) { 50 HeapMonitor.enableSampling(); 51 // We are testing for the recent garbage sampler: 52 // First run for 10 iterations to fill up the garbage sampler. 53 List<Frame> firstFrameList = runner(10); 54 firstFrameList.add(new Frame("main", "([Ljava/lang/String;)V", "HeapMonitorFrequentTest.java", 55 53)); 56 57 // Now because we are in a different stack frame line here, we can just re-use the same runner. 58 // Run for 10, this would normally replace the garbage history but frequent has a back-off 59 // mechanism making it less and less likely to populate the history buffer. 60 // See the HeapMonitorRecentTest to understand the Frequent variation. 61 List<Frame> secondFrameList = runner(10); 62 secondFrameList.add(new Frame("main", "([Ljava/lang/String;)V", "HeapMonitorFrequentTest.java", 63 61)); 64 65 // Both types should exist in frequent since it was frequent enough. 66 boolean status = framesExistInFrequent(firstFrameList.toArray(new Frame[0])); 67 if (!status) { 68 throw new RuntimeException("Old frames no longer exist in the frequent list."); 69 } 70 71 status = framesExistInFrequent(secondFrameList.toArray(new Frame[0])); 72 if (!status) { 73 throw new RuntimeException("New frames no longer exist in the frequent list"); 74 } 75 } 76 }