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