11/*
2- * Copyright (c) 2005, 2016 , Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 2005, 2024 , Oracle and/or its affiliates. All rights reserved.
33 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44 *
55 * This code is free software; you can redistribute it and/or modify it
2323
2424package jdk .test .lib .jittester ;
2525
26- import jdk .test .lib .util .Pair ;
27- import jdk .test .lib .jittester .factories .IRNodeBuilder ;
28- import jdk .test .lib .jittester .types .TypeKlass ;
29- import jdk .test .lib .jittester .utils .FixedTrees ;
30- import jdk .test .lib .jittester .utils .OptionResolver ;
31- import jdk .test .lib .jittester .utils .OptionResolver .Option ;
32- import jdk .test .lib .jittester .utils .PseudoRandom ;
3326import java .time .LocalTime ;
3427import java .util .ArrayList ;
3528import java .util .List ;
3932public class Automatic {
4033 public static final int MINUTES_TO_WAIT = Integer .getInteger ("jdk.test.lib.jittester" , 3 );
4134
42- private static Pair <IRNode , IRNode > generateIRTree (String name ) {
43- SymbolTable .removeAll ();
44- TypeList .removeAll ();
45-
46- IRNodeBuilder builder = new IRNodeBuilder ()
47- .setPrefix (name )
48- .setName (name )
49- .setLevel (0 );
50-
51- Long complexityLimit = ProductionParams .complexityLimit .value ();
52- IRNode privateClasses = null ;
53- if (!ProductionParams .disableClasses .value ()) {
54- long privateClassComlexity = (long ) (complexityLimit * PseudoRandom .random ());
55- try {
56- privateClasses = builder .setComplexityLimit (privateClassComlexity )
57- .getClassDefinitionBlockFactory ()
58- .produce ();
59- } catch (ProductionFailedException ex ) {
60- ex .printStackTrace (System .out );
61- }
62- }
63- long mainClassComplexity = (long ) (complexityLimit * PseudoRandom .random ());
64- IRNode mainClass = null ;
65- try {
66- mainClass = builder .setComplexityLimit (mainClassComplexity )
67- .getMainKlassFactory ()
68- .produce ();
69- TypeKlass aClass = new TypeKlass (name );
70- mainClass .getChild (1 ).addChild (FixedTrees .generateMainOrExecuteMethod (aClass , true ));
71- mainClass .getChild (1 ).addChild (FixedTrees .generateMainOrExecuteMethod (aClass , false ));
72- } catch (ProductionFailedException ex ) {
73- ex .printStackTrace (System .out );
74- }
75- return new Pair <>(mainClass , privateClasses );
76- }
77-
78- private static void initializeTestGenerator (String [] params ) {
79- OptionResolver parser = new OptionResolver ();
80- Option <String > propertyFileOpt = parser .addStringOption ('p' , "property-file" ,
81- "conf/default.properties" , "File to read properties from" );
82- ProductionParams .register (parser );
83- parser .parse (params , propertyFileOpt );
84- PseudoRandom .reset (ProductionParams .seed .value ());
85- TypesParser .parseTypesAndMethods (ProductionParams .classesFile .value (),
86- ProductionParams .excludeMethodsFile .value ());
87- if (ProductionParams .specificSeed .isSet ()) {
88- PseudoRandom .setCurrentSeed (ProductionParams .specificSeed .value ());
89- }
90- }
91-
9235 private static List <TestsGenerator > getTestGenerators () {
9336 List <TestsGenerator > result = new ArrayList <>();
9437 Class <?> factoryClass ;
@@ -108,7 +51,9 @@ private static List<TestsGenerator> getTestGenerators() {
10851 }
10952
11053 public static void main (String [] args ) {
111- initializeTestGenerator (args );
54+ ProductionParams .initializeFromCmdline (args );
55+ IRTreeGenerator .initializeWithProductionParams ();
56+
11257 int counter = 0 ;
11358 System .out .printf ("Generating %d tests...%n" , ProductionParams .numberOfTests .value ());
11459 System .out .printf (" %13s | %8s | %8s | %8s |%n" , "start time" , "count" , "generat" ,
@@ -117,34 +62,41 @@ public static void main(String[] args) {
11762 List <TestsGenerator > generators = getTestGenerators ();
11863 do {
11964 double start = System .currentTimeMillis ();
120- System .out .print ("[" + LocalTime .now () + "] |" );
121- String name = "Test_" + counter ;
122- Pair <IRNode , IRNode > irTree = generateIRTree (name );
123- System .out .printf (" %8d |" , counter );
124- long maxWaitTime = TimeUnit .MINUTES .toMillis (MINUTES_TO_WAIT );
125- double generationTime = System .currentTimeMillis () - start ;
126- System .out .printf (" %8.0f |" , generationTime );
127- start = System .currentTimeMillis ();
128- Thread generatorThread = new Thread (() -> {
129- for (TestsGenerator generator : generators ) {
130- generator .accept (irTree .first , irTree .second );
131- }
132- });
133- generatorThread .start ();
13465 try {
135- generatorThread .join (maxWaitTime );
136- } catch (InterruptedException ie ) {
137- throw new Error ("Test generation interrupted: " + ie , ie );
138- }
139- if (generatorThread .isAlive ()) {
140- // maxTime reached, so, proceed to next test generation
141- generatorThread .interrupt ();
142- } else {
143- double runningTime = System .currentTimeMillis () - start ;
144- System .out .printf (" %8.0f |%n" , runningTime );
145- if (runningTime < maxWaitTime ) {
146- ++counter ;
66+ System .out .print ("[" + LocalTime .now () + "] |" );
67+ String name = "Test_" + counter ;
68+ var test = IRTreeGenerator .generateIRTree (name );
69+ System .out .printf (" %8d |" , counter );
70+ long maxWaitTime = TimeUnit .MINUTES .toMillis (MINUTES_TO_WAIT );
71+ double generationTime = System .currentTimeMillis () - start ;
72+ System .out .printf (" %8.0f |" , generationTime );
73+ start = System .currentTimeMillis ();
74+ Thread generatorThread = new Thread (() -> {
75+ for (TestsGenerator generator : generators ) {
76+ generator .accept (test );
77+ }
78+ });
79+ generatorThread .start ();
80+ try {
81+ generatorThread .join (maxWaitTime );
82+ } catch (InterruptedException ie ) {
83+ throw new Error ("Test generation interrupted: " + ie , ie );
84+ }
85+ if (generatorThread .isAlive ()) {
86+ // maxTime reached, so, proceed to next test generation
87+ generatorThread .interrupt ();
88+ } else {
89+ double runningTime = System .currentTimeMillis () - start ;
90+ System .out .printf (" %8.0f |%n" , runningTime );
91+ if (runningTime < maxWaitTime ) {
92+ ++counter ;
93+ }
14794 }
95+ } catch (RuntimeException ignored ) {
96+ // Generation failures happen due to nature of fuzzing test generators,
97+ // such errors are ignored.
98+ System .out .println ("Test_" + counter + " ignored, generation failed due to " +
99+ ignored .getMessage ());
148100 }
149101 } while (counter < ProductionParams .numberOfTests .value ());
150102 }
0 commit comments