Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/test/runtime/NMT/SummarySanityCheck.java
32284 views
/*1* Copyright (c) 2013, 2014, 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* @test25* @key nmt jcmd26* @summary Sanity check the output of NMT27* @library /testlibrary /testlibrary/whitebox28* @build SummarySanityCheck29* @run main ClassFileInstaller sun.hotspot.WhiteBox30* @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:NativeMemoryTracking=summary -XX:+WhiteBoxAPI SummarySanityCheck31*/3233import com.oracle.java.testlibrary.*;3435import java.util.regex.Matcher;36import java.util.regex.Pattern;37import sun.hotspot.WhiteBox;3839public class SummarySanityCheck {4041private static String jcmdout;42public static void main(String args[]) throws Exception {43// Grab my own PID44String pid = Integer.toString(ProcessTools.getProcessId());4546ProcessBuilder pb = new ProcessBuilder();4748// Run 'jcmd <pid> VM.native_memory summary scale=KB'49pb.command(new String[] { JDKToolFinder.getJDKTool("jcmd"), pid, "VM.native_memory", "summary", "scale=KB"});50OutputAnalyzer output = new OutputAnalyzer(pb.start());5152jcmdout = output.getOutput();53// Split by '-' to get the 'groups'54String[] lines = jcmdout.split("\n");5556if (lines.length == 0) {57throwTestException("Failed to parse jcmd output");58}5960int totalCommitted = 0, totalReserved = 0;61int totalCommittedSum = 0, totalReservedSum = 0;6263// Match '- <mtType> (reserved=<reserved>KB, committed=<committed>KB)64Pattern mtTypePattern = Pattern.compile("-\\s+(?<typename>[\\w\\s]+)\\(reserved=(?<reserved>\\d+)KB,\\scommitted=(?<committed>\\d+)KB\\)");65// Match 'Total: reserved=<reserved>KB, committed=<committed>KB'66Pattern totalMemoryPattern = Pattern.compile("Total\\:\\sreserved=(?<reserved>\\d+)KB,\\scommitted=(?<committed>\\d+)KB");6768for (int i = 0; i < lines.length; i++) {69if (lines[i].startsWith("Total")) {70Matcher totalMemoryMatcher = totalMemoryPattern.matcher(lines[i]);7172if (totalMemoryMatcher.matches()) {73totalCommitted = Integer.parseInt(totalMemoryMatcher.group("committed"));74totalReserved = Integer.parseInt(totalMemoryMatcher.group("reserved"));75} else {76throwTestException("Failed to match the expected groups in 'Total' memory part");77}78} else if (lines[i].startsWith("-")) {79Matcher typeMatcher = mtTypePattern.matcher(lines[i]);80if (typeMatcher.matches()) {81int typeCommitted = Integer.parseInt(typeMatcher.group("committed"));82int typeReserved = Integer.parseInt(typeMatcher.group("reserved"));8384// Make sure reserved is always less or equals85if (typeCommitted > typeReserved) {86throwTestException("Committed (" + typeCommitted + ") was more than Reserved ("87+ typeReserved + ") for mtType: " + typeMatcher.group("typename"));88}8990// Add to total and compare them in the end91totalCommittedSum += typeCommitted;92totalReservedSum += typeReserved;93} else {94throwTestException("Failed to match the group on line " + i);95}96}97}9899// See if they add up correctly, rounding is a problem so make sure we're within +/- 8KB100int committedDiff = totalCommitted - totalCommittedSum;101if (committedDiff > 8 || committedDiff < -8) {102throwTestException("Total committed (" + totalCommitted + ") did not match the summarized committed (" + totalCommittedSum + ")" );103}104105int reservedDiff = totalReserved - totalReservedSum;106if (reservedDiff > 8 || reservedDiff < -8) {107throwTestException("Total reserved (" + totalReserved + ") did not match the summarized reserved (" + totalReservedSum + ")" );108}109}110111private static void throwTestException(String reason) throws Exception {112throw new Exception(reason + " . Stdout is :\n" + jcmdout);113}114}115116117