test/runtime/NMT/SummarySanityCheck.java

Wed, 27 Apr 2016 01:25:04 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:25:04 +0800
changeset 0
f90c822e73f8
child 6876
710a3c8b516e
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/
changeset: 6782:28b50d07f6f8
tag: jdk8u25-b17

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 */
aoqi@0 23
aoqi@0 24 /*
aoqi@0 25 * @test
aoqi@0 26 * @key nmt jcmd
aoqi@0 27 * @summary Sanity check the output of NMT
aoqi@0 28 * @library /testlibrary /testlibrary/whitebox
aoqi@0 29 * @build SummarySanityCheck
aoqi@0 30 * @run main ClassFileInstaller sun.hotspot.WhiteBox
aoqi@0 31 * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:NativeMemoryTracking=summary -XX:+WhiteBoxAPI SummarySanityCheck
aoqi@0 32 */
aoqi@0 33
aoqi@0 34 import com.oracle.java.testlibrary.*;
aoqi@0 35
aoqi@0 36 import java.util.regex.Matcher;
aoqi@0 37 import java.util.regex.Pattern;
aoqi@0 38 import sun.hotspot.WhiteBox;
aoqi@0 39
aoqi@0 40 public class SummarySanityCheck {
aoqi@0 41
aoqi@0 42 private static String jcmdout;
aoqi@0 43 public static void main(String args[]) throws Exception {
aoqi@0 44 // Grab my own PID
aoqi@0 45 String pid = Integer.toString(ProcessTools.getProcessId());
aoqi@0 46
aoqi@0 47 // Use WB API to ensure that all data has been merged before we continue
aoqi@0 48 if (!WhiteBox.getWhiteBox().NMTWaitForDataMerge()) {
aoqi@0 49 throw new Exception("Call to WB API NMTWaitForDataMerge() failed");
aoqi@0 50 }
aoqi@0 51
aoqi@0 52 ProcessBuilder pb = new ProcessBuilder();
aoqi@0 53
aoqi@0 54 // Run 'jcmd <pid> VM.native_memory summary scale=KB'
aoqi@0 55 pb.command(new String[] { JDKToolFinder.getJDKTool("jcmd"), pid, "VM.native_memory", "summary", "scale=KB"});
aoqi@0 56 OutputAnalyzer output = new OutputAnalyzer(pb.start());
aoqi@0 57
aoqi@0 58 jcmdout = output.getOutput();
aoqi@0 59 // Split by '-' to get the 'groups'
aoqi@0 60 String[] lines = jcmdout.split("\n");
aoqi@0 61
aoqi@0 62 if (lines.length == 0) {
aoqi@0 63 throwTestException("Failed to parse jcmd output");
aoqi@0 64 }
aoqi@0 65
aoqi@0 66 int totalCommitted = 0, totalReserved = 0;
aoqi@0 67 int totalCommittedSum = 0, totalReservedSum = 0;
aoqi@0 68
aoqi@0 69 // Match '- <mtType> (reserved=<reserved>KB, committed=<committed>KB)
aoqi@0 70 Pattern mtTypePattern = Pattern.compile("-\\s+(?<typename>[\\w\\s]+)\\(reserved=(?<reserved>\\d+)KB,\\scommitted=(?<committed>\\d+)KB\\)");
aoqi@0 71 // Match 'Total: reserved=<reserved>KB, committed=<committed>KB'
aoqi@0 72 Pattern totalMemoryPattern = Pattern.compile("Total\\:\\s\\sreserved=(?<reserved>\\d+)KB,\\s\\scommitted=(?<committed>\\d+)KB");
aoqi@0 73
aoqi@0 74 for (int i = 0; i < lines.length; i++) {
aoqi@0 75 if (lines[i].startsWith("Total")) {
aoqi@0 76 Matcher totalMemoryMatcher = totalMemoryPattern.matcher(lines[i]);
aoqi@0 77
aoqi@0 78 if (totalMemoryMatcher.matches() && totalMemoryMatcher.groupCount() == 2) {
aoqi@0 79 totalCommitted = Integer.parseInt(totalMemoryMatcher.group("committed"));
aoqi@0 80 totalReserved = Integer.parseInt(totalMemoryMatcher.group("reserved"));
aoqi@0 81 } else {
aoqi@0 82 throwTestException("Failed to match the expected groups in 'Total' memory part");
aoqi@0 83 }
aoqi@0 84 } else if (lines[i].startsWith("-")) {
aoqi@0 85 Matcher typeMatcher = mtTypePattern.matcher(lines[i]);
aoqi@0 86 if (typeMatcher.matches()) {
aoqi@0 87 int typeCommitted = Integer.parseInt(typeMatcher.group("committed"));
aoqi@0 88 int typeReserved = Integer.parseInt(typeMatcher.group("reserved"));
aoqi@0 89
aoqi@0 90 // Make sure reserved is always less or equals
aoqi@0 91 if (typeCommitted > typeReserved) {
aoqi@0 92 throwTestException("Committed (" + typeCommitted + ") was more than Reserved ("
aoqi@0 93 + typeReserved + ") for mtType: " + typeMatcher.group("typename"));
aoqi@0 94 }
aoqi@0 95
aoqi@0 96 // Add to total and compare them in the end
aoqi@0 97 totalCommittedSum += typeCommitted;
aoqi@0 98 totalReservedSum += typeReserved;
aoqi@0 99 } else {
aoqi@0 100 throwTestException("Failed to match the group on line " + i);
aoqi@0 101 }
aoqi@0 102 }
aoqi@0 103 }
aoqi@0 104
aoqi@0 105 // See if they add up correctly, rounding is a problem so make sure we're within +/- 8KB
aoqi@0 106 int committedDiff = totalCommitted - totalCommittedSum;
aoqi@0 107 if (committedDiff > 8 || committedDiff < -8) {
aoqi@0 108 throwTestException("Total committed (" + totalCommitted + ") did not match the summarized committed (" + totalCommittedSum + ")" );
aoqi@0 109 }
aoqi@0 110
aoqi@0 111 int reservedDiff = totalReserved - totalReservedSum;
aoqi@0 112 if (reservedDiff > 8 || reservedDiff < -8) {
aoqi@0 113 throwTestException("Total reserved (" + totalReserved + ") did not match the summarized reserved (" + totalReservedSum + ")" );
aoqi@0 114 }
aoqi@0 115 }
aoqi@0 116
aoqi@0 117 private static void throwTestException(String reason) throws Exception {
aoqi@0 118 throw new Exception(reason + " . Stdout is :\n" + jcmdout);
aoqi@0 119 }
aoqi@0 120 }

mercurial