hotspot/test/serviceability/tmtools/jstack/DaemonThreadTest.java
author duke
Wed, 05 Jul 2017 21:30:26 +0200
changeset 36720 7359994942f8
parent 35493 863fb33f9940
child 38152 80e5da81fb2c
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
35493
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
     1
/*
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
     4
 *
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
     7
 * published by the Free Software Foundation.
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
     8
 *
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    13
 * accompanied this code).
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    14
 *
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    18
 *
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    21
 * questions.
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    22
 */
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    23
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    24
/*
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    25
 * @test
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    26
 * @summary Create daemon and non-deamon threads.
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    27
 *          Check the correctness of thread's status from jstack.
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    28
 * @library /test/lib/share/classes
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    29
 * @library ../share
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    30
 * @build common.*
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    31
 *
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    32
 * @run main/othervm -XX:+UsePerfData DaemonThreadTest
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    33
 */
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    34
import common.ToolResults;
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    35
import utils.*;
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    36
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    37
public class DaemonThreadTest {
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    38
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    39
    static class NormalThread extends Thread {
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    40
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    41
        NormalThread() {
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    42
        }
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    43
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    44
        @Override
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    45
        public void run() {
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    46
            Utils.sleep();
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    47
        }
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    48
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    49
    }
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    50
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    51
    static class DaemonThread extends Thread {
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    52
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    53
        DaemonThread() {
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    54
            setDaemon(true);
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    55
        }
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    56
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    57
        @Override
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    58
        public void run() {
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    59
            Utils.sleep();
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    60
        }
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    61
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    62
    }
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    63
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    64
    public static void main(String[] args) throws Exception {
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    65
        testNoDaemon();
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    66
        testDaemon();
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    67
    }
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    68
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    69
    private static void testNoDaemon() throws Exception {
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    70
        testThread(new NormalThread(), "");
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    71
    }
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    72
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    73
    private static void testDaemon() throws Exception {
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    74
        testThread(new DaemonThread(), "daemon");
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    75
    }
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    76
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    77
    private static void testThread(Thread thread, String expectedType) throws Exception {
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    78
        // Start the thread
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    79
        thread.start();
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    80
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    81
        // Run jstack tool and collect the output
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    82
        JstackTool jstackTool = new JstackTool(ProcessHandle.current().getPid());
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    83
        ToolResults results = jstackTool.measure();
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    84
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    85
        // Analyze the jstack output for the correct thread type
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    86
        JStack jstack = new DefaultFormat().parse(results.getStdoutString());
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    87
        ThreadStack ti = jstack.getThreadStack(thread.getName());
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    88
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    89
        if (!ti.getType().trim().equals(expectedType)) {
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    90
            throw new RuntimeException("incorrect thread type '" + ti.getType() + "' for the thread '" + thread.getName() + "'");
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    91
        }
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    92
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    93
    }
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    94
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    95
}