J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2004 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * @test |
| 26 | * @bug 4982301 |
| 27 | * @summary Sanity Test for GarbageCollectorMXBean.getLastGcInfo(). |
| 28 | * @author Mandy Chung |
| 29 | * |
| 30 | * @run main LastGCInfo |
| 31 | */ |
| 32 | |
| 33 | import java.lang.management.ManagementFactory; |
| 34 | import java.lang.management.MemoryUsage; |
| 35 | import java.lang.management.MemoryPoolMXBean; |
| 36 | import java.util.*; |
| 37 | import com.sun.management.GcInfo; |
| 38 | import com.sun.management.GarbageCollectorMXBean; |
| 39 | |
| 40 | public class LastGCInfo { |
| 41 | public static void main(String[] argv) throws Exception { |
| 42 | boolean hasGcInfo = false; |
| 43 | |
| 44 | System.gc(); |
| 45 | List mgrs = ManagementFactory.getGarbageCollectorMXBeans(); |
| 46 | for (ListIterator iter = mgrs.listIterator(); iter.hasNext(); ) { |
| 47 | Object mgr = iter.next(); |
| 48 | if (mgr instanceof GarbageCollectorMXBean) { |
| 49 | GarbageCollectorMXBean gc = (GarbageCollectorMXBean) mgr; |
| 50 | GcInfo info = gc.getLastGcInfo(); |
| 51 | if (info != null) { |
| 52 | checkGcInfo(gc.getName(), info); |
| 53 | hasGcInfo = true; |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | if (! hasGcInfo) { |
| 59 | throw new RuntimeException("No GcInfo returned"); |
| 60 | } |
| 61 | System.out.println("Test passed."); |
| 62 | } |
| 63 | |
| 64 | private static void checkGcInfo(String name, GcInfo info) throws Exception { |
| 65 | System.out.println("GC statistic for : " + name); |
| 66 | System.out.print("GC #" + info.getId()); |
| 67 | System.out.print(" start:" + info.getStartTime()); |
| 68 | System.out.print(" end:" + info.getEndTime()); |
| 69 | System.out.println(" (" + info.getDuration() + "ms)"); |
| 70 | Map usage = info.getMemoryUsageBeforeGc(); |
| 71 | |
| 72 | List pnames = new ArrayList(); |
| 73 | for (Iterator iter = usage.entrySet().iterator(); iter.hasNext(); ) { |
| 74 | Map.Entry entry = (Map.Entry) iter.next(); |
| 75 | String poolname = (String) entry.getKey(); |
| 76 | pnames.add(poolname); |
| 77 | MemoryUsage busage = (MemoryUsage) entry.getValue(); |
| 78 | MemoryUsage ausage = (MemoryUsage) info.getMemoryUsageAfterGc().get(poolname); |
| 79 | if (ausage == null) { |
| 80 | throw new RuntimeException("After Gc Memory does not exist" + |
| 81 | " for " + poolname); |
| 82 | } |
| 83 | System.out.println("Usage for pool " + poolname); |
| 84 | System.out.println(" Before GC: " + busage); |
| 85 | System.out.println(" After GC: " + ausage); |
| 86 | } |
| 87 | |
| 88 | // check if memory usage for all memory pools are returned |
| 89 | List pools = ManagementFactory.getMemoryPoolMXBeans(); |
| 90 | for (Iterator iter = pools.iterator(); iter.hasNext(); ) { |
| 91 | MemoryPoolMXBean p = (MemoryPoolMXBean) iter.next(); |
| 92 | if (!pnames.contains(p.getName())) { |
| 93 | throw new RuntimeException("GcInfo does not contain " + |
| 94 | "memory usage for pool " + p.getName()); |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | } |