1 /*
  2  * Copyright (c) 2015, 2021, 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 import org.testng.annotations.DataProvider;
 25 import org.testng.annotations.Test;
 26 
 27 import java.util.regex.Pattern;
 28 
 29 import jdk.test.lib.process.OutputAnalyzer;
 30 import jdk.test.lib.dcmd.CommandExecutor;
 31 import jdk.test.lib.dcmd.JMXExecutor;
 32 
 33 /*
 34  * @test
 35  * @summary Test of diagnostic command GC.class_histogram
 36  * @library /test/lib
 37  * @modules java.base/jdk.internal.misc
 38  *          java.compiler
 39  *          java.management
 40  *          jdk.internal.jvmstat/sun.jvmstat.monitor
 41  * @run testng ClassHistogramTest
 42  */
 43 public class ClassHistogramTest {
 44     public static class TestClass {}
 45     public static TestClass[] instances = new TestClass[1024];
 46 
 47     static {
 48         for (int i = 0; i < instances.length; ++i) {
 49             instances[i] = new TestClass();
 50         }
 51     }
 52 
 53     public void run(CommandExecutor executor, String classHistogramArgs, String expactedErrMsg) {
 54         OutputAnalyzer output = executor.execute("GC.class_histogram " + classHistogramArgs);
 55         if (!expactedErrMsg.isEmpty()) {
 56             output.shouldMatch(expactedErrMsg);
 57             return;
 58         }
 59 
 60         /*
 61          * example output:
 62          *  num     #instances         #bytes  class name (module)
 63          * -------------------------------------------------------
 64          *    1:          7991         757792  [B (java.base@9-internal)
 65          *    2:          1811         217872  java.lang.Class (java.base@9-internal)
 66          *    3:          6724         215168  java.util.HashMap$Node (java.base@9-internal)
 67          *    4:          7852         188448  java.lang.String (java.base@9-internal)
 68          *    5:          1378         105040  [Ljava.util.HashMap$Node; (java.base@9-internal)
 69          *    6:          1863          95096  [Ljava.lang.Object; (java.base@9-internal)
 70 
 71          * ...
 72          */
 73 
 74         String moduleRegex = "\\(java.base(?:@\\S*)?\\)";
 75 
 76         /* Require at least one java.lang.Class */
 77         output.shouldMatch("^\\s+\\d+:\\s+\\d+\\s+\\d+\\s+java.lang.Class " + moduleRegex + "\\s*$");
 78 
 79         /* Require at least one java.lang.String */
 80         output.shouldMatch("^\\s+\\d+:\\s+\\d+\\s+\\d+\\s+java.lang.String " + moduleRegex + "\\s*$");
 81 
 82         /* Require at least one java.lang.Object */
 83         output.shouldMatch("^\\s+\\d+:\\s+\\d+\\s+\\d+\\s+java.lang.Object " + moduleRegex + "\\s*$");
 84 
 85         /* Require at exactly one TestClass[] */
 86         output.shouldMatch("^\\s+\\d+:\\s+1\\s+\\d+\\s+" +
 87             Pattern.quote(TestClass[].class.getName()) + "\\s*$");
 88 
 89         /* Require at exactly 1024 TestClass */
 90         output.shouldMatch("^\\s+\\d+:\\s+1024\\s+\\d+\\s+" +
 91             Pattern.quote(TestClass.class.getName()) + "\\s*$");
 92     }
 93 
 94     @DataProvider(name="ArgsProvider")
 95     private Object[][] getArgs() {
 96         String parallelErr = "Parallel thread number out of range";
 97         return new Object[][] {
 98                 // valid args
 99                 {"", ""},
100                 {"-parallel=0", ""},
101                 {"-parallel=1", ""},
102                 {"-parallel=2", ""},
103                 {"-parallel="+Long.MAX_VALUE, ""},
104                 {"-all=false -parallel=0", ""},
105                 {"-all=false -parallel=1", ""},
106                 {"-all=false -parallel=2", ""},
107                 {"-all=true", ""},
108                 {"-all=true -parallel=0", ""},
109                 {"-all=true -parallel=1", ""},
110                 {"-all=true -parallel=2", ""},
111                 {"-parallel=2 -all=true", ""},
112                 // invalid args
113                 {"-parallel=-1", parallelErr},
114                 {"-parallel="+Long.MIN_VALUE, parallelErr},
115                 {"-all=false -parallel=-10", parallelErr},
116                 {"-all=true -parallel=-100", parallelErr},
117         };
118     }
119 
120     @Test(dataProvider="ArgsProvider")
121     public void jmx(String args, String expactedErrMsg) {
122         run(new JMXExecutor(), args, expactedErrMsg);
123     }
124 }