1 /*
   2  * Copyright (c) 2014, 2015, 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 CompilerQueueTest
  26  * @bug 8054889
  27  * @library /testlibrary
  28  * @modules java.base/sun.misc
  29  *          java.compiler
  30  *          java.management
  31  *          jdk.jvmstat/sun.jvmstat.monitor
  32  * @ignore 8069160
  33  * @build jdk.test.lib.*
  34  * @build jdk.test.lib.dcmd.*
  35  * @run testng CompilerQueueTest
  36  * @run testng/othervm -XX:-TieredCompilation CompilerQueueTest
  37  * @run testng/othervm -Xint CompilerQueueTest
  38  * @summary Test of diagnostic command Compiler.queue
  39  */
  40 
  41 import jdk.test.lib.OutputAnalyzer;
  42 import jdk.test.lib.dcmd.CommandExecutor;
  43 import jdk.test.lib.dcmd.JMXExecutor;
  44 import org.testng.annotations.Test;
  45 import org.testng.Assert;
  46 
  47 import java.util.Iterator;
  48 
  49 public class CompilerQueueTest {
  50 
  51     /**
  52      * This test calls Jcmd (diagnostic command tool) Compiler.queue and
  53      * then parses the output, making sure that the output look ok.
  54      *
  55      *
  56      * Output example:
  57      *
  58      * Contents of C1 compile queue
  59      * ----------------------------
  60      * 73       3       java.lang.AbstractStringBuilder::append (50 bytes)
  61      * 74       1       java.util.TreeMap::size (5 bytes)
  62      * 75       3       java.lang.StringBuilder::append (8 bytes)
  63      * 83       3       java.util.TreeMap$ValueIterator::next (8 bytes)
  64      * 84       1       javax.management.MBeanFeatureInfo::getName (5 bytes)
  65      * ----------------------------
  66      * Contents of C2 compile queue
  67      * ----------------------------
  68      * Empty
  69      * ----------------------------
  70      *
  71      **/
  72 
  73     public void run(CommandExecutor executor) {
  74 
  75         // Get output from dcmd (diagnostic command)
  76         OutputAnalyzer output = executor.execute("Compiler.queue");
  77         Iterator<String> lines = output.asLines().iterator();
  78 
  79         while (lines.hasNext()) {
  80             String str = lines.next();
  81             if (str.startsWith("Contents of C")) {
  82                 match(lines.next(), "----------------------------");
  83                 str = lines.next();
  84                 if (!str.equals("Empty")) {
  85                     while (str.charAt(0) != '-') {
  86                         validateMethodLine(str);
  87                         str = lines.next();
  88                     }
  89                 } else {
  90                     str = lines.next();
  91                 }
  92                 match(str,"----------------------------");
  93             } else {
  94                 Assert.fail("Failed parsing dcmd queue, line: " + str);
  95             }
  96         }
  97     }
  98 
  99     private static void validateMethodLine(String str) {
 100         // Skip until package/class name begins. Trim to remove whitespace that
 101         // may differ.
 102         String name = str.substring(14).trim();
 103         int sep = name.indexOf("::");
 104         if (sep == -1) {
 105             Assert.fail("Failed dcmd queue, didn't find separator :: in: " + name);
 106         }
 107         try {
 108             Class.forName(name.substring(0, sep));
 109         } catch (ClassNotFoundException e) {
 110             Assert.fail("Failed dcmd queue, Class for name: " + str);
 111         }
 112     }
 113 
 114     public static void match(String line, String str) {
 115         if (!line.equals(str)) {
 116             Assert.fail("String equals: " + line + ", " + str);
 117         }
 118     }
 119 
 120     @Test
 121     public void jmx() {
 122         run(new JMXExecutor());
 123     }
 124 }