test/jdk/com/sun/management/ThreadMXBean/ThreadAllocatedMemory.java
author dcubed
Wed, 18 Sep 2019 20:49:13 -0400
changeset 58223 778fc2dcbdaa
parent 58208 0f3c23c374a4
child 58348 c29e49148be7
permissions -rw-r--r--
8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread Reviewed-by: phh, dholmes
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
/*
58223
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
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
58223
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
    26
 * @bug     6173675
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    27
 * @summary Basic test of ThreadMXBean.getThreadAllocatedBytes
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    28
 * @author  Paul Hohensee
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    29
 */
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    30
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    31
import java.lang.management.*;
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
public class ThreadAllocatedMemory {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    34
    private static com.sun.management.ThreadMXBean mbean =
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    35
        (com.sun.management.ThreadMXBean)ManagementFactory.getThreadMXBean();
58223
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
    36
    private static boolean testFailed = false;
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
    37
    private static boolean done = false;
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
    38
    private static boolean done1 = false;
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    39
    private static Object obj = new Object();
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    40
    private static final int NUM_THREADS = 10;
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    41
    private static Thread[] threads = new Thread[NUM_THREADS];
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    42
    private static long[] sizes = new long[NUM_THREADS];
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    43
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    44
    public static void main(String[] argv)
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    45
        throws Exception {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    46
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    47
        if (!mbean.isThreadAllocatedMemorySupported()) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    48
            return;
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    49
        }
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
        // disable allocated memory measurement
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    52
        if (mbean.isThreadAllocatedMemoryEnabled()) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    53
            mbean.setThreadAllocatedMemoryEnabled(false);
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
        if (mbean.isThreadAllocatedMemoryEnabled()) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    57
            throw new RuntimeException(
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    58
                "ThreadAllocatedMemory is expected to be disabled");
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
58223
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
    61
        Thread curThread = Thread.currentThread();
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
    62
        long id = curThread.getId();
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
    63
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
    64
        long s = mbean.getThreadAllocatedBytes(id);
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    65
        if (s != -1) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    66
            throw new RuntimeException(
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    67
                "Invalid ThreadAllocatedBytes returned = " +
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    68
                s + " expected = -1");
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    69
        }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    70
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    71
        // enable allocated memory measurement
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    72
        if (!mbean.isThreadAllocatedMemoryEnabled()) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    73
            mbean.setThreadAllocatedMemoryEnabled(true);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    74
        }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    75
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    76
        if (!mbean.isThreadAllocatedMemoryEnabled()) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    77
            throw new RuntimeException(
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    78
                "ThreadAllocatedMemory is expected to be enabled");
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    79
        }
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
        long size = mbean.getThreadAllocatedBytes(id);
58223
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
    82
        // implementation could have started measurement when
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
    83
        // measurement was enabled, in which case size can be 0
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
    84
        if (size < 0) {
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
    85
            throw new RuntimeException(
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
    86
                "Invalid allocated bytes returned = " + size);
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
    87
        }
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    88
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    89
        doit();
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    90
58223
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
    91
        // Expected to be size1 >= size
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
    92
        long size1 = mbean.getThreadAllocatedBytes(id);
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
    93
        if (size1 < size) {
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
    94
            throw new RuntimeException("Allocated bytes " + size1 +
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
    95
                " expected >= " + size);
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
    96
        }
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
    97
        System.out.println(curThread.getName() +
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
    98
            " Current thread allocated bytes = " + size +
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
    99
            " allocated bytes = " + size1);
58208
0f3c23c374a4 8207266: ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 47216
diff changeset
   100
0f3c23c374a4 8207266: ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 47216
diff changeset
   101
58223
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
   102
        // start threads, wait for them to block
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
   103
        for (int i = 0; i < NUM_THREADS; i++) {
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
   104
            threads[i] = new MyThread("MyThread-" + i);
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
   105
            threads[i].start();
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
   106
        }
58208
0f3c23c374a4 8207266: ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 47216
diff changeset
   107
58223
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
   108
        // threads block after doing some allocation
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
   109
        waitUntilThreadBlocked();
58208
0f3c23c374a4 8207266: ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 47216
diff changeset
   110
58223
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
   111
        for (int i = 0; i < NUM_THREADS; i++) {
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
   112
            sizes[i] = mbean.getThreadAllocatedBytes(threads[i].getId());
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
   113
        }
58208
0f3c23c374a4 8207266: ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 47216
diff changeset
   114
58223
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
   115
        // let threads go and do some more allocation
58208
0f3c23c374a4 8207266: ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 47216
diff changeset
   116
        synchronized (obj) {
0f3c23c374a4 8207266: ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 47216
diff changeset
   117
            done = true;
0f3c23c374a4 8207266: ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 47216
diff changeset
   118
            obj.notifyAll();
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   119
        }
