/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 6173675
* @summary Basic test of ThreadMXBean.getThreadAllocatedBytes
* @author Paul Hohensee
*/
public class ThreadAllocatedMemory {
private static boolean testFailed = false;
private static boolean done = false;
private static boolean done1 = false;
throws Exception {
if (!mbean.isThreadAllocatedMemorySupported()) {
return;
}
// disable allocated memory measurement
if (mbean.isThreadAllocatedMemoryEnabled()) {
mbean.setThreadAllocatedMemoryEnabled(false);
}
if (mbean.isThreadAllocatedMemoryEnabled()) {
throw new RuntimeException(
"ThreadAllocatedMemory is expected to be disabled");
}
if (s != -1) {
throw new RuntimeException(
"Invalid ThreadAllocatedBytes returned = " +
s + " expected = -1");
}
// enable allocated memory measurement
if (!mbean.isThreadAllocatedMemoryEnabled()) {
}
if (!mbean.isThreadAllocatedMemoryEnabled()) {
throw new RuntimeException(
"ThreadAllocatedMemory is expected to be enabled");
}
// implementation could have started measurement when
// measurement was enabled, in which case size can be 0
if (size < 0) {
throw new RuntimeException(
"Invalid allocated bytes returned = " + size);
}
doit();
// Expected to be size1 >= size
" expected >= " + size);
}
" Current thread allocated bytes = " + size +
" allocated bytes = " + size1);
// start threads, wait for them to block
for (int i = 0; i < NUM_THREADS; i++) {
}
// threads block after doing some allocation
for (int i = 0; i < NUM_THREADS; i++) {
}
// let threads go and do some more allocation
synchronized (obj) {
done = true;
}
// wait for threads to get going again. we don't care if we
// catch them in mid-execution or if some of them haven't
// restarted after we're done sleeping.
goSleep(400);
for (int i = 0; i < NUM_THREADS; i++) {
throw new RuntimeException("TEST FAILED: " +
" previous allocated bytes = " + sizes[i] +
" > current allocated bytes = " + newSize);
}
" Previous allocated bytes = " + sizes[i] +
" Current allocated bytes = " + newSize);
}
// let threads exit
synchronized (obj) {
done1 = true;
}
for (int i = 0; i < NUM_THREADS; i++) {
try {
} catch (InterruptedException e) {
testFailed = true;
break;
}
}
if (testFailed) {
throw new RuntimeException("TEST FAILED");
}
}
try {
} catch (InterruptedException e) {
throw e;
}
}
private static void waitUntilThreadBlocked()
throws Exception {
int count = 0;
while (count != NUM_THREADS) {
goSleep(100);
count = 0;
for (int i = 0; i < NUM_THREADS; i++) {
count++;
}
}
}
}
public static void doit() {
long hashCode = 0;
}
" hashcode: " + hashCode);
}
super(name);
}
public void run() {
synchronized (obj) {
while (!done) {
try {
} catch (InterruptedException e) {
testFailed = true;
break;
}
}
}
"ThreadAllocatedBytes = " + size1 +
" ThreadAllocatedBytes = " + size2);
" ThreadAllocatedBytes = " + size1 +
" > ThreadAllocatedBytes = " + size2);
}
synchronized (obj) {
while (!done1) {
try {
} catch (InterruptedException e) {
testFailed = true;
break;
}
}
}
}
}
}