TestOldGenCollectionUsage.java 9.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
/*
 * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * @test TestOldGenCollectionUsage.java
 * @bug 8195115
 * @summary G1 Old Gen's CollectionUsage.used is zero after mixed GC which is incorrect
 * @key gc
 * @requires vm.gc=="G1" | vm.gc=="null"
 * @library /testlibrary /testlibrary/whitebox
 * @build ClassFileInstaller com.oracle.java.testlibrary.* sun.hotspot.WhiteBox TestOldGenCollectionUsage
 * @run main ClassFileInstaller sun.hotspot.WhiteBox
 *                              sun.hotspot.WhiteBox$WhiteBoxPermission
 * @run main/othervm -Xbootclasspath/a:. -XX:+UseG1GC -XX:+UnlockExperimentalVMOptions -XX:+UnlockDiagnosticVMOptions
 *                   -XX:+WhiteBoxAPI -verbose:gc -XX:SurvivorRatio=1 -Xmx14m -Xms14m -XX:MaxTenuringThreshold=1
 *                   -XX:InitiatingHeapOccupancyPercent=100 -XX:G1MixedGCCountTarget=4 -XX:MaxGCPauseMillis=30000
 *                   -XX:G1HeapRegionSize=1m -XX:G1HeapWastePercent=0 -XX:G1MixedGCLiveThresholdPercent=100
 *                   TestOldGenCollectionUsage
 */

import com.oracle.java.testlibrary.Asserts;
import sun.hotspot.WhiteBox;

import java.util.ArrayList;
import java.util.List;
import java.util.Collections;

import java.lang.management.*;

// 8195115 says that for the "G1 Old Gen" MemoryPool, CollectionUsage.used
// is zero for G1 after a mixed collection, which is incorrect.

public class TestOldGenCollectionUsage {

    private String poolName = "G1 Old Gen";
    private String collectorName = "G1 Young Generation";

    public static void main(String [] args) throws Exception {
        TestOldGenCollectionUsage t = new TestOldGenCollectionUsage();
        t.run();
    }

    public TestOldGenCollectionUsage() {
        System.out.println("Monitor G1 Old Gen pool with G1 Young Generation collector.");
    }

    public void run() {
        // Find memory pool and collector
        List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();
        MemoryPoolMXBean pool = null;
        boolean foundPool = false;
        for (int i = 0; i < pools.size(); i++) {
            pool = pools.get(i);
            String name = pool.getName();
            if (name.contains(poolName)) {
                System.out.println("Found pool: " + name);
                foundPool = true;
                break;
            }
        }
        if (!foundPool) {
            throw new RuntimeException(poolName + " not found, test with -XX:+UseG1GC");
        }

        List<GarbageCollectorMXBean> collectors = ManagementFactory.getGarbageCollectorMXBeans();
        GarbageCollectorMXBean collector = null;
        boolean foundCollector = false;
        for (int i = 0; i < collectors.size(); i++) {
            collector = collectors.get(i);
            String name = collector.getName();
            if (name.contains(collectorName)) {
                System.out.println("Found collector: " + name);
                foundCollector = true;
                break;
            }
        }
        if (!foundCollector) {
            throw new RuntimeException(collectorName + " not found, test with -XX:+UseG1GC");
        }

        MixedGCProvoker gcProvoker = new MixedGCProvoker();
        gcProvoker.allocateOldObjects();

        // Verify no non-zero result was stored
        long usage = pool.getCollectionUsage().getUsed();
        System.out.println(poolName + ": usage after GC = " + usage);
        if (usage > 0) {
            throw new RuntimeException("Premature mixed collections(s)");
        }

        // Verify that collections were done
        long collectionCount = collector.getCollectionCount();
        System.out.println(collectorName + ": collection count = "
                           + collectionCount);
        long collectionTime = collector.getCollectionTime();
        System.out.println(collectorName + ": collection time  = "
                           + collectionTime);
        if (collectionCount <= 0) {
            throw new RuntimeException("Collection count <= 0");
        }
        if (collectionTime <= 0) {
            throw new RuntimeException("Collector has not run");
        }

        gcProvoker.provokeMixedGC();

        usage = pool.getCollectionUsage().getUsed();
        System.out.println(poolName + ": usage after GC = " + usage);
        if (usage <= 0) {
            throw new RuntimeException(poolName + " found with zero usage");
        }

        long newCollectionCount = collector.getCollectionCount();
        System.out.println(collectorName + ": collection count = "
                           + newCollectionCount);
        long newCollectionTime = collector.getCollectionTime();
        System.out.println(collectorName + ": collection time  = "
                           + newCollectionTime);
        if (newCollectionCount <= collectionCount) {
            throw new RuntimeException("No new collection");
        }
        if (newCollectionTime <= collectionTime) {
            throw new RuntimeException("Collector has not run some more");
        }

        System.out.println("Test passed.");
    }

