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