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