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 -server -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 
  60 public Test6581734(String pool, String collector) {
  61     poolName = pool;
  62     collectorName = collector;
  63 }
  64 public Test6581734() {
  65 }
  66 
  67 public void run() {
  68 
  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 
  74         allocationWork(300*1024*1024);
  75         System.out.println("Done allocationWork");
  76 
  77         // Verify some non-zero results are stored.
  78 
  79                 List<MemoryPoolMXBean> pools = 
  80                     ManagementFactory.getMemoryPoolMXBeans();
  81                 int poolsFound = 0;
  82                 int poolsWithStats = 0;
  83                 for (int i=0; i<pools.size(); i++) {
  84                     MemoryPoolMXBean pool = pools.get(i);
  85                     String name = pool.getName();
  86                     System.out.println("found pool: " + name);
  87 
  88                     if (name.contains(poolName)) {
  89                         long usage = pool.getCollectionUsage().getUsed();
  90                         System.out.println(name + ": usage after GC = " + usage);
  91                         poolsFound++;
  92                         if (usage > 0) {
  93                             poolsWithStats++;
  94                         }
  95                     }
  96                 }       
  97                 if (poolsFound == 0) {
  98                     throw new RuntimeException(
  99                         "No matching memory pools found: test with -XX:+UseConcMarkSweepGC");
 100                 } 
 101 
 102                 List<GarbageCollectorMXBean> collectors =
 103                     ManagementFactory.getGarbageCollectorMXBeans();
 104                 int collectorsFound = 0;
 105                 int collectorsWithTime= 0;
 106                 for (int i=0; i<collectors.size(); i++) {
 107                     GarbageCollectorMXBean collector = collectors.get(i);
 108                     String name = collector.getName();
 109                     System.out.println("found collector: " + name);
 110                     if (name.contains(collectorName)) {
 111                         collectorsFound++;
 112                         
 113                         System.out.println(name + ": collection count = " 
 114                             + collector.getCollectionCount());
 115                         System.out.println(name + ": collection time  = " 
 116                             + collector.getCollectionTime());
 117                         if (collector.getCollectionCount() <= 0) {
 118                           throw new RuntimeException("collection count <= 0");
 119                         }
 120                         if (collector.getCollectionTime() > 0) {
 121                            collectorsWithTime++;
 122                         }
 123                     }
 124                 }
 125         // verify:
 126         if (poolsWithStats < poolsFound) {
 127           throw new RuntimeException("pools found with zero stats");
 128         } 
 129         
 130         if (collectorsWithTime<collectorsFound) {
 131           throw new RuntimeException("collectors found with zero time");
 132 
 133         }
 134 
 135         System.out.println("Test passed.");
 136 }
 137 
 138 
 139     public void allocationWork(long target) {
 140 
 141         long sizeAllocated = 0;
 142         List list = new LinkedList();
 143         long delay = 50;
 144         long count = 0;
 145 
 146         while (sizeAllocated < target) {
 147             int size = 1024*1024;
 148 
 149             byte [] alloc = new byte[size];
 150             if (count % 2 == 0) {
 151                 list.add(alloc);
 152                 sizeAllocated+=size;
 153                 System.out.print(".");
 154                 //System.out.println("allocated = " + sizeAllocated + " target = " + target);
 155             }
 156 
 157             try { Thread.sleep(delay); } catch (InterruptedException ie) { }
 158             count++;
 159         }
 160     }
 161 
 162 
 163 
 164 }