Path: blob/master/test/hotspot/jtreg/compiler/compilercontrol/parser/HugeDirectiveUtil.java
41154 views
/*1* Copyright (c) 2015, 2020, 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*/2223package compiler.compilercontrol.parser;2425import compiler.compilercontrol.share.JSONFile;26import compiler.compilercontrol.share.method.MethodDescriptor;27import compiler.compilercontrol.share.scenario.DirectiveWriter;28import compiler.compilercontrol.share.scenario.Scenario;29import jdk.test.lib.process.OutputAnalyzer;30import jdk.test.lib.process.ProcessTools;31import jdk.test.lib.Utils;3233import java.util.EnumSet;34import java.util.List;35import java.util.Random;36import java.util.stream.Collectors;3738import static compiler.compilercontrol.share.IntrinsicCommand.VALID_INTRINSIC_SAMPLES;3940/**41* Creates a huge directive file42*/43public final class HugeDirectiveUtil {44protected static final String EXPECTED_ERROR_STRING = "Parsing of compiler "45+ "directives failed";4647private HugeDirectiveUtil() { }4849/**50* Creates huge file with specified amount of directives51*52* @param descriptors a list of descriptors to be randomly used53* in match and inline blocks54* @param fileName a directives file name to be created55* @param amount an amount of match objects56*/57public static void createHugeFile(List<MethodDescriptor> descriptors,58String fileName, int amount) {59try (DirectiveWriter file = new DirectiveWriter(fileName)) {60file.write(JSONFile.Element.ARRAY);61for (int i = 0; i < amount; i++) {62createMatchObject(descriptors, file, 1);63}64file.end();65}66}6768/**69* Creates match object in the given file with specified size70*71* @param descriptors a list of method descriptors to be used72* @param file a directive file to write at73* @param objectSize a size of the match object74*/75public static void createMatchObject(List<MethodDescriptor> descriptors,76DirectiveWriter file, int objectSize) {77// get random amount of methods for the match78List<String> methods = getRandomDescriptors(descriptors);79file.match(methods.toArray(new String[methods.size()]));80Random random = Utils.getRandomInstance();81for (int i = 0; i < objectSize; i++) {82// emit compiler block83file.emitCompiler(Utils.getRandomElement(84Scenario.Compiler.values()));85// add option inside the compiler block86DirectiveWriter.Option option = Utils.getRandomElement(DirectiveWriter.Option.values());87file.option(option,88option != DirectiveWriter.Option.INTRINSIC89? random.nextBoolean()90: "\"" + Utils.getRandomElement(VALID_INTRINSIC_SAMPLES) + "\"");91file.end(); // ends compiler block9293// add standalone option, enable can't be used standalone94EnumSet<DirectiveWriter.Option> options = EnumSet.complementOf(95EnumSet.of(DirectiveWriter.Option.ENABLE));96file.option(Utils.getRandomElement(options), random.nextBoolean());97}98// add inline block with random inlinees99methods = getRandomDescriptors(descriptors).stream()100.map(s -> (random.nextBoolean() ? "+" : "-") + s)101.collect(Collectors.toList());102file.inline(methods);103104// end match block105file.end();106}107108private static List<String> getRandomDescriptors(109List<MethodDescriptor> descriptors) {110Random random = Utils.getRandomInstance();111int amount = 1 + random.nextInt(descriptors.size() - 1);112int skipAmount = random.nextInt(descriptors.size() - amount);113return descriptors.stream()114.skip(skipAmount)115.limit(amount)116.map(MethodDescriptor::getString)117.collect(Collectors.toList());118}119120protected static OutputAnalyzer execute(String fileName) {121OutputAnalyzer output;122try {123output = ProcessTools.executeTestJvm(124"-XX:+UnlockDiagnosticVMOptions",125"-XX:CompilerDirectivesLimit=1000",126"-XX:CompilerDirectivesFile=" + fileName,127"-version");128} catch (Throwable thr) {129throw new Error("Execution failed with: " + thr, thr);130}131return output;132}133}134135136