Path: blob/master/test/hotspot/jtreg/serviceability/dcmd/gc/ClassHistogramTest.java
41153 views
/*1* Copyright (c) 2015, 2021, 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*/2223import org.testng.annotations.DataProvider;24import org.testng.annotations.Test;2526import java.util.regex.Pattern;2728import jdk.test.lib.process.OutputAnalyzer;29import jdk.test.lib.dcmd.CommandExecutor;30import jdk.test.lib.dcmd.JMXExecutor;3132/*33* @test34* @summary Test of diagnostic command GC.class_histogram35* @library /test/lib36* @modules java.base/jdk.internal.misc37* java.compiler38* java.management39* jdk.internal.jvmstat/sun.jvmstat.monitor40* @run testng ClassHistogramTest41*/42public class ClassHistogramTest {43public static class TestClass {}44public static TestClass[] instances = new TestClass[1024];4546static {47for (int i = 0; i < instances.length; ++i) {48instances[i] = new TestClass();49}50}5152public void run(CommandExecutor executor, String classHistogramArgs, String expactedErrMsg) {53OutputAnalyzer output = executor.execute("GC.class_histogram " + classHistogramArgs);54if (!expactedErrMsg.isEmpty()) {55output.shouldMatch(expactedErrMsg);56return;57}5859/*60* example output:61* num #instances #bytes class name (module)62* -------------------------------------------------------63* 1: 7991 757792 [B (java.base@9-internal)64* 2: 1811 217872 java.lang.Class (java.base@9-internal)65* 3: 6724 215168 java.util.HashMap$Node (java.base@9-internal)66* 4: 7852 188448 java.lang.String (java.base@9-internal)67* 5: 1378 105040 [Ljava.util.HashMap$Node; (java.base@9-internal)68* 6: 1863 95096 [Ljava.lang.Object; (java.base@9-internal)6970* ...71*/7273String moduleRegex = "\\(java.base(?:@\\S*)?\\)";7475/* Require at least one java.lang.Class */76output.shouldMatch("^\\s+\\d+:\\s+\\d+\\s+\\d+\\s+java.lang.Class " + moduleRegex + "\\s*$");7778/* Require at least one java.lang.String */79output.shouldMatch("^\\s+\\d+:\\s+\\d+\\s+\\d+\\s+java.lang.String " + moduleRegex + "\\s*$");8081/* Require at least one java.lang.Object */82output.shouldMatch("^\\s+\\d+:\\s+\\d+\\s+\\d+\\s+java.lang.Object " + moduleRegex + "\\s*$");8384/* Require at exactly one TestClass[] */85output.shouldMatch("^\\s+\\d+:\\s+1\\s+\\d+\\s+" +86Pattern.quote(TestClass[].class.getName()) + "\\s*$");8788/* Require at exactly 1024 TestClass */89output.shouldMatch("^\\s+\\d+:\\s+1024\\s+\\d+\\s+" +90Pattern.quote(TestClass.class.getName()) + "\\s*$");91}9293@DataProvider(name="ArgsProvider")94private Object[][] getArgs() {95String parallelErr = "Parallel thread number out of range";96return new Object[][] {97// valid args98{"", ""},99{"-parallel=0", ""},100{"-parallel=1", ""},101{"-parallel=2", ""},102{"-parallel="+Long.MAX_VALUE, ""},103{"-all=false -parallel=0", ""},104{"-all=false -parallel=1", ""},105{"-all=false -parallel=2", ""},106{"-all=true", ""},107{"-all=true -parallel=0", ""},108{"-all=true -parallel=1", ""},109{"-all=true -parallel=2", ""},110{"-parallel=2 -all=true", ""},111// invalid args112{"-parallel=-1", parallelErr},113{"-parallel="+Long.MIN_VALUE, parallelErr},114{"-all=false -parallel=-10", parallelErr},115{"-all=true -parallel=-100", parallelErr},116};117}118119@Test(dataProvider="ArgsProvider")120public void jmx(String args, String expactedErrMsg) {121run(new JMXExecutor(), args, expactedErrMsg);122}123}124125126