1 /*
   2  * Copyright (c) 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 /*
  25  * @test TestOldGenCollectionUsage.java
  26  * @bug 8195115
  27  * @summary G1 Old Gen's CollectionUsage.used is zero after mixed GC which is incorrect
  28  * @key gc
  29  * @requires vm.gc.G1
  30  * @requires vm.opt.MaxGCPauseMillis == "null"
  31  * @library /test/lib
  32  * @modules java.base/jdk.internal.misc
  33  * @modules java.management
  34  * @build sun.hotspot.WhiteBox
  35  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
  36  * @run main/othervm -Xbootclasspath/a:. -XX:+UseG1GC -XX:+UnlockExperimentalVMOptions
  37  * -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -verbose:gc -XX:SurvivorRatio=1 -Xmx14m -Xms14m -XX:MaxTenuringThreshold=1 -XX:InitiatingHeapOccupancyPercent=100 -XX:-G1UseAdaptiveIHOP -XX:G1MixedGCCountTarget=4 -XX:MaxGCPauseMillis=30000 -XX:G1HeapRegionSize=1m -XX:G1HeapWastePercent=0 -XX:G1MixedGCLiveThresholdPercent=100 TestOldGenCollectionUsage
  38  */
  39 
  40 import jdk.test.lib.Asserts;
  41 import sun.hotspot.WhiteBox;
  42 
  43 import java.util.ArrayList;
  44 import java.util.List;
  45 import java.util.Collections;
  46 
  47 import java.lang.management.*;
  48 
  49 // 8195115 says that for the "G1 Old Gen" MemoryPool, CollectionUsage.used
  50 // is zero for G1 after a mixed collection, which is incorrect.
  51 
  52 public class TestOldGenCollectionUsage {
  53 
  54     private String poolName = "G1 Old Gen";
  55     private String collectorName = "G1 Young Generation";
  56 
  57     public static void main(String [] args) throws Exception {
  58         TestOldGenCollectionUsage t = new TestOldGenCollectionUsage();
  59         t.run();
  60     }
  61 
  62     public TestOldGenCollectionUsage() {
  63         System.out.println("Monitor G1 Old Gen pool with G1 Young Generation collector.");
  64     }
  65 
  66     public void run() {
  67         // Find memory pool and collector
  68         List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();
  69         MemoryPoolMXBean pool = null;
  70         boolean foundPool = false;
  71         for (int i = 0; i < pools.size(); i++) {
  72             pool = pools.get(i);
  73             String name = pool.getName();
  74             if (name.contains(poolName)) {
  75                 System.out.println("Found pool: " + name);
  76                 foundPool = true;
  77                 break;
  78             }
  79         }
  80         if (!foundPool) {
  81             throw new RuntimeException(poolName + " not found, test with -XX:+UseG1GC");
  82         }
  83 
  84         List<GarbageCollectorMXBean> collectors = ManagementFactory.getGarbageCollectorMXBeans();
  85         GarbageCollectorMXBean collector = null;
  86         boolean foundCollector = false;
  87         for (int i = 0; i < collectors.size(); i++) {
  88             collector = collectors.get(i);
  89             String name = collector.getName();
  90             if (name.contains(collectorName)) {
  91                 System.out.println("Found collector: " + name);
  92                 foundCollector = true;
  93                 break;
  94             }
  95         }
  96         if (!foundCollector) {
  97             throw new RuntimeException(collectorName + " not found, test with -XX:+UseG1GC");
  98         }
  99 
 100         MixedGCProvoker gcProvoker = new MixedGCProvoker();
 101         gcProvoker.allocateOldObjects();
 102 
 103         // Verify no non-zero result was stored
 104         long usage = pool.getCollectionUsage().getUsed();
 105         System.out.println(poolName + ": usage after GC = " + usage);
 106         if (usage > 0) {
 107             throw new RuntimeException("Premature mixed collections(s)");
 108         }
 109 
 110         // Verify that collections were done
 111         long collectionCount = collector.getCollectionCount();
 112         System.out.println(collectorName + ": collection count = "
 113                            + collectionCount);
 114         long collectionTime = collector.getCollectionTime();
 115         System.out.println(collectorName + ": collection time  = "
 116                            + collectionTime);
 117         if (collectionCount <= 0) {
 118             throw new RuntimeException("Collection count <= 0");
 119         }
 120         if (collectionTime <= 0) {
 121             throw new RuntimeException("Collector has not run");
 122         }
 123 
 124         gcProvoker.provokeMixedGC();
 125 
 126         usage = pool.getCollectionUsage().getUsed();
 127         System.out.println(poolName + ": usage after GC = " + usage);
 128         if (usage <= 0) {
 129             throw new RuntimeException(poolName + " found with zero usage");
 130         }
 131 
 132         long newCollectionCount = collector.getCollectionCount();
 133         System.out.println(collectorName + ": collection count = "
 134                            + newCollectionCount);
 135         long newCollectionTime = collector.getCollectionTime();
 136         System.out.println(collectorName + ": collection time  = "
 137                            + newCollectionTime);
 138         if (newCollectionCount <= collectionCount) {
 139             throw new RuntimeException("No new collection");
 140         }
 141         if (newCollectionTime <= collectionTime) {
 142             throw new RuntimeException("Collector has not run some more");
 143         }
 144 
 145         System.out.println("Test passed.");
 146     }
 147 
 148     /**
 149      * Utility class to guarantee a mixed GC. The class allocates several arrays and
 150      * promotes them to the oldgen. After that it tries to provoke mixed GC by
 151      * allocating new objects.
 152      *
 153      * The necessary condition for guaranteed mixed GC is running MixedGCProvoker is
 154      * running in VM with the following flags: -XX:MaxTenuringThreshold=1 -Xms12M
 155      * -Xmx12M -XX:G1MixedGCLiveThresholdPercent=100 -XX:G1HeapWastePercent=0
 156      * -XX:G1HeapRegionSize=1m
 157      */
 158     public class MixedGCProvoker {
 159         private final WhiteBox WB = WhiteBox.getWhiteBox();
 160         private final List<byte[]> liveOldObjects = new ArrayList<>();
 161         private final List<byte[]> newObjects = new ArrayList<>();
 162 
 163         public static final int ALLOCATION_SIZE = 20000;
 164         public static final int ALLOCATION_COUNT = 15;
 165 
 166         public void allocateOldObjects() {
 167             List<byte[]> deadOldObjects = new ArrayList<>();
 168             // Allocates buffer and promotes it to the old gen. Mix live and dead old
 169             // objects
 170             for (int i = 0; i < ALLOCATION_COUNT; ++i) {
 171                 liveOldObjects.add(new byte[ALLOCATION_SIZE * 5]);
 172                 deadOldObjects.add(new byte[ALLOCATION_SIZE * 5]);
 173             }
 174 
 175             // Do two young collections, MaxTenuringThreshold=1 will force promotion.
 176             // G1HeapRegionSize=1m guarantees that old gen regions will be filled.
 177             WB.youngGC();
 178             WB.youngGC();
 179             // Check it is promoted & keep alive
 180             Asserts.assertTrue(WB.isObjectInOldGen(liveOldObjects),
 181                                "List of the objects is suppose to be in OldGen");
 182             Asserts.assertTrue(WB.isObjectInOldGen(deadOldObjects),
 183                                "List of the objects is suppose to be in OldGen");
 184         }
 185 
 186         /**
 187          * Waits until Concurent Mark Cycle finishes
 188          * @param wb  Whitebox instance
 189          * @param sleepTime sleep time
 190          */
 191         private void waitTillCMCFinished(int sleepTime) {
 192             while (WB.g1InConcurrentMark()) {
 193                 if (sleepTime > -1) {
 194                     try {
 195                         Thread.sleep(sleepTime);
 196                     } catch (InterruptedException e) {
 197                         System.out.println("Got InterruptedException while waiting for ConcMarkCycle to finish");
 198                     }
 199                 }
 200             }
 201         }
 202 
 203         public void provokeMixedGC() {
 204             waitTillCMCFinished(0);
 205             WB.g1StartConcMarkCycle();
 206             waitTillCMCFinished(0);
 207             WB.youngGC();
 208 
 209             System.out.println("Allocating new objects to provoke mixed GC");
 210             // Provoke a mixed collection. G1MixedGCLiveThresholdPercent=100
 211             // guarantees that full old gen regions will be included.
 212             for (int i = 0; i < (ALLOCATION_COUNT * 20); i++) {
 213                 try {       
 214                     newObjects.add(new byte[ALLOCATION_SIZE]);
 215                 } catch (OutOfMemoryError e) {
 216                     newObjects.clear();
 217                     WB.youngGC();
 218                     WB.youngGC();
 219                     System.out.println("OutOfMemoryError is reported, stop allocating new objects");
 220                     break; 
 221                 }
 222             }
 223             // check that liveOldObjects still alive
 224             Asserts.assertTrue(WB.isObjectInOldGen(liveOldObjects),
 225                                "List of the objects is suppose to be in OldGen");
 226         }
 227 
 228     }
 229 
 230 }