hotspot/test/runtime/7194254/Test7194254.java
author mgerdin
Mon, 11 Aug 2014 17:12:41 +0400
changeset 25958 8dc85547d6d6
parent 13956 1a25205d266e
child 27468 71af3eb557de
permissions -rw-r--r--
8011397: JTREG needs to copy additional WhiteBox class file to JTwork/scratch/sun/hotspot Reviewed-by: kvn, iignatyev, mgerdin Contributed-by: andrey.x.zakharov@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13857
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
     1
/*
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
     2
 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
     4
 *
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
     7
 * published by the Free Software Foundation.
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
     8
 *
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    13
 * accompanied this code).
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    14
 *
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    18
 *
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    21
 * questions.
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    22
 */
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    23
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    24
/*
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    25
 * @test
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    26
 * @bug     7194254
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    27
 * @summary Creates several threads with different java priorities and checks
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    28
 *      whether jstack reports correct priorities for them.
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    29
 *
13956
1a25205d266e 7199186: runtime/7194254/Test7194254.java fails - wrong test name on @run
dholmes
parents: 13857
diff changeset
    30
 * @run main Test7194254
13857
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    31
 */
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    32
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    33
import java.io.BufferedReader;
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    34
import java.io.InputStreamReader;
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    35
import java.lang.management.ManagementFactory;
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    36
import java.lang.management.RuntimeMXBean;
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    37
import java.util.ArrayList;
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    38
import java.util.List;
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    39
import java.util.concurrent.CyclicBarrier;
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    40
import java.util.regex.Matcher;
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    41
import java.util.regex.Pattern;
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    42
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    43
public class Test7194254 {
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    44
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    45
    public static void main(String[] args) throws Exception {
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    46
        final int NUMBER_OF_JAVA_PRIORITIES =
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    47
                Thread.MAX_PRIORITY - Thread.MIN_PRIORITY + 1;
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    48
        final CyclicBarrier barrier =
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    49
                new CyclicBarrier(NUMBER_OF_JAVA_PRIORITIES + 1);
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    50
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    51
        for (int p = Thread.MIN_PRIORITY; p <= Thread.MAX_PRIORITY; ++p) {
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    52
            final int priority = p;
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    53
            new Thread("Priority=" + p) {
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    54
                {
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    55
                    setPriority(priority);
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    56
                }
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    57
                public void run() {
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    58
                    try {
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    59
                        barrier.await(); // 1st
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    60
                        barrier.await(); // 2nd
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    61
                    } catch (Exception exc) {
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    62
                        // ignore
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    63
                    }
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    64
                }
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    65
            }.start();
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    66
        }
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    67
        barrier.await(); // 1st
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    68
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    69
        int matches = 0;
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    70
        List<String> failed = new ArrayList<>();
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    71
        try {
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    72
            String pid = getPid();
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    73
            String jstack = System.getProperty("java.home") + "/../bin/jstack";
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    74
            Process process = new ProcessBuilder(jstack, pid)
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    75
                    .redirectErrorStream(true).start();
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    76
            Pattern pattern = Pattern.compile(
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    77
                    "\\\"Priority=(\\d+)\\\".* prio=(\\d+).*");
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    78
            try (BufferedReader reader = new BufferedReader(
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    79
                    new InputStreamReader(process.getInputStream()))) {
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    80
                String line;
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    81
                while((line = reader.readLine()) != null) {
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    82
                    Matcher matcher = pattern.matcher(line);
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    83
                    if (matcher.matches()) {
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    84
                        matches += 1;
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    85
                        String expected = matcher.group(1);
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    86
                        String actual = matcher.group(2);
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    87
                        if (!expected.equals(actual)) {
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    88
                            failed.add(line);
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    89
                        }
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    90
                    }
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    91
                }
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    92
            }
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    93
            barrier.await(); // 2nd
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    94
        } finally {
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    95
            barrier.reset();
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    96
        }
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    97
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    98
        if (matches != NUMBER_OF_JAVA_PRIORITIES) {
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
    99
            throw new AssertionError("matches: expected " +
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
   100
                    NUMBER_OF_JAVA_PRIORITIES + ", but was " + matches);
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
   101
        }
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
   102
        if (!failed.isEmpty()) {
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
   103
            throw new AssertionError(failed.size() + ":" + failed);
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
   104
        }
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
   105
        System.out.println("Test passes.");
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
   106
    }
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
   107
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
   108
    static String getPid() {
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
   109
        RuntimeMXBean runtimebean = ManagementFactory.getRuntimeMXBean();
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
   110
        String vmname = runtimebean.getName();
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
   111
        int i = vmname.indexOf('@');
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
   112
        if (i != -1) {
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
   113
            vmname = vmname.substring(0, i);
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
   114
        }
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
   115
        return vmname;
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
   116
    }
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
   117
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
   118
}
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents:
diff changeset
   119