1 /* 2 * Copyright (c) 2004, 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 26 * @bug 4982301 27 * @summary Sanity Test for GarbageCollectorMXBean.getLastGcInfo(). 28 * @author Mandy Chung 29 * 30 * @run main/othervm -XX:-ExplicitGCInvokesConcurrent LastGCInfo 31 */ 32 // Passing "-XX:-ExplicitGCInvokesConcurrent" to force System.gc() 33 // run on foreground when CMS is used and prevent situations when "GcInfo" 34 // is missing even though System.gc() was successfuly processed. 35 36 import java.lang.management.ManagementFactory; 37 import java.lang.management.MemoryUsage; 38 import java.lang.management.MemoryPoolMXBean; 39 import java.util.*; 40 import com.sun.management.GcInfo; 41 import com.sun.management.GarbageCollectorMXBean; 42 43 public class LastGCInfo { main(String[] argv)44 public static void main(String[] argv) throws Exception { 45 boolean hasGcInfo = false; 46 47 System.gc(); 48 List mgrs = ManagementFactory.getGarbageCollectorMXBeans(); 49 for (ListIterator iter = mgrs.listIterator(); iter.hasNext(); ) { 50 Object mgr = iter.next(); 51 if (mgr instanceof GarbageCollectorMXBean) { 52 GarbageCollectorMXBean gc = (GarbageCollectorMXBean) mgr; 53 GcInfo info = gc.getLastGcInfo(); 54 if (info != null) { 55 checkGcInfo(gc.getName(), info); 56 hasGcInfo = true; 57 } 58 } 59 } 60 61 if (! hasGcInfo) { 62 throw new RuntimeException("No GcInfo returned"); 63 } 64 System.out.println("Test passed."); 65 } 66 checkGcInfo(String name, GcInfo info)67 private static void checkGcInfo(String name, GcInfo info) throws Exception { 68 System.out.println("GC statistic for : " + name); 69 System.out.print("GC #" + info.getId()); 70 System.out.print(" start:" + info.getStartTime()); 71 System.out.print(" end:" + info.getEndTime()); 72 System.out.println(" (" + info.getDuration() + "ms)"); 73 Map usage = info.getMemoryUsageBeforeGc(); 74 75 List pnames = new ArrayList(); 76 for (Iterator iter = usage.entrySet().iterator(); iter.hasNext(); ) { 77 Map.Entry entry = (Map.Entry) iter.next(); 78 String poolname = (String) entry.getKey(); 79 pnames.add(poolname); 80 MemoryUsage busage = (MemoryUsage) entry.getValue(); 81 MemoryUsage ausage = (MemoryUsage) info.getMemoryUsageAfterGc().get(poolname); 82 if (ausage == null) { 83 throw new RuntimeException("After Gc Memory does not exist" + 84 " for " + poolname); 85 } 86 System.out.println("Usage for pool " + poolname); 87 System.out.println(" Before GC: " + busage); 88 System.out.println(" After GC: " + ausage); 89 } 90 91 // check if memory usage for all memory pools are returned 92 List pools = ManagementFactory.getMemoryPoolMXBeans(); 93 for (Iterator iter = pools.iterator(); iter.hasNext(); ) { 94 MemoryPoolMXBean p = (MemoryPoolMXBean) iter.next(); 95 if (!pnames.contains(p.getName())) { 96 throw new RuntimeException("GcInfo does not contain " + 97 "memory usage for pool " + p.getName()); 98 } 99 } 100 } 101 } 102