Path: blob/master/test/hotspot/jtreg/vmTestbase/vm/share/VMRuntimeEnvUtils.java
41153 views
/*1* Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22package vm.share;2324import com.sun.management.HotSpotDiagnosticMXBean;25import com.sun.management.VMOption;2627import java.lang.management.ManagementFactory;28import java.util.Objects;2930public class VMRuntimeEnvUtils {31private static HotSpotDiagnosticMXBean DIAGNOSTIC_BEAN32= ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class);3334private VMRuntimeEnvUtils() {35}3637public static boolean isJITEnabled() {38boolean isJITEnabled = ManagementFactory.getCompilationMXBean() != null;3940return isJITEnabled;41}4243/**44* Returns value of VM option.45*46* @param name option's name47* @return value of option or {@code null}, if option doesn't exist48* @throws NullPointerException if name is null49* @see HotSpotDiagnosticMXBean#getVMOption(String)50*/51public static String getVMOption(String name) {52Objects.requireNonNull(name);53VMOption tmp;54try {55tmp = DIAGNOSTIC_BEAN.getVMOption(name);56} catch (IllegalArgumentException e) {57tmp = null;58}59return (tmp == null ? null : tmp.getValue());60}6162/**63* Returns value of VM option or default value.64*65* @param name option's name66* @param defaultValue default value67* @return value of option or {@code defaultValue}, if option doesn't exist68* @throws NullPointerException if name is null69* @see #getVMOption(String)70*/71public static String getVMOption(String name, String defaultValue) {72String result = getVMOption(name);73return result == null ? defaultValue : result;74}7576/**77* Returns if a boolean VM option is enabled or not.78*79* @param name option's name80* @return true iff enabled81* @throws IllegalArgumentException if naming non-boolean or non-existing option82*/83public static boolean isVMOptionEnabled(String name) {84String isSet = getVMOption(name, "error");85if (isSet.equals("true")) {86return true;87} else if (isSet.equals("false")) {88return false;89}90throw new IllegalArgumentException(name + " is not a boolean option.");91}9293/**94* Sets a specified value for VM option of given name.95*96* @param name option's name97* @param value new value98* @throws NullPointerException if name is null99* @throws IllegalArgumentException if new value is invalid or if vm option100* is not writable.101* @see HotSpotDiagnosticMXBean#setVMOption(String, String)102*/103public static void setVMOption(String name, String value) {104Objects.requireNonNull(name);105DIAGNOSTIC_BEAN.setVMOption(name, value);106}107}108109110