/* * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ /* * @test TestDynamicGCThreadsStats * @bug 8176140 * @summary Check that G1 reports per thread statistics. * @requires vm.gc=="G1" | vm.gc=="null" * @key gc * @library /testlibrary /test/lib * @build sun.hotspot.WhiteBox * @run main ClassFileInstaller sun.hotspot.WhiteBox * @run driver TestDynamicGCThreadsStats */ import sun.hotspot.WhiteBox; import java.text.DecimalFormatSymbols; import java.util.regex.Matcher; import java.util.regex.Pattern; import jdk.test.lib.OutputAnalyzer; import jdk.test.lib.Platform; import jdk.test.lib.ProcessTools; import static jdk.test.lib.Asserts.*; public class TestDynamicGCThreadsStats { public static void runTest() throws Exception { final String[] arguments = { "-Xbootclasspath/a:.", "-XX:+UnlockExperimentalVMOptions", "-XX:+UnlockDiagnosticVMOptions", "-XX:+UseDynamicNumberOfGCThreads", "-XX:+WhiteBoxAPI", "-XX:+UseG1GC", "-Xlog:gc*=trace", "-Xmx10M", GCTest.class.getName() }; ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(arguments); OutputAnalyzer output = new OutputAnalyzer(pb.start()); output.shouldHaveExitValue(0); // Looking for: // [0.234s][debug ][gc,phases ] GC(0) Evacuate Collection Set: 3.7 ms String parallel_phase_leader = "Evacuate Collection Set: \\d+\\.\\d+ms"; String std_out = output.getStdout(); Matcher m = Pattern.compile(parallel_phase_leader, Pattern.MULTILINE).matcher(std_out); if (!m.find()) { throw new Exception("Could not find correct output for Evacuate Collection Set: in stdout," + " should match the pattern \"" + parallel_phase_leader + "\", but stdout is \n" + output.getStdout()); } else { // Find data with per thread times. // Any of the metrics with per thread times can be used. // Chose: // [0.234s][debug ][gc,phases ] GC(0) Ext Root Scanning: Min: 0.6, Avg: 1.1, Max: 1.6, Diff: 1.0, Sum: 2.2 String stats = "Ext Root Scanning \\(ms\\):"; Pattern stats_pattern = Pattern.compile(stats); m.usePattern(stats_pattern); if (!m.find()) { throw new Exception("Could not find correct output for chosen statistics in stdout," + " should match the pattern \"" + stats + "\", but stdout is \n" + output.getStdout()); } else { // Find the printed average from the log. Pattern avg_pattern = Pattern.compile("(Avg: +)(\\d+\\.\\d+)(,)"); m.usePattern(avg_pattern); if (!m.find()) { throw new Exception("Could not find Avg: dd.d in stdout\n," + output.getStdout()); } else { String value_string = m.group(2); // Count the decimal places in the printed average. The // number of decimal places will be used to calculate the // rounding error when the per thread values are summed in // this test. DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(); char decimal_symbol = symbols.getDecimalSeparator(); String decimal_places_string = value_string.substring(value_string.indexOf(decimal_symbol) + 1); int decimal_points = decimal_places_string.length(); double avg_value = 0.0; try { avg_value = Double.parseDouble(value_string); } catch (java.lang.NumberFormatException e) { System.out.println("Non Number Format - " + value_string); } // Go to the next line which will have a "GC" in it. For // example: // [0.234s][trace ][gc,phases ] GC(0) 1.58 0.58 Pattern GC_pattern = Pattern.compile("GC"); m.usePattern(GC_pattern); if (m.find()) { String[] per_thread_values = std_out.substring(m.end()).split(System.lineSeparator(), 2); String value_regex = " +\\d+\\.\\d+"; Matcher value_m = Pattern.compile(value_regex, Pattern.MULTILINE).matcher(per_thread_values[0]); int workers = 0; Double sum = 0.0; // Count and sum the values. Note that each parsed value has rounding error in it. while (value_m.find()) { try { Double value = Double.parseDouble(value_m.group()); sum += value; workers++; } catch (java.lang.NumberFormatException e) { System.out.println("Non Number Format - " + m.group()); } } if (workers > 0) { Double calculated_avg = sum / workers; // Rounding error in per thread data Double rounding_error = workers * 0.5 * Math.pow(10.0, -decimal_points); if (Math.abs(calculated_avg - avg_value) > rounding_error) { throw new Exception("Average from log " + avg_value + " and average calculated by test " + calculated_avg + " can differ by rounding error " + rounding_error); } else { System.err.println("PASSED: Average from log " + avg_value + " and average calculated by test " + calculated_avg + " can differ by rounding error " + rounding_error); } } } System.out.println(output.getStdout()); } } } } public static void main(String[] args) throws Exception { runTest(); } static class GCTest { public static void main(String [] args) { int numGCs = 4; // Perform the requested amount of GCs. WhiteBox wb = WhiteBox.getWhiteBox(); for (int i = 0; i < numGCs - 1; i++) { wb.youngGC(); } if (numGCs > 0) { wb.fullGC(); } System.out.println("Done"); } } }