/*
* 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.getThreadCpuTime(long[]) and
* getThreadUserTime(long[]).
* @author Paul Hohensee
*/
public class ThreadCpuTimeArray {
private static boolean testFailed = false;
private static boolean done = false;
// careful about this value
throws Exception {
if (!mbean.isThreadCpuTimeSupported()) {
return;
}
// disable CPU time
if (mbean.isThreadCpuTimeEnabled()) {
mbean.setThreadCpuTimeEnabled(false);
}
if (mbean.isThreadCpuTimeEnabled()) {
throw new RuntimeException("ThreadCpuTime is expected to be disabled");
}
// start threads, wait for them to block
long[] ids = new long[NUM_THREADS];
for (int i = 0; i < NUM_THREADS; i++) {
}
// threads block after doing some computation
throw new RuntimeException("Null ThreadCpuTime array returned");
}
for (int i = 0; i < NUM_THREADS; i++) {
long t = times[i];
if (t != -1) {
throw new RuntimeException(
"Invalid ThreadCpuTime returned for thread " +
}
if (ut != -1) {
throw new RuntimeException(
"Invalid ThreadUserTime returned for thread " +
}
}
// Enable CPU Time measurement
if (!mbean.isThreadCpuTimeEnabled()) {
mbean.setThreadCpuTimeEnabled(true);
}
if (!mbean.isThreadCpuTimeEnabled()) {
throw new RuntimeException("ThreadCpuTime is expected to be enabled");
}
goSleep(200);
for (int i = 0; i < NUM_THREADS; i++) {
long t = times[i];
if (t < 0) {
throw new RuntimeException(
"Invalid CPU time returned for thread " +
}
if (ut < 0) {
throw new RuntimeException(
"Invalid user time returned for thread " +
}
}
for (int i = 0; i < NUM_THREADS; i++) {
long newUserTime = userTimes1[i];
throw new RuntimeException("TEST FAILED: " +
" previous CPU time = " + times[i] +
" > current CPU time = " + newTime);
}
throw new RuntimeException("TEST FAILED: " +
" CPU time = " + newTime +
" previous CPU time " + times[i] +
" out of expected range");
}
" Previous Cpu Time = " + times[i] +
" Current CPU time = " + newTime);
" Previous User Time = " + userTimes[i] +
" Current User time = " + newUserTime);
}
try {
} catch (NullPointerException e) {
"Caught expected NullPointerException: " + e.getMessage());
}
try {
} catch (IllegalArgumentException e) {
"Caught expected IllegalArgumentException: " + e.getMessage());
}
// let threads exit
synchronized (obj) {
done = 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() {
double sum = 0;
for (int i = 0; i < 5000; i++) {
sum += x - r;
}
" sum = " + sum);
}
super(name);
}
public void run() {
synchronized (obj) {
while (!done) {
try {
} catch (InterruptedException e) {
testFailed = true;
break;
}
}
}
}
}
}