jdk/src/demo/share/management/VerboseGC/VerboseGC.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 javax.management.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.management.remote.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.net.MalformedURLException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * This VerboseGC class demonstrates the capability to get
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * the garbage collection statistics and memory usage remotely.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
public class VerboseGC {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private MBeanServerConnection server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private JMXConnector jmxc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    public VerboseGC(String hostname, int port) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        System.out.println("Connecting to " + hostname + ":" + port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        // Create an RMI connector client and connect it to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        // the RMI connector server
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        String urlPath = "/jndi/rmi://" + hostname + ":" + port + "/jmxrmi";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        connect(urlPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
   public void dump(long interval, long samples) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            PrintGCStat pstat = new PrintGCStat(server);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            for (int i = 0; i < samples; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                pstat.printVerboseGc();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                    Thread.sleep(interval);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                    System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            System.err.println("\nCommunication error: " + e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * Connect to a JMX agent of a given URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    private void connect(String urlPath) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            JMXServiceURL url = new JMXServiceURL("rmi", "", 0, urlPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            this.jmxc = JMXConnectorFactory.connect(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            this.server = jmxc.getMBeanServerConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        } catch (MalformedURLException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            // should not reach here
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            System.err.println("\nCommunication error: " + e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        if (args.length < 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            usage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        String hostname = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        int port = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        long interval = 5000; // default is 5 second interval
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        long mins = 5;
9520
99d378796e54 7029383: Refresh of non-client demos
nloodin
parents: 5506
diff changeset
   107
        for (String arg: args) {
99d378796e54 7029383: Refresh of non-client demos
nloodin
parents: 5506
diff changeset
   108
            if (arg.startsWith("-")) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                if (arg.equals("-h") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                    arg.equals("-help") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                    arg.equals("-?")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                    usage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                } else if (arg.startsWith("-interval=")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                        interval = Integer.parseInt(arg.substring(10)) * 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                    } catch (NumberFormatException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                        usage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                } else if (arg.startsWith("-duration=")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                        mins = Integer.parseInt(arg.substring(10));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                    } catch (NumberFormatException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                        usage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                    // Unknown switch
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                    System.err.println("Unrecognized option: " + arg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                    usage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                String[] arg2 = arg.split(":");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                if (arg2.length != 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                    usage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                hostname = arg2[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                    port = Integer.parseInt(arg2[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                } catch (NumberFormatException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                    usage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                if (port < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                    usage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        // get full thread dump and perform deadlock detection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        VerboseGC vgc = new VerboseGC(hostname, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        long samples = (mins * 60 * 1000) / interval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        vgc.dump(interval, samples);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    private static void usage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        System.out.print("Usage: java VerboseGC <hostname>:<port> ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        System.out.println(" [-interval=seconds] [-duration=minutes]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
}