test/runtime/NMT/SummarySanityCheck.java

Sat, 02 Feb 2013 16:34:10 +0100

author
ctornqvi
date
Sat, 02 Feb 2013 16:34:10 +0100
changeset 4518
879c6de913d6
child 4637
1b0dc9f87e75
permissions
-rw-r--r--

8005013: Add NMT tests
Summary: Add tests for the Native Memory Tracking feature, includes regression tests for 8005936 and 8004802
Reviewed-by: zgu, coleenp

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

mercurial