Mon, 06 Nov 2017 16:51:47 +0800
[Code Reorganization] remove trailing whitespace to pass jcheck test
aoqi@0 | 1 | /* |
jwilhelm@7785 | 2 | * Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. |
aoqi@0 | 8 | * |
aoqi@0 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 13 | * accompanied this code). |
aoqi@0 | 14 | * |
aoqi@0 | 15 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 18 | * |
aoqi@0 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 20 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 21 | * questions. |
aoqi@0 | 22 | */ |
aoqi@0 | 23 | |
aoqi@0 | 24 | /* |
aoqi@0 | 25 | * @test Test6581734.java |
aoqi@0 | 26 | * @bug 6581734 |
jwilhelm@7785 | 27 | * @requires vm.gc=="ConcMarkSweep" | vm.gc=="null" |
aoqi@0 | 28 | * @summary CMS Old Gen's collection usage is zero after GC which is incorrect |
aoqi@0 | 29 | * @run main/othervm -Xmx512m -verbose:gc -XX:+UseConcMarkSweepGC Test6581734 |
aoqi@0 | 30 | * |
aoqi@0 | 31 | */ |
aoqi@0 | 32 | import java.util.*; |
aoqi@0 | 33 | import java.lang.management.*; |
aoqi@0 | 34 | |
aoqi@0 | 35 | // 6581734 states that memory pool usage via the mbean is wrong |
aoqi@0 | 36 | // for CMS (zero, even after a collection). |
aoqi@0 | 37 | // |
aoqi@0 | 38 | // 6580448 states that the collection count similarly is wrong |
aoqi@0 | 39 | // (stays at zero for CMS collections) |
aoqi@0 | 40 | // -- closed as dup of 6581734 as the same fix resolves both. |
aoqi@0 | 41 | |
aoqi@0 | 42 | |
aoqi@0 | 43 | public class Test6581734 { |
aoqi@0 | 44 | |
aoqi@0 | 45 | private String poolName = "CMS"; |
aoqi@0 | 46 | private String collectorName = "ConcurrentMarkSweep"; |
aoqi@0 | 47 | |
aoqi@0 | 48 | public static void main(String [] args) { |
aoqi@0 | 49 | |
aoqi@0 | 50 | Test6581734 t = null; |
aoqi@0 | 51 | if (args.length==2) { |
aoqi@0 | 52 | t = new Test6581734(args[0], args[1]); |
aoqi@0 | 53 | } else { |
aoqi@0 | 54 | System.out.println("Defaulting to monitor CMS pool and collector."); |
aoqi@0 | 55 | t = new Test6581734(); |
aoqi@0 | 56 | } |
aoqi@0 | 57 | t.run(); |
aoqi@0 | 58 | } |
aoqi@0 | 59 | |
aoqi@0 | 60 | public Test6581734(String pool, String collector) { |
aoqi@0 | 61 | poolName = pool; |
aoqi@0 | 62 | collectorName = collector; |
aoqi@0 | 63 | } |
aoqi@0 | 64 | |
aoqi@0 | 65 | public Test6581734() { |
aoqi@0 | 66 | } |
aoqi@0 | 67 | |
aoqi@0 | 68 | public void run() { |
aoqi@0 | 69 | // Use some memory, enough that we expect collections should |
aoqi@0 | 70 | // have happened. |
aoqi@0 | 71 | // Must run with options to ensure no stop the world full GC, |
aoqi@0 | 72 | // but e.g. at least one CMS cycle. |
aoqi@0 | 73 | allocationWork(300*1024*1024); |
aoqi@0 | 74 | System.out.println("Done allocationWork"); |
aoqi@0 | 75 | |
aoqi@0 | 76 | // Verify some non-zero results are stored. |
aoqi@0 | 77 | List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans(); |
aoqi@0 | 78 | int poolsFound = 0; |
aoqi@0 | 79 | int poolsWithStats = 0; |
aoqi@0 | 80 | for (int i=0; i<pools.size(); i++) { |
aoqi@0 | 81 | MemoryPoolMXBean pool = pools.get(i); |
aoqi@0 | 82 | String name = pool.getName(); |
aoqi@0 | 83 | System.out.println("found pool: " + name); |
aoqi@0 | 84 | |
aoqi@0 | 85 | if (name.contains(poolName)) { |
aoqi@0 | 86 | long usage = pool.getCollectionUsage().getUsed(); |
aoqi@0 | 87 | System.out.println(name + ": usage after GC = " + usage); |
aoqi@0 | 88 | poolsFound++; |
aoqi@0 | 89 | if (usage > 0) { |
aoqi@0 | 90 | poolsWithStats++; |
aoqi@0 | 91 | } |
aoqi@0 | 92 | } |
aoqi@0 | 93 | } |
aoqi@0 | 94 | if (poolsFound == 0) { |
aoqi@0 | 95 | throw new RuntimeException("No matching memory pools found: test with -XX:+UseConcMarkSweepGC"); |
aoqi@0 | 96 | } |
aoqi@0 | 97 | |
aoqi@0 | 98 | List<GarbageCollectorMXBean> collectors = ManagementFactory.getGarbageCollectorMXBeans(); |
aoqi@0 | 99 | int collectorsFound = 0; |
aoqi@0 | 100 | int collectorsWithTime= 0; |
aoqi@0 | 101 | for (int i=0; i<collectors.size(); i++) { |
aoqi@0 | 102 | GarbageCollectorMXBean collector = collectors.get(i); |
aoqi@0 | 103 | String name = collector.getName(); |
aoqi@0 | 104 | System.out.println("found collector: " + name); |
aoqi@0 | 105 | if (name.contains(collectorName)) { |
aoqi@0 | 106 | collectorsFound++; |
aoqi@0 | 107 | System.out.println(name + ": collection count = " |
aoqi@0 | 108 | + collector.getCollectionCount()); |
aoqi@0 | 109 | System.out.println(name + ": collection time = " |
aoqi@0 | 110 | + collector.getCollectionTime()); |
aoqi@0 | 111 | if (collector.getCollectionCount() <= 0) { |
aoqi@0 | 112 | throw new RuntimeException("collection count <= 0"); |
aoqi@0 | 113 | } |
aoqi@0 | 114 | if (collector.getCollectionTime() > 0) { |
aoqi@0 | 115 | collectorsWithTime++; |
aoqi@0 | 116 | } |
aoqi@0 | 117 | } |
aoqi@0 | 118 | } |
aoqi@0 | 119 | // verify: |
aoqi@0 | 120 | if (poolsWithStats < poolsFound) { |
aoqi@0 | 121 | throw new RuntimeException("pools found with zero stats"); |
aoqi@0 | 122 | } |
aoqi@0 | 123 | |
aoqi@0 | 124 | if (collectorsWithTime<collectorsFound) { |
aoqi@0 | 125 | throw new RuntimeException("collectors found with zero time"); |
aoqi@0 | 126 | } |
aoqi@0 | 127 | System.out.println("Test passed."); |
aoqi@0 | 128 | } |
aoqi@0 | 129 | |
aoqi@0 | 130 | public void allocationWork(long target) { |
aoqi@0 | 131 | |
aoqi@0 | 132 | long sizeAllocated = 0; |
aoqi@0 | 133 | List list = new LinkedList(); |
aoqi@0 | 134 | long delay = 50; |
aoqi@0 | 135 | long count = 0; |
aoqi@0 | 136 | |
aoqi@0 | 137 | while (sizeAllocated < target) { |
aoqi@0 | 138 | int size = 1024*1024; |
aoqi@0 | 139 | byte [] alloc = new byte[size]; |
aoqi@0 | 140 | if (count % 2 == 0) { |
aoqi@0 | 141 | list.add(alloc); |
aoqi@0 | 142 | sizeAllocated+=size; |
aoqi@0 | 143 | System.out.print("."); |
aoqi@0 | 144 | } |
aoqi@0 | 145 | try { Thread.sleep(delay); } catch (InterruptedException ie) { } |
aoqi@0 | 146 | count++; |
aoqi@0 | 147 | } |
aoqi@0 | 148 | } |
aoqi@0 | 149 | |
aoqi@0 | 150 | } |