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