1 /*
   2  * Copyright (c) 2007, 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     6546089
  27  * @summary Basic unit test of MemoryPoolMXBean.isUsageThresholdExceeded() and
  28  *          MemoryPoolMXBean.isCollectionThresholdExceeded().
  29  * @author  Mandy Chung
  30  *
  31  * @modules java.management
  32  * @run main/othervm ThresholdTest
  33  */
  34 
  35 import java.lang.management.*;
  36 import java.util.*;
  37 
  38 public class ThresholdTest {
  39     public static void main(String args[]) throws Exception {
  40         long[] bigObject = new long[1000000];
  41 
  42         System.gc(); // force an initial full-gc
  43         List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();
  44         try {
  45             for (MemoryPoolMXBean p : pools) {
  46                 // verify if isUsageThresholdExceeded() returns correct value
  47                 checkUsageThreshold(p);
  48                 // verify if isCollectionUsageThresholdExceeded() returns correct value
  49                 checkCollectionUsageThreshold(p);
  50             }
  51         } finally {
  52             // restore the default
  53             for (MemoryPoolMXBean p : pools) {
  54                 if (p.isUsageThresholdSupported()) {
  55                     p.setUsageThreshold(0);
  56                 }
  57                 if (p.isCollectionUsageThresholdSupported()) {
  58                     p.setCollectionUsageThreshold(0);
  59                 }
  60             }
  61         }
  62 
  63         System.out.println("Test passed.");
  64     }
  65 
  66     private static void checkUsageThreshold(MemoryPoolMXBean p) throws Exception {
  67 
  68         if (!p.isUsageThresholdSupported()) {
  69             return;
  70         }
  71 
  72         long threshold = p.getUsageThreshold();
  73         if (threshold != 0) {
  74             // Expect the default threshold is zero (disabled)
  75             throw new RuntimeException("TEST FAILED: " +
  76                 "Pool " + p.getName() +
  77                 " has non-zero threshold (" + threshold);
  78         }
  79 
  80         // isUsageThresholdExceeded() should return false if threshold == 0
  81         if (p.isUsageThresholdExceeded()) {
  82             throw new RuntimeException("TEST FAILED: " +
  83                 "Pool " + p.getName() +
  84                 " isUsageThresholdExceeded() returned true" +
  85                 " but threshold = 0");
  86         }
  87 
  88         p.setUsageThreshold(1);
  89         // force a full gc to minimize the likelihood of running GC
  90         // between getting the usage and checking the threshold
  91         System.gc();
  92 
  93         MemoryUsage u = p.getUsage();
  94         if (u.getUsed() >= 1) {
  95             if (!p.isUsageThresholdExceeded()) {
  96                 throw new RuntimeException("TEST FAILED: " +
  97                     "Pool " + p.getName() +
  98                     " isUsageThresholdExceeded() returned false but " +
  99                     " threshold(" + p.getUsageThreshold() +
 100                     ") <= used(" + u.getUsed() + ")");
 101             }
 102         } else {
 103             if (p.isUsageThresholdExceeded()) {
 104                 throw new RuntimeException("TEST FAILED: " +
 105                     "Pool " + p.getName() +
 106                     " isUsageThresholdExceeded() returned true but" +
 107                     " threshold(" + p.getUsageThreshold() +
 108                     ") > used(" + u.getUsed() + ")");
 109             }
 110         }
 111 
 112         // disable low memory detection and isUsageThresholdExceeded()
 113         // should return false
 114         p.setUsageThreshold(0);
 115         if (p.isUsageThresholdExceeded()) {
 116             throw new RuntimeException("TEST FAILED: " +
 117                 "Pool " + p.getName() +
 118                 " isUsageThresholdExceeded() returned true but threshold = 0");
 119         }
 120     }
 121 
 122     private static void checkCollectionUsageThreshold(MemoryPoolMXBean p) throws Exception {
 123 
 124         if (!p.isCollectionUsageThresholdSupported()) {
 125             return;
 126         }
 127 
 128         long threshold = p.getCollectionUsageThreshold();
 129         if (threshold != 0) {
 130             // Expect the default threshold is zero (disabled)
 131             throw new RuntimeException("TEST FAILED: " +
 132                 "Pool " + p.getName() +
 133                 " has non-zero threshold (" + threshold);
 134         }
 135 
 136         // isCollectionUsageThresholdExceeded() should return false if threshold == 0
 137         if (p.isCollectionUsageThresholdExceeded()) {
 138             throw new RuntimeException("TEST FAILED: " +
 139                 "Pool " + p.getName() +
 140                 " isCollectionUsageThresholdExceeded() returned true" +
 141                 " but threshold = 0");
 142         }
 143 
 144         p.setCollectionUsageThreshold(1);
 145         MemoryUsage u = p.getCollectionUsage();
 146         if (u == null) {
 147             if (p.isCollectionUsageThresholdExceeded()) {
 148                 throw new RuntimeException("TEST FAILED: " +
 149                     "Pool " + p.getName() +
 150                     " isCollectionUsageThresholdExceeded() returned true but" +
 151                     " getCollectionUsage() return null");
 152             }
 153         } else if (u.getUsed() >= 1) {
 154             if (!p.isCollectionUsageThresholdExceeded()) {
 155                 throw new RuntimeException("TEST FAILED: " +
 156                     "Pool " + p.getName() +
 157                     " isCollectionUsageThresholdExceeded() returned false but " +
 158                     " threshold(" + p.getCollectionUsageThreshold() +
 159                     ") < used(" + u.getUsed() + ")");
 160             }
 161         } else {
 162             if (p.isCollectionUsageThresholdExceeded()) {
 163                 throw new RuntimeException("TEST FAILED: " +
 164                     "Pool " + p.getName() +
 165                     " isCollectionUsageThresholdExceeded() returned true but" +
 166                     " threshold(" + p.getCollectionUsageThreshold() +
 167                     ") > used(" + u.getUsed() + ")");
 168             }
 169         }
 170 
 171         // disable low memory detection and isCollectionUsageThresholdExceeded()
 172         // should return false
 173         p.setCollectionUsageThreshold(0);
 174         if (p.isCollectionUsageThresholdExceeded()) {
 175             throw new RuntimeException("TEST FAILED: " +
 176                 "Pool " + p.getName() +
 177                 " isCollectionUsageThresholdExceeded() returned true but threshold = 0");
 178         }
 179     }
 180 }