1 /*
   2  * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug     4530538
  27  * @summary Basic unit test of
  28  *          ThreadMXBean.setThreadContentionMonitoringEnabled()
  29  *          and ThreadMXBean.setThreadCpuTimeEnabled().
  30  * @author  Mandy Chung
  31  *
  32  * @modules java.management
  33  * @run main EnableTest
  34  */
  35 
  36 import java.lang.management.*;
  37 
  38 public class EnableTest {
  39     private static ThreadMXBean tm = ManagementFactory.getThreadMXBean();
  40 
  41     private static void checkThreadContentionMonitoring(boolean expectedValue)
  42         throws Exception {
  43         boolean value = tm.isThreadContentionMonitoringEnabled();
  44         if (value != expectedValue) {
  45              throw new RuntimeException("TEST FAILED: " +
  46                  "isThreadContentionMonitoringEnabled() returns " + value +
  47                  " but expected to be " + expectedValue);
  48         }
  49 
  50     }
  51 
  52     private static void testThreadContentionMonitoring()
  53         throws Exception {
  54         if (!tm.isThreadContentionMonitoringSupported()) return;
  55 
  56         // Test setThreadContentionMonitoringEnabled()
  57         checkThreadContentionMonitoring(false);
  58 
  59         // Check enabled once
  60         tm.setThreadContentionMonitoringEnabled(true);
  61         checkThreadContentionMonitoring(true);
  62 
  63         // Enable it two more times
  64         tm.setThreadContentionMonitoringEnabled(true);
  65         checkThreadContentionMonitoring(true);
  66 
  67         tm.setThreadContentionMonitoringEnabled(true);
  68 
  69         // Disable once will globally disable the thread contention monitoring
  70         tm.setThreadContentionMonitoringEnabled(false);
  71         checkThreadContentionMonitoring(false);
  72 
  73         tm.setThreadContentionMonitoringEnabled(false);
  74         checkThreadContentionMonitoring(false);
  75 
  76         tm.setThreadContentionMonitoringEnabled(true);
  77         checkThreadContentionMonitoring(true);
  78     }
  79 
  80     private static void checkThreadCpuTime(boolean expectedValue)
  81         throws Exception {
  82         boolean value = tm.isThreadCpuTimeEnabled();
  83         if (value != expectedValue) {
  84              throw new RuntimeException("TEST FAILED: " +
  85                  "isThreadCpuTimeEnabled() returns " + value +
  86                  " but expected to be " + expectedValue);
  87         }
  88     }
  89 
  90     private static void testThreadCpuTime()
  91         throws Exception {
  92         if (!tm.isThreadCpuTimeSupported()) return;
  93 
  94         // Test setThreadCpuTimeEnabled()
  95         if (!tm.isThreadCpuTimeEnabled()) {
  96             tm.setThreadCpuTimeEnabled(true);
  97             checkThreadCpuTime(true);
  98         }
  99 
 100         tm.setThreadCpuTimeEnabled(false);
 101         checkThreadCpuTime(false);
 102 
 103         tm.setThreadCpuTimeEnabled(true);
 104         checkThreadCpuTime(true);
 105 
 106         tm.setThreadCpuTimeEnabled(true);
 107         checkThreadCpuTime(true);
 108 
 109         tm.setThreadCpuTimeEnabled(true);
 110 
 111         // disable globally
 112         tm.setThreadCpuTimeEnabled(false);
 113         checkThreadCpuTime(false);
 114 
 115         tm.setThreadCpuTimeEnabled(false);
 116         checkThreadCpuTime(false);
 117 
 118         tm.setThreadCpuTimeEnabled(true);
 119         checkThreadCpuTime(true);
 120     }
 121 
 122     public static void main(String args[]) throws Exception {
 123         try {
 124             testThreadContentionMonitoring();
 125             testThreadCpuTime();
 126         } finally {
 127             // restore the default
 128             if (tm.isThreadContentionMonitoringSupported()) {
 129                 tm.setThreadContentionMonitoringEnabled(false);
 130             }
 131             if (tm.isThreadCpuTimeSupported()) {
 132                 tm.setThreadCpuTimeEnabled(false);
 133             }
 134         }
 135 
 136 
 137         System.out.println("Test passed.");
 138     }
 139 }