Path: blob/master/test/hotspot/jtreg/serviceability/dcmd/compiler/CodeCacheTest.java
41153 views
/*1* Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test CodeCacheTest25* @bug 805488926* @library /test/lib27* @modules java.base/jdk.internal.misc28* java.compiler29* java.management30* jdk.internal.jvmstat/sun.jvmstat.monitor31* @run testng/othervm -XX:+SegmentedCodeCache CodeCacheTest32* @run testng/othervm -XX:-SegmentedCodeCache CodeCacheTest33* @run testng/othervm -Xint -XX:+SegmentedCodeCache CodeCacheTest34* @summary Test of diagnostic command Compiler.codecache35*/3637import org.testng.annotations.Test;38import org.testng.Assert;3940import jdk.test.lib.process.OutputAnalyzer;41import jdk.test.lib.dcmd.CommandExecutor;42import jdk.test.lib.dcmd.JMXExecutor;4344import java.util.Iterator;45import java.util.regex.Matcher;46import java.util.regex.Pattern;4748public class CodeCacheTest {4950/**51* This test calls Jcmd (diagnostic command tool) Compiler.codecache and then parses the output,52* making sure that all numbers look ok53*54*55* Expected output without code cache segmentation:56*57* CodeCache: size=245760Kb used=4680Kb max_used=4680Kb free=241079Kb58* bounds [0x00007f5bd9000000, 0x00007f5bd94a0000, 0x00007f5be8000000]59* total_blobs=575 nmethods=69 adapters=42360* compilation: enabled61*62* Expected output with code cache segmentation (number of segments may change):63*64* CodeHeap 'non-nmethods': size=5696Kb used=2236Kb max_used=2238Kb free=3459Kb65* bounds [0x00007fa0f0ffe000, 0x00007fa0f126e000, 0x00007fa0f158e000]66* CodeHeap 'profiled nmethods': size=120036Kb used=8Kb max_used=8Kb free=120027Kb67* bounds [0x00007fa0f158e000, 0x00007fa0f17fe000, 0x00007fa0f8ac7000]68* CodeHeap 'non-profiled nmethods': size=120036Kb used=2Kb max_used=2Kb free=120034Kb69* bounds [0x00007fa0f8ac7000, 0x00007fa0f8d37000, 0x00007fa100000000]70* total_blobs=486 nmethods=8 adapters=39971* compilation: enabled72*/7374static Pattern line1 = Pattern.compile("(CodeCache|CodeHeap.*): size=(\\p{Digit}*)Kb used=(\\p{Digit}*)Kb max_used=(\\p{Digit}*)Kb free=(\\p{Digit}*)Kb");75static Pattern line2 = Pattern.compile(" bounds \\[0x(\\p{XDigit}*), 0x(\\p{XDigit}*), 0x(\\p{XDigit}*)\\]");76static Pattern line3 = Pattern.compile(" total_blobs=(\\p{Digit}*) nmethods=(\\p{Digit}*) adapters=(\\p{Digit}*)");77static Pattern line4 = Pattern.compile(" compilation: (.*)");7879private static boolean getFlagBool(String flag, String where) {80Matcher m = Pattern.compile(flag + "\\s+:?= (true|false)").matcher(where);81if (!m.find()) {82Assert.fail("Could not find value for flag " + flag + " in output string");83}84return m.group(1).equals("true");85}8687private static int getFlagInt(String flag, String where) {88Matcher m = Pattern.compile(flag + "\\s+:?=\\s+\\d+").matcher(where);89if (!m.find()) {90Assert.fail("Could not find value for flag " + flag + " in output string");91}92String match = m.group();93return Integer.parseInt(match.substring(match.lastIndexOf(" ") + 1, match.length()));94}9596public void run(CommandExecutor executor) {97// Get number of code cache segments98int segmentsCount = 0;99String flags = executor.execute("VM.flags -all").getOutput();100if (!getFlagBool("SegmentedCodeCache", flags) || !getFlagBool("UseCompiler", flags)) {101// No segmentation102segmentsCount = 1;103} else if (getFlagBool("TieredCompilation", flags) && getFlagInt("TieredStopAtLevel", flags) > 1) {104// Tiered compilation: use all segments105segmentsCount = 3;106} else {107// No TieredCompilation: only non-nmethod and non-profiled segment108segmentsCount = 2;109}110111// Get output from dcmd (diagnostic command)112OutputAnalyzer output = executor.execute("Compiler.codecache");113Iterator<String> lines = output.asLines().iterator();114115// Validate code cache segments116String line;117Matcher m;118int matchedCount = 0;119while (true) {120// Validate first line121line = lines.next();122m = line1.matcher(line);123if (m.matches()) {124for (int i = 2; i <= 5; i++) {125int val = Integer.parseInt(m.group(i));126if (val < 0) {127Assert.fail("Failed parsing dcmd codecache output");128}129}130} else {131break;132}133134// Validate second line135line = lines.next();136m = line2.matcher(line);137if (m.matches()) {138String start = m.group(1);139String mark = m.group(2);140String top = m.group(3);141142// Lexical compare of hex numbers to check that they look sane.143if (start.compareTo(mark) > 1) {144Assert.fail("Failed parsing dcmd codecache output");145}146if (mark.compareTo(top) > 1) {147Assert.fail("Failed parsing dcmd codecache output");148}149} else {150Assert.fail("Regexp 2 failed to match line: " + line);151}152++matchedCount;153}154// Because of CodeCacheExtensions, we could match more than expected155if (matchedCount < segmentsCount) {156Assert.fail("Fewer segments matched (" + matchedCount + ") than expected (" + segmentsCount + ")");157}158159// Validate third line160m = line3.matcher(line);161if (m.matches()) {162int blobs = Integer.parseInt(m.group(1));163if (blobs <= 0) {164Assert.fail("Failed parsing dcmd codecache output");165}166int nmethods = Integer.parseInt(m.group(2));167if (nmethods < 0) {168Assert.fail("Failed parsing dcmd codecache output");169}170int adapters = Integer.parseInt(m.group(3));171if (adapters <= 0) {172Assert.fail("Failed parsing dcmd codecache output");173}174if (blobs < (nmethods + adapters)) {175Assert.fail("Failed parsing dcmd codecache output");176}177} else {178Assert.fail("Regexp 3 failed to match line: " + line);179}180181// Validate fourth line182line = lines.next();183m = line4.matcher(line);184if (!m.matches()) {185Assert.fail("Regexp 4 failed to match line: " + line);186}187}188189@Test190public void jmx() {191run(new JMXExecutor());192}193}194195196