jdk/test/demo/jvmti/Context.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2004 Sun Microsystems, Inc.  All Rights Reserved.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
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 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                if ( thread_turns == turns_taken ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        } catch (InterruptedException intEx) { /* skip */ }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        /* Make sure we got all our turns */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        if ( thread_turns != turns_taken ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            System.out.println("ERROR: thread got " + turns_taken
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                                        + " turns, expected " + thread_turns);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
}