hotspot/test/gc/concurrent_phase_control/CheckSupported.java
changeset 46384 dacebddcdea0
equal deleted inserted replaced
46383:24999171edf9 46384:dacebddcdea0
       
     1 /*
       
     2  * Copyright (c) 2017, 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 package gc.concurrent_phase_control;
       
    25 
       
    26 /*
       
    27  * Utility class that provides verification of expected behavior of
       
    28  * the Concurrent GC Phase Control WB API when the current GC supports
       
    29  * phase control.  The invoking test must provide WhiteBox access.
       
    30  */
       
    31 
       
    32 import sun.hotspot.WhiteBox;
       
    33 import java.util.Arrays;
       
    34 import java.util.HashSet;
       
    35 import java.util.List;
       
    36 import java.util.Set;
       
    37 
       
    38 public class CheckSupported {
       
    39 
       
    40     private static final WhiteBox WB = WhiteBox.getWhiteBox();
       
    41 
       
    42     private static Set<String> toSet(List<String> list, String which)
       
    43         throws Exception
       
    44     {
       
    45         Set<String> result = new HashSet<String>(list);
       
    46         if (result.size() < list.size()) {
       
    47             throw new RuntimeException(which + " phases contains duplicates");
       
    48         }
       
    49         return result;
       
    50     }
       
    51 
       
    52     private static void checkPhases(String[] expectedPhases) throws Exception {
       
    53         String[] actualPhases = WB.getConcurrentGCPhases();
       
    54 
       
    55         List<String> expectedList = Arrays.asList(expectedPhases);
       
    56         List<String> actualList = Arrays.asList(actualPhases);
       
    57 
       
    58         Set<String> expected = toSet(expectedList, "Expected");
       
    59         Set<String> actual = toSet(actualList, "Actual");
       
    60 
       
    61         expected.removeAll(actualList);
       
    62         actual.removeAll(expectedList);
       
    63 
       
    64         boolean match = true;
       
    65         if (!expected.isEmpty()) {
       
    66             match = false;
       
    67             System.out.println("Unexpected phases:");
       
    68             for (String s: expected) {
       
    69                 System.out.println("  " + s);
       
    70             }
       
    71         }
       
    72         if (!actual.isEmpty()) {
       
    73             match = false;
       
    74             System.out.println("Expected but missing phases:");
       
    75             for (String s: actual) {
       
    76                 System.out.println("  " + s);
       
    77             }
       
    78         }
       
    79         if (!match) {
       
    80             throw new RuntimeException("Mismatch between expected and actual phases");
       
    81         }
       
    82     }
       
    83 
       
    84     public static void check(String gcName, String[] phases) throws Exception {
       
    85         // Verify supported.
       
    86         if (!WB.supportsConcurrentGCPhaseControl()) {
       
    87             throw new RuntimeException(
       
    88                 gcName + " unexpectedly missing phase control support");
       
    89         }
       
    90 
       
    91         checkPhases(phases);
       
    92 
       
    93         // Verify IllegalArgumentException thrown by request attempt
       
    94         // with unknown phase.
       
    95         boolean illegalArgumentThrown = false;
       
    96         try {
       
    97             WB.requestConcurrentGCPhase("UNKNOWN PHASE");
       
    98         } catch (IllegalArgumentException e) {
       
    99             // Expected.
       
   100             illegalArgumentThrown = true;
       
   101         } catch (Exception e) {
       
   102             throw new RuntimeException(
       
   103                 gcName + ": Unexpected exception when requesting unknown phase: " + e.toString());
       
   104         }
       
   105         if (!illegalArgumentThrown) {
       
   106             throw new RuntimeException(
       
   107                 gcName + ": No exception when requesting unknown phase");
       
   108         }
       
   109     }
       
   110 }
       
   111