001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.util;
019
020import java.io.BufferedReader;
021import java.io.IOException;
022import java.io.InputStream;
023import java.io.InputStreamReader;
024import java.lang.management.ManagementFactory;
025import java.lang.management.OperatingSystemMXBean;
026import java.lang.management.RuntimeMXBean;
027import java.lang.reflect.Method;
028import java.nio.charset.StandardCharsets;
029import org.apache.yetus.audience.InterfaceAudience;
030import org.slf4j.Logger;
031import org.slf4j.LoggerFactory;
032
033/**
034 * This class is a wrapper for the implementation of com.sun.management.UnixOperatingSystemMXBean It
035 * will decide to use the sun api or its own implementation depending on the runtime (vendor) used.
036 */
037
038@InterfaceAudience.Private
039public class JVM {
040  private static final Logger LOG = LoggerFactory.getLogger(JVM.class);
041  private OperatingSystemMXBean osMbean;
042
043  private static final boolean ibmvendor =
044    System.getProperty("java.vendor") != null && System.getProperty("java.vendor").contains("IBM");
045  // At least on my systems os.name reports as "linux", not "Linux". Prefer case insensitive tests.
046  private static final boolean windows = System.getProperty("os.name") != null
047    && System.getProperty("os.name").toLowerCase().contains("windows");
048  private static final boolean linux = System.getProperty("os.name") != null
049    && System.getProperty("os.name").toLowerCase().contains("linux");
050  private static final boolean amd64 =
051    System.getProperty("os.arch") != null && System.getProperty("os.arch").contains("amd64");
052  private static final boolean aarch64 =
053    System.getProperty("os.arch") != null && System.getProperty("os.arch").contains("aarch64");
054
055  private static final String JVMVersion = System.getProperty("java.version");
056
057  /**
058   * The raw String of java specification version. "1.8" for java8, "9","10"... for Java 9, 10...
059   */
060  private static final String JVM_SPEC_VERSION_STRING =
061    System.getProperty("java.specification.version");
062
063  /**
064   * The Integer represent of JVM_SPEC_VERSION, for the JVM version comparison. Java 8, 9, 10 ...
065   * will be noted as 8, 9 10 ...
066   */
067  private static final int JVM_SPEC_VERSION = JVM_SPEC_VERSION_STRING.contains(".")
068    ? (int) (Float.parseFloat(JVM_SPEC_VERSION_STRING) * 10 % 10)
069    : Integer.parseInt(JVM_SPEC_VERSION_STRING);
070
071  /**
072   * Constructor. Get the running Operating System instance
073   */
074  public JVM() {
075    this.osMbean = ManagementFactory.getOperatingSystemMXBean();
076  }
077
078  /**
079   * Check if the OS is unix.
080   * @return whether this is unix or not.
081   */
082  public static boolean isUnix() {
083    if (windows) {
084      return false;
085    }
086    return (ibmvendor ? linux : true);
087  }
088
089  /**
090   * Check if the OS is linux.
091   * @return whether this is linux or not.
092   */
093  public static boolean isLinux() {
094    return linux;
095  }
096
097  /**
098   * Check if the arch is amd64;
099   * @return whether this is amd64 or not.
100   */
101  public static boolean isAmd64() {
102    return amd64;
103  }
104
105  /**
106   * Check if the arch is aarch64;
107   * @return whether this is aarch64 or not.
108   */
109  public static boolean isAarch64() {
110    return aarch64;
111  }
112
113  /**
114   * Check if the finish() method of GZIPOutputStream is broken
115   * @return whether GZIPOutputStream.finish() is broken.
116   */
117  public static boolean isGZIPOutputStreamFinishBroken() {
118    return ibmvendor && JVMVersion.contains("1.6.0");
119  }
120
121  public static int getJVMSpecVersion() {
122    return JVM_SPEC_VERSION;
123  }
124
125  /**
126   * Load the implementation of UnixOperatingSystemMXBean for Oracle jvm and runs the desired
127   * method.
128   * @param mBeanMethodName : method to run from the interface UnixOperatingSystemMXBean
129   * @return the method result
130   */
131  private Long runUnixMXBeanMethod(String mBeanMethodName) {
132    Object unixos;
133    Class<?> classRef;
134    Method mBeanMethod;
135
136    try {
137      classRef = Class.forName("com.sun.management.UnixOperatingSystemMXBean");
138      if (classRef.isInstance(osMbean)) {
139        mBeanMethod = classRef.getMethod(mBeanMethodName);
140        unixos = classRef.cast(osMbean);
141        return (Long) mBeanMethod.invoke(unixos);
142      }
143    } catch (Exception e) {
144      LOG.warn(
145        "Not able to load class or method for" + " com.sun.management.UnixOperatingSystemMXBean.",
146        e);
147    }
148    return null;
149  }
150
151  /**
152   * Get the number of opened filed descriptor for the runtime jvm. If Oracle java, it will use the
153   * com.sun.management interfaces. Otherwise, this methods implements it (linux only).
154   * @return number of open file descriptors for the jvm
155   */
156  public long getOpenFileDescriptorCount() {
157    Long ofdc;
158
159    if (!ibmvendor) {
160      ofdc = runUnixMXBeanMethod("getOpenFileDescriptorCount");
161      return (ofdc != null ? ofdc : -1);
162    }
163    InputStream inputStream = null;
164    InputStreamReader inputStreamReader = null;
165    BufferedReader bufferedReader = null;
166    try {
167      // need to get the PID number of the process first
168      RuntimeMXBean rtmbean = ManagementFactory.getRuntimeMXBean();
169      String rtname = rtmbean.getName();
170      String[] pidhost = rtname.split("@");
171
172      // using linux bash commands to retrieve info
173      Process p = Runtime.getRuntime()
174        .exec(new String[] { "bash", "-c", "ls /proc/" + pidhost[0] + "/fdinfo | wc -l" });
175      inputStream = p.getInputStream();
176      inputStreamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
177      bufferedReader = new BufferedReader(inputStreamReader);
178      String openFileDesCount;
179      if ((openFileDesCount = bufferedReader.readLine()) != null) {
180        return Long.parseLong(openFileDesCount);
181      }
182    } catch (IOException ie) {
183      LOG.warn("Not able to get the number of open file descriptors", ie);
184    } finally {
185      if (bufferedReader != null) {
186        try {
187          bufferedReader.close();
188        } catch (IOException e) {
189          LOG.warn("Not able to close the BufferedReader", e);
190        }
191      }
192      if (inputStreamReader != null) {
193        try {
194          inputStreamReader.close();
195        } catch (IOException e) {
196          LOG.warn("Not able to close the InputStreamReader", e);
197        }
198      }
199      if (inputStream != null) {
200        try {
201          inputStream.close();
202        } catch (IOException e) {
203          LOG.warn("Not able to close the InputStream", e);
204        }
205      }
206    }
207    return -1;
208  }
209
210  /**
211   * @see java.lang.management.OperatingSystemMXBean#getSystemLoadAverage
212   */
213  public double getSystemLoadAverage() {
214    return osMbean.getSystemLoadAverage();
215  }
216
217  /**
218   * @return the physical free memory (not the JVM one, as it's not very useful as it depends on the
219   *         GC), but the one from the OS as it allows a little bit more to guess if the machine is
220   *         overloaded or not).
221   */
222  public long getFreeMemory() {
223    if (ibmvendor) {
224      return 0;
225    }
226
227    Long r = runUnixMXBeanMethod("getFreePhysicalMemorySize");
228    return (r != null ? r : -1);
229  }
230
231  /**
232   * Workaround to get the current number of process running. Approach is the one described here:
233   * http://stackoverflow.com/questions/54686/how-to-get-a-list-of-current-open-windows-process-with-java
234   */
235  @edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "RV_DONT_JUST_NULL_CHECK_READLINE",
236      justification = "used by testing")
237  public int getNumberOfRunningProcess() {
238    if (!isUnix()) {
239      return 0;
240    }
241
242    InputStream inputStream = null;
243    InputStreamReader inputStreamReader = null;
244    BufferedReader bufferedReader = null;
245
246    try {
247      int count = 0;
248      Process p = Runtime.getRuntime().exec("ps -e");
249      inputStream = p.getInputStream();
250      inputStreamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
251      bufferedReader = new BufferedReader(inputStreamReader);
252      while (bufferedReader.readLine() != null) {
253        count++;
254      }
255      return count - 1; // -1 because there is a headline
256    } catch (IOException e) {
257      return -1;
258    } finally {
259      if (bufferedReader != null) {
260        try {
261          bufferedReader.close();
262        } catch (IOException e) {
263          LOG.warn("Not able to close the BufferedReader", e);
264        }
265      }
266      if (inputStreamReader != null) {
267        try {
268          inputStreamReader.close();
269        } catch (IOException e) {
270          LOG.warn("Not able to close the InputStreamReader", e);
271        }
272      }
273      if (inputStream != null) {
274        try {
275          inputStream.close();
276        } catch (IOException e) {
277          LOG.warn("Not able to close the InputStream", e);
278        }
279      }
280    }
281  }
282
283  /**
284   * Get the number of the maximum file descriptors the system can use. If Oracle java, it will use
285   * the com.sun.management interfaces. Otherwise, this methods implements it (linux only).
286   * @return max number of file descriptors the operating system can use.
287   */
288  public long getMaxFileDescriptorCount() {
289    Long mfdc;
290    if (!ibmvendor) {
291      mfdc = runUnixMXBeanMethod("getMaxFileDescriptorCount");
292      return (mfdc != null ? mfdc : -1);
293    }
294    InputStream in = null;
295    BufferedReader output = null;
296    try {
297      // using linux bash commands to retrieve info
298      Process p = Runtime.getRuntime().exec(new String[] { "bash", "-c", "ulimit -n" });
299      in = p.getInputStream();
300      output = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8));
301      String maxFileDesCount;
302      if ((maxFileDesCount = output.readLine()) != null) {
303        return Long.parseLong(maxFileDesCount);
304      }
305    } catch (IOException ie) {
306      LOG.warn("Not able to get the max number of file descriptors", ie);
307    } finally {
308      if (output != null) {
309        try {
310          output.close();
311        } catch (IOException e) {
312          LOG.warn("Not able to close the reader", e);
313        }
314      }
315      if (in != null) {
316        try {
317          in.close();
318        } catch (IOException e) {
319          LOG.warn("Not able to close the InputStream", e);
320        }
321      }
322    }
323    return -1;
324  }
325}