/*
* 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 4997799
* @summary Basic test of ThreadMXBean.getThreadUserTime and
* getCurrentThreadUserTime.
* @author Mandy Chung
*/
public class ThreadUserTime {
private static boolean testFailed = false;
private static boolean done = false;
// careful about this value
throws Exception {
if (!mbean.isCurrentThreadCpuTimeSupported()) {
return;
}
// disable user time
if (mbean.isThreadCpuTimeEnabled()) {
mbean.setThreadCpuTimeEnabled(false);
}
long t = mbean.getCurrentThreadUserTime();
if (t != -1) {
throw new RuntimeException("Invalid CurrenThreadUserTime returned = " +
t + " expected = -1");
}
if (mbean.isThreadCpuTimeSupported()) {
if (t1 != -1) {
throw new RuntimeException("Invalid ThreadUserTime returned = " +
t1 + " expected = -1");
}
}
// Enable CPU Time measurement
if (!mbean.isThreadCpuTimeEnabled()) {
mbean.setThreadCpuTimeEnabled(true);
}
if (!mbean.isThreadCpuTimeEnabled()) {
throw new RuntimeException("ThreadUserTime is expected to be enabled");
}
if (time < 0) {
}
if (!mbean.isThreadCpuTimeSupported()) {
return;
}
// Expected to be time1 >= time
" expected >= " + time);
}
" Current Thread User Time = " + time +
" user time = " + time1);
for (int i = 0; i < NUM_THREADS; i++) {
}
for (int i = 0; i < NUM_THREADS; i++) {
}
goSleep(200);
for (int i = 0; i < NUM_THREADS; i++) {
throw new RuntimeException("TEST FAILED: " +
" previous user user time = " + times[i] +
" > current user user time = " + newTime);
}
throw new RuntimeException("TEST FAILED: " +
" user time = " + newTime +
" previous user time " + times[i] +
" out of expected range");
}
" Previous User Time = " + times[i] +
" Current User time = " + newTime);
}
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++;
}
}
}
}
super(name);
}
public void run() {
double sum = 0;
for (int i = 0; i < 5000; i++) {
sum += x - r;
}
synchronized (obj) {
while (!done) {
try {
} catch (InterruptedException e) {
testFailed = true;
break;
}
}
}
sum = 0;
for (int i = 0; i < 5000; i++) {
sum += x - r;
}
" ThreadUserTime = " + utime2);
" ThreadCpuTime = " + time2);
" CurrentThreadCpuTime = " + time1 +
" > ThreadCpuTime = " + time2);
}
" CurrentThreadUserTime = " + utime1 +
" > ThreadUserTime = " + utime2);
}
}
}
}