Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/management/ThreadMXBean/SynchronizationStatistics.java
38821 views
/*1* Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 453053826* @summary Basic unit test of the synchronization statistics support:27*28* @author Mandy Chung29* @author Jaroslav Bachorik30*31* @run main/othervm SynchronizationStatistics32*/3334import java.lang.management.*;35import java.util.concurrent.Phaser;36import java.util.function.Supplier;3738public class SynchronizationStatistics {39private static class LockerThread extends Thread {40public LockerThread(Runnable r) {41super(r, "LockerThread");42}43}4445private static final ThreadMXBean mbean = ManagementFactory.getThreadMXBean();4647private static final boolean blockedTimeCheck =48mbean.isThreadContentionMonitoringSupported();495051public static void main(String args[]) throws Exception {52if (blockedTimeCheck) {53mbean.setThreadContentionMonitoringEnabled(true);54}5556if (!mbean.isThreadContentionMonitoringEnabled()) {57throw new RuntimeException("TEST FAILED: " +58"Thread Contention Monitoring is not enabled");59}6061testBlockingOnSimpleMonitor();62testBlockingOnNestedMonitor();63testWaitingOnSimpleMonitor();64testMultiWaitingOnSimpleMonitor();65testWaitingOnNestedMonitor();6667System.out.println("Test passed.");68}6970private static LockerThread newLockerThread(Runnable r) {71LockerThread t = new LockerThread(r);72t.setDaemon(true);73return t;74}7576private static void waitForThreadState(Thread t, Thread.State state) throws InterruptedException {77while (t.getState() != state) {78Thread.sleep(3);79}80}8182/**83* Tests that blocking on a single monitor properly increases the84* blocked count at least by 1. Also asserts that the correct lock name is provided.85*/86private static void testBlockingOnSimpleMonitor() throws Exception {87System.out.println("testBlockingOnSimpleMonitor");88final Object lock1 = new Object();89System.out.println("Lock1 = " + lock1);9091final Phaser p = new Phaser(2);92LockerThread lt = newLockerThread(new Runnable() {93@Override94public void run() {95p.arriveAndAwaitAdvance(); // phase[1]96synchronized(lock1) {97System.out.println("[LockerThread obtained Lock1]");98p.arriveAndAwaitAdvance(); // phase[2]99}100p.arriveAndAwaitAdvance(); // phase[3]101}102});103104lt.start();105long tid = lt.getId();106ThreadInfo ti = mbean.getThreadInfo(tid);107String lockName = null;108synchronized(lock1) {109p.arriveAndAwaitAdvance(); // phase[1]110waitForThreadState(lt, Thread.State.BLOCKED);111do {112lockName = mbean.getThreadInfo(tid).getLockName();113} while (lockName == null);114}115116p.arriveAndAwaitAdvance(); // phase[2]117testBlocked(ti, () -> mbean.getThreadInfo(tid), lockName, lock1);118p.arriveAndDeregister(); // phase[3]119120lt.join();121122printok();123}124125/**126* Tests that blocking on a nested monitor properly increases the127* blocked count at least by 1 - it is not affected by the nesting depth.128* Also asserts that the correct lock name is provided.129*/130private static void testBlockingOnNestedMonitor() throws Exception {131System.out.println("testBlockingOnNestedMonitor");132final Object lock1 = new Object();133final Object lock2 = new Object();134135System.out.println("Lock1 = " + lock1);136System.out.println("Lock2 = " + lock2);137138final Phaser p = new Phaser(2);139LockerThread lt = newLockerThread(new Runnable() {140@Override141public void run() {142p.arriveAndAwaitAdvance(); // phase[1]143synchronized(lock1) {144System.out.println("[LockerThread obtained Lock1]");145p.arriveAndAwaitAdvance(); // phase[2]146p.arriveAndAwaitAdvance(); // phase[3]147synchronized(lock2) {148System.out.println("[LockerThread obtained Lock2]");149p.arriveAndAwaitAdvance(); // phase[4]150}151p.arriveAndAwaitAdvance(); // phase[5]152}153}154});155156lt.start();157long tid = lt.getId();158ThreadInfo ti = mbean.getThreadInfo(tid);159String lockName = null;160synchronized(lock1) {161p.arriveAndAwaitAdvance(); // phase[1]162waitForThreadState(lt, Thread.State.BLOCKED);163do {164lockName = mbean.getThreadInfo(tid).getLockName();165} while (lockName == null);166}167p.arriveAndAwaitAdvance(); // phase[2]168169ti = testBlocked(ti, () -> mbean.getThreadInfo(tid), lockName, lock1);170171synchronized(lock2) {172p.arriveAndAwaitAdvance(); // phase [3]173waitForThreadState(lt, Thread.State.BLOCKED);174do {175lockName = mbean.getThreadInfo(tid).getLockName();176} while (lockName == null);177}178p.arriveAndAwaitAdvance(); // phase [4]179testBlocked(ti, () -> mbean.getThreadInfo(tid), lockName, lock2);180p.arriveAndDeregister();181182lt.join();183184printok();185}186187/**188* Tests that waiting on a single monitor properly increases the waited189* count by 1 and the waited time by a positive number.190*/191private static void testWaitingOnSimpleMonitor() throws Exception {192System.out.println("testWaitingOnSimpleMonitor");193final Object lock1 = new Object();194final Phaser p = new Phaser(2);195LockerThread lt = newLockerThread(new Runnable() {196@Override197public void run() {198p.arriveAndAwaitAdvance(); // phase[1]199synchronized(lock1) {200System.out.println("[LockerThread obtained Lock1]");201try {202lock1.wait(300);203} catch (InterruptedException ex) {204// ignore205}206p.arriveAndAwaitAdvance(); // phase[2]207}208p.arriveAndAwaitAdvance(); // phase[3]209}210});211212lt.start();213ThreadInfo ti1 = mbean.getThreadInfo(lt.getId());214synchronized(lock1) {215p.arriveAndAwaitAdvance(); // phase[1]216waitForThreadState(lt, Thread.State.BLOCKED);217}218p.arriveAndAwaitAdvance(); // phase[2]219220testWaited(ti1, () -> mbean.getThreadInfo(lt.getId()), 1);221p.arriveAndDeregister(); // phase[3]222223lt.join();224225printok();226}227228/**229* Tests that waiting multiple times on the same monitor subsequently230* increases the waited count by the number of subsequent calls and the231* waited time by a positive number.232*/233private static void testMultiWaitingOnSimpleMonitor() throws Exception {234System.out.println("testWaitingOnMultipleMonitors");235final Object lock1 = new Object();236237final Phaser p = new Phaser(2);238LockerThread lt = newLockerThread(new Runnable() {239@Override240public void run() {241p.arriveAndAwaitAdvance(); // phase[1]242synchronized(lock1) {243System.out.println("[LockerThread obtained Lock1]");244for (int i = 0; i < 3; i++) {245try {246lock1.wait(300);247} catch (InterruptedException ex) {248// ignore249}250p.arriveAndAwaitAdvance(); // phase[2-4]251}252}253p.arriveAndAwaitAdvance(); // phase[5]254}255});256257lt.start();258ThreadInfo ti1 = mbean.getThreadInfo(lt.getId());259synchronized(lock1) {260p.arriveAndAwaitAdvance(); //phase[1]261waitForThreadState(lt, Thread.State.BLOCKED);262}263int phase = p.getPhase();264while ((p.arriveAndAwaitAdvance() - phase) < 3); // phase[2-4]265266testWaited(ti1, () -> mbean.getThreadInfo(lt.getId()), 3);267p.arriveAndDeregister(); // phase[5]268269lt.join();270271printok();272}273274/**275* Tests that waiting on monitors places in nested synchronized blocks276* properly increases the waited count by the number of times the "lock.wait()"277* was invoked and the waited time by a positive number.278*/279private static void testWaitingOnNestedMonitor() throws Exception {280System.out.println("testWaitingOnNestedMonitor");281final Object lock1 = new Object();282final Object lock2 = new Object();283final Object lock3 = new Object();284285final Phaser p = new Phaser(2);286LockerThread lt = newLockerThread(new Runnable() {287@Override288public void run() {289p.arriveAndAwaitAdvance(); // phase[1]290synchronized(lock1) {291System.out.println("[LockerThread obtained Lock1]");292try {293lock1.wait(300);294} catch (InterruptedException ex) {295// ignore296}297298p.arriveAndAwaitAdvance(); // phase[2]299synchronized(lock2) {300System.out.println("[LockerThread obtained Lock2]");301try {302lock2.wait(300);303} catch (InterruptedException ex) {304// ignore305}306307p.arriveAndAwaitAdvance(); // phase[3]308synchronized(lock3) {309System.out.println("[LockerThread obtained Lock3]");310try {311lock3.wait(300);312} catch (InterruptedException ex) {313// ignore314}315p.arriveAndAwaitAdvance(); // phase[4]316}317}318}319p.arriveAndAwaitAdvance(); // phase[5]320}321});322323lt.start();324ThreadInfo ti1 = mbean.getThreadInfo(lt.getId());325synchronized(lock1) {326p.arriveAndAwaitAdvance(); // phase[1]327waitForThreadState(lt, Thread.State.BLOCKED);328}329330synchronized(lock2) {331p.arriveAndAwaitAdvance(); // phase[2]332waitForThreadState(lt, Thread.State.BLOCKED);333}334335synchronized(lock3) {336p.arriveAndAwaitAdvance(); // phase[3]337waitForThreadState(lt, Thread.State.BLOCKED);338}339340p.arriveAndAwaitAdvance(); // phase[4]341testWaited(ti1, () -> mbean.getThreadInfo(lt.getId()), 3);342p.arriveAndDeregister(); // phase[5]343344lt.join();345printok();346}347348private static void printok() {349System.out.println("OK\n");350}351352private static void testWaited(ThreadInfo ti1, Supplier<ThreadInfo> ti2, int waited)353throws InterruptedException {354boolean error;355do {356error = false;357ThreadInfo ti = ti2.get();358long waitCntDiff = ti.getWaitedCount() - ti1.getWaitedCount();359long waitTimeDiff = ti.getWaitedTime() - ti1.getWaitedTime();360if (waitCntDiff < waited) {361System.err.println(362"Unexpected diff in waited count. Expecting at least "363+ waited + " , got " + waitCntDiff364);365error = true;366}367if (waitTimeDiff <= 0) {368System.err.println(369"Unexpected diff in waited time. Expecting increasing " +370"value, got " + waitTimeDiff + "ms"371);372error = true;373}374if (error) {375System.err.println("Retrying in 20ms ...");376Thread.sleep(20);377}378} while (error);379}380381private static ThreadInfo testBlocked(ThreadInfo ti1, Supplier<ThreadInfo> ti2,382String lockName, final Object lock)383throws InterruptedException {384boolean error;385ThreadInfo ti = null;386do {387error = false;388ti = ti2.get();389long blkCntDiff = ti.getBlockedCount() - ti1.getBlockedCount();390long blkTimeDiff = ti.getBlockedTime() - ti1.getBlockedTime();391392System.out.println("testBlocked: [" + blkCntDiff + ", " + blkTimeDiff + ", " + lockName + "]");393394if (blkCntDiff < 1) {395System.err.println(396"Unexpected diff in blocked count. Expecting at least 1, " +397"got " + blkCntDiff398);399error = true;400}401if (blkTimeDiff < 0) {402System.err.println(403"Unexpected diff in blocked time. Expecting a positive " +404"number, got " + blkTimeDiff405);406error = true;407}408if (!lockName.equals(lock.toString())) {409System.err.println(410"Unexpected blocked monitor name. Expecting " +411lock.toString() + ", got " + lockName412);413error = true;414}415if (error) {416System.err.println("Retrying in 20ms ...");417Thread.sleep(20);418}419} while (error);420return ti;421}422}423424425