hotspot/test/runtime/7194254/Test7194254.java
changeset 28678 5e7636bd2f65
parent 28677 41e20ea91d56
parent 28670 bb9afe681988
child 28679 42f73a4a323b
equal deleted inserted replaced
28677:41e20ea91d56 28678:5e7636bd2f65
     1 /*
       
     2  * Copyright (c) 2012, 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  * @test
       
    26  * @bug     7194254
       
    27  * @summary Creates several threads with different java priorities and checks
       
    28  *      whether jstack reports correct priorities for them.
       
    29  *
       
    30  * @ignore 8060219
       
    31  * @run main Test7194254
       
    32  */
       
    33 
       
    34 import java.io.BufferedReader;
       
    35 import java.io.InputStreamReader;
       
    36 import java.lang.management.ManagementFactory;
       
    37 import java.lang.management.RuntimeMXBean;
       
    38 import java.util.ArrayList;
       
    39 import java.util.List;
       
    40 import java.util.concurrent.CyclicBarrier;
       
    41 import java.util.regex.Matcher;
       
    42 import java.util.regex.Pattern;
       
    43 
       
    44 public class Test7194254 {
       
    45 
       
    46     public static void main(String[] args) throws Exception {
       
    47         final int NUMBER_OF_JAVA_PRIORITIES =
       
    48                 Thread.MAX_PRIORITY - Thread.MIN_PRIORITY + 1;
       
    49         final CyclicBarrier barrier =
       
    50                 new CyclicBarrier(NUMBER_OF_JAVA_PRIORITIES + 1);
       
    51 
       
    52         for (int p = Thread.MIN_PRIORITY; p <= Thread.MAX_PRIORITY; ++p) {
       
    53             final int priority = p;
       
    54             new Thread("Priority=" + p) {
       
    55                 {
       
    56                     setPriority(priority);
       
    57                 }
       
    58                 public void run() {
       
    59                     try {
       
    60                         barrier.await(); // 1st
       
    61                         barrier.await(); // 2nd
       
    62                     } catch (Exception exc) {
       
    63                         // ignore
       
    64                     }
       
    65                 }
       
    66             }.start();
       
    67         }
       
    68         barrier.await(); // 1st
       
    69 
       
    70         int matches = 0;
       
    71         List<String> failed = new ArrayList<>();
       
    72         try {
       
    73             String pid = getPid();
       
    74             String jstack = System.getProperty("java.home") + "/../bin/jstack";
       
    75             Process process = new ProcessBuilder(jstack, pid)
       
    76                     .redirectErrorStream(true).start();
       
    77             Pattern pattern = Pattern.compile(
       
    78                     "\\\"Priority=(\\d+)\\\".* prio=(\\d+).*");
       
    79             try (BufferedReader reader = new BufferedReader(
       
    80                     new InputStreamReader(process.getInputStream()))) {
       
    81                 String line;
       
    82                 while((line = reader.readLine()) != null) {
       
    83                     Matcher matcher = pattern.matcher(line);
       
    84                     if (matcher.matches()) {
       
    85                         matches += 1;
       
    86                         String expected = matcher.group(1);
       
    87                         String actual = matcher.group(2);
       
    88                         if (!expected.equals(actual)) {
       
    89                             failed.add(line);
       
    90                         }
       
    91                     }
       
    92                 }
       
    93             }
       
    94             barrier.await(); // 2nd
       
    95         } finally {
       
    96             barrier.reset();
       
    97         }
       
    98 
       
    99         if (matches != NUMBER_OF_JAVA_PRIORITIES) {
       
   100             throw new AssertionError("matches: expected " +
       
   101                     NUMBER_OF_JAVA_PRIORITIES + ", but was " + matches);
       
   102         }
       
   103         if (!failed.isEmpty()) {
       
   104             throw new AssertionError(failed.size() + ":" + failed);
       
   105         }
       
   106         System.out.println("Test passes.");
       
   107     }
       
   108 
       
   109     static String getPid() {
       
   110         RuntimeMXBean runtimebean = ManagementFactory.getRuntimeMXBean();
       
   111         String vmname = runtimebean.getName();
       
   112         int i = vmname.indexOf('@');
       
   113         if (i != -1) {
       
   114             vmname = vmname.substring(0, i);
       
   115         }
       
   116         return vmname;
       
   117     }
       
   118 
       
   119 }
       
   120