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