jdk/test/com/sun/management/GarbageCollectorMXBean/LastGCInfo.java
author ihse
Tue, 09 May 2017 12:57:30 +0200
changeset 45028 b0ea3c0bfb81
parent 44423 306c020eb154
permissions -rw-r--r--
8179889: Fix typographic errors in copyright headers Reviewed-by: erikj, dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
45028
b0ea3c0bfb81 8179889: Fix typographic errors in copyright headers
ihse
parents: 44423
diff changeset
     2
 * Copyright (c) 2004, 2015, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug     4982301
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Sanity Test for GarbageCollectorMXBean.getLastGcInfo().
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author  Mandy Chung
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *
21944
924b78f629be 6987597: ManagementFactory.getGarbageCollectorMXBeans() returns empty list with CMS
jbachorik
parents: 5506
diff changeset
    30
 * @run main/othervm -XX:-ExplicitGCInvokesConcurrent LastGCInfo
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 */
21944
924b78f629be 6987597: ManagementFactory.getGarbageCollectorMXBeans() returns empty list with CMS
jbachorik
parents: 5506
diff changeset
    32
// Passing "-XX:-ExplicitGCInvokesConcurrent" to force System.gc()
924b78f629be 6987597: ManagementFactory.getGarbageCollectorMXBeans() returns empty list with CMS
jbachorik
parents: 5506
diff changeset
    33
// run on foreground when CMS is used and prevent situations when "GcInfo"
924b78f629be 6987597: ManagementFactory.getGarbageCollectorMXBeans() returns empty list with CMS
jbachorik
parents: 5506
diff changeset
    34
// is missing even though System.gc() was successfuly processed.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.lang.management.ManagementFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.lang.management.MemoryUsage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.lang.management.MemoryPoolMXBean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import com.sun.management.GcInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import com.sun.management.GarbageCollectorMXBean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
public class LastGCInfo {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    public static void main(String[] argv) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        boolean hasGcInfo = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        System.gc();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        List mgrs = ManagementFactory.getGarbageCollectorMXBeans();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        for (ListIterator iter = mgrs.listIterator(); iter.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            Object mgr = iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            if (mgr instanceof GarbageCollectorMXBean) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
                GarbageCollectorMXBean gc = (GarbageCollectorMXBean) mgr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
                GcInfo info = gc.getLastGcInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                if (info != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                    checkGcInfo(gc.getName(), info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                    hasGcInfo = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        if (! hasGcInfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            throw new RuntimeException("No GcInfo returned");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        System.out.println("Test passed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private static void checkGcInfo(String name, GcInfo info) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        System.out.println("GC statistic for : " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        System.out.print("GC #" + info.getId());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        System.out.print(" start:" + info.getStartTime());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        System.out.print(" end:" + info.getEndTime());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        System.out.println(" (" + info.getDuration() + "ms)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        Map usage = info.getMemoryUsageBeforeGc();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        List pnames = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        for (Iterator iter = usage.entrySet().iterator(); iter.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            Map.Entry entry = (Map.Entry) iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            String poolname = (String) entry.getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            pnames.add(poolname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            MemoryUsage busage = (MemoryUsage) entry.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            MemoryUsage ausage = (MemoryUsage) info.getMemoryUsageAfterGc().get(poolname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            if (ausage == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                throw new RuntimeException("After Gc Memory does not exist" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                    " for " + poolname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            System.out.println("Usage for pool " + poolname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            System.out.println("   Before GC: " + busage);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            System.out.println("   After GC: " + ausage);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        // check if memory usage for all memory pools are returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        List pools = ManagementFactory.getMemoryPoolMXBeans();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        for (Iterator iter = pools.iterator(); iter.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            MemoryPoolMXBean p = (MemoryPoolMXBean) iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            if (!pnames.contains(p.getName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                throw new RuntimeException("GcInfo does not contain " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                    "memory usage for pool " + p.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
}