31
31
32
32
import java .io .ByteArrayOutputStream ;
33
33
import java .io .PrintStream ;
34
- import java .util .ArrayList ;
35
- import java .util .Arrays ;
36
- import java .util .List ;
34
+ import java .util .*;
37
35
import java .util .regex .Matcher ;
38
36
import java .util .regex .Pattern ;
39
37
51
49
52
50
public class TestIRMatching {
53
51
54
- private static final List <Exception > exceptions = new ArrayList <>();
52
+ private static final Map <Exception , String > exceptions = new LinkedHashMap <>();
53
+ private static final ByteArrayOutputStream baos = new ByteArrayOutputStream ();
54
+ private static final ByteArrayOutputStream baosErr = new ByteArrayOutputStream ();
55
+ private static final PrintStream ps = new PrintStream (baos );
56
+ private static final PrintStream psErr = new PrintStream (baosErr );
57
+ private static final PrintStream oldOut = System .out ;
58
+ private static final PrintStream oldErr = System .err ;
55
59
56
60
private static void addException (Exception e ) {
57
- System .out .println (TestFramework .getLastTestVMOutput ());
58
- exceptions .add (e );
61
+ System .out .flush ();
62
+ System .err .flush ();
63
+ exceptions .put (e , baos .toString () + System .lineSeparator () + baosErr .toString ());
59
64
}
60
65
61
66
public static void main (String [] args ) {
62
- runFailOnTestsArgs ( BadFailOnConstraint . create ( AndOr1 . class , "test1(int)" , 1 , "CallStaticJava" ), "-XX:TLABRefillWasteFraction=50" , "-XX:+UsePerfData" , "-XX:+UseTLAB" );
63
- runFailOnTestsArgs ( BadFailOnConstraint . create ( AndOr1 . class , "test2()" , 1 , "CallStaticJava" ), "-XX:TLABRefillWasteFraction=50" , "-XX:-UsePerfData" , "-XX:+UseTLAB" );
64
-
67
+ // Redirect System.out and System.err to reduce noise.
68
+ System . setOut ( ps );
69
+ System . setErr ( psErr );
65
70
runWithArguments (AndOr1 .class , "-XX:TLABRefillWasteFraction=52" , "-XX:+UsePerfData" , "-XX:+UseTLAB" );
66
71
runWithArguments (CountComparisons .class , "-XX:TLABRefillWasteFraction=50" );
67
72
runWithArguments (GoodCount .class , "-XX:TLABRefillWasteFraction=50" );
68
73
runWithArguments (MultipleFailOnGood .class , "-XX:TLABRefillWasteFraction=50" );
69
74
75
+ runCheck (new String [] {"-XX:TLABRefillWasteFraction=50" , "-XX:+UsePerfData" , "-XX:+UseTLAB" }, BadFailOnConstraint .create (AndOr1 .class , "test1(int)" , 1 , "CallStaticJava" ));
76
+ runCheck (new String [] {"-XX:TLABRefillWasteFraction=50" , "-XX:-UsePerfData" , "-XX:+UseTLAB" }, BadFailOnConstraint .create (AndOr1 .class , "test2()" , 1 , "CallStaticJava" ));
77
+
70
78
String [] allocMatches = { "MyClass" , "wrapper for: _new_instance_Java" };
71
79
runCheck (BadFailOnConstraint .create (MultipleFailOnBad .class , "fail1()" , 1 , 1 , "Store" ),
72
80
BadFailOnConstraint .create (MultipleFailOnBad .class , "fail1()" , 1 , 3 , "Store" ),
@@ -223,55 +231,49 @@ public static void main(String[] args) {
223
231
: BadFailOnConstraint .create (CheckCastArray .class , "arrayCopy(java.lang.Object[],java.lang.Class)" , 1 , "checkcast_arraycopy" )
224
232
);
225
233
226
- // Redirect stdout to stream and then check if we find required IR encoding read from socket.
227
- ByteArrayOutputStream baos = new ByteArrayOutputStream ();
228
- PrintStream ps = new PrintStream (baos );
229
- PrintStream old = System .out ;
230
- System .setOut (ps );
231
-
232
234
try {
233
235
runWithArgumentsFail (CompilationOutputOfFails .class );
234
- Utils .shouldHaveThrownException ();
236
+ Utils .shouldHaveThrownException (baos . toString () );
235
237
} catch (IRViolationException e ) {
236
238
try {
237
- boolean failed = false ;
239
+ StringBuilder failures = new StringBuilder () ;
238
240
System .out .flush ();
239
241
String output = baos .toString ();
240
242
baos .reset ();
241
243
Pattern pattern = Pattern .compile (">>> Compilation.*both\\ d.*\\ RPrintIdeal:(?:(?!PrintOpto|>>> Compilation)[\\ S\\ s])+PrintOptoAssembly" );
242
244
Matcher matcher = pattern .matcher (output );
243
245
long bothCount = matcher .results ().count ();
244
246
if (bothCount != 7L ) {
245
- exceptions .add (new RuntimeException ("Could not find all both() methods, expected 7 but found " + bothCount ));
246
- failed = true ;
247
+ failures .append ("- Could not find all both() methods, expected 7 but found " ).append (bothCount ).append (System .lineSeparator ());
247
248
}
248
249
pattern = Pattern .compile (">>> Compilation.*ideal\\ d.*\\ RPrintIdeal:(?:(?!>>> Compilation)[\\ S\\ s])+" );
249
250
matcher = pattern .matcher (output );
250
251
int count = 0 ;
251
252
while (matcher .find ()) {
252
253
String match = matcher .group ();
253
- Asserts .assertFalse (match .contains ("PrintOptoAssembly" ), "Cannot contain opto assembly: " + output );
254
+ if (match .contains ("PrintOptoAssembly" )) {
255
+ failures .append ("Cannot contain opto assembly: " ).append (System .lineSeparator ()).append (match );
256
+ }
254
257
count ++;
255
258
}
256
259
if (count != 7 ) {
257
- exceptions .add (new RuntimeException ("Could not find all ideal() methods, expected 7 but found " + count ));
258
- failed = true ;
260
+ failures .append ("- Could not find all ideal() methods, expected 7 but found " ).append (count ).append (System .lineSeparator ());
259
261
}
260
262
pattern = Pattern .compile (">>> Compilation.*opto\\ d.*\\ RPrintOptoAssembly:(?:(?!>>> Compilation)[\\ S\\ s])+" );
261
263
matcher = pattern .matcher (output );
262
264
count = 0 ;
263
265
while (matcher .find ()) {
264
266
String match = matcher .group ();
265
- Asserts .assertFalse (match .contains ("PrintIdeal" ), "Cannot contain opto assembly: " + output );
267
+ if (match .contains ("PrintIdeal" )) {
268
+ failures .append ("Cannot contain print assembly: " ).append (System .lineSeparator ()).append (match );
269
+ }
266
270
count ++;
267
271
}
268
272
if (count != 7 ) {
269
- exceptions .add (new RuntimeException ("Could not find all opto() methods, expected 7 but found " + count ));
270
- failed = true ;
273
+ failures .append ("- Could not find all opto() methods, expected 7 but found " ).append (count ).append (System .lineSeparator ());
271
274
}
272
- if (failed ) {
273
- System .err .println (TestFramework .getLastTestVMOutput ());
274
- System .err .println (output );
275
+ if (!failures .isEmpty ()) {
276
+ addException (new RuntimeException (failures .toString ()));
275
277
}
276
278
} catch (Exception e1 ) {
277
279
addException (e1 );
@@ -283,52 +285,80 @@ public static void main(String[] args) {
283
285
runWithArguments (FlagComparisons .class , "-XX:TLABRefillWasteFraction=50" );
284
286
System .out .flush ();
285
287
String output = baos .toString ();
286
- baos .reset ();
287
288
findIrIds (output , "testMatchAllIf50" , 0 , 21 );
288
289
findIrIds (output , "testMatchNoneIf50" , -1 , -1 );
289
290
290
291
runWithArguments (FlagComparisons .class , "-XX:TLABRefillWasteFraction=49" );
291
292
System .out .flush ();
292
293
output = baos .toString ();
293
- baos .reset ();
294
294
findIrIds (output , "testMatchAllIf50" , 4 , 6 , 13 , 18 );
295
295
findIrIds (output , "testMatchNoneIf50" , 0 , 3 , 8 , 10 , 17 , 22 );
296
296
297
297
runWithArguments (FlagComparisons .class , "-XX:TLABRefillWasteFraction=51" );
298
298
System .out .flush ();
299
299
output = baos .toString ();
300
- baos .reset ();
301
300
findIrIds (output , "testMatchAllIf50" , 7 , 12 , 19 , 21 );
302
301
findIrIds (output , "testMatchNoneIf50" , 4 , 7 , 11 , 16 , 20 , 22 );
303
- System .setOut (old );
302
+ System .setOut (oldOut );
303
+ System .setErr (oldErr );
304
304
305
305
if (!exceptions .isEmpty ()) {
306
- System .err .println ("TestIRMatching failed with one or more exceptions:" );
307
- for (Exception e : exceptions ) {
308
- System .err .println (e .getMessage ());
306
+ System .err .println ("TestIRMatching failed with " + exceptions .size () + " exception(s):" );
307
+ int i = 1 ;
308
+ System .err .println ("************************" );
309
+ for (Map .Entry <Exception , String > entry : exceptions .entrySet ()) {
310
+ System .err .println ("***** Exception " + String .format ("%02d" , i ++) +" *****" );
311
+ System .err .println ("************************" );
312
+
313
+ Exception e = entry .getKey ();
309
314
e .printStackTrace (System .err );
310
- System .err .println ("---------" );
315
+ System .err .println ();
316
+ System .err .println ("===== OUTPUT ======" );
317
+ System .err .println (entry .getValue ());
318
+ System .err .println ("MESSAGE: " + e .getMessage ());
319
+ System .err .println ("************************" );
311
320
}
312
- throw new RuntimeException ("TestIRMatching failed with one or more exceptions - check stderr and stdout" );
321
+ i = 1 ;
322
+ System .err .println ("====================================" );
323
+ System .err .println ("********************" );
324
+ System .err .println ("***** OVERVIEW *****" );
325
+ System .err .println ("********************" );
326
+ for (Map .Entry <Exception , String > entry : exceptions .entrySet ()) {
327
+ Exception e = entry .getKey ();
328
+ System .err .print ((i ++) + ") " );
329
+ entry .getKey ().printStackTrace (System .err );
330
+ System .err .println ("********************" );
331
+ }
332
+ throw new RuntimeException ("TestIRMatching failed with " + exceptions .size () + " exception(s) - check stderr and stdout" );
313
333
}
314
334
}
315
335
336
+ private static void runFramework (TestFramework framework ) {
337
+ baos .reset ();
338
+ baosErr .reset ();
339
+ framework .start ();
340
+ }
341
+
316
342
private static void runWithArguments (Class <?> clazz , String ... args ) {
317
343
try {
318
- new TestFramework (clazz ).addFlags (args ). start ( );
344
+ runFramework ( new TestFramework (clazz ).addFlags (args ));
319
345
} catch (Exception e ) {
320
346
addException (e );
321
347
}
322
348
}
323
349
324
350
private static void runWithArgumentsFail (Class <?> clazz , String ... args ) {
325
- new TestFramework (clazz ).addFlags (args ). start ( );
351
+ runFramework ( new TestFramework (clazz ).addFlags (args ));
326
352
}
327
353
328
354
private static void runCheck (String [] args , Constraint ... constraints ) {
329
355
try {
330
- new TestFramework (constraints [0 ].getKlass ()).addFlags (args ).start (); // All constraints have the same class.
331
- Utils .shouldHaveThrownException ();
356
+ TestFramework framework = new TestFramework (constraints [0 ].getKlass ()); // All constraints have the same class.
357
+ if (args != null ) {
358
+ framework .addFlags (args );
359
+ }
360
+ runFramework (framework );
361
+ Utils .shouldHaveThrownException (baos .toString ());
332
362
} catch (IRViolationException e ) {
333
363
checkConstraints (e , constraints );
334
364
} catch (Exception e ) {
@@ -337,14 +367,7 @@ private static void runCheck(String[] args , Constraint... constraints) {
337
367
}
338
368
339
369
private static void runCheck (Constraint ... constraints ) {
340
- try {
341
- TestFramework .run (constraints [0 ].getKlass ()); // All constraints have the same class.
342
- Utils .shouldHaveThrownException ();
343
- } catch (IRViolationException e ) {
344
- checkConstraints (e , constraints );
345
- } catch (Exception e ) {
346
- addException (e );
347
- }
370
+ runCheck (null , constraints );
348
371
}
349
372
350
373
private static void checkConstraints (IRViolationException e , Constraint [] constraints ) {
@@ -354,25 +377,9 @@ private static void checkConstraints(IRViolationException e, Constraint[] constr
354
377
constraint .checkConstraint (e );
355
378
}
356
379
} catch (Exception e1 ) {
357
- System .out .println (TestFramework . getLastTestVMOutput ());
380
+ System .out .println (e . getCompilations ());
358
381
System .out .println (message );
359
- exceptions .add (e1 );
360
- }
361
- }
362
-
363
- // Single constraint
364
- private static void runFailOnTestsArgs (Constraint constraint , String ... args ) {
365
- try {
366
- new TestFramework (constraint .getKlass ()).addFlags (args ).start (); // All constraints have the same class.
367
- Utils .shouldHaveThrownException ();
368
- } catch (IRViolationException e ) {
369
- try {
370
- constraint .checkConstraint (e );
371
- } catch (Exception e1 ) {
372
- addException (e );
373
- }
374
- } catch (Exception e ) {
375
- addException (e );
382
+ addException (e1 );
376
383
}
377
384
}
378
385
@@ -387,8 +394,9 @@ public static void findIrIds(String output, String method, int... numbers) {
387
394
builder .append (j );
388
395
}
389
396
}
390
- Asserts .assertTrue (output .contains (builder .toString ()), "Could not find encoding: \" " + builder .toString ()
391
- + System .lineSeparator ());
397
+ if (!output .contains (builder .toString ())) {
398
+ addException (new RuntimeException ("Could not find encoding: \" " + builder .toString () + System .lineSeparator ()));
399
+ }
392
400
}
393
401
}
394
402
1 commit comments
openjdk-notifier[bot] commentedon Dec 27, 2022
Review
Issues