test/runtime/NMT/SummarySanityCheck.java

changeset 4518
879c6de913d6
child 4637
1b0dc9f87e75
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/runtime/NMT/SummarySanityCheck.java	Sat Feb 02 16:34:10 2013 +0100
     1.3 @@ -0,0 +1,119 @@
     1.4 +/*
     1.5 + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +
    1.27 +/*
    1.28 + * @test
    1.29 + * @key nmt jcmd
    1.30 + * @summary Sanity check the output of NMT
    1.31 + * @library /testlibrary
    1.32 + * @run compile -J-XX:+UnlockDiagnosticVMOptions -J-XX:+WhiteBoxAPI SummarySanityCheck.java
    1.33 + * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:NativeMemoryTracking=summary -XX:+WhiteBoxAPI SummarySanityCheck
    1.34 + */
    1.35 +
    1.36 +import com.oracle.java.testlibrary.*;
    1.37 +
    1.38 +import java.util.regex.Matcher;
    1.39 +import java.util.regex.Pattern;
    1.40 +import sun.hotspot.WhiteBox;
    1.41 +
    1.42 +public class SummarySanityCheck {
    1.43 +
    1.44 +  private static String jcmdout;
    1.45 +  public static void main(String args[]) throws Exception {
    1.46 +    // Grab my own PID
    1.47 +    String pid = Integer.toString(ProcessTools.getProcessId());
    1.48 +
    1.49 +    // Use WB API to ensure that all data has been merged before we continue
    1.50 +    if (!WhiteBox.getWhiteBox().NMTWaitForDataMerge()) {
    1.51 +      throw new Exception("Call to WB API NMTWaitForDataMerge() failed");
    1.52 +    }
    1.53 +
    1.54 +    ProcessBuilder pb = new ProcessBuilder();
    1.55 +
    1.56 +    // Run  'jcmd <pid> VM.native_memory summary scale=KB'
    1.57 +    pb.command(new String[] { JDKToolFinder.getJDKTool("jcmd"), pid, "VM.native_memory", "summary", "scale=KB"});
    1.58 +    OutputAnalyzer output = new OutputAnalyzer(pb.start());
    1.59 +
    1.60 +    jcmdout = output.getOutput();
    1.61 +    // Split by '-' to get the 'groups'
    1.62 +    String[] lines = jcmdout.split("\n");
    1.63 +
    1.64 +    if (lines.length == 0) {
    1.65 +      throwTestException("Failed to parse jcmd output");
    1.66 +    }
    1.67 +
    1.68 +    int totalCommitted = 0, totalReserved = 0;
    1.69 +    int totalCommittedSum = 0, totalReservedSum = 0;
    1.70 +
    1.71 +    // Match '- <mtType> (reserved=<reserved>KB, committed=<committed>KB)
    1.72 +    Pattern mtTypePattern = Pattern.compile("-\\s+(?<typename>[\\w\\s]+)\\(reserved=(?<reserved>\\d+)KB,\\scommitted=(?<committed>\\d+)KB\\)");
    1.73 +    // Match 'Total: reserved=<reserved>KB, committed=<committed>KB'
    1.74 +    Pattern totalMemoryPattern = Pattern.compile("Total\\:\\s\\sreserved=(?<reserved>\\d+)KB,\\s\\scommitted=(?<committed>\\d+)KB");
    1.75 +
    1.76 +    for (int i = 0; i < lines.length; i++) {
    1.77 +      if (lines[i].startsWith("Total")) {
    1.78 +        Matcher totalMemoryMatcher = totalMemoryPattern.matcher(lines[i]);
    1.79 +
    1.80 +        if (totalMemoryMatcher.matches() && totalMemoryMatcher.groupCount() == 2) {
    1.81 +          totalCommitted = Integer.parseInt(totalMemoryMatcher.group("committed"));
    1.82 +          totalReserved = Integer.parseInt(totalMemoryMatcher.group("reserved"));
    1.83 +        } else {
    1.84 +          throwTestException("Failed to match the expected groups in 'Total' memory part");
    1.85 +        }
    1.86 +      } else if (lines[i].startsWith("-")) {
    1.87 +        Matcher typeMatcher = mtTypePattern.matcher(lines[i]);
    1.88 +        if (typeMatcher.matches()) {
    1.89 +          int typeCommitted = Integer.parseInt(typeMatcher.group("committed"));
    1.90 +          int typeReserved = Integer.parseInt(typeMatcher.group("reserved"));
    1.91 +
    1.92 +          // Make sure reserved is always less or equals
    1.93 +          if (typeCommitted > typeReserved) {
    1.94 +            throwTestException("Committed (" + typeCommitted + ") was more than Reserved ("
    1.95 +                + typeReserved + ") for mtType: " + typeMatcher.group("typename"));
    1.96 +          }
    1.97 +
    1.98 +          // Add to total and compare them in the end
    1.99 +          totalCommittedSum += typeCommitted;
   1.100 +          totalReservedSum += typeReserved;
   1.101 +        } else {
   1.102 +          throwTestException("Failed to match the group on line " + i);
   1.103 +        }
   1.104 +      }
   1.105 +    }
   1.106 +
   1.107 +    // See if they add up correctly, rounding is a problem so make sure we're within +/- 8KB
   1.108 +    int committedDiff = totalCommitted - totalCommittedSum;
   1.109 +    if (committedDiff > 8 || committedDiff < -8) {
   1.110 +      throwTestException("Total committed (" + totalCommitted + ") did not match the summarized committed (" + totalCommittedSum + ")" );
   1.111 +    }
   1.112 +
   1.113 +    int reservedDiff = totalReserved - totalReservedSum;
   1.114 +    if (reservedDiff > 8 || reservedDiff < -8) {
   1.115 +      throwTestException("Total reserved (" + totalReserved + ") did not match the summarized reserved (" + totalReservedSum + ")" );
   1.116 +    }
   1.117 +  }
   1.118 +
   1.119 +  private static void throwTestException(String reason) throws Exception {
   1.120 +      throw new Exception(reason + " . Stdout is :\n" + jcmdout);
   1.121 +  }
   1.122 +}

mercurial