KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > VerboseGC


1 /*
2  * @(#)VerboseGC.java 1.4 05/11/17
3  *
4  * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * -Redistribution of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  *
12  * -Redistribution in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * Neither the name of Sun Microsystems, Inc. or the names of contributors may
17  * be used to endorse or promote products derived from this software without
18  * specific prior written permission.
19  *
20  * This software is provided "AS IS," without a warranty of any kind. ALL
21  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
22  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
23  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
24  * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
25  * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
26  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
27  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
28  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
29  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
30  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31  *
32  * You acknowledge that this software is not designed, licensed or intended
33  * for use in the design, construction, operation or maintenance of any
34  * nuclear facility.
35  */

36
37 /*
38  * @(#)VerboseGC.java 1.4 05/11/17
39  */

40
41 import javax.management.*;
42 import javax.management.remote.*;
43 import java.io.IOException JavaDoc;
44 import java.net.MalformedURLException JavaDoc;
45
46 /**
47  * This VerboseGC class demonstrates the capability to get
48  * the garbage collection statistics and memory usage remotely.
49  */

50 public class VerboseGC {
51     private MBeanServerConnection server;
52     private JMXConnector jmxc;
53     public VerboseGC(String JavaDoc hostname, int port) {
54         System.out.println("Connecting to " + hostname + ":" + port);
55
56         // Create an RMI connector client and connect it to
57
// the RMI connector server
58
String JavaDoc urlPath = "/jndi/rmi://" + hostname + ":" + port + "/jmxrmi";
59         connect(urlPath);
60    }
61    
62    public void dump(long interval, long samples) {
63         try {
64             PrintGCStat pstat = new PrintGCStat(server);
65             for (int i = 0; i < samples; i++) {
66                 pstat.printVerboseGc();
67                 try {
68                     Thread.sleep(interval);
69                 } catch (InterruptedException JavaDoc e) {
70                     System.exit(1);
71                 }
72             }
73         } catch (IOException JavaDoc e) {
74             System.err.println("\nCommunication error: " + e.getMessage());
75             System.exit(1);
76         }
77     }
78
79     /**
80      * Connect to a JMX agent of a given URL.
81      */

82     private void connect(String JavaDoc urlPath) {
83         try {
84             JMXServiceURL url = new JMXServiceURL("rmi", "", 0, urlPath);
85             this.jmxc = JMXConnectorFactory.connect(url);
86             this.server = jmxc.getMBeanServerConnection();
87         } catch (MalformedURLException JavaDoc e) {
88             // should not reach here
89
} catch (IOException JavaDoc e) {
90             System.err.println("\nCommunication error: " + e.getMessage());
91             System.exit(1);
92         }
93     }
94
95     public static void main(String JavaDoc[] args) {
96         if (args.length < 1) {
97             usage();
98         }
99
100         String JavaDoc hostname = "";
101         int port = -1;
102         long interval = 5000; // default is 5 second interval
103
long mins = 5;
104         for (int argIndex = 0; argIndex < args.length; argIndex++) {
105            String JavaDoc arg = args[argIndex];
106             if (args[argIndex].startsWith("-")) {
107                 if (arg.equals("-h") ||
108                     arg.equals("-help") ||
109                     arg.equals("-?")) {
110                     usage();
111                 } else if (arg.startsWith("-interval=")) {
112                     try {
113                         interval = Integer.parseInt(arg.substring(10)) * 1000;
114                     } catch (NumberFormatException JavaDoc ex) {
115                         usage();
116                     }
117                 } else if (arg.startsWith("-duration=")) {
118                     try {
119                         mins = Integer.parseInt(arg.substring(10));
120                     } catch (NumberFormatException JavaDoc ex) {
121                         usage();
122                     }
123                 } else {
124                     // Unknown switch
125
System.err.println("Unrecognized option: " + arg);
126                     usage();
127                 }
128             } else {
129                 String JavaDoc[] arg2 = arg.split(":");
130                 if (arg2.length != 2) {
131                     usage();
132                 }
133                 hostname = arg2[0];
134                 try {
135                     port = Integer.parseInt(arg2[1]);
136                 } catch (NumberFormatException JavaDoc x) {
137                     usage();
138                 }
139                 if (port < 0) {
140                     usage();
141                 }
142             }
143         }
144
145         // get full thread dump and perform deadlock detection
146
VerboseGC vgc = new VerboseGC(hostname, port);
147         long samples = (mins * 60 * 1000) / interval;
148         vgc.dump(interval, samples);
149
150     }
151
152     private static void usage() {
153         System.out.print("Usage: java VerboseGC <hostname>:<port> ");
154         System.out.println(" [-interval=seconds] [-duration=minutes]");
155     }
156 }
157
Popular Tags