Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/test/gc/6581734/Test6581734.java
32284 views
/*1* Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test Test6581734.java25* @bug 658173426* @requires vm.gc=="ConcMarkSweep" | vm.gc=="null"27* @summary CMS Old Gen's collection usage is zero after GC which is incorrect28* @run main/othervm -Xmx512m -verbose:gc -XX:+UseConcMarkSweepGC Test658173429*30*/31import java.util.*;32import java.lang.management.*;3334// 6581734 states that memory pool usage via the mbean is wrong35// for CMS (zero, even after a collection).36//37// 6580448 states that the collection count similarly is wrong38// (stays at zero for CMS collections)39// -- closed as dup of 6581734 as the same fix resolves both.404142public class Test6581734 {4344private String poolName = "CMS";45private String collectorName = "ConcurrentMarkSweep";4647public static void main(String [] args) {4849Test6581734 t = null;50if (args.length==2) {51t = new Test6581734(args[0], args[1]);52} else {53System.out.println("Defaulting to monitor CMS pool and collector.");54t = new Test6581734();55}56t.run();57}5859public Test6581734(String pool, String collector) {60poolName = pool;61collectorName = collector;62}6364public Test6581734() {65}6667public void run() {68// Use some memory, enough that we expect collections should69// have happened.70// Must run with options to ensure no stop the world full GC,71// but e.g. at least one CMS cycle.72allocationWork(300*1024*1024);73System.out.println("Done allocationWork");7475// Verify some non-zero results are stored.76List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();77int poolsFound = 0;78int poolsWithStats = 0;79for (int i=0; i<pools.size(); i++) {80MemoryPoolMXBean pool = pools.get(i);81String name = pool.getName();82System.out.println("found pool: " + name);8384if (name.contains(poolName)) {85long usage = pool.getCollectionUsage().getUsed();86System.out.println(name + ": usage after GC = " + usage);87poolsFound++;88if (usage > 0) {89poolsWithStats++;90}91}92}93if (poolsFound == 0) {94throw new RuntimeException("No matching memory pools found: test with -XX:+UseConcMarkSweepGC");95}9697List<GarbageCollectorMXBean> collectors = ManagementFactory.getGarbageCollectorMXBeans();98int collectorsFound = 0;99int collectorsWithTime= 0;100for (int i=0; i<collectors.size(); i++) {101GarbageCollectorMXBean collector = collectors.get(i);102String name = collector.getName();103System.out.println("found collector: " + name);104if (name.contains(collectorName)) {105collectorsFound++;106System.out.println(name + ": collection count = "107+ collector.getCollectionCount());108System.out.println(name + ": collection time = "109+ collector.getCollectionTime());110if (collector.getCollectionCount() <= 0) {111throw new RuntimeException("collection count <= 0");112}113if (collector.getCollectionTime() > 0) {114collectorsWithTime++;115}116}117}118// verify:119if (poolsWithStats < poolsFound) {120throw new RuntimeException("pools found with zero stats");121}122123if (collectorsWithTime<collectorsFound) {124throw new RuntimeException("collectors found with zero time");125}126System.out.println("Test passed.");127}128129public void allocationWork(long target) {130131long sizeAllocated = 0;132List list = new LinkedList();133long delay = 50;134long count = 0;135136while (sizeAllocated < target) {137int size = 1024*1024;138byte [] alloc = new byte[size];139if (count % 2 == 0) {140list.add(alloc);141sizeAllocated+=size;142System.out.print(".");143}144try { Thread.sleep(delay); } catch (InterruptedException ie) { }145count++;146}147}148149}150151152