test/runtime/NMT/SummarySanityCheck.java

Wed, 27 Aug 2014 08:35:03 -0400

author
zgu
date
Wed, 27 Aug 2014 08:35:03 -0400
changeset 7075
ac12996df59b
parent 4637
1b0dc9f87e75
child 7110
6640f982c1be
permissions
-rw-r--r--

8044140: Create NMT (Native Memory Tracking) tests for NMT2
Summary: Create new/modify existing tests for NMT2, which is an internal redesign to address scalability issues in the first implementation.
Reviewed-by: ctornqvi, zgu
Contributed-by: George Triantafillou <george.triantafillou@oracle.com>

ctornqvi@4518 1 /*
zgu@7075 2 * Copyright (c) 2013, 2014, 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
mgerdin@4637 28 * @library /testlibrary /testlibrary/whitebox
mgerdin@4637 29 * @build SummarySanityCheck
zgu@7075 30 * @ignore
mgerdin@4637 31 * @run main ClassFileInstaller sun.hotspot.WhiteBox
mgerdin@4637 32 * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:NativeMemoryTracking=summary -XX:+WhiteBoxAPI SummarySanityCheck
ctornqvi@4518 33 */
ctornqvi@4518 34
ctornqvi@4518 35 import com.oracle.java.testlibrary.*;
ctornqvi@4518 36
ctornqvi@4518 37 import java.util.regex.Matcher;
ctornqvi@4518 38 import java.util.regex.Pattern;
ctornqvi@4518 39 import sun.hotspot.WhiteBox;
ctornqvi@4518 40
ctornqvi@4518 41 public class SummarySanityCheck {
ctornqvi@4518 42
ctornqvi@4518 43 private static String jcmdout;
ctornqvi@4518 44 public static void main(String args[]) throws Exception {
ctornqvi@4518 45 // Grab my own PID
ctornqvi@4518 46 String pid = Integer.toString(ProcessTools.getProcessId());
ctornqvi@4518 47
ctornqvi@4518 48 ProcessBuilder pb = new ProcessBuilder();
ctornqvi@4518 49
ctornqvi@4518 50 // Run 'jcmd <pid> VM.native_memory summary scale=KB'
ctornqvi@4518 51 pb.command(new String[] { JDKToolFinder.getJDKTool("jcmd"), pid, "VM.native_memory", "summary", "scale=KB"});
ctornqvi@4518 52 OutputAnalyzer output = new OutputAnalyzer(pb.start());
ctornqvi@4518 53
ctornqvi@4518 54 jcmdout = output.getOutput();
ctornqvi@4518 55 // Split by '-' to get the 'groups'
ctornqvi@4518 56 String[] lines = jcmdout.split("\n");
ctornqvi@4518 57
ctornqvi@4518 58 if (lines.length == 0) {
ctornqvi@4518 59 throwTestException("Failed to parse jcmd output");
ctornqvi@4518 60 }
ctornqvi@4518 61
ctornqvi@4518 62 int totalCommitted = 0, totalReserved = 0;
ctornqvi@4518 63 int totalCommittedSum = 0, totalReservedSum = 0;
ctornqvi@4518 64
ctornqvi@4518 65 // Match '- <mtType> (reserved=<reserved>KB, committed=<committed>KB)
ctornqvi@4518 66 Pattern mtTypePattern = Pattern.compile("-\\s+(?<typename>[\\w\\s]+)\\(reserved=(?<reserved>\\d+)KB,\\scommitted=(?<committed>\\d+)KB\\)");
ctornqvi@4518 67 // Match 'Total: reserved=<reserved>KB, committed=<committed>KB'
zgu@7075 68 Pattern totalMemoryPattern = Pattern.compile("Total\\:\\sreserved=(?<reserved>\\d+)KB,\\scommitted=(?<committed>\\d+)KB");
ctornqvi@4518 69
ctornqvi@4518 70 for (int i = 0; i < lines.length; i++) {
ctornqvi@4518 71 if (lines[i].startsWith("Total")) {
ctornqvi@4518 72 Matcher totalMemoryMatcher = totalMemoryPattern.matcher(lines[i]);
ctornqvi@4518 73
zgu@7075 74 if (totalMemoryMatcher.matches()) {
ctornqvi@4518 75 totalCommitted = Integer.parseInt(totalMemoryMatcher.group("committed"));
ctornqvi@4518 76 totalReserved = Integer.parseInt(totalMemoryMatcher.group("reserved"));
ctornqvi@4518 77 } else {
ctornqvi@4518 78 throwTestException("Failed to match the expected groups in 'Total' memory part");
ctornqvi@4518 79 }
ctornqvi@4518 80 } else if (lines[i].startsWith("-")) {
ctornqvi@4518 81 Matcher typeMatcher = mtTypePattern.matcher(lines[i]);
ctornqvi@4518 82 if (typeMatcher.matches()) {
ctornqvi@4518 83 int typeCommitted = Integer.parseInt(typeMatcher.group("committed"));
ctornqvi@4518 84 int typeReserved = Integer.parseInt(typeMatcher.group("reserved"));
ctornqvi@4518 85
ctornqvi@4518 86 // Make sure reserved is always less or equals
ctornqvi@4518 87 if (typeCommitted > typeReserved) {
ctornqvi@4518 88 throwTestException("Committed (" + typeCommitted + ") was more than Reserved ("
ctornqvi@4518 89 + typeReserved + ") for mtType: " + typeMatcher.group("typename"));
ctornqvi@4518 90 }
ctornqvi@4518 91
ctornqvi@4518 92 // Add to total and compare them in the end
ctornqvi@4518 93 totalCommittedSum += typeCommitted;
ctornqvi@4518 94 totalReservedSum += typeReserved;
ctornqvi@4518 95 } else {
ctornqvi@4518 96 throwTestException("Failed to match the group on line " + i);
ctornqvi@4518 97 }
ctornqvi@4518 98 }
ctornqvi@4518 99 }
ctornqvi@4518 100
ctornqvi@4518 101 // See if they add up correctly, rounding is a problem so make sure we're within +/- 8KB
ctornqvi@4518 102 int committedDiff = totalCommitted - totalCommittedSum;
ctornqvi@4518 103 if (committedDiff > 8 || committedDiff < -8) {
ctornqvi@4518 104 throwTestException("Total committed (" + totalCommitted + ") did not match the summarized committed (" + totalCommittedSum + ")" );
ctornqvi@4518 105 }
ctornqvi@4518 106
ctornqvi@4518 107 int reservedDiff = totalReserved - totalReservedSum;
ctornqvi@4518 108 if (reservedDiff > 8 || reservedDiff < -8) {
ctornqvi@4518 109 throwTestException("Total reserved (" + totalReserved + ") did not match the summarized reserved (" + totalReservedSum + ")" );
ctornqvi@4518 110 }
ctornqvi@4518 111 }
ctornqvi@4518 112
ctornqvi@4518 113 private static void throwTestException(String reason) throws Exception {
ctornqvi@4518 114 throw new Exception(reason + " . Stdout is :\n" + jcmdout);
ctornqvi@4518 115 }
ctornqvi@4518 116 }

mercurial