author | chegar |
Tue, 22 Mar 2016 10:43:19 +0000 | |
changeset 36709 | f9f252088afa |
parent 25859 | 3317bb8137f4 |
permissions | -rw-r--r-- |
2 | 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 | 3 |
* |
4 |
* Redistribution and use in source and binary forms, with or without |
|
5 |
* modification, are permitted provided that the following conditions |
|
6 |
* are met: |
|
7 |
* |
|
8 |
* - Redistributions of source code must retain the above copyright |
|
9 |
* notice, this list of conditions and the following disclaimer. |
|
10 |
* |
|
11 |
* - Redistributions in binary form must reproduce the above copyright |
|
12 |
* notice, this list of conditions and the following disclaimer in the |
|
13 |
* documentation and/or other materials provided with the distribution. |
|
14 |
* |
|
5506 | 15 |
* - Neither the name of Oracle nor the names of its |
2 | 16 |
* contributors may be used to endorse or promote products derived |
17 |
* from this software without specific prior written permission. |
|
18 |
* |
|
19 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
|
20 |
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
|
21 |
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|
22 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
|
23 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
|
24 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
25 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
|
26 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
|
27 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|
28 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|
29 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
30 |
*/ |
|
31 |
||
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 | 42 |
*/ |
43 |
||
44 |
import javax.management.*; |
|
45 |
import javax.management.remote.*; |
|
46 |
import java.io.IOException; |
|
47 |
import java.net.MalformedURLException; |
|
48 |
||
49 |
/** |
|
50 |
* This VerboseGC class demonstrates the capability to get |
|
51 |
* the garbage collection statistics and memory usage remotely. |
|
52 |
*/ |
|
53 |
public class VerboseGC { |
|
54 |
private MBeanServerConnection server; |
|
55 |
private JMXConnector jmxc; |
|
56 |
public VerboseGC(String hostname, int port) { |
|
57 |
System.out.println("Connecting to " + hostname + ":" + port); |
|
58 |
||
59 |
// Create an RMI connector client and connect it to |
|
60 |
// the RMI connector server |
|
61 |
String urlPath = "/jndi/rmi://" + hostname + ":" + port + "/jmxrmi"; |
|
62 |
connect(urlPath); |
|
63 |
} |
|
64 |
||
65 |
public void dump(long interval, long samples) { |
|
66 |
try { |
|
67 |
PrintGCStat pstat = new PrintGCStat(server); |
|
68 |
for (int i = 0; i < samples; i++) { |
|
69 |
pstat.printVerboseGc(); |
|
70 |
try { |
|
71 |
Thread.sleep(interval); |
|
72 |
} catch (InterruptedException e) { |
|
73 |
System.exit(1); |
|
74 |
} |
|
75 |
} |
|
76 |
} catch (IOException e) { |
|
77 |
System.err.println("\nCommunication error: " + e.getMessage()); |
|
78 |
System.exit(1); |
|
79 |
} |
|
80 |
} |
|
81 |
||
82 |
/** |
|
83 |
* Connect to a JMX agent of a given URL. |
|
84 |
*/ |
|
85 |
private void connect(String urlPath) { |
|
86 |
try { |
|
87 |
JMXServiceURL url = new JMXServiceURL("rmi", "", 0, urlPath); |
|
88 |
this.jmxc = JMXConnectorFactory.connect(url); |
|
89 |
this.server = jmxc.getMBeanServerConnection(); |
|
90 |
} catch (MalformedURLException e) { |
|
91 |
// should not reach here |
|
92 |
} catch (IOException e) { |
|
93 |
System.err.println("\nCommunication error: " + e.getMessage()); |
|
94 |
System.exit(1); |
|
95 |
} |
|
96 |
} |
|
97 |
||
98 |
public static void main(String[] args) { |
|
99 |
if (args.length < 1) { |
|
100 |
usage(); |
|
101 |
} |
|
102 |
||
103 |
String hostname = ""; |
|
104 |
int port = -1; |
|
105 |
long interval = 5000; // default is 5 second interval |
|
106 |
long mins = 5; |
|
9520 | 107 |
for (String arg: args) { |
108 |
if (arg.startsWith("-")) { |
|
2 | 109 |
if (arg.equals("-h") || |
110 |
arg.equals("-help") || |
|
111 |
arg.equals("-?")) { |
|
112 |
usage(); |
|
113 |
} else if (arg.startsWith("-interval=")) { |
|
114 |
try { |
|
115 |
interval = Integer.parseInt(arg.substring(10)) * 1000; |
|
116 |
} catch (NumberFormatException ex) { |
|
117 |
usage(); |
|
118 |
} |
|
119 |
} else if (arg.startsWith("-duration=")) { |
|
120 |
try { |
|
121 |
mins = Integer.parseInt(arg.substring(10)); |
|
122 |
} catch (NumberFormatException ex) { |
|
123 |
usage(); |
|
124 |
} |
|
125 |
} else { |
|
126 |
// Unknown switch |
|
127 |
System.err.println("Unrecognized option: " + arg); |
|
128 |
usage(); |
|
129 |
} |
|
130 |
} else { |
|
131 |
String[] arg2 = arg.split(":"); |
|
132 |
if (arg2.length != 2) { |
|
133 |
usage(); |
|
134 |
} |
|
135 |
hostname = arg2[0]; |
|
136 |
try { |
|
137 |
port = Integer.parseInt(arg2[1]); |
|
138 |
} catch (NumberFormatException x) { |
|
139 |
usage(); |
|
140 |
} |
|
141 |
if (port < 0) { |
|
142 |
usage(); |
|
143 |
} |
|
144 |
} |
|
145 |
} |
|
146 |
||
147 |
// get full thread dump and perform deadlock detection |
|
148 |
VerboseGC vgc = new VerboseGC(hostname, port); |
|
149 |
long samples = (mins * 60 * 1000) / interval; |
|
150 |
vgc.dump(interval, samples); |
|
151 |
||
152 |
} |
|
153 |
||
154 |
private static void usage() { |
|
155 |
System.out.print("Usage: java VerboseGC <hostname>:<port> "); |
|
156 |
System.out.println(" [-interval=seconds] [-duration=minutes]"); |
|
157 |
} |
|
158 |
} |