Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/test/gc/7072527/TestFullGCCount.java
32284 views
/*1* Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test TestFullGCount.java25* @bug 707252726* @summary CMS: JMM GC counters overcount in some cases27* @run main/othervm -XX:+PrintGC TestFullGCCount28*/29import java.util.*;30import java.lang.management.*;3132/*33* Originally for a specific failure in CMS, this test now monitors all34* collectors for double-counting of collections.35*/36public class TestFullGCCount {3738static List<GarbageCollectorMXBean> collectors = ManagementFactory.getGarbageCollectorMXBeans();3940public static void main(String[] args) {41int iterations = 20;42boolean failed = false;43String errorMessage = "";44HashMap<String, List> counts = new HashMap<String, List>();4546// Prime the collection of count lists for all collectors.47for (int i = 0; i < collectors.size(); i++) {48GarbageCollectorMXBean collector = collectors.get(i);49counts.put(collector.getName(), new ArrayList<Long>(iterations));50}5152// Perform some gc, record collector counts.53for (int i = 0; i < iterations; i++) {54System.gc();55addCollectionCount(counts, i);56}5758// Check the increments:59// Old gen collectors should increase by one,60// New collectors may or may not increase.61// Any increase >=2 is unexpected.62for (String collector : counts.keySet()) {63System.out.println("Checking: " + collector);6465for (int i = 0; i < iterations - 1; i++) {66List<Long> theseCounts = counts.get(collector);67long a = theseCounts.get(i);68long b = theseCounts.get(i + 1);69if (b - a >= 2) {70failed = true;71errorMessage += "Collector '" + collector + "' has increment " + (b - a) +72" at iteration " + i + "\n";73}74}75}76if (failed) {77System.err.println(errorMessage);78throw new RuntimeException("FAILED: System.gc collections miscounted.");79}80System.out.println("Passed.");81}8283private static void addCollectionCount(HashMap<String, List> counts, int iteration) {84for (int i = 0; i < collectors.size(); i++) {85GarbageCollectorMXBean collector = collectors.get(i);86List thisList = counts.get(collector.getName());87thisList.add(collector.getCollectionCount());88}89}90}919293