1 /*
   2  * Copyright (c) 2018, 2019, 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 package gc.g1.mixedgc;
  25 
  26 import static sun.hotspot.WhiteBox.getWhiteBox;
  27 
  28 import java.lang.management.*;
  29 import java.util.ArrayList;
  30 import java.util.List;
  31 
  32 import gc.testlibrary.g1.MixedGCProvoker;
  33 
  34 /*
  35  * @test TestOldGenCollectionUsage.java
  36  * @bug 8195115
  37  * @summary G1 Old Gen's CollectionUsage.used is zero after mixed GC which is incorrect
  38  * @key gc
  39  * @requires vm.gc.G1
  40  * @library /test/lib /
  41  * @modules java.base/jdk.internal.misc
  42  * @modules java.management
  43  * @build sun.hotspot.WhiteBox
  44  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
  45  * @run main/othervm -Xbootclasspath/a:. -XX:+UseG1GC -XX:+UnlockExperimentalVMOptions -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -verbose:gc -XX:NewSize=2m -XX:MaxNewSize=2m -Xmx14m -Xms14m -XX:+AlwaysTenure -XX:InitiatingHeapOccupancyPercent=100 -XX:-G1UseAdaptiveIHOP -XX:G1MixedGCCountTarget=4 -XX:MaxGCPauseMillis=30000 -XX:G1HeapRegionSize=1m -XX:G1HeapWastePercent=0 -XX:G1MixedGCLiveThresholdPercent=100 gc.g1.mixedgc.TestOldGenCollectionUsage
  46  */
  47 
  48 // 8195115 says that for the "G1 Old Gen" MemoryPool, CollectionUsage.used
  49 // is zero for G1 after a mixed collection, which is incorrect. We can not guarantee
  50 // that we start with an old gen occupancy of zero though due to allocation during
  51 // initialization. We check for "increases during mixed gc" instead.
  52 
  53 public class TestOldGenCollectionUsage {
  54 
  55     private static final int HeapRegionSize = 1024 * 1024;
  56     private static final int ObjectSize = 20_000;
  57 
  58     private String poolName = "G1 Old Gen";
  59     private String collectorName = "G1 Young Generation";
  60 
  61     public static void main(String [] args) throws Exception {
  62         TestOldGenCollectionUsage t = new TestOldGenCollectionUsage();
  63         t.run();
  64     }
  65 
  66     public TestOldGenCollectionUsage() {
  67         System.out.println("Monitor G1 Old Gen pool with G1 Young Generation collector.");
  68     }
  69 
  70     public void run() {
  71         // Find memory pool and collector
  72         List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();
  73         MemoryPoolMXBean pool = null;
  74         boolean foundPool = false;
  75         for (int i = 0; i < pools.size(); i++) {
  76             pool = pools.get(i);
  77             String name = pool.getName();
  78             if (name.contains(poolName)) {
  79                 System.out.println("Found pool: " + name);
  80                 foundPool = true;
  81                 break;
  82             }
  83         }
  84         if (!foundPool) {
  85             throw new RuntimeException(poolName + " not found, test with -XX:+UseG1GC");
  86         }
  87 
  88         List<GarbageCollectorMXBean> collectors = ManagementFactory.getGarbageCollectorMXBeans();
  89         GarbageCollectorMXBean collector = null;
  90         boolean foundCollector = false;
  91         for (int i = 0; i < collectors.size(); i++) {
  92             collector = collectors.get(i);
  93             String name = collector.getName();
  94             if (name.contains(collectorName)) {
  95                 System.out.println("Found collector: " + name);
  96                 foundCollector = true;
  97                 break;
  98             }
  99         }
 100         if (!foundCollector) {
 101             throw new RuntimeException(collectorName + " not found, test with -XX:+UseG1GC");
 102         }
 103 
 104         getWhiteBox().fullGC(); // Make sure the heap is in a known state.
 105 
 106         var liveOldObjects = new ArrayList<byte[]>();
 107         MixedGCProvoker.allocateOldObjects(liveOldObjects, HeapRegionSize, ObjectSize);
 108 
 109         long baseUsage = pool.getCollectionUsage().getUsed();
 110         System.out.println(poolName + ": usage after GC = " + baseUsage);
 111 
 112         // Verify that collections were done
 113         long collectionCount = collector.getCollectionCount();
 114         System.out.println(collectorName + ": collection count = "
 115                            + collectionCount);
 116         long collectionTime = collector.getCollectionTime();
 117         System.out.println(collectorName + ": collection time  = "
 118                            + collectionTime);
 119         if (collectionCount <= 0) {
 120             throw new RuntimeException("Collection count <= 0");
 121         }
 122         if (collectionTime <= 0) {
 123             throw new RuntimeException("Collector has not run");
 124         }
 125 
 126         MixedGCProvoker.provokeMixedGC(liveOldObjects);
 127 
 128         long usage = pool.getCollectionUsage().getUsed();
 129         System.out.println(poolName + ": usage after GC = " + usage);
 130         if (usage <= baseUsage) {
 131             throw new RuntimeException(poolName + " found not updating usage");
 132         }
 133 
 134         long newCollectionCount = collector.getCollectionCount();
 135         System.out.println(collectorName + ": collection count = "
 136                            + newCollectionCount);
 137         long newCollectionTime = collector.getCollectionTime();
 138         System.out.println(collectorName + ": collection time  = "
 139                            + newCollectionTime);
 140         if (newCollectionCount <= collectionCount) {
 141             throw new RuntimeException("No new collection");
 142         }
 143         if (newCollectionTime <= collectionTime) {
 144             throw new RuntimeException("Collector has not run some more");
 145         }
 146 
 147         System.out.println("Test passed.");
 148     }
 149 }
 150