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