1 /*
   2  * Copyright (c) 2013, 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 import java.util.List;
  25 import java.lang.management.*;
  26 
  27 import com.oracle.java.testlibrary.*;
  28 import static com.oracle.java.testlibrary.Asserts.*;
  29 
  30 /* @test TestPerfCountersAndMemoryPools
  31  * @bug 8023476
  32  * @summary Tests that a MemoryPoolMXBeans and PerfCounters for metaspace
  33  *          report the same data.
  34  * @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:-UseCompressedOops -XX:-UseCompressedKlassPointers -XX:+UseSerialGC TestPerfCountersAndMemoryPools
  35  * @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:+UseCompressedOops -XX:+UseCompressedKlassPointers -XX:+UseSerialGC TestPerfCountersAndMemoryPools
  36  */
  37 public class TestPerfCountersAndMemoryPools {
  38     public static void main(String[] args) throws Exception {
  39         checkMemoryUsage("Metaspace", "sun.gc.metaspace");
  40 
  41         if (InputArguments.contains("-XX:+UseCompressedKlassPointers") && Platform.is64bit()) {
  42             checkMemoryUsage("Compressed Class Space", "sun.gc.compressedclassspace");
  43         }
  44     }
  45 
  46     private static MemoryUsage getMemoryUsage(String memoryPoolName) {
  47         List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();
  48         for (MemoryPoolMXBean pool : pools) {
  49             if (pool.getName().equals(memoryPoolName)) {
  50                 return pool.getUsage();
  51             }
  52         }
  53 
  54         throw new RuntimeException("Excpted to find a memory pool with name " +
  55                                    memoryPoolName);
  56     }
  57 
  58     private static void checkMemoryUsage(String memoryPoolName, String perfNS)
  59         throws Exception {
  60         // Need to do a gc before each comparison to update the perf counters
  61 
  62         System.gc();
  63         MemoryUsage mu = getMemoryUsage(memoryPoolName);
  64         assertEQ(getMinCapacity(perfNS), mu.getInit());
  65 
  66         System.gc();
  67         mu = getMemoryUsage(memoryPoolName);
  68         assertEQ(getUsed(perfNS), mu.getUsed());
  69 
  70         System.gc();
  71         mu = getMemoryUsage(memoryPoolName);
  72         assertEQ(getCapacity(perfNS), mu.getCommitted());
  73     }
  74 
  75     private static long getMinCapacity(String ns) throws Exception {
  76         return PerfCounters.findByName(ns + ".minCapacity").longValue();
  77     }
  78 
  79     private static long getCapacity(String ns) throws Exception {
  80         return PerfCounters.findByName(ns + ".capacity").longValue();
  81     }
  82 
  83     private static long getUsed(String ns) throws Exception {
  84         return PerfCounters.findByName(ns + ".used").longValue();
  85     }
  86 }