Path: blob/master/test/hotspot/jtreg/gc/TestFullGCCount.java
40930 views
/*1* Copyright (c) 2011, 2021, 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*/2223package gc;2425/**26* @test TestFullGCCount.java27* @bug 707252728* @summary JMM GC counters overcount in some cases29* @comment Shenandoah has "ExplicitGCInvokesConcurrent" on by default30* @requires !(vm.gc == "Shenandoah" & vm.opt.ExplicitGCInvokesConcurrent != false)31* @requires vm.gc != "Z"32* @modules java.management33* @run main/othervm -Xlog:gc gc.TestFullGCCount34*/3536import java.lang.management.GarbageCollectorMXBean;37import java.lang.management.ManagementFactory;38import java.util.ArrayList;39import java.util.HashMap;40import java.util.List;4142/*43* Originally for a specific failure in CMS[[keep]], this test now monitors all44* collectors for double-counting of collections.45*/46public class TestFullGCCount {4748static List<GarbageCollectorMXBean> collectors = ManagementFactory.getGarbageCollectorMXBeans();4950public static void main(String[] args) {51int iterations = 20;52boolean failed = false;53String errorMessage = "";54HashMap<String, List<Long>> counts = new HashMap<>();5556// Prime the collection of count lists for all collectors.57for (int i = 0; i < collectors.size(); i++) {58GarbageCollectorMXBean collector = collectors.get(i);59counts.put(collector.getName(), new ArrayList<>(iterations));60}6162// Perform some gc, record collector counts.63for (int i = 0; i < iterations; i++) {64System.gc();65addCollectionCount(counts, i);66}6768// Check the increments:69// Old gen collectors should increase by one,70// New collectors may or may not increase.71// Any increase >=2 is unexpected.72for (String collector : counts.keySet()) {73System.out.println("Checking: " + collector);7475for (int i = 0; i < iterations - 1; i++) {76List<Long> theseCounts = counts.get(collector);77long a = theseCounts.get(i);78long b = theseCounts.get(i + 1);79if (b - a >= 2) {80failed = true;81errorMessage += "Collector '" + collector + "' has increment " + (b - a) +82" at iteration " + i + "\n";83}84}85}86if (failed) {87System.err.println(errorMessage);88throw new RuntimeException("FAILED: System.gc collections miscounted.");89}90System.out.println("Passed.");91}9293private static void addCollectionCount(HashMap<String, List<Long>> counts, int iteration) {94for (int i = 0; i < collectors.size(); i++) {95GarbageCollectorMXBean collector = collectors.get(i);96List<Long> thisList = counts.get(collector.getName());97thisList.add(collector.getCollectionCount());98}99}100}101102103