hotspot/test/gc/7072527/TestFullGCCount.java
author xuelei
Fri, 09 Nov 2012 01:15:04 -0800
changeset 14422 ecbc54a46e8b
parent 10240 c3931827e0cf
child 17102 fd0261ab7079
permissions -rw-r--r--
8001569: Regression test GetPeerHost uses static port number Reviewed-by: weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10240
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
     1
/*
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
     2
 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
     4
 *
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
     7
 * published by the Free Software Foundation.
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
     8
 *
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    13
 * accompanied this code).
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    14
 *
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    18
 *
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    21
 * questions.
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    22
 */
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    23
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    24
/*
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    25
 * @test TestFullGCount.java
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    26
 * @bug 7072527
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    27
 * @summary CMS: JMM GC counters overcount in some cases
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    28
 * @run main/othervm -XX:+UseConcMarkSweepGC TestFullGCCount
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    29
 *
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    30
 */
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    31
import java.util.*;
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    32
import java.lang.management.*;
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    33
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    34
public class TestFullGCCount {
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    35
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    36
    public String collectorName = "ConcurrentMarkSweep";
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    37
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    38
    public static void main(String [] args) {
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    39
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    40
        TestFullGCCount t = null;
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    41
        if (args.length==2) {
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    42
            t = new TestFullGCCount(args[0], args[1]);
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    43
        } else {
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    44
            t = new TestFullGCCount();
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    45
        }
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    46
        System.out.println("Monitoring collector: " + t.collectorName);
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    47
        t.run();
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    48
    }
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    49
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    50
    public TestFullGCCount(String pool, String collector) {
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    51
        collectorName = collector;
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    52
    }
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    53
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    54
    public TestFullGCCount() {
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    55
    }
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    56
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    57
    public void run() {
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    58
        int count = 0;
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    59
        int iterations = 20;
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    60
        long counts[] = new long[iterations];
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    61
        boolean diffAlways2 = true; // assume we will fail
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    62
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    63
        for (int i=0; i<iterations; i++) {
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    64
            System.gc();
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    65
            counts[i] = getCollectionCount();
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    66
            if (i>0) {
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    67
                if (counts[i] - counts[i-1] != 2) {
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    68
                    diffAlways2 = false;
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    69
                }
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    70
            }
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    71
        }
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    72
        if (diffAlways2) {
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    73
            throw new RuntimeException("FAILED: System.gc must be incrementing count twice.");
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    74
        }
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    75
        System.out.println("Passed.");
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    76
    }
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    77
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    78
    private long getCollectionCount() {
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    79
        long count = 0;
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    80
        List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    81
        List<GarbageCollectorMXBean> collectors = ManagementFactory.getGarbageCollectorMXBeans();
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    82
        for (int i=0; i<collectors.size(); i++) {
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    83
            GarbageCollectorMXBean collector = collectors.get(i);
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    84
            String name = collector.getName();
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    85
            if (name.contains(collectorName)) {
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    86
                System.out.println(name + ": collection count = "
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    87
                                   + collector.getCollectionCount());
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    88
                count = collector.getCollectionCount();
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    89
            }
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    90
        }
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    91
        return count;
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    92
    }
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    93
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    94
}
c3931827e0cf 7072527: CMS: JMM GC counters overcount in some cases
kevinw
parents:
diff changeset
    95