58208
0f3c23c374a4 8207266: ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 47216
diff changeset
   120
58223
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
   121
        // wait for threads to get going again.  we don't care if we
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   122
        // catch them in mid-execution or if some of them haven't
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   123
        // restarted after we're done sleeping.
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   124
        goSleep(400);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   125
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   126
        for (int i = 0; i < NUM_THREADS; i++) {
58223
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
   127
            long newSize = mbean.getThreadAllocatedBytes(threads[i].getId());
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
   128
            if (sizes[i] > newSize) {
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
   129
                throw new RuntimeException("TEST FAILED: " +
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
   130
                    threads[i].getName() +
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
   131
                    " previous allocated bytes = " + sizes[i] +
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
   132
                    " > current allocated bytes = " + newSize);
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
   133
            }
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
   134
            System.out.println(threads[i].getName() +
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
   135
                " Previous allocated bytes = " + sizes[i] +
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
   136
                " Current allocated bytes = " + newSize);
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   137
        }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   138
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   139
        // let threads exit
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   140
        synchronized (obj) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   141
            done1 = true;
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   142
            obj.notifyAll();
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   143
        }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   144
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   145
        for (int i = 0; i < NUM_THREADS; i++) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   146
            try {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   147
                threads[i].join();
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   148
            } catch (InterruptedException e) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   149
                System.out.println("Unexpected exception is thrown.");
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   150
                e.printStackTrace(System.out);
58223
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
   151
                testFailed = true;
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   152
                break;
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   153
            }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   154
        }
58223
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
   155
        if (testFailed) {
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
   156
            throw new RuntimeException("TEST FAILED");
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
   157
        }
58208
0f3c23c374a4 8207266: ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 47216
diff changeset
   158
58223
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
   159
        System.out.println("Test passed");
8001
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
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   162
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   163
    private static void goSleep(long ms) throws Exception {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   164
        try {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   165
            Thread.sleep(ms);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   166
        } catch (InterruptedException e) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   167
            System.out.println("Unexpected exception is thrown.");
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   168
            throw e;
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   169
        }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   170
    }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   171
58223
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
   172
    private static void waitUntilThreadBlocked()
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   173
        throws Exception {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   174
        int count = 0;
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   175
        while (count != NUM_THREADS) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   176
            goSleep(100);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   177
            count = 0;
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   178
            for (int i = 0; i < NUM_THREADS; i++) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   179
                ThreadInfo info = mbean.getThreadInfo(threads[i].getId());
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   180
                if (info.getThreadState() == Thread.State.WAITING) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   181
                    count++;
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
            }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   184
        }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   185
    }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   186
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   187
    public static void doit() {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   188
        String tmp = "";
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   189
        long hashCode = 0;
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   190
        for (int counter = 0; counter < 1000; counter++) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   191
            tmp += counter;
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   192
            hashCode = tmp.hashCode();
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
        System.out.println(Thread.currentThread().getName() +
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   195
                           " hashcode: " + hashCode);
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
    static class MyThread extends Thread {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   199
        public MyThread(String name) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   200
            super(name);
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
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   203
        public void run() {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   204
            ThreadAllocatedMemory.doit();
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   205
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   206
            synchronized (obj) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   207
                while (!done) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   208
                    try {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   209
                        obj.wait();
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   210
                    } catch (InterruptedException e) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   211
                        System.out.println("Unexpected exception is thrown.");
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   212
                        e.printStackTrace(System.out);
58223
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
   213
                        testFailed = true;
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   214
                        break;
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   215
                    }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   216
                }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   217
            }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   218
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   219
            long size1 = mbean.getThreadAllocatedBytes(getId());
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   220
            ThreadAllocatedMemory.doit();
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   221
            long size2 = mbean.getThreadAllocatedBytes(getId());
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
            System.out.println(getName() + ": " +
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   224
                "ThreadAllocatedBytes  = " + size1 +
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   225
                " ThreadAllocatedBytes  = " + size2);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   226
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   227
            if (size1 > size2) {
58223
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
   228
                throw new RuntimeException("TEST FAILED: " + getName() +
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   229
                    " ThreadAllocatedBytes = " + size1 +
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   230
                    " > ThreadAllocatedBytes = " + size2);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   231
            }
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
            synchronized (obj) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   234
                while (!done1) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   235
                    try {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   236
                        obj.wait();
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   237
                    } catch (InterruptedException e) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   238
                        System.out.println("Unexpected exception is thrown.");
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   239
                        e.printStackTrace(System.out);
58223
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
   240
                        testFailed = true;
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   241
                        break;
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   242
                    }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   243
                }
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
        }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   246
    }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   247
}