author | ihse |
Tue, 09 May 2017 12:57:30 +0200 | |
changeset 45028 | b0ea3c0bfb81 |
parent 44423 | 306c020eb154 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
45028
b0ea3c0bfb81
8179889: Fix typographic errors in copyright headers
ihse
parents:
44423
diff
changeset
|
2 |
* Copyright (c) 2004, 2015, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 22 |
*/ |
23 |
||
24 |
/* |
|
25 |
* @test |
|
26 |
* @bug 4982301 |
|
27 |
* @summary Sanity Test for GarbageCollectorMXBean.getLastGcInfo(). |
|
28 |
* @author Mandy Chung |
|
29 |
* |
|
21944
924b78f629be
6987597: ManagementFactory.getGarbageCollectorMXBeans() returns empty list with CMS
jbachorik
parents:
5506
diff
changeset
|
30 |
* @run main/othervm -XX:-ExplicitGCInvokesConcurrent LastGCInfo |
2 | 31 |
*/ |
21944
924b78f629be
6987597: ManagementFactory.getGarbageCollectorMXBeans() returns empty list with CMS
jbachorik
parents:
5506
diff
changeset
|
32 |
// Passing "-XX:-ExplicitGCInvokesConcurrent" to force System.gc() |
924b78f629be
6987597: ManagementFactory.getGarbageCollectorMXBeans() returns empty list with CMS
jbachorik
parents:
5506
diff
changeset
|
33 |
// run on foreground when CMS is used and prevent situations when "GcInfo" |
924b78f629be
6987597: ManagementFactory.getGarbageCollectorMXBeans() returns empty list with CMS
jbachorik
parents:
5506
diff
changeset
|
34 |
// is missing even though System.gc() was successfuly processed. |
2 | 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 { |
|
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 |
||
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 |
} |