1 /* 2 * Copyright (c) 2017, 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 24 25 package org.graalvm.compiler.hotspot; 26 27 import static jdk.vm.ci.common.InitTimer.timer; 28 29 import java.io.File; 30 import java.io.FileReader; 31 import java.io.IOException; 32 import java.util.Map; 33 import java.util.Properties; 34 35 import jdk.internal.vm.compiler.collections.EconomicMap; 36 import org.graalvm.compiler.options.Option; 37 import org.graalvm.compiler.options.OptionDescriptors; 38 import org.graalvm.compiler.options.OptionKey; 39 import org.graalvm.compiler.options.OptionValues; 40 import org.graalvm.compiler.options.OptionsParser; 41 42 import jdk.vm.ci.common.InitTimer; 43 import jdk.vm.ci.common.NativeImageReinitialize; 44 import jdk.vm.ci.services.Services; 45 46 /** 47 * The {@link #defaultOptions()} method returns the options values initialized in a HotSpot VM. The 48 * values are set via system properties with the {@value #GRAAL_OPTION_PROPERTY_PREFIX} prefix. 49 */ 50 public class HotSpotGraalOptionValues { 51 52 /** 53 * The name of the system property specifying a file containing extra Graal option settings. 54 */ 55 private static final String GRAAL_OPTIONS_FILE_PROPERTY_NAME = "graal.options.file"; 56 57 /** 58 * The name of the system property specifying the Graal version. 59 */ 60 private static final String GRAAL_VERSION_PROPERTY_NAME = "graal.version"; 61 62 /** 63 * The prefix for system properties that correspond to {@link Option} annotated fields. A field 64 * named {@code MyOption} will have its value set from a system property with the name 65 * {@code GRAAL_OPTION_PROPERTY_PREFIX + "MyOption"}. 66 */ 67 public static final String GRAAL_OPTION_PROPERTY_PREFIX = "graal."; 68 69 /** 70 * Gets the system property assignment that would set the current value for a given option. 71 */ 72 public static String asSystemPropertySetting(OptionValues options, OptionKey<?> value) { 73 return GRAAL_OPTION_PROPERTY_PREFIX + value.getName() + "=" + value.getValue(options); 74 } 75 76 @NativeImageReinitialize private static volatile OptionValues hotspotOptions; 77 78 public static OptionValues defaultOptions() { 79 OptionValues res = hotspotOptions; 80 if (res == null) { 81 synchronized (HotSpotGraalOptionValues.class) { 82 res = hotspotOptions; 83 if (res == null) { 84 res = initializeOptions(); 85 hotspotOptions = res; 86 } 87 } 88 } 89 return res; 90 } 91 92 /** 93 * Gets and parses options based on {@linkplain Services#getSavedProperties() saved system 94 * properties}. The values for these options are initialized by parsing the file denoted by the 95 * {@value #GRAAL_OPTIONS_FILE_PROPERTY_NAME} property followed by parsing the options encoded 96 * in properties whose names start with {@value #GRAAL_OPTION_PROPERTY_PREFIX}. Key/value pairs 97 * are parsed from the aforementioned file with {@link Properties#load(java.io.Reader)}. 98 */ 99 @SuppressWarnings("try") 100 public static EconomicMap<OptionKey<?>, Object> parseOptions() { 101 EconomicMap<OptionKey<?>, Object> values = OptionValues.newOptionMap(); 102 try (InitTimer t = timer("InitializeOptions")) { 103 104 Iterable<OptionDescriptors> loader = OptionsParser.getOptionsLoader(); 105 Map<String, String> savedProps = jdk.vm.ci.services.Services.getSavedProperties(); 106 String optionsFile = savedProps.get(GRAAL_OPTIONS_FILE_PROPERTY_NAME); 107 108 if (optionsFile != null) { 109 File graalOptions = new File(optionsFile); 110 if (graalOptions.exists()) { 111 try (FileReader fr = new FileReader(graalOptions)) { 112 Properties props = new Properties(); 113 props.load(fr); 114 EconomicMap<String, String> optionSettings = EconomicMap.create(); 115 for (Map.Entry<Object, Object> e : props.entrySet()) { 116 optionSettings.put((String) e.getKey(), (String) e.getValue()); 117 } 118 try { 119 OptionsParser.parseOptions(optionSettings, values, loader); 120 } catch (Throwable e) { 121 throw new InternalError("Error parsing an option from " + graalOptions, e); 122 } 123 } catch (IOException e) { 124 throw new InternalError("Error reading " + graalOptions, e); 125 } 126 } 127 } 128 129 EconomicMap<String, String> optionSettings = EconomicMap.create(); 130 for (Map.Entry<String, String> e : savedProps.entrySet()) { 131 String name = e.getKey(); 132 if (name.startsWith(GRAAL_OPTION_PROPERTY_PREFIX)) { 133 if (name.equals("graal.PrintFlags") || name.equals("graal.ShowFlags")) { 134 System.err.println("The " + name + " option has been removed and will be ignored. Use -XX:+JVMCIPrintProperties instead."); 135 } else if (name.equals(GRAAL_OPTIONS_FILE_PROPERTY_NAME) || name.equals(GRAAL_VERSION_PROPERTY_NAME)) { 136 // Ignore well known properties that do not denote an option 137 } else { 138 String value = e.getValue(); 139 optionSettings.put(name.substring(GRAAL_OPTION_PROPERTY_PREFIX.length()), value); 140 } 141 } 142 } 143 144 OptionsParser.parseOptions(optionSettings, values, loader); 145 return values; 146 } 147 } 148 149 /** 150 * Substituted by 151 * {@code com.oracle.svm.graal.hotspot.libgraal.Target_org_graalvm_compiler_hotspot_HotSpotGraalOptionValues} 152 * to update {@code com.oracle.svm.core.option.RuntimeOptionValues.singleton()} instead of 153 * creating a new {@link OptionValues} object. 154 */ 155 private static OptionValues initializeOptions() { 156 return new OptionValues(parseOptions()); 157 } 158 }