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