1 /*
   2  * Copyright (c) 2017, 2018, 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 /* @test TestMemoryMXBeansAndPoolsPresence
  25  * @key gc
  26  * @bug 8191564
  27  * @summary Tests that GarbageCollectorMXBeans and GC MemoryPools are created.
  28  * @library /testlibrary
  29  * @requires vm.gc == null
  30  * @run main/othervm -XX:+UseParallelGC TestMemoryMXBeansAndPoolsPresence Parallel
  31  * @run main/othervm -XX:+UseSerialGC TestMemoryMXBeansAndPoolsPresence Serial
  32  * @run main/othervm -XX:+UseConcMarkSweepGC TestMemoryMXBeansAndPoolsPresence CMS
  33  * @run main/othervm -XX:+UseG1GC TestMemoryMXBeansAndPoolsPresence G1
  34  */
  35 
  36 import java.util.List;
  37 import java.util.ArrayList;
  38 import java.lang.management.*;
  39 import java.util.stream.*;
  40 
  41 import com.oracle.java.testlibrary.Asserts;
  42 
  43 class GCBeanDescription {
  44     public String name;
  45     public String[] poolNames;
  46 
  47     public GCBeanDescription(String name, String[] poolNames) {
  48         this.name = name;
  49         this.poolNames = poolNames;
  50     }
  51 }
  52 
  53 public class TestMemoryMXBeansAndPoolsPresence {
  54     public static void test(GCBeanDescription... expectedBeans) {
  55         List<MemoryPoolMXBean> memoryPools = ManagementFactory.getMemoryPoolMXBeans();
  56 
  57         List<GarbageCollectorMXBean> gcBeans = ManagementFactory.getGarbageCollectorMXBeans();
  58         Asserts.assertEQ(expectedBeans.length, gcBeans.size());
  59 
  60         for (GCBeanDescription desc : expectedBeans) {
  61             List<GarbageCollectorMXBean> beans = gcBeans.stream()
  62                                                         .filter(b -> b.getName().equals(desc.name))
  63                                                         .collect(Collectors.toList());
  64             Asserts.assertEQ(beans.size(), 1);
  65 
  66             GarbageCollectorMXBean bean = beans.get(0);
  67             Asserts.assertEQ(desc.name, bean.getName());
  68 
  69             String[] pools = bean.getMemoryPoolNames();
  70             Asserts.assertEQ(desc.poolNames.length, pools.length);
  71             for (int i = 0; i < desc.poolNames.length; i++) {
  72                 Asserts.assertEQ(desc.poolNames[i], pools[i]);
  73             }
  74         }
  75     }
  76 
  77     public static void main(String[] args) {
  78         switch (args[0]) {
  79             case "G1":
  80                 test(new GCBeanDescription("G1 Young Generation", new String[] {"G1 Eden Space", "G1 Survivor Space", "G1 Old Gen"}),
  81                      new GCBeanDescription("G1 Old Generation",   new String[] {"G1 Eden Space", "G1 Survivor Space", "G1 Old Gen"}));
  82                 break;
  83             case "CMS":
  84                 test(new GCBeanDescription("ParNew",              new String[] {"Par Eden Space", "Par Survivor Space"}),
  85                      new GCBeanDescription("ConcurrentMarkSweep", new String[] {"Par Eden Space", "Par Survivor Space", "CMS Old Gen"}));
  86                 break;
  87             case "Parallel":
  88                 test(new GCBeanDescription("PS Scavenge",         new String[] {"PS Eden Space", "PS Survivor Space"}),
  89                      new GCBeanDescription("PS MarkSweep",        new String[] {"PS Eden Space", "PS Survivor Space", "PS Old Gen"}));
  90                 break;
  91             case "Serial":
  92                 test(new GCBeanDescription("Copy",              new String[] {"Eden Space", "Survivor Space"}),
  93                      new GCBeanDescription("MarkSweepCompact",  new String[] {"Eden Space", "Survivor Space", "Tenured Gen"}));
  94                 break;
  95             default:
  96                 Asserts.assertTrue(false);
  97                 break;
  98 
  99         }
 100     }
 101 }