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 45 /** 46 * The {@link #defaultOptions()} method returns the options values initialized in a HotSpot VM. The 47 * values are set via system properties with the {@value #GRAAL_OPTION_PROPERTY_PREFIX} prefix. 48 */ 49 public class HotSpotGraalOptionValues { 50 51 /** 52 * The name of the system property specifying a file containing extra Graal option settings. 53 */ 54 private static final String GRAAL_OPTIONS_FILE_PROPERTY_NAME = "graal.options.file"; 55 56 /** 57 * The name of the system property specifying the Graal version. 58 */ 59 private static final String GRAAL_VERSION_PROPERTY_NAME = "graal.version"; 60 61 /** 62 * The prefix for system properties that correspond to {@link Option} annotated fields. A field 63 * named {@code MyOption} will have its value set from a system property with the name 64 * {@code GRAAL_OPTION_PROPERTY_PREFIX + "MyOption"}. 65 */ 66 public static final String GRAAL_OPTION_PROPERTY_PREFIX = "graal."; 67 68 /** 69 * Gets the system property assignment that would set the current value for a given option. 70 */ 71 public static String asSystemPropertySetting(OptionValues options, OptionKey<?> value) { 72 return GRAAL_OPTION_PROPERTY_PREFIX + value.getName() + "=" + value.getValue(options); 73 } 74 75 @NativeImageReinitialize private static volatile OptionValues hotspotOptions; 76 77 public static OptionValues defaultOptions() { 78 OptionValues res = hotspotOptions; 79 if (res == null) { 80 synchronized (HotSpotGraalOptionValues.class) { 81 res = hotspotOptions; 82 if (res == null) { 83 res = initializeOptions(); 84 hotspotOptions = res; 85 } 86 } 87 } 88 return res; 89 } 90 91 /** 92 * Global options. The values for these options are initialized by parsing the file denoted by 93 * the {@code VM.getSavedProperty(String) saved} system property named 94 * {@value #GRAAL_OPTIONS_FILE_PROPERTY_NAME} if the file exists followed by parsing the options 95 * encoded in saved system properties whose names start with 96 * {@value #GRAAL_OPTION_PROPERTY_PREFIX}. Key/value pairs are parsed from the aforementioned 97 * file with {@link Properties#load(java.io.Reader)}. 98 */ 99 @SuppressWarnings("try") 100 private static OptionValues initializeOptions() { 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 new OptionValues(values); 146 } 147 } 148 }