2652N/A/*
4494N/A * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
2652N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2652N/A *
2652N/A * This code is free software; you can redistribute it and/or modify it
2652N/A * under the terms of the GNU General Public License version 2 only, as
2652N/A * published by the Free Software Foundation.
2652N/A *
2652N/A * This code is distributed in the hope that it will be useful, but WITHOUT
2652N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2652N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
2652N/A * version 2 for more details (a copy is included in the LICENSE file that
2652N/A * accompanied this code).
2652N/A *
2652N/A * You should have received a copy of the GNU General Public License version
2652N/A * 2 along with this work; if not, write to the Free Software Foundation,
2652N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2652N/A *
2652N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2652N/A * or visit www.oracle.com if you need additional information or have any
2652N/A * questions.
2652N/A */
2652N/A
2652N/A/*
2652N/A * @test TestFullGCount.java
2652N/A * @bug 7072527
2652N/A * @summary CMS: JMM GC counters overcount in some cases
4494N/A * @run main/othervm -XX:+PrintGC TestFullGCCount
2652N/A */
2652N/Aimport java.util.*;
2652N/Aimport java.lang.management.*;
2652N/A
4494N/A/*
4494N/A * Originally for a specific failure in CMS, this test now monitors all
4494N/A * collectors for double-counting of collections.
4494N/A */
2652N/Apublic class TestFullGCCount {
2652N/A
4494N/A static List<GarbageCollectorMXBean> collectors = ManagementFactory.getGarbageCollectorMXBeans();
2652N/A
4494N/A public static void main(String[] args) {
4494N/A int iterations = 20;
4494N/A boolean failed = false;
4494N/A String errorMessage = "";
4494N/A HashMap<String, List> counts = new HashMap<String, List>();
4494N/A
4494N/A // Prime the collection of count lists for all collectors.
4494N/A for (int i = 0; i < collectors.size(); i++) {
4494N/A GarbageCollectorMXBean collector = collectors.get(i);
4494N/A counts.put(collector.getName(), new ArrayList<Long>(iterations));
2652N/A }
2652N/A
4494N/A // Perform some gc, record collector counts.
4494N/A for (int i = 0; i < iterations; i++) {
4494N/A System.gc();
4494N/A addCollectionCount(counts, i);
4494N/A }
2652N/A
4494N/A // Check the increments:
4494N/A // Old gen collectors should increase by one,
4494N/A // New collectors may or may not increase.
4494N/A // Any increase >=2 is unexpected.
4494N/A for (String collector : counts.keySet()) {
4494N/A System.out.println("Checking: " + collector);
2652N/A
4494N/A for (int i = 0; i < iterations - 1; i++) {
4494N/A List<Long> theseCounts = counts.get(collector);
4494N/A long a = theseCounts.get(i);
4494N/A long b = theseCounts.get(i + 1);
4494N/A if (b - a >= 2) {
4494N/A failed = true;
4494N/A errorMessage += "Collector '" + collector + "' has increment " + (b - a) +
4494N/A " at iteration " + i + "\n";
2652N/A }
2652N/A }
2652N/A }
4494N/A if (failed) {
4494N/A System.err.println(errorMessage);
4494N/A throw new RuntimeException("FAILED: System.gc collections miscounted.");
2652N/A }
2652N/A System.out.println("Passed.");
2652N/A }
2652N/A
4494N/A private static void addCollectionCount(HashMap<String, List> counts, int iteration) {
4494N/A for (int i = 0; i < collectors.size(); i++) {
2652N/A GarbageCollectorMXBean collector = collectors.get(i);
4494N/A List thisList = counts.get(collector.getName());
4494N/A thisList.add(collector.getCollectionCount());
2652N/A }
2652N/A }
2652N/A}