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             long start = Long.parseLong(m.group(1), 16);
  84             if (start < 0) {
  85                 throw new Exception("Failed parsing dcmd codecache output");
  86             }
  87             long mark = Long.parseLong(m.group(2), 16);
  88             if (mark < 0) {
  89                 throw new Exception("Failed parsing dcmd codecache output");
  90             }
  91             long top = Long.parseLong(m.group(3), 16);
  92             if (top < 0) {
  93                 throw new Exception("Failed parsing dcmd codecache output");
  94             }
  95             if (start > mark) {
  96                 throw new Exception("Failed parsing dcmd codecache output");
  97             }
  98             if (mark > top) {
  99                 throw new Exception("Failed parsing dcmd codecache output");
 100             }
 101         } else {
 102             throw new Exception("Regexp 2 failed line: " + line);
 103         }
 104 
 105         // Validate third line
 106         line = r.readLine();
 107         m = line3.matcher(line);
 108         if (m.matches()) {
 109             int blobs = Integer.parseInt(m.group(1));
 110             if (blobs <= 0) {
 111                 throw new Exception("Failed parsing dcmd codecache output");
 112             }
 113             int nmethods = Integer.parseInt(m.group(2));
 114             if (nmethods <= 0) {
 115                 throw new Exception("Failed parsing dcmd codecache output");
 116             }
 117             int adapters = Integer.parseInt(m.group(3));
 118             if (adapters <= 0) {
 119                 throw new Exception("Failed parsing dcmd codecache output");
 120             }
 121             if (blobs < (nmethods + adapters)) {
 122                 throw new Exception("Failed parsing dcmd codecache output");
 123             }
 124         } else {
 125             throw new Exception("Regexp 3 failed");
 126         }
 127 
 128         // Validate fourth line
 129         line = r.readLine();
 130         m = line4.matcher(line);
 131         if (m.matches()) {
 132             if (!m.group(1).equals("enabled")) {
 133                 throw new Exception("Invalid message: '" + m.group(1) + "'");
 134             }
 135         } else {
 136             throw new Exception("Regexp 4 failed");
 137         }
 138     }
 139 }