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 44 /** 45 * The {@link #HOTSPOT_OPTIONS} value contains the options values initialized in a HotSpot VM. The 46 * values are set via system properties with the {@value #GRAAL_OPTION_PROPERTY_PREFIX} prefix. 47 */ 48 public class HotSpotGraalOptionValues { 49 50 /** 51 * The name of the system property specifying a file containing extra Graal option settings. 52 */ 53 private static final String GRAAL_OPTIONS_FILE_PROPERTY_NAME = "graal.options.file"; 54 55 /** 56 * The name of the system property specifying the Graal version. 57 */ 58 private static final String GRAAL_VERSION_PROPERTY_NAME = "graal.version"; 59 60 /** 61 * The prefix for system properties that correspond to {@link Option} annotated fields. A field 62 * named {@code MyOption} will have its value set from a system property with the name 63 * {@code GRAAL_OPTION_PROPERTY_PREFIX + "MyOption"}. 64 */ 65 public static final String GRAAL_OPTION_PROPERTY_PREFIX = "graal."; 66 67 /** 68 * Gets the system property assignment that would set the current value for a given option. 69 */ 70 public static String asSystemPropertySetting(OptionValues options, OptionKey<?> value) { 71 return GRAAL_OPTION_PROPERTY_PREFIX + value.getName() + "=" + value.getValue(options); 72 } 73 74 public static final OptionValues HOTSPOT_OPTIONS = initializeOptions(); 75 76 /** 77 * Global options. The values for these options are initialized by parsing the file denoted by 78 * the {@code VM.getSavedProperty(String) saved} system property named 79 * {@value #GRAAL_OPTIONS_FILE_PROPERTY_NAME} if the file exists followed by parsing the options 80 * encoded in saved system properties whose names start with 81 * {@value #GRAAL_OPTION_PROPERTY_PREFIX}. Key/value pairs are parsed from the aforementioned 82 * file with {@link Properties#load(java.io.Reader)}. 83 */ 84 @SuppressWarnings("try") 85 private static OptionValues initializeOptions() { 86 EconomicMap<OptionKey<?>, Object> values = OptionValues.newOptionMap(); 87 try (InitTimer t = timer("InitializeOptions")) { 88 89 Iterable<OptionDescriptors> loader = OptionsParser.getOptionsLoader(); 90 Map<String, String> savedProps = jdk.vm.ci.services.Services.getSavedProperties(); 91 String optionsFile = savedProps.get(GRAAL_OPTIONS_FILE_PROPERTY_NAME); 92 93 if (optionsFile != null) { 94 File graalOptions = new File(optionsFile); 95 if (graalOptions.exists()) { 96 try (FileReader fr = new FileReader(graalOptions)) { 97 Properties props = new Properties(); 98 props.load(fr); 99 EconomicMap<String, String> optionSettings = EconomicMap.create(); 100 for (Map.Entry<Object, Object> e : props.entrySet()) { 101 optionSettings.put((String) e.getKey(), (String) e.getValue()); 102 } 103 try { 104 OptionsParser.parseOptions(optionSettings, values, loader); 105 } catch (Throwable e) { 106 throw new InternalError("Error parsing an option from " + graalOptions, e); 107 } 108 } catch (IOException e) { 109 throw new InternalError("Error reading " + graalOptions, e); 110 } 111 } 112 } 113 114 EconomicMap<String, String> optionSettings = EconomicMap.create(); 115 for (Map.Entry<String, String> e : savedProps.entrySet()) { 116 String name = e.getKey(); 117 if (name.startsWith(GRAAL_OPTION_PROPERTY_PREFIX)) { 118 if (name.equals("graal.PrintFlags") || name.equals("graal.ShowFlags")) { 119 System.err.println("The " + name + " option has been removed and will be ignored. Use -XX:+JVMCIPrintProperties instead."); 120 } else if (name.equals(GRAAL_OPTIONS_FILE_PROPERTY_NAME) || name.equals(GRAAL_VERSION_PROPERTY_NAME)) { 121 // Ignore well known properties that do not denote an option 122 } else { 123 String value = e.getValue(); 124 optionSettings.put(name.substring(GRAAL_OPTION_PROPERTY_PREFIX.length()), value); 125 } 126 } 127 } 128 129 OptionsParser.parseOptions(optionSettings, values, loader); 130 return new OptionValues(values); 131 } 132 } 133 }