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 CodeCacheTest
  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  * @build com.oracle.java.testlibrary.*
  33  * @build com.oracle.java.testlibrary.dcmd.*
  34  * @run testng/othervm -XX:+SegmentedCodeCache CodeCacheTest
  35  * @run testng/othervm -XX:-SegmentedCodeCache CodeCacheTest
  36  * @run testng/othervm -Xint -XX:+SegmentedCodeCache CodeCacheTest
  37  * @summary Test of diagnostic command Compiler.codecache
  38  */
  39 
  40 import org.testng.annotations.Test;
  41 import org.testng.Assert;
  42 
  43 import com.oracle.java.testlibrary.OutputAnalyzer;
  44 import com.oracle.java.testlibrary.dcmd.CommandExecutor;
  45 import com.oracle.java.testlibrary.dcmd.JMXExecutor;
  46 
  47 import java.util.Iterator;
  48 import java.util.regex.Matcher;
  49 import java.util.regex.Pattern;
  50 
  51 public class CodeCacheTest {
  52 
  53     /**
  54      * This test calls Jcmd (diagnostic command tool) Compiler.codecache and then parses the output,
  55      * making sure that all numbers look ok
  56      *
  57      *
  58      * Expected output without code cache segmentation:
  59      *
  60      * CodeCache: size=245760Kb used=4680Kb max_used=4680Kb free=241079Kb
  61      * bounds [0x00007f5bd9000000, 0x00007f5bd94a0000, 0x00007f5be8000000]
  62      * total_blobs=575 nmethods=69 adapters=423
  63      * compilation: enabled
  64      *
  65      * Expected output with code cache segmentation (number of segments may change):
  66      *
  67      * CodeHeap 'non-nmethods': size=5696Kb used=2236Kb max_used=2238Kb free=3459Kb
  68      *  bounds [0x00007fa0f0ffe000, 0x00007fa0f126e000, 0x00007fa0f158e000]
  69      * CodeHeap 'profiled nmethods': size=120036Kb used=8Kb max_used=8Kb free=120027Kb
  70      *  bounds [0x00007fa0f158e000, 0x00007fa0f17fe000, 0x00007fa0f8ac7000]
  71      * CodeHeap 'non-profiled nmethods': size=120036Kb used=2Kb max_used=2Kb free=120034Kb
  72      *  bounds [0x00007fa0f8ac7000, 0x00007fa0f8d37000, 0x00007fa100000000]
  73      * total_blobs=486 nmethods=8 adapters=399
  74      * compilation: enabled
  75      */
  76 
  77     static Pattern line1 = Pattern.compile("(CodeCache|CodeHeap.*): size=(\\p{Digit}*)Kb used=(\\p{Digit}*)Kb max_used=(\\p{Digit}*)Kb free=(\\p{Digit}*)Kb");
  78     static Pattern line2 = Pattern.compile(" bounds \\[0x(\\p{XDigit}*), 0x(\\p{XDigit}*), 0x(\\p{XDigit}*)\\]");
  79     static Pattern line3 = Pattern.compile(" total_blobs=(\\p{Digit}*) nmethods=(\\p{Digit}*) adapters=(\\p{Digit}*)");
  80     static Pattern line4 = Pattern.compile(" compilation: (.*)");
  81 
  82     private static boolean getFlagBool(String flag, String where) {
  83       Matcher m = Pattern.compile(flag + "\\s+:?= (true|false)").matcher(where);
  84       if (!m.find()) {
  85         Assert.fail("Could not find value for flag " + flag + " in output string");
  86       }
  87       return m.group(1).equals("true");
  88     }
  89 
  90     private static int getFlagInt(String flag, String where) {
  91       Matcher m = Pattern.compile(flag + "\\s+:?=\\s+\\d+").matcher(where);
  92       if (!m.find()) {
  93         Assert.fail("Could not find value for flag " + flag + " in output string");
  94       }
  95       String match = m.group();
  96       return Integer.parseInt(match.substring(match.lastIndexOf(" ") + 1, match.length()));
  97     }
  98 
  99     public void run(CommandExecutor executor) {
 100         // Get number of code cache segments
 101         int segmentsCount = 0;
 102         String flags = executor.execute("VM.flags -all").getOutput();
 103         if (!getFlagBool("SegmentedCodeCache", flags) || !getFlagBool("UseCompiler", flags)) {
 104           // No segmentation
 105           segmentsCount = 1;
 106         } else if (getFlagBool("TieredCompilation", flags) && getFlagInt("TieredStopAtLevel", flags) > 1) {
 107           // Tiered compilation: use all segments
 108           segmentsCount = 3;
 109         } else {
 110           // No TieredCompilation: only non-nmethod and non-profiled segment
 111           segmentsCount = 2;
 112         }
 113 
 114         // Get output from dcmd (diagnostic command)
 115         OutputAnalyzer output = executor.execute("Compiler.codecache");
 116         Iterator<String> lines = output.asLines().iterator();
 117 
 118         // Validate code cache segments
 119         String line;
 120         Matcher m;
 121         for (int s = 0; s < segmentsCount; ++s) {
 122           // Validate first line
 123           line = lines.next();
 124           m = line1.matcher(line);
 125           if (m.matches()) {
 126               for (int i = 2; i <= 5; i++) {
 127                   int val = Integer.parseInt(m.group(i));
 128                   if (val < 0) {
 129                       Assert.fail("Failed parsing dcmd codecache output");
 130                   }
 131               }
 132           } else {
 133               Assert.fail("Regexp 1 failed to match line: " + line);
 134           }
 135 
 136           // Validate second line
 137           line = lines.next();
 138           m = line2.matcher(line);
 139           if (m.matches()) {
 140               String start = m.group(1);
 141               String mark  = m.group(2);
 142               String top   = m.group(3);
 143 
 144               // Lexical compare of hex numbers to check that they look sane.
 145               if (start.compareTo(mark) > 1) {
 146                   Assert.fail("Failed parsing dcmd codecache output");
 147               }
 148               if (mark.compareTo(top) > 1) {
 149                   Assert.fail("Failed parsing dcmd codecache output");
 150               }
 151           } else {
 152               Assert.fail("Regexp 2 failed to match line: " + line);
 153           }
 154         }
 155 
 156         // Validate third line
 157         line = lines.next();
 158         m = line3.matcher(line);
 159         if (m.matches()) {
 160             int blobs = Integer.parseInt(m.group(1));
 161             if (blobs <= 0) {
 162                 Assert.fail("Failed parsing dcmd codecache output");
 163             }
 164             int nmethods = Integer.parseInt(m.group(2));
 165             if (nmethods < 0) {
 166                 Assert.fail("Failed parsing dcmd codecache output");
 167             }
 168             int adapters = Integer.parseInt(m.group(3));
 169             if (adapters <= 0) {
 170                 Assert.fail("Failed parsing dcmd codecache output");
 171             }
 172             if (blobs < (nmethods + adapters)) {
 173                 Assert.fail("Failed parsing dcmd codecache output");
 174             }
 175         } else {
 176             Assert.fail("Regexp 3 failed to match line: " + line);
 177         }
 178 
 179         // Validate fourth line
 180         line = lines.next();
 181         m = line4.matcher(line);
 182         if (!m.matches()) {
 183             Assert.fail("Regexp 4 failed to match line: " + line);
 184         }
 185     }
 186 
 187     @Test
 188     public void jmx() {
 189         run(new JMXExecutor());
 190     }
 191 }