Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/demo/management/VerboseGC/VerboseGC.java
38829 views
/*1* Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved.2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions5* are met:6*7* - Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9*10* - Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* - Neither the name of Oracle nor the names of its15* contributors may be used to endorse or promote products derived16* from this software without specific prior written permission.17*18* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS19* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,20* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR21* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR22* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,23* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,24* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR25* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF26* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING27* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS28* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.29*/3031/*32* This source code is provided to illustrate the usage of a given feature33* or technique and has been deliberately simplified. Additional steps34* required for a production-quality application, such as security checks,35* input validation and proper error handling, might not be present in36* this sample code.37*/383940/*41*/4243import javax.management.*;44import javax.management.remote.*;45import java.io.IOException;46import java.net.MalformedURLException;4748/**49* This VerboseGC class demonstrates the capability to get50* the garbage collection statistics and memory usage remotely.51*/52public class VerboseGC {53private MBeanServerConnection server;54private JMXConnector jmxc;55public VerboseGC(String hostname, int port) {56System.out.println("Connecting to " + hostname + ":" + port);5758// Create an RMI connector client and connect it to59// the RMI connector server60String urlPath = "/jndi/rmi://" + hostname + ":" + port + "/jmxrmi";61connect(urlPath);62}6364public void dump(long interval, long samples) {65try {66PrintGCStat pstat = new PrintGCStat(server);67for (int i = 0; i < samples; i++) {68pstat.printVerboseGc();69try {70Thread.sleep(interval);71} catch (InterruptedException e) {72System.exit(1);73}74}75} catch (IOException e) {76System.err.println("\nCommunication error: " + e.getMessage());77System.exit(1);78}79}8081/**82* Connect to a JMX agent of a given URL.83*/84private void connect(String urlPath) {85try {86JMXServiceURL url = new JMXServiceURL("rmi", "", 0, urlPath);87this.jmxc = JMXConnectorFactory.connect(url);88this.server = jmxc.getMBeanServerConnection();89} catch (MalformedURLException e) {90// should not reach here91} catch (IOException e) {92System.err.println("\nCommunication error: " + e.getMessage());93System.exit(1);94}95}9697public static void main(String[] args) {98if (args.length < 1) {99usage();100}101102String hostname = "";103int port = -1;104long interval = 5000; // default is 5 second interval105long mins = 5;106for (String arg: args) {107if (arg.startsWith("-")) {108if (arg.equals("-h") ||109arg.equals("-help") ||110arg.equals("-?")) {111usage();112} else if (arg.startsWith("-interval=")) {113try {114interval = Integer.parseInt(arg.substring(10)) * 1000;115} catch (NumberFormatException ex) {116usage();117}118} else if (arg.startsWith("-duration=")) {119try {120mins = Integer.parseInt(arg.substring(10));121} catch (NumberFormatException ex) {122usage();123}124} else {125// Unknown switch126System.err.println("Unrecognized option: " + arg);127usage();128}129} else {130String[] arg2 = arg.split(":");131if (arg2.length != 2) {132usage();133}134hostname = arg2[0];135try {136port = Integer.parseInt(arg2[1]);137} catch (NumberFormatException x) {138usage();139}140if (port < 0) {141usage();142}143}144}145146// get full thread dump and perform deadlock detection147VerboseGC vgc = new VerboseGC(hostname, port);148long samples = (mins * 60 * 1000) / interval;149vgc.dump(interval, samples);150151}152153private static void usage() {154System.out.print("Usage: java VerboseGC <hostname>:<port> ");155System.out.println(" [-interval=seconds] [-duration=minutes]");156}157}158159160