jdk/test/demo/jvmti/Context.java
author pchelko
Fri, 18 Apr 2014 10:47:23 +0400
changeset 24520 e8afd90fcb69
parent 22274 10fc71a1d166
child 31814 b8431927ad72
permissions -rw-r--r--
8035169: Move ThreadGroupUtils from the sun.misc package Reviewed-by: anthony, serb, azvegint
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
22274
10fc71a1d166 7027502: Remove demo/jvmti/hprof/MonitorTest.java from exclude list with additional debug output
alanb
parents: 5506
diff changeset
     2
 * Copyright (c) 2004, 2014, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 *   Sample target application for jvmti demos
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *     java Context [threadCount [iterationCount [sleepContention]]]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *           Default: java Context 5 10 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *      threadCount     Number of threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *      iterationCount  Total turns taken for all threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *      sleepContention Time for main thread to sleep while holding lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *                      (creates monitor contention on all other threads)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/* Used to sync up turns and keep track of who's turn it is */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
final class TurnChecker {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    int thread_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    TurnChecker(int thread_index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        this.thread_index = thread_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
/* Creates a bunch of threads that sequentially take turns */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
public final class Context extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    /* Used to track threads */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private static long startTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private static TurnChecker turn = new TurnChecker(-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private static int total_turns_taken;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /* Used for each Context thread */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private final int thread_count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private final int thread_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private final int thread_turns;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /* Main program */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    public static void main(String[] argv) throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        int default_thread_count = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        int default_thread_turns = 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        int default_contention_sleep = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        int expected_turns_taken;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        long sleepTime = 10L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        /* Override defaults */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        if ( argv.length >= 1 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            default_thread_count = Integer.parseInt(argv[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        if ( argv.length >= 2 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            expected_turns_taken = Integer.parseInt(argv[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            default_thread_turns = expected_turns_taken/default_thread_count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        expected_turns_taken = default_thread_count*default_thread_turns;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        if ( argv.length >= 3 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            default_contention_sleep = Integer.parseInt(argv[2]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        System.out.println("Context started with "
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                 + default_thread_count + " threads and "
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                 + default_thread_turns + " turns per thread");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        /* Get all threads running (they will block until we set turn) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        for (int i = 0; i < default_thread_count; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            new Context(default_thread_count, i, default_thread_turns).start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        /* Sleep to make sure thread_index 0 make it to the wait call */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        System.out.println("Context sleeping, so threads will start wait");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        Thread.yield();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        Thread.sleep(sleepTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        /* Save start time */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        startTime = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        /* This triggers the starting of taking turns */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        synchronized (turn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            turn.thread_index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            turn.notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        System.out.println("Context sleeping, so threads can run");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        Thread.yield();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        Thread.sleep(sleepTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        /* Wait for threads to finish (after everyone has had their turns) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        while ( true ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            boolean done;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            done = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            synchronized (turn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                if ( total_turns_taken == expected_turns_taken ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                    done = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                /* Create some monitor contention by sleeping with lock */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                if ( default_contention_sleep > 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                    System.out.println("Context sleeping, to create contention");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                    Thread.yield();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                    Thread.sleep((long)default_contention_sleep);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            if ( done )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            System.out.println("Context sleeping, so threads will complete");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            Thread.sleep(sleepTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        long endTime   = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        long totalTime = endTime - startTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        System.out.println("Total time (milliseconds): " + totalTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        System.out.println("Milliseconds per thread: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                           ((double)totalTime / (default_thread_count)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        System.out.println("Context completed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        System.exit(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    /* Thread object to run */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    Context(int thread_count, int thread_index, int thread_turns) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        this.thread_count = thread_count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        this.thread_index = thread_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        this.thread_turns = thread_turns;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /* Main for thread */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        int next_thread_index = (thread_index + 1) % thread_count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        int turns_taken       = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            /* Loop until we make sure we get all our turns */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            for (int i = 0; i < thread_turns * thread_count; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                synchronized (turn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                    /* Keep waiting for our turn */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                    while (turn.thread_index != thread_index)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                        turn.wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                    /* MY TURN! Each thread gets thread_turns */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                    total_turns_taken++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                    turns_taken++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                    System.out.println("Turn #" + total_turns_taken
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                                + " taken by thread " + thread_index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                                + ", " + turns_taken
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                                + " turns taken by this thread");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                    /* Give next thread a turn */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                    turn.thread_index = next_thread_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                    turn.notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                /* If we've had all our turns, break out of this loop */
22274
10fc71a1d166 7027502: Remove demo/jvmti/hprof/MonitorTest.java from exclude list with additional debug output
alanb
parents: 5506
diff changeset
   170
                if (thread_turns == turns_taken) {
10fc71a1d166 7027502: Remove demo/jvmti/hprof/MonitorTest.java from exclude list with additional debug output
alanb
parents: 5506
diff changeset
   171
                    System.out.println("Loop end: thread got " + turns_taken
10fc71a1d166 7027502: Remove demo/jvmti/hprof/MonitorTest.java from exclude list with additional debug output
alanb
parents: 5506
diff changeset
   172
                            + " turns, expected " + thread_turns);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            }
22274
10fc71a1d166 7027502: Remove demo/jvmti/hprof/MonitorTest.java from exclude list with additional debug output
alanb
parents: 5506
diff changeset
   176
        } catch (InterruptedException intEx) {
10fc71a1d166 7027502: Remove demo/jvmti/hprof/MonitorTest.java from exclude list with additional debug output
alanb
parents: 5506
diff changeset
   177
            System.out.println("Got an InterruptedException:" + intEx);
10fc71a1d166 7027502: Remove demo/jvmti/hprof/MonitorTest.java from exclude list with additional debug output
alanb
parents: 5506
diff changeset
   178
            /* skip */
10fc71a1d166 7027502: Remove demo/jvmti/hprof/MonitorTest.java from exclude list with additional debug output
alanb
parents: 5506
diff changeset
   179
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        /* Make sure we got all our turns */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        if ( thread_turns != turns_taken ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            System.out.println("ERROR: thread got " + turns_taken
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                                        + " turns, expected " + thread_turns);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
}