comparison test/runtime/NMT/SummarySanityCheck.java @ 7977:879c6de913d6

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