1 /* 2 * Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 package vm.share; 24 25 import com.sun.management.HotSpotDiagnosticMXBean; 26 import com.sun.management.VMOption; 27 28 import java.lang.management.ManagementFactory; 29 import java.util.Objects; 30 31 public class VMRuntimeEnvUtils { 32 private static HotSpotDiagnosticMXBean DIAGNOSTIC_BEAN 33 = ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class); 34 VMRuntimeEnvUtils()35 private VMRuntimeEnvUtils() { 36 } 37 isJITEnabled()38 public static boolean isJITEnabled() { 39 boolean isJITEnabled = ManagementFactory.getCompilationMXBean() != null; 40 41 return isJITEnabled; 42 } 43 44 /** 45 * Returns value of VM option. 46 * 47 * @param name option's name 48 * @return value of option or {@code null}, if option doesn't exist 49 * @throws NullPointerException if name is null 50 * @see HotSpotDiagnosticMXBean#getVMOption(String) 51 */ getVMOption(String name)52 public static String getVMOption(String name) { 53 Objects.requireNonNull(name); 54 VMOption tmp; 55 try { 56 tmp = DIAGNOSTIC_BEAN.getVMOption(name); 57 } catch (IllegalArgumentException e) { 58 tmp = null; 59 } 60 return (tmp == null ? null : tmp.getValue()); 61 } 62 63 /** 64 * Returns value of VM option or default value. 65 * 66 * @param name option's name 67 * @param defaultValue default value 68 * @return value of option or {@code defaultValue}, if option doesn't exist 69 * @throws NullPointerException if name is null 70 * @see #getVMOption(String) 71 */ getVMOption(String name, String defaultValue)72 public static String getVMOption(String name, String defaultValue) { 73 String result = getVMOption(name); 74 return result == null ? defaultValue : result; 75 } 76 77 /** 78 * Returns if a boolean VM option is enabled or not. 79 * 80 * @param name option's name 81 * @return true iff enabled 82 * @throws IllegalArgumentException if naming non-boolean or non-existing option 83 */ isVMOptionEnabled(String name)84 public static boolean isVMOptionEnabled(String name) { 85 String isSet = getVMOption(name, "error"); 86 if (isSet.equals("true")) { 87 return true; 88 } else if (isSet.equals("false")) { 89 return false; 90 } 91 throw new IllegalArgumentException(name + " is not a boolean option."); 92 } 93 94 /** 95 * Sets a specified value for VM option of given name. 96 * 97 * @param name option's name 98 * @param value new value 99 * @throws NullPointerException if name is null 100 * @throws IllegalArgumentException if new value is invalid or if vm option 101 * is not writable. 102 * @see HotSpotDiagnosticMXBean#setVMOption(String, String) 103 */ setVMOption(String name, String value)104 public static void setVMOption(String name, String value) { 105 Objects.requireNonNull(name); 106 DIAGNOSTIC_BEAN.setVMOption(name, value); 107 } 108 } 109