1/*
2 * Copyright (c) 2010, 2016, 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 TestMBeanCMS.java
26 * @bug 6581734
27 * @requires vm.gc.ConcMarkSweep
28 * @summary CMS Old Gen's collection usage is zero after GC which is incorrect
29 * @modules java.management
30 * @run main/othervm -Xmx512m -verbose:gc -XX:+UseConcMarkSweepGC TestMBeanCMS
31 *
32 */
33
34import java.lang.management.GarbageCollectorMXBean;
35import java.lang.management.ManagementFactory;
36import java.lang.management.MemoryPoolMXBean;
37import java.util.LinkedList;
38import java.util.List;
39
40// 6581734 states that memory pool usage via the mbean is wrong
41// for CMS (zero, even after a collection).
42//
43// 6580448 states that the collection count similarly is wrong
44// (stays at zero for CMS collections)
45// -- closed as dup of 6581734 as the same fix resolves both.
46
47
48public class TestMBeanCMS {
49
50    private String poolName = "CMS";
51    private String collectorName = "ConcurrentMarkSweep";
52
53    public static void main(String [] args) {
54
55        TestMBeanCMS t = null;
56        if (args.length==2) {
57            t = new TestMBeanCMS(args[0], args[1]);
58        } else {
59            System.out.println("Defaulting to monitor CMS pool and collector.");
60            t = new TestMBeanCMS();
61        }
62        t.run();
63    }
64
65    public TestMBeanCMS(String pool, String collector) {
66        poolName = pool;
67        collectorName = collector;
68    }
69
70    public TestMBeanCMS() {
71    }
72
73    public void run() {
74        // Use some memory, enough that we expect collections should
75        // have happened.
76        // Must run with options to ensure no stop the world full GC,
77        // but e.g. at least one CMS cycle.
78        allocationWork(300*1024*1024);
79        System.out.println("Done allocationWork");
80
81        // Verify some non-zero results are stored.
82        List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();
83        int poolsFound = 0;
84        int poolsWithStats = 0;
85        for (int i=0; i<pools.size(); i++) {
86            MemoryPoolMXBean pool = pools.get(i);
87            String name = pool.getName();
88            System.out.println("found pool: " + name);
89
90            if (name.contains(poolName)) {
91                long usage = pool.getCollectionUsage().getUsed();
92                System.out.println(name + ": usage after GC = " + usage);
93                poolsFound++;
94                if (usage > 0) {
95                    poolsWithStats++;
96                }
97            }
98        }
99        if (poolsFound == 0) {
100            throw new RuntimeException("No matching memory pools found: test with -XX:+UseConcMarkSweepGC");
101        }
102
103        List<GarbageCollectorMXBean> collectors = 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                System.out.println(name + ": collection count = "
113                                   + collector.getCollectionCount());
114                System.out.println(name + ": collection time  = "
115                                   + collector.getCollectionTime());
116                if (collector.getCollectionCount() <= 0) {
117                    throw new RuntimeException("collection count <= 0");
118                }
119                if (collector.getCollectionTime() > 0) {
120                    collectorsWithTime++;
121                }
122            }
123        }
124        // verify:
125        if (poolsWithStats < poolsFound) {
126            throw new RuntimeException("pools found with zero stats");
127        }
128
129        if (collectorsWithTime<collectorsFound) {
130            throw new RuntimeException("collectors found with zero time");
131        }
132        System.out.println("Test passed.");
133    }
134
135    public void allocationWork(long target) {
136
137        long sizeAllocated = 0;
138        List list = new LinkedList();
139        long delay = 50;
140        long count = 0;
141
142        while (sizeAllocated < target) {
143            int size = 1024*1024;
144            byte [] alloc = new byte[size];
145            if (count % 2 == 0) {
146                list.add(alloc);
147                sizeAllocated+=size;
148                System.out.print(".");
149            }
150            try { Thread.sleep(delay); } catch (InterruptedException ie) { }
151            count++;
152        }
153    }
154
155}
156