1 /* 2 * Copyright (c) 2010, 2015, 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 Test6581734.java 26 * @bug 6581734 27 * @requires vm.gc=="ConcMarkSweep" | vm.gc=="null" 28 * @summary CMS Old Gen's collection usage is zero after GC which is incorrect 29 * @modules java.management 30 * @run main/othervm -Xmx512m -verbose:gc -XX:+UseConcMarkSweepGC Test6581734 31 * 32 */ 33 import java.util.*; 34 import java.lang.management.*; 35 36 // 6581734 states that memory pool usage via the mbean is wrong 37 // for CMS (zero, even after a collection). 38 // 39 // 6580448 states that the collection count similarly is wrong 40 // (stays at zero for CMS collections) 41 // -- closed as dup of 6581734 as the same fix resolves both. 42 43 44 public class Test6581734 { 45 46 private String poolName = "CMS"; 47 private String collectorName = "ConcurrentMarkSweep"; 48 49 public static void main(String [] args) { 50 51 Test6581734 t = null; 52 if (args.length==2) { 53 t = new Test6581734(args[0], args[1]); 54 } else { 55 System.out.println("Defaulting to monitor CMS pool and collector."); 56 t = new Test6581734(); 57 } 58 t.run(); 59 } 60 61 public Test6581734(String pool, String collector) { 62 poolName = pool; 63 collectorName = collector; 64 } 65 66 public Test6581734() { 67 } 68 69 public void run() { 70 // Use some memory, enough that we expect collections should 71 // have happened. 72 // Must run with options to ensure no stop the world full GC, 73 // but e.g. at least one CMS cycle. 74 allocationWork(300*1024*1024); 75 System.out.println("Done allocationWork"); 76 77 // Verify some non-zero results are stored. 78 List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans(); 79 int poolsFound = 0; 80 int poolsWithStats = 0; 81 for (int i=0; i<pools.size(); i++) { 82 MemoryPoolMXBean pool = pools.get(i); 83 String name = pool.getName(); 84 System.out.println("found pool: " + name); 85 86 if (name.contains(poolName)) { 87 long usage = pool.getCollectionUsage().getUsed(); 88 System.out.println(name + ": usage after GC = " + usage); 89 poolsFound++; 90 if (usage > 0) { 91 poolsWithStats++; 92 } 93 } 94 } 95 if (poolsFound == 0) { 96 throw new RuntimeException("No matching memory pools found: test with -XX:+UseConcMarkSweepGC"); 97 } 98 99 List<GarbageCollectorMXBean> collectors = ManagementFactory.getGarbageCollectorMXBeans(); 100 int collectorsFound = 0; 101 int collectorsWithTime= 0; 102 for (int i=0; i<collectors.size(); i++) { 103 GarbageCollectorMXBean collector = collectors.get(i); 104 String name = collector.getName(); 105 System.out.println("found collector: " + name); 106 if (name.contains(collectorName)) { 107 collectorsFound++; 108 System.out.println(name + ": collection count = " 109 + collector.getCollectionCount()); 110 System.out.println(name + ": collection time = " 111 + collector.getCollectionTime()); 112 if (collector.getCollectionCount() <= 0) { 113 throw new RuntimeException("collection count <= 0"); 114 } 115 if (collector.getCollectionTime() > 0) { 116 collectorsWithTime++; 117 } 118 } 119 } 120 // verify: 121 if (poolsWithStats < poolsFound) { 122 throw new RuntimeException("pools found with zero stats"); 123 } 124 125 if (collectorsWithTime<collectorsFound) { 126 throw new RuntimeException("collectors found with zero time"); 127 } 128 System.out.println("Test passed."); 129 } 130 131 public void allocationWork(long target) { 132 133 long sizeAllocated = 0; 134 List list = new LinkedList(); 135 long delay = 50; 136 long count = 0; 137 138 while (sizeAllocated < target) { 139 int size = 1024*1024; 140 byte [] alloc = new byte[size]; 141 if (count % 2 == 0) { 142 list.add(alloc); 143 sizeAllocated+=size; 144 System.out.print("."); 145 } 146 try { Thread.sleep(delay); } catch (InterruptedException ie) { } 147 count++; 148 } 149 } 150 151 }