jdk/test/com/sun/management/ThreadMXBean/ThreadCpuTimeArray.java
author ykantser
Thu, 07 May 2015 09:11:49 +0200
changeset 30376 2ccf2cf7ea48
parent 30046 cf2c86e1819e
child 44423 306c020eb154
permissions -rw-r--r--
8078896: Add @modules as needed to the jdk_svc tests Reviewed-by: alanb, mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
     1
/*
30376
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 30046
diff changeset
     2
 * Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
     4
 *
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
     7
 * published by the Free Software Foundation.
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
     8
 *
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    13
 * accompanied this code).
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    14
 *
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    18
 *
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    21
 * questions.
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    22
 */
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    23
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    24
/*
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    25
 * @test
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    26
 * @bug     6173675
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    27
 * @summary Basic test of ThreadMXBean.getThreadCpuTime(long[]) and
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    28
 *          getThreadUserTime(long[]).
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    29
 * @author  Paul Hohensee
30046
cf2c86e1819e 8078334: Mark regression tests using randomness
darcy
parents: 8001
diff changeset
    30
 * @key randomness
30376
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 30046
diff changeset
    31
 * @modules jdk.management
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    32
 */
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    33
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    34
import java.lang.management.*;
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    35
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    36
public class ThreadCpuTimeArray {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    37
    private static com.sun.management.ThreadMXBean mbean =
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    38
        (com.sun.management.ThreadMXBean)ManagementFactory.getThreadMXBean();
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    39
    private static boolean testFailed = false;
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    40
    private static boolean done = false;
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    41
    private static Object obj = new Object();
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    42
    private static final int NUM_THREADS = 10;
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    43
    private static Thread[] threads = new Thread[NUM_THREADS];
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    44
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    45
    // careful about this value
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    46
    private static final int DELTA = 100;
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    47
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    48
    public static void main(String[] argv)
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    49
        throws Exception {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    50
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    51
        if (!mbean.isThreadCpuTimeSupported()) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    52
            return;
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    53
        }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    54
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    55
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    56
        // disable CPU time
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    57
        if (mbean.isThreadCpuTimeEnabled()) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    58
            mbean.setThreadCpuTimeEnabled(false);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    59
        }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    60
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    61
        if (mbean.isThreadCpuTimeEnabled()) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    62
            throw new RuntimeException("ThreadCpuTime is expected to be disabled");
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    63
        }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    64
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    65
        // start threads, wait for them to block
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    66
        long[] ids = new long[NUM_THREADS];
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    67
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    68
        for (int i = 0; i < NUM_THREADS; i++) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    69
            threads[i] = new MyThread("MyThread-" + i);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    70
            threads[i].start();
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    71
            ids[i] = threads[i].getId();
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    72
        }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    73
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    74
        // threads block after doing some computation
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    75
        waitUntilThreadBlocked();
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    76
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    77
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    78
        long times[] = mbean.getThreadCpuTime(ids);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    79
        long userTimes[] = mbean.getThreadUserTime(ids);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    80
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    81
        if (times == null) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    82
            throw new RuntimeException("Null ThreadCpuTime array returned");
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    83
        }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    84
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    85
        for (int i = 0; i < NUM_THREADS; i++) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    86
            long t = times[i];
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    87
            if (t != -1) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    88
                throw new RuntimeException(
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    89
                    "Invalid ThreadCpuTime returned for thread " +
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    90
                    threads[i].getName() + " = " +  t + " expected = -1");
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    91
            }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    92
            long ut = userTimes[i];
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    93
            if (ut != -1) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    94
                throw new RuntimeException(
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    95
                    "Invalid ThreadUserTime returned for thread " +
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    96
                    threads[i].getName() + " = " +  ut + " expected = -1");
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    97
            }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    98
        }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    99
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   100
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   101
        // Enable CPU Time measurement
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   102
        if (!mbean.isThreadCpuTimeEnabled()) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   103
            mbean.setThreadCpuTimeEnabled(true);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   104
        }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   105
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   106
        if (!mbean.isThreadCpuTimeEnabled()) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   107
            throw new RuntimeException("ThreadCpuTime is expected to be enabled");
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   108
        }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   109
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   110
        times = mbean.getThreadCpuTime(ids);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   111
        userTimes = mbean.getThreadUserTime(ids);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   112
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   113
        goSleep(200);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   114
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   115
        for (int i = 0; i < NUM_THREADS; i++) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   116
            long t = times[i];
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   117
            if (t < 0) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   118
                throw new RuntimeException(
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   119
                    "Invalid CPU time returned for thread " +
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   120
                    threads[i].getName() + " = " + t);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   121
            }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   122
            long ut = userTimes[i];
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   123
            if (ut < 0) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   124
                throw new RuntimeException(
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   125
                    "Invalid user time returned for thread " +
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   126
                    threads[i].getName() + " = " + ut);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   127
            }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   128
        }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   129
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   130
        long[] times1 = mbean.getThreadCpuTime(ids);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   131
        long[] userTimes1 = mbean.getThreadUserTime(ids);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   132
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   133
        for (int i = 0; i < NUM_THREADS; i++) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   134
            long newTime = times1[i];
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   135
            long newUserTime = userTimes1[i];
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   136
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   137
            if (times[i] > newTime) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   138
                throw new RuntimeException("TEST FAILED: " +
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   139
                    threads[i].getName() +
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   140
                    " previous CPU time = " + times[i] +
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   141
                    " > current CPU time = " + newTime);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   142
            }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   143
            if ((times[i] + DELTA) < newTime) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   144
                throw new RuntimeException("TEST FAILED: " +
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   145
                    threads[i].getName() +
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   146
                    " CPU time = " + newTime +
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   147
                    " previous CPU time " + times[i] +
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   148
                    " out of expected range");
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   149
            }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   150
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   151
            System.out.println(threads[i].getName() +
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   152
                " Previous Cpu Time = " + times[i] +
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   153
                " Current CPU time = " + newTime);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   154
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   155
            System.out.println(threads[i].getName() +
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   156
                " Previous User Time = " + userTimes[i] +
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   157
                " Current User time = " + newUserTime);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   158
        }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   159
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   160
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   161
        try {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   162
            times = mbean.getThreadCpuTime(null);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   163
        } catch (NullPointerException e) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   164
            System.out.println(
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   165
                "Caught expected NullPointerException: " + e.getMessage());
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   166
        }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   167
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   168
        try {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   169
            ids[0] = 0;
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   170
            times = mbean.getThreadCpuTime(ids);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   171
        } catch (IllegalArgumentException e) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   172
            System.out.println(
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   173
                "Caught expected IllegalArgumentException: " + e.getMessage());
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   174
        }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   175
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   176
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   177
        // let threads exit
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   178
        synchronized (obj) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   179
            done = true;
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   180
            obj.notifyAll();
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   181
        }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   182
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   183
        for (int i = 0; i < NUM_THREADS; i++) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   184
            try {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   185
                threads[i].join();
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   186
            } catch (InterruptedException e) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   187
                System.out.println("Unexpected exception is thrown.");
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   188
                e.printStackTrace(System.out);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   189
                testFailed = true;
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   190
                break;
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   191
            }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   192
        }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   193
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   194
        if (testFailed) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   195
            throw new RuntimeException("TEST FAILED");
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   196
        }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   197
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   198
        System.out.println("Test passed");
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   199
    }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   200
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   201
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   202
    private static void goSleep(long ms) throws Exception {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   203
        try {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   204
            Thread.sleep(ms);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   205
        } catch (InterruptedException e) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   206
            System.out.println("Unexpected exception is thrown.");
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   207
            throw e;
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   208
        }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   209
    }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   210
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   211
    private static void waitUntilThreadBlocked()
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   212
        throws Exception {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   213
        int count = 0;
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   214
        while (count != NUM_THREADS) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   215
            goSleep(100);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   216
            count = 0;
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   217
            for (int i = 0; i < NUM_THREADS; i++) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   218
                ThreadInfo info = mbean.getThreadInfo(threads[i].getId());
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   219
                if (info.getThreadState() == Thread.State.WAITING) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   220
                    count++;
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   221
                }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   222
            }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   223
        }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   224
    }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   225
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   226
    public static void doit() {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   227
        double sum = 0;
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   228
        for (int i = 0; i < 5000; i++) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   229
           double r = Math.random();
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   230
           double x = Math.pow(3, r);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   231
           sum += x - r;
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   232
        }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   233
        System.out.println(Thread.currentThread().getName() +
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   234
                           " sum = " + sum);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   235
    }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   236
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   237
    static class MyThread extends Thread {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   238
        public MyThread(String name) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   239
            super(name);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   240
        }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   241
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   242
        public void run() {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   243
            ThreadCpuTimeArray.doit();
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   244
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   245
            synchronized (obj) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   246
                while (!done) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   247
                    try {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   248
                        obj.wait();
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   249
                    } catch (InterruptedException e) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   250
                        System.out.println("Unexpected exception is thrown.");
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   251
                        e.printStackTrace(System.out);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   252
                        testFailed = true;
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   253
                        break;
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   254
                    }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   255
                }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   256
            }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   257
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   258
            ThreadCpuTimeArray.doit();
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   259
        }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   260
    }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   261
}