|
| 1 | +/* |
| 2 | + * Copyright Amazon.com Inc. 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 | +import java.util.regex.Pattern; |
| 25 | +import java.util.regex.Matcher; |
| 26 | + |
| 27 | +import jdk.test.lib.process.ProcessTools; |
| 28 | +import jdk.test.lib.process.OutputAnalyzer; |
| 29 | + |
| 30 | +/** |
| 31 | + * @test |
| 32 | + * @summary Check that timer slack options work |
| 33 | + * @requires os.family == "linux" |
| 34 | + * @requires vm.flagless |
| 35 | + * @library /test/lib |
| 36 | + * @run driver TestTimerSlack |
| 37 | + */ |
| 38 | +public class TestTimerSlack { |
| 39 | + |
| 40 | + public static void main(String[] args) throws Exception { |
| 41 | + int defaultSlack; |
| 42 | + |
| 43 | + // Check the timer slack value is not printed by default |
| 44 | + { |
| 45 | + ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-Xlog:os+thread", |
| 46 | + "TestTimerSlack$TestMain"); |
| 47 | + |
| 48 | + OutputAnalyzer output = new OutputAnalyzer(pb.start()); |
| 49 | + output.shouldHaveExitValue(0); |
| 50 | + output.shouldNotContain("timer slack:"); |
| 51 | + } |
| 52 | + |
| 53 | + // Check the timer slack value is not printed when explicitly disabled |
| 54 | + { |
| 55 | + ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-Xlog:os+thread", |
| 56 | + "-XX:+UnlockExperimentalVMOptions", |
| 57 | + "-XX:TimerSlack=-1", |
| 58 | + "TestTimerSlack$TestMain"); |
| 59 | + |
| 60 | + OutputAnalyzer output = new OutputAnalyzer(pb.start()); |
| 61 | + output.shouldHaveExitValue(0); |
| 62 | + output.shouldNotContain("timer slack:"); |
| 63 | + } |
| 64 | + |
| 65 | + // Check the timer slack value is good when system-wide default is requested |
| 66 | + { |
| 67 | + ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-Xlog:os+thread", |
| 68 | + "-XX:+UnlockExperimentalVMOptions", |
| 69 | + "-XX:TimerSlack=0", |
| 70 | + "TestTimerSlack$TestMain"); |
| 71 | + |
| 72 | + OutputAnalyzer output = new OutputAnalyzer(pb.start()); |
| 73 | + output.shouldHaveExitValue(0); |
| 74 | + output.shouldContain("timer slack:"); |
| 75 | + |
| 76 | + defaultSlack = parseSlackValue(output); |
| 77 | + |
| 78 | + if (defaultSlack == 0) { |
| 79 | + fail(output, "Default slack value (" + defaultSlack + ") is unexpected"); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + // Check the timer slack value is accepted by all threads |
| 84 | + for (int slack : new int[] {1, 10, 100, 1000, 10000, 100000, 1000000}) { |
| 85 | + ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-Xlog:os+thread", |
| 86 | + "-XX:+UnlockExperimentalVMOptions", |
| 87 | + "-XX:TimerSlack=" + slack, |
| 88 | + "TestTimerSlack$TestMain"); |
| 89 | + |
| 90 | + OutputAnalyzer output = new OutputAnalyzer(pb.start()); |
| 91 | + output.shouldHaveExitValue(0); |
| 92 | + output.shouldContain("timer slack:"); |
| 93 | + |
| 94 | + int actualSlack = parseSlackValue(output); |
| 95 | + if (actualSlack != slack) { |
| 96 | + fail(output, "Actual slack value (" + actualSlack + ") is not the requested one (" + slack + ")"); |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + static final Pattern EXTRACT_PATTERN = Pattern.compile("(.*)timer slack: ([0-9]+)ns(.*)"); |
| 102 | + |
| 103 | + public static int parseSlackValue(OutputAnalyzer output) { |
| 104 | + Integer value = null; |
| 105 | + for (String s : output.asLines()) { |
| 106 | + Matcher m = EXTRACT_PATTERN.matcher(s); |
| 107 | + if (m.matches()) { |
| 108 | + Integer parsedValue = Integer.parseInt(m.group(2)); |
| 109 | + if (value == null) { |
| 110 | + value = parsedValue; |
| 111 | + } else if (!value.equals(parsedValue)) { |
| 112 | + fail(output, "Multiple timer slack values detected"); |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + if (value == null) { |
| 117 | + fail(output, "No timer slack values detected"); |
| 118 | + } |
| 119 | + return value; |
| 120 | + } |
| 121 | + |
| 122 | + private static void fail(OutputAnalyzer output, String msg) { |
| 123 | + output.reportDiagnosticSummary(); |
| 124 | + throw new IllegalStateException(msg); |
| 125 | + } |
| 126 | + |
| 127 | + public static class TestMain { |
| 128 | + static final int THREADS = 8; |
| 129 | + |
| 130 | + public static void main(String... args) throws Exception { |
| 131 | + Thread[] ts = new Thread[THREADS]; |
| 132 | + for (int c = 0; c < THREADS; c++) { |
| 133 | + ts[c] = new Thread(); |
| 134 | + ts[c].start(); |
| 135 | + } |
| 136 | + |
| 137 | + for (int c = 0; c < THREADS; c++) { |
| 138 | + ts[c].join(); |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | +} |
0 commit comments