1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.util;
20
21 import java.lang.management.ManagementFactory;
22 import java.lang.management.OperatingSystemMXBean;
23 import java.lang.management.RuntimeMXBean;
24
25 import java.io.BufferedReader;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.io.InputStreamReader;
29
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import java.lang.reflect.Method;
33
34 import org.apache.hadoop.classification.InterfaceAudience;
35 import org.apache.hadoop.classification.InterfaceStability;
36
37
38
39
40
41
42
43
44
45 @InterfaceAudience.Public
46 @InterfaceStability.Evolving
47 public class JVM
48 {
49 static final Logger LOG = LoggerFactory.getLogger(JVM.class);
50
51 private OperatingSystemMXBean osMbean;
52
53 private static final boolean ibmvendor =
54 System.getProperty("java.vendor").contains("IBM");
55 private static final boolean windows =
56 System.getProperty("os.name").startsWith("Windows");
57 private static final boolean linux =
58 System.getProperty("os.name").startsWith("Linux");
59 private static final String JVMVersion = System.getProperty("java.version");
60
61
62
63
64 public JVM () {
65 this.osMbean = ManagementFactory.getOperatingSystemMXBean();
66 }
67
68
69
70
71
72
73 public static boolean isUnix() {
74 if (windows) {
75 return false;
76 }
77 return (ibmvendor ? linux : true);
78 }
79
80
81
82
83
84
85 public static boolean isGZIPOutputStreamFinishBroken() {
86 return ibmvendor && JVMVersion.contains("1.6.0");
87 }
88
89
90
91
92
93
94
95 private Long runUnixMXBeanMethod (String mBeanMethodName) {
96 Object unixos;
97 Class<?> classRef;
98 Method mBeanMethod;
99
100 try {
101 classRef = Class.forName("com.sun.management.UnixOperatingSystemMXBean");
102 if (classRef.isInstance(osMbean)) {
103 mBeanMethod = classRef.getMethod(mBeanMethodName, new Class[0]);
104 unixos = classRef.cast(osMbean);
105 return (Long)mBeanMethod.invoke(unixos);
106 }
107 }
108 catch(Exception e) {
109 LOG.warn("Not able to load class or method for" +
110 " com.sun.management.UnixOperatingSystemMXBean.", e);
111 }
112 return null;
113 }
114
115
116
117
118
119
120
121 public long getOpenFileDescriptorCount() {
122
123 Long ofdc;
124
125 if (!ibmvendor) {
126 ofdc = runUnixMXBeanMethod("getOpenFileDescriptorCount");
127 return (ofdc != null ? ofdc.longValue () : -1);
128 }
129 InputStream in = null;
130 BufferedReader output = null;
131 try {
132
133 RuntimeMXBean rtmbean = ManagementFactory.getRuntimeMXBean();
134 String rtname = rtmbean.getName();
135 String[] pidhost = rtname.split("@");
136
137
138 Process p = Runtime.getRuntime().exec(
139 new String[] { "bash", "-c",
140 "ls /proc/" + pidhost[0] + "/fdinfo | wc -l" });
141 in = p.getInputStream();
142 output = new BufferedReader(new InputStreamReader(in));
143 String openFileDesCount;
144 if ((openFileDesCount = output.readLine()) != null)
145 return Long.parseLong(openFileDesCount);
146 } catch (IOException ie) {
147 LOG.warn("Not able to get the number of open file descriptors", ie);
148 } finally {
149 if (output != null) {
150 try {
151 output.close();
152 } catch (IOException e) {
153 LOG.warn("Not able to close the InputStream", e);
154 }
155 }
156 if (in != null){
157 try {
158 in.close();
159 } catch (IOException e) {
160 LOG.warn("Not able to close the InputStream", e);
161 }
162 }
163 }
164 return -1;
165 }
166
167
168
169
170 public double getSystemLoadAverage() {
171 return osMbean.getSystemLoadAverage();
172 }
173
174
175
176
177
178
179 public long getFreeMemory() {
180 if (ibmvendor){
181 return 0;
182 }
183
184 Long r = runUnixMXBeanMethod("getFreePhysicalMemorySize");
185 return (r != null ? r : -1);
186 }
187
188
189
190
191
192
193 public int getNumberOfRunningProcess(){
194 if (!isUnix()){
195 return 0;
196 }
197
198 BufferedReader input = null;
199 try {
200 int count = 0;
201 Process p = Runtime.getRuntime().exec("ps -e");
202 input = new BufferedReader(new InputStreamReader(p.getInputStream()));
203 while (input.readLine() != null) {
204 count++;
205 }
206 return count - 1;
207 } catch (IOException e) {
208 return -1;
209 } finally {
210 if (input != null){
211 try {
212 input.close();
213 } catch (IOException ignored) {
214 }
215 }
216 }
217 }
218
219
220
221
222
223
224
225 public long getMaxFileDescriptorCount() {
226 Long mfdc;
227 if (!ibmvendor) {
228 mfdc = runUnixMXBeanMethod("getMaxFileDescriptorCount");
229 return (mfdc != null ? mfdc.longValue () : -1);
230 }
231 InputStream in = null;
232 BufferedReader output = null;
233 try {
234
235 Process p = Runtime.getRuntime().exec(new String[] { "bash", "-c", "ulimit -n" });
236 in = p.getInputStream();
237 output = new BufferedReader(new InputStreamReader(in));
238 String maxFileDesCount;
239 if ((maxFileDesCount = output.readLine()) != null) return Long.parseLong(maxFileDesCount);
240 } catch (IOException ie) {
241 LOG.warn("Not able to get the max number of file descriptors", ie);
242 } finally {
243 if (output != null) {
244 try {
245 output.close();
246 } catch (IOException e) {
247 LOG.warn("Not able to close the reader", e);
248 }
249 }
250 if (in != null){
251 try {
252 in.close();
253 } catch (IOException e) {
254 LOG.warn("Not able to close the InputStream", e);
255 }
256 }
257 }
258 return -1;
259 }
260 }