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