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  * @build DcmdUtil CodeCacheTest
  28  * @run main CodeCacheTest
  29  * @summary Test of diagnostic command Compiler.codecache
  30  */
  31 
  32 import java.io.BufferedReader;
  33 import java.io.StringReader;
  34 import java.lang.reflect.Method;
  35 import java.util.regex.Matcher;
  36 import java.util.regex.Pattern;
  37 
  38 public class CodeCacheTest {
  39 
  40     /**
  41      * This test calls Jcmd (diagnostic command tool) Compiler.codecache and then parses the output,
  42      * making sure that all number look ok
  43      *
  44      *
  45      * Expected output:
  46      *
  47      * CodeCache: size=245760Kb used=4680Kb max_used=4680Kb free=241079Kb
  48      * bounds [0x00007f5bd9000000, 0x00007f5bd94a0000, 0x00007f5be8000000]
  49      * total_blobs=575 nmethods=69 adapters=423
  50      * compilation: enabled
  51      */
  52 
  53     static Pattern line1 = Pattern.compile("CodeCache: size=(\\p{Digit}*)Kb used=(\\p{Digit}*)Kb max_used=(\\p{Digit}*)Kb free=(\\p{Digit}*)Kb");
  54     static Pattern line2 = Pattern.compile(" bounds \\[0x(\\p{XDigit}*), 0x(\\p{XDigit}*), 0x(\\p{XDigit}*)\\]");
  55     static Pattern line3 = Pattern.compile(" total_blobs=(\\p{Digit}*) nmethods=(\\p{Digit}*) adapters=(\\p{Digit}*)");
  56     static Pattern line4 = Pattern.compile(" compilation: (\\w*)");
  57 
  58     public static void main(String arg[]) throws Exception {
  59 
  60         // Get output from dcmd (diagnostic command)
  61         String result = DcmdUtil.executeDcmd("Compiler.codecache");
  62         BufferedReader r = new BufferedReader(new StringReader(result));
  63 
  64         // Validate first line
  65         String line;
  66         line = r.readLine();
  67         Matcher m = line1.matcher(line);
  68         if (m.matches()) {
  69             for(int i = 1; i <= 4; i++) {
  70                 int val = Integer.parseInt(m.group(i));
  71                 if (val < 0) {
  72                     throw new Exception("Failed parsing dcmd codecache output");
  73                 }
  74             }
  75         } else {
  76             throw new Exception("Regexp 1 failed");
  77         }
  78 
  79         // Validate second line
  80         line = r.readLine();
  81         m = line2.matcher(line);
  82         if (m.matches()) {
  83             String start = m.group(1);
  84             String mark  = m.group(2);
  85             String top   = m.group(3);
  86 
  87             // Lexical compare of hex numbers to check that they look sane.
  88             if (start.compareTo(mark) > 1) {
  89                 throw new Exception("Failed parsing dcmd codecache output");
  90             }
  91             if (mark.compareTo(top) > 1) {
  92                 throw new Exception("Failed parsing dcmd codecache output");
  93             }
  94         } else {
  95             throw new Exception("Regexp 2 failed line: " + line);
  96         }
  97 
  98         // Validate third line
  99         line = r.readLine();
 100         m = line3.matcher(line);
 101         if (m.matches()) {
 102             int blobs = Integer.parseInt(m.group(1));
 103             if (blobs <= 0) {
 104                 throw new Exception("Failed parsing dcmd codecache output");
 105             }
 106             int nmethods = Integer.parseInt(m.group(2));
 107             if (nmethods <= 0) {
 108                 throw new Exception("Failed parsing dcmd codecache output");
 109             }
 110             int adapters = Integer.parseInt(m.group(3));
 111             if (adapters <= 0) {
 112                 throw new Exception("Failed parsing dcmd codecache output");
 113             }
 114             if (blobs < (nmethods + adapters)) {
 115                 throw new Exception("Failed parsing dcmd codecache output");
 116             }
 117         } else {
 118             throw new Exception("Regexp 3 failed");
 119         }
 120 
 121         // Validate fourth line
 122         line = r.readLine();
 123         m = line4.matcher(line);
 124         if (m.matches()) {
 125             if (!m.group(1).equals("enabled")) {
 126                 throw new Exception("Invalid message: '" + m.group(1) + "'");
 127             }
 128         } else {
 129             throw new Exception("Regexp 4 failed");
 130         }
 131     }
 132 }