test/jdk/com/sun/management/ThreadMXBean/ThreadAllocatedMemory.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 58348 c29e49148be7
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
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
/*
58348
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
     2
 * Copyright (c) 2011, 2019, 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
58348
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
    26
 * @bug     6173675 8231209
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();
58348
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
    36
    private static volatile boolean done = false;
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
    37
    private static volatile boolean done1 = false;
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    38
    private static Object obj = new Object();
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    39
    private static final int NUM_THREADS = 10;
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    40
    private static Thread[] threads = new Thread[NUM_THREADS];
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    41
    private static long[] sizes = new long[NUM_THREADS];
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    42
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    43
    public static void main(String[] argv)
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    44
        throws Exception {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    45
58348
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
    46
        testSupportEnableDisable();
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
    47
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
    48
        // Test current thread two ways
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
    49
        testGetCurrentThreadAllocatedBytes();
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
    50
        testCurrentThreadGetThreadAllocatedBytes();
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
    51
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
    52
        // Test a single thread that is not this one
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
    53
        testGetThreadAllocatedBytes();
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
    54
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
    55
        // Test many threads that are not this one
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
    56
        testGetThreadsAllocatedBytes();
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
    57
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
    58
        System.out.println("Test passed");
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
    59
    }
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
    60
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
    61
    private static void testSupportEnableDisable() {
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    62
        if (!mbean.isThreadAllocatedMemorySupported()) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    63
            return;
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
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    66
        // disable allocated memory measurement
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    67
        if (mbean.isThreadAllocatedMemoryEnabled()) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    68
            mbean.setThreadAllocatedMemoryEnabled(false);
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
        if (mbean.isThreadAllocatedMemoryEnabled()) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    72
            throw new RuntimeException(
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    73
                "ThreadAllocatedMemory is expected to be disabled");
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
58348
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
    76
        long s = mbean.getCurrentThreadAllocatedBytes();
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    77
        if (s != -1) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    78
            throw new RuntimeException(
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    79
                "Invalid ThreadAllocatedBytes returned = " +
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    80
                s + " expected = -1");
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    81
        }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    82
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    83
        // enable allocated memory measurement
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    84
        if (!mbean.isThreadAllocatedMemoryEnabled()) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    85
            mbean.setThreadAllocatedMemoryEnabled(true);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    86
        }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    87
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    88
        if (!mbean.isThreadAllocatedMemoryEnabled()) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    89
            throw new RuntimeException(
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    90
                "ThreadAllocatedMemory is expected to be enabled");
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
    91
        }
58348
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
    92
    }
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
    93
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
    94
    private static void testGetCurrentThreadAllocatedBytes() {
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
    95
        long size = mbean.getCurrentThreadAllocatedBytes();
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
    96
        ensureValidSize(size);
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
    97
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
    98
        // do some more allocation
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
    99
        doit();
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   100
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   101
        checkResult(Thread.currentThread(), size,
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   102
                    mbean.getCurrentThreadAllocatedBytes());
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   103
    }
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   104
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   105
    private static void testCurrentThreadGetThreadAllocatedBytes() {
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   106
        Thread curThread = Thread.currentThread();
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   107
        long id = curThread.getId();
8001
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
        long size = mbean.getThreadAllocatedBytes(id);
58348
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   110
        ensureValidSize(size);
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   111
58348
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   112
        // do some more allocation
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   113
        doit();
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   114
58348
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   115
        checkResult(curThread, size, mbean.getThreadAllocatedBytes(id));
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   116
    }
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   117
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   118
    private static void testGetThreadAllocatedBytes()
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   119
        throws Exception {
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   120
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   121
        // start a thread
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   122
        done = false; done1 = false;
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   123
        Thread curThread = new MyThread("MyThread");
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   124
        curThread.start();
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   125
        long id = curThread.getId();
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   126
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   127
        // wait for thread to block after doing some allocation
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   128
        waitUntilThreadBlocked(curThread);
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   129
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   130
        long size = mbean.getThreadAllocatedBytes(id);
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   131
        ensureValidSize(size);
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   132
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   133
        // let thread go to do some more allocation
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   134
        synchronized (obj) {
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   135
            done = true;
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   136
            obj.notifyAll();
58223
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
   137
        }
58348
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   138
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   139
        // wait for thread to get going again. we don't care if we
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   140
        // catch it in mid-execution or if it hasn't
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   141
        // restarted after we're done sleeping.
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   142
        goSleep(400);
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   143
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   144
        checkResult(curThread, size, mbean.getThreadAllocatedBytes(id));
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   145
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   146
        // let thread exit
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   147
        synchronized (obj) {
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   148
            done1 = true;
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   149
            obj.notifyAll();
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   150
        }
58208
0f3c23c374a4 8207266: ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 47216
diff changeset
   151
58348
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   152
        try {
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   153
            curThread.join();
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   154
        } catch (InterruptedException e) {
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   155
            System.out.println("Unexpected exception is thrown.");
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   156
            e.printStackTrace(System.out);
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   157
        }
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   158
    }
58208
0f3c23c374a4 8207266: ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 47216
diff changeset
   159
58348
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   160
    private static void testGetThreadsAllocatedBytes()
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   161
        throws Exception {
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   162
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   163
        // start threads
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   164
        done = false; done1 = false;
58223
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
   165
        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
   166
            threads[i] = new MyThread("MyThread-" + i);
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
   167
            threads[i].start();
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
   168
        }
58208
0f3c23c374a4 8207266: ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 47216
diff changeset
   169
58348
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   170
        // wait for threads to block after doing some allocation
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   171
        waitUntilThreadsBlocked();
58208
0f3c23c374a4 8207266: ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 47216
diff changeset
   172
58223
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
   173
        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
   174
            sizes[i] = mbean.getThreadAllocatedBytes(threads[i].getId());
58348
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   175
            ensureValidSize(sizes[i]);
58223
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
   176
        }
58208
0f3c23c374a4 8207266: ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 47216
diff changeset
   177
58348
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   178
        // let threads go to do some more allocation
58208
0f3c23c374a4 8207266: ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 47216
diff changeset
   179
        synchronized (obj) {
0f3c23c374a4 8207266: ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 47216
diff changeset
   180
            done = true;
0f3c23c374a4 8207266: ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 47216
diff changeset
   181
            obj.notifyAll();
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   182
        }
58208
0f3c23c374a4 8207266: ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 47216
diff changeset
   183
58348
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   184
        // 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
   185
        // 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
   186
        // restarted after we're done sleeping.
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   187
        goSleep(400);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   188
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   189
        for (int i = 0; i < NUM_THREADS; i++) {
58348
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   190
            checkResult(threads[i], sizes[i],
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   191
                        mbean.getThreadAllocatedBytes(threads[i].getId()));
8001
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
        // let threads exit
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   195
        synchronized (obj) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   196
            done1 = true;
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   197
            obj.notifyAll();
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   198
        }
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
        for (int i = 0; i < NUM_THREADS; i++) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   201
            try {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   202
                threads[i].join();
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   203
            } catch (InterruptedException e) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   204
                System.out.println("Unexpected exception is thrown.");
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   205
                e.printStackTrace(System.out);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   206
                break;
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   207
            }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   208
        }
58348
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   209
    }
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   210
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   211
    private static void ensureValidSize(long size) {
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   212
        // implementation could have started measurement when
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   213
        // measurement was enabled, in which case size can be 0
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   214
        if (size < 0) {
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   215
            throw new RuntimeException(
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   216
                "Invalid allocated bytes returned = " + size);
58223
778fc2dcbdaa 8231210: [BACKOUT] JDK-8207266 ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
dcubed
parents: 58208
diff changeset
   217
        }
8001
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
58348
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   220
    private static void checkResult(Thread curThread,
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   221
                                    long prev_size, long curr_size) {
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   222
        if (curr_size < prev_size) {
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   223
            throw new RuntimeException("Allocated bytes " + curr_size +
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   224
                                       " expected >= " + prev_size);
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   225
        }
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   226
        System.out.println(curThread.getName() +
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   227
                           " Previous allocated bytes = " + prev_size +
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   228
                           " Current allocated bytes = " + curr_size);
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   229
    }
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   230
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   231
    private static void goSleep(long ms) throws Exception {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   232
        try {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   233
            Thread.sleep(ms);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   234
        } catch (InterruptedException e) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   235
            System.out.println("Unexpected exception is thrown.");
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   236
            throw e;
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   237
        }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   238
    }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   239
58348
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   240
    private static void waitUntilThreadBlocked(Thread thread)
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   241
        throws Exception {
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   242
        while (true) {
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   243
            goSleep(100);
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   244
            ThreadInfo info = mbean.getThreadInfo(thread.getId());
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   245
            if (info.getThreadState() == Thread.State.WAITING) {
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   246
                break;
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   247
            }
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   248
        }
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   249
    }
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   250
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   251
    private static void waitUntilThreadsBlocked()
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   252
        throws Exception {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   253
        int count = 0;
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   254
        while (count != NUM_THREADS) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   255
            goSleep(100);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   256
            count = 0;
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   257
            for (int i = 0; i < NUM_THREADS; i++) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   258
                ThreadInfo info = mbean.getThreadInfo(threads[i].getId());
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   259
                if (info.getThreadState() == Thread.State.WAITING) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   260
                    count++;
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   261
                }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   262
            }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   263
        }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   264
    }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   265
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   266
    public static void doit() {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   267
        String tmp = "";
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   268
        long hashCode = 0;
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   269
        for (int counter = 0; counter < 1000; counter++) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   270
            tmp += counter;
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   271
            hashCode = tmp.hashCode();
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   272
        }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   273
        System.out.println(Thread.currentThread().getName() +
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   274
                           " hashcode: " + hashCode);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   275
    }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   276
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   277
    static class MyThread extends Thread {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   278
        public MyThread(String name) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   279
            super(name);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   280
        }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   281
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   282
        public void run() {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   283
            ThreadAllocatedMemory.doit();
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   284
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   285
            synchronized (obj) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   286
                while (!done) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   287
                    try {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   288
                        obj.wait();
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   289
                    } catch (InterruptedException e) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   290
                        System.out.println("Unexpected exception is thrown.");
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   291
                        e.printStackTrace(System.out);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   292
                        break;
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   293
                    }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   294
                }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   295
            }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   296
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   297
            long size1 = mbean.getThreadAllocatedBytes(getId());
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   298
            ThreadAllocatedMemory.doit();
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   299
            long size2 = mbean.getThreadAllocatedBytes(getId());
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   300
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   301
            System.out.println(getName() + ": " +
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   302
                "ThreadAllocatedBytes  = " + size1 +
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   303
                " ThreadAllocatedBytes  = " + size2);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   304
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   305
            if (size1 > size2) {
58348
c29e49148be7 8231209: [REDO] ThreadMXBean::getThreadAllocatedBytes() can be quicker for self thread
phh
parents: 58223
diff changeset
   306
                throw new RuntimeException(getName() +
8001
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   307
                    " ThreadAllocatedBytes = " + size1 +
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   308
                    " > ThreadAllocatedBytes = " + size2);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   309
            }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   310
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   311
            synchronized (obj) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   312
                while (!done1) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   313
                    try {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   314
                        obj.wait();
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   315
                    } catch (InterruptedException e) {
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   316
                        System.out.println("Unexpected exception is thrown.");
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   317
                        e.printStackTrace(System.out);
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   318
                        break;
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   319
                    }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   320
                }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   321
            }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   322
        }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   323
    }
192adf3627b7 6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
diff changeset
   324
}