0N/A/*
3619N/A * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
0N/A */
0N/A
0N/A/*
1879N/A * @test Test6581734.java
1879N/A * @bug 6581734
1879N/A * @summary CMS Old Gen's collection usage is zero after GC which is incorrect
1879N/A * @run main/othervm -Xmx512m -verbose:gc -XX:+UseConcMarkSweepGC Test6581734
1879N/A *
1879N/A */
1879N/Aimport java.util.*;
1879N/Aimport java.lang.management.*;
1879N/A
1879N/A// 6581734 states that memory pool usage via the mbean is wrong
1879N/A// for CMS (zero, even after a collection).
1879N/A//
1879N/A// 6580448 states that the collection count similarly is wrong
1879N/A// (stays at zero for CMS collections)
1879N/A// -- closed as dup of 6581734 as the same fix resolves both.
1879N/A
1879N/A
1879N/Apublic class Test6581734 {
1879N/A
1879N/A private String poolName = "CMS";
1879N/A private String collectorName = "ConcurrentMarkSweep";
1879N/A
1879N/A public static void main(String [] args) {
1879N/A
1879N/A Test6581734 t = null;
1879N/A if (args.length==2) {
1879N/A t = new Test6581734(args[0], args[1]);
1879N/A } else {
1879N/A System.out.println("Defaulting to monitor CMS pool and collector.");
1879N/A t = new Test6581734();
1879N/A }
1879N/A t.run();
1879N/A }
1879N/A
1879N/A public Test6581734(String pool, String collector) {
1879N/A poolName = pool;
1879N/A collectorName = collector;
1879N/A }
1879N/A
4141N/A public Test6581734() {
1879N/A }
1879N/A
1879N/A public void run() {
1879N/A // Use some memory, enough that we expect collections should
1879N/A // have happened.
1879N/A // Must run with options to ensure no stop the world full GC,
1879N/A // but e.g. at least one CMS cycle.
1879N/A allocationWork(300*1024*1024);
1879N/A System.out.println("Done allocationWork");
1879N/A
1879N/A // Verify some non-zero results are stored.
1879N/A List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();
1879N/A int poolsFound = 0;
2073N/A int poolsWithStats = 0;
2073N/A for (int i=0; i<pools.size(); i++) {
2073N/A MemoryPoolMXBean pool = pools.get(i);
2073N/A String name = pool.getName();
2073N/A System.out.println("found pool: " + name);
2073N/A
0N/A if (name.contains(poolName)) {
1915N/A long usage = pool.getCollectionUsage().getUsed();
1915N/A System.out.println(name + ": usage after GC = " + usage);
1915N/A poolsFound++;
1915N/A if (usage > 0) {
1915N/A poolsWithStats++;
1915N/A }
1915N/A }
1915N/A }
1915N/A if (poolsFound == 0) {
1915N/A throw new RuntimeException("No matching memory pools found: test with -XX:+UseConcMarkSweepGC");
1915N/A }
1915N/A
0N/A List<GarbageCollectorMXBean> collectors = ManagementFactory.getGarbageCollectorMXBeans();
0N/A int collectorsFound = 0;
0N/A int collectorsWithTime= 0;
0N/A for (int i=0; i<collectors.size(); i++) {
0N/A GarbageCollectorMXBean collector = collectors.get(i);
0N/A String name = collector.getName();
0N/A System.out.println("found collector: " + name);
0N/A if (name.contains(collectorName)) {
0N/A collectorsFound++;
0N/A System.out.println(name + ": collection count = "
0N/A + collector.getCollectionCount());
0N/A System.out.println(name + ": collection time = "
0N/A + collector.getCollectionTime());
0N/A if (collector.getCollectionCount() <= 0) {
0N/A throw new RuntimeException("collection count <= 0");
0N/A }
0N/A if (collector.getCollectionTime() > 0) {
0N/A collectorsWithTime++;
0N/A }
0N/A }
0N/A }
0N/A // verify:
0N/A if (poolsWithStats < poolsFound) {
0N/A throw new RuntimeException("pools found with zero stats");
0N/A }
0N/A
0N/A if (collectorsWithTime<collectorsFound) {
0N/A throw new RuntimeException("collectors found with zero time");
0N/A }
0N/A System.out.println("Test passed.");
0N/A }
0N/A
0N/A public void allocationWork(long target) {
0N/A
0N/A long sizeAllocated = 0;
0N/A List list = new LinkedList();
0N/A long delay = 50;
0N/A long count = 0;
0N/A
0N/A while (sizeAllocated < target) {
0N/A int size = 1024*1024;
4132N/A byte [] alloc = new byte[size];
0N/A if (count % 2 == 0) {
0N/A list.add(alloc);
0N/A sizeAllocated+=size;
0N/A System.out.print(".");
0N/A }
0N/A try { Thread.sleep(delay); } catch (InterruptedException ie) { }
0N/A count++;
0N/A }
0N/A }
0N/A
0N/A}
0N/A