Test6581734.java 5.2 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
/*
 * Copyright (c) 2010, 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 Test6581734.java
 * @bug 6581734
 * @summary CMS Old Gen's collection usage is zero after GC which is incorrect
 * @run main/othervm -Xmx512m -verbose:gc -XX:+UseConcMarkSweepGC Test6581734
 *
 */
import java.util.*;
import java.lang.management.*;

// 6581734 states that memory pool usage via the mbean is wrong
// for CMS (zero, even after a collection).
//
// 6580448 states that the collection count similarly is wrong
// (stays at zero for CMS collections)
// -- closed as dup of 6581734 as the same fix resolves both.


public class Test6581734 {

    private String poolName = "CMS";
    private String collectorName = "ConcurrentMarkSweep";

    public static void main(String [] args) {

        Test6581734 t = null;
        if (args.length==2) {
            t = new Test6581734(args[0], args[1]);
        } else {
            System.out.println("Defaulting to monitor CMS pool and collector.");
            t = new Test6581734();
        }
        t.run();
    }

    public Test6581734(String pool, String collector) {
        poolName = pool;
        collectorName = collector;
    }

    public Test6581734() {
    }

    public void run() {
        // Use some memory, enough that we expect collections should
        // have happened.
        // Must run with options to ensure no stop the world full GC,
        // but e.g. at least one CMS cycle.
        allocationWork(300*1024*1024);
        System.out.println("Done allocationWork");

        // Verify some non-zero results are stored.
        List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();
        int poolsFound = 0;
        int poolsWithStats = 0;
        for (int i=0; i<pools.size(); i++) {
            MemoryPoolMXBean pool = pools.get(i);
            String name = pool.getName();
            System.out.println("found pool: " + name);

            if (name.contains(poolName)) {
                long usage = pool.getCollectionUsage().getUsed();
                System.out.println(name + ": usage after GC = " + usage);
                poolsFound++;
                if (usage > 0) {
                    poolsWithStats++;
                }
            }
        }
        if (poolsFound == 0) {
            throw new RuntimeException("No matching memory pools found: test with -XX:+UseConcMarkSweepGC");
        }

        List<GarbageCollectorMXBean> collectors = ManagementFactory.getGarbageCollectorMXBeans();
        int collectorsFound = 0;
        int collectorsWithTime= 0;
        for (int i=0; i<collectors.size(); i++) {
            GarbageCollectorMXBean collector = collectors.get(i);
            String name = collector.getName();
            System.out.println("found collector: " + name);
            if (name.contains(collectorName)) {
                collectorsFound++;
                System.out.println(name + ": collection count = "
                                   + collector.getCollectionCount());
                System.out.println(name + ": collection time  = "
                                   + collector.getCollectionTime());
                if (collector.getCollectionCount() <= 0) {
                    throw new RuntimeException("collection count <= 0");
                }
                if (collector.getCollectionTime() > 0) {
                    collectorsWithTime++;
                }
            }
        }
        // verify:
        if (poolsWithStats < poolsFound) {
            throw new RuntimeException("pools found with zero stats");
        }

        if (collectorsWithTime<collectorsFound) {
            throw new RuntimeException("collectors found with zero time";
        }
        System.out.println("Test passed.");
    }

    public void allocationWork(long target) {

        long sizeAllocated = 0;
        List list = new LinkedList();
        long delay = 50;
        long count = 0;

        while (sizeAllocated < target) {
            int size = 1024*1024;
            byte [] alloc = new byte[size];
            if (count % 2 == 0) {
                list.add(alloc);
                sizeAllocated+=size;
                System.out.print(".");
            }
            try { Thread.sleep(delay); } catch (InterruptedException ie) { }
            count++;
        }
    }

}