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