    /**
     * Utility class to guarantee a mixed GC. The class allocates several arrays and
     * promotes them to the oldgen. After that it tries to provoke mixed GC by
     * allocating new objects.
     *
     * The necessary condition for guaranteed mixed GC is running MixedGCProvoker is
     * running in VM with the following flags: -XX:MaxTenuringThreshold=1 -Xms12M
     * -Xmx12M -XX:G1MixedGCLiveThresholdPercent=100 -XX:G1HeapWastePercent=0
     * -XX:G1HeapRegionSize=1m
     */
    public class MixedGCProvoker {
        private final WhiteBox WB = WhiteBox.getWhiteBox();
        private final List<byte[]> liveOldObjects = new ArrayList<>();
        private final List<byte[]> newObjects = new ArrayList<>();

        public static final int ALLOCATION_SIZE = 20000;
        public static final int ALLOCATION_COUNT = 15;

        public void allocateOldObjects() {
            List<byte[]> deadOldObjects = new ArrayList<>();
            // Allocates buffer and promotes it to the old gen. Mix live and dead old
            // objects
            for (int i = 0; i < ALLOCATION_COUNT; ++i) {
                liveOldObjects.add(new byte[ALLOCATION_SIZE * 5]);
                deadOldObjects.add(new byte[ALLOCATION_SIZE * 5]);
            }

            // Do two young collections, MaxTenuringThreshold=1 will force promotion.
            // G1HeapRegionSize=1m guarantees that old gen regions will be filled.
            WB.youngGC();
            WB.youngGC();
            // Check it is promoted & keep alive
            Asserts.assertTrue(WB.isObjectInOldGen(liveOldObjects),
                               "List of the objects is suppose to be in OldGen");
            Asserts.assertTrue(WB.isObjectInOldGen(deadOldObjects),
                               "List of the objects is suppose to be in OldGen");
        }

        /**
         * Waits until Concurent Mark Cycle finishes
         * @param wb  Whitebox instance
         * @param sleepTime sleep time
         */
        private void waitTillCMCFinished(int sleepTime) {
            while (WB.g1InConcurrentMark()) {
                if (sleepTime > -1) {
                    try {
                        Thread.sleep(sleepTime);
                    } catch (InterruptedException e) {
                        System.out.println("Got InterruptedException while waiting for ConcMarkCycle to finish");
                    }
                }
            }
        }

        public void provokeMixedGC() {
            waitTillCMCFinished(0);
            WB.g1StartConcMarkCycle();
            waitTillCMCFinished(0);
            WB.youngGC();

            System.out.println("Allocating new objects to provoke mixed GC");
            // Provoke a mixed collection. G1MixedGCLiveThresholdPercent=100
            // guarantees that full old gen regions will be included.
            for (int i = 0; i < (ALLOCATION_COUNT * 20); i++) {
                try {
                    newObjects.add(new byte[ALLOCATION_SIZE]);
                } catch (OutOfMemoryError e) {
                    newObjects.clear();
                    WB.youngGC();
                    WB.youngGC();
                    System.out.println("OutOfMemoryError is reported, stop allocating new objects");
                    break;
                }
            }
            // check that liveOldObjects still alive
            Asserts.assertTrue(WB.isObjectInOldGen(liveOldObjects),
                               "List of the objects is suppose to be in OldGen");
        }

    }

}