jdk/src/demo/share/management/VerboseGC/PrintGCStat.java
author chegar
Tue, 22 Mar 2016 10:43:19 +0000
changeset 36709 f9f252088afa
parent 25859 3317bb8137f4
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 10292
diff changeset
     2
 * Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * Redistribution and use in source and binary forms, with or without
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * modification, are permitted provided that the following conditions
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * are met:
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *   - Redistributions of source code must retain the above copyright
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *     notice, this list of conditions and the following disclaimer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 *   - Redistributions in binary form must reproduce the above copyright
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 *     notice, this list of conditions and the following disclaimer in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 *     documentation and/or other materials provided with the distribution.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    15
 *   - Neither the name of Oracle nor the names of its
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *     contributors may be used to endorse or promote products derived
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 *     from this software without specific prior written permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/*
10292
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 9520
diff changeset
    33
 * This source code is provided to illustrate the usage of a given feature
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 9520
diff changeset
    34
 * or technique and has been deliberately simplified. Additional steps
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 9520
diff changeset
    35
 * required for a production-quality application, such as security checks,
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 9520
diff changeset
    36
 * input validation and proper error handling, might not be present in
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 9520
diff changeset
    37
 * this sample code.
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 9520
diff changeset
    38
 */
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 9520
diff changeset
    39
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 9520
diff changeset
    40
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 9520
diff changeset
    41
/*
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import static java.lang.management.ManagementFactory.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.lang.management.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import javax.management.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * Example of using the java.lang.management API to monitor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * the memory usage and garbage collection statistics.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * @author  Mandy Chung
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
public class PrintGCStat {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private RuntimeMXBean rmbean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private MemoryMXBean mmbean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private List<MemoryPoolMXBean> pools;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private List<GarbageCollectorMXBean> gcmbeans;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * Constructs a PrintGCStat object to monitor a remote JVM.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    public PrintGCStat(MBeanServerConnection server) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        // Create the platform mxbean proxies
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        this.rmbean = newPlatformMXBeanProxy(server,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                                             RUNTIME_MXBEAN_NAME,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                                             RuntimeMXBean.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        this.mmbean = newPlatformMXBeanProxy(server,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                                             MEMORY_MXBEAN_NAME,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                                             MemoryMXBean.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        ObjectName poolName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        ObjectName gcName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            poolName = new ObjectName(MEMORY_POOL_MXBEAN_DOMAIN_TYPE+",*");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            gcName = new ObjectName(GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE+",*");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        } catch (MalformedObjectNameException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            // should not reach here
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            assert(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
9520
99d378796e54 7029383: Refresh of non-client demos
nloodin
parents: 5506
diff changeset
    83
        Set<ObjectName> mbeans = server.queryNames(poolName, null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        if (mbeans != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            pools = new ArrayList<MemoryPoolMXBean>();
9520
99d378796e54 7029383: Refresh of non-client demos
nloodin
parents: 5506
diff changeset
    86
            for (ObjectName objName : mbeans) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                MemoryPoolMXBean p =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                    newPlatformMXBeanProxy(server,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                                           objName.getCanonicalName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                                           MemoryPoolMXBean.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                pools.add(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        mbeans = server.queryNames(gcName, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        if (mbeans != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            gcmbeans = new ArrayList<GarbageCollectorMXBean>();
9520
99d378796e54 7029383: Refresh of non-client demos
nloodin
parents: 5506
diff changeset
    98
            for (ObjectName objName : mbeans) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                GarbageCollectorMXBean gc =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                    newPlatformMXBeanProxy(server,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                                           objName.getCanonicalName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                                           GarbageCollectorMXBean.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                gcmbeans.add(gc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * Constructs a PrintGCStat object to monitor the local JVM.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    public PrintGCStat() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        // Obtain the platform mxbean instances for the running JVM.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        this.rmbean = getRuntimeMXBean();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        this.mmbean = getMemoryMXBean();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        this.pools = getMemoryPoolMXBeans();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        this.gcmbeans = getGarbageCollectorMXBeans();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * Prints the verbose GC log to System.out to list the memory usage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * of all memory pools as well as the GC statistics.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public void printVerboseGc() {
9520
99d378796e54 7029383: Refresh of non-client demos
nloodin
parents: 5506
diff changeset
   124
        System.out.println("Uptime: " + formatMillis(rmbean.getUptime()));
99d378796e54 7029383: Refresh of non-client demos
nloodin
parents: 5506
diff changeset
   125
        System.out.println("Heap usage: " + mmbean.getHeapMemoryUsage());
99d378796e54 7029383: Refresh of non-client demos
nloodin
parents: 5506
diff changeset
   126
        System.out.println("Non-Heap memory usage: " + mmbean.getNonHeapMemoryUsage());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        for (GarbageCollectorMXBean gc : gcmbeans) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            System.out.print(" [" + gc.getName() + ": ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            System.out.print("Count=" + gc.getCollectionCount());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            System.out.print(" GCTime=" + formatMillis(gc.getCollectionTime()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            System.out.print("]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        for (MemoryPoolMXBean p : pools) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            System.out.print("  [" + p.getName() + ":");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            MemoryUsage u = p.getUsage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            System.out.print(" Used=" + formatBytes(u.getUsed()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            System.out.print(" Committed=" + formatBytes(u.getCommitted()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            System.out.println("]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    private String formatMillis(long ms) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        return String.format("%.4fsec", ms / (double) 1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    private String formatBytes(long bytes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        long kb = bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        if (bytes > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            kb = bytes / 1024;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        return kb + "K";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
}