1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
|
/*
* Copyright (c) 2020, 2022, 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
* @requires vm.gc != "Epsilon"
* @summary Stress the string table and cleaning.
* Test argument is the approximate number of seconds to run.
* @library /test/lib
* @modules java.base/jdk.internal.misc
* @build jdk.test.whitebox.WhiteBox
* @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
* @run main/othervm
* -Xbootclasspath/a:.
* -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
* runtime.stringtable.StringTableCleaningTest 30
*/
package runtime.stringtable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import jdk.test.lib.process.OutputAnalyzer;
import jdk.test.lib.process.ProcessTools;
import jdk.test.whitebox.gc.GC;
public class StringTableCleaningTest {
public static void main(String[] args) throws Exception {
List<String> subargs = new ArrayList<String>();
subargs.addAll(List.of("-Xlog:gc,gc+start,stringtable*=trace", "-Xmx1g"));
subargs.add(Tester.class.getName());
subargs.addAll(Arrays.asList(args));
OutputAnalyzer output = ProcessTools.executeTestJava(subargs);
output.shouldHaveExitValue(0);
checkOutput(output);
}
private static int fail(String msg) throws Exception {
throw new RuntimeException(msg);
}
// Recognizing GC start and end log lines.
private static final String gcPrefix = "\\[info\\s*\\]\\[gc";
private static final String gcMiddle = "\\s*\\] GC\\(\\p{Digit}+\\) ";
private static final String gcStartPrefix = gcPrefix + ",start" + gcMiddle;
private static final String gcEndPrefix = gcPrefix + gcMiddle;
// Suffix for SerialGC and ParallelGC.
private static final String spSuffix = "Pause";
// All G1 pauses except Cleanup do weak reference clearing.
private static final String g1Suffix = "Pause(?! Cleanup)";
// Suffix for ZGC.
private static final String zStartSuffix = "Garbage Collection (.*)$";
private static final String zEndSuffix = "Garbage Collection (.*) .*->.*$";
// Suffix for Shenandoah.
private static final String shenSuffix = "Concurrent weak roots";
private static String getGcStartString() {
if (GC.Serial.isSelected() || GC.Parallel.isSelected()) {
return gcStartPrefix + spSuffix;
} else if (GC.G1.isSelected()) {
return gcStartPrefix + g1Suffix;
} else if (GC.Z.isSelected()) {
return gcStartPrefix + zStartSuffix;
} else if (GC.Shenandoah.isSelected()) {
return gcStartPrefix + shenSuffix;
} else {
return "unsupported GC";
}
}
private static String getGcEndString() {
if (GC.Serial.isSelected() || GC.Parallel.isSelected()) {
return gcEndPrefix + spSuffix;
} else if (GC.G1.isSelected()) {
return gcEndPrefix + g1Suffix;
} else if (GC.Z.isSelected()) {
return gcEndPrefix + zEndSuffix;
} else if (GC.Shenandoah.isSelected()) {
return gcEndPrefix + shenSuffix;
} else {
return "unsupported GC";
}
}
private static Pattern getGcStartPattern() {
return Pattern.compile(getGcStartString());
}
private static Pattern getGcEndPattern() {
return Pattern.compile(getGcEndString());
}
private static final Pattern pGcStart = getGcStartPattern();
private static final Pattern pGcEnd = getGcEndPattern();
// Recognizing StringTable GC callback log lines.
private static final Pattern pCallback =
Pattern.compile("\\[trace\\s*\\]\\[stringtable\\s*\\] Uncleaned items:");
private static boolean matchesPattern(String line, Pattern regex) {
return regex.matcher(line).find();
}
private static boolean matchesStart(String line) {
return matchesPattern(line, pGcStart);
}
private static boolean matchesEnd(String line) {
return matchesPattern(line, pGcEnd);
}
private static boolean matchesCallback(String line) {
return matchesPattern(line, pCallback);
}
// Search the lines for the first GC start log line in lines, starting
// from fromIndex. Returns the index of that line, or -1 if no GC start
// line found. Throws if a callback or GC end line is found first.
private static int findStart(List<String> lines, int fromIndex)
throws Exception
{
for (int i = fromIndex; i < lines.size(); ++i) {
String line = lines.get(i);
if (matchesStart(line)) {
return i;
} else if (matchesEnd(line)) {
fail("End without Start: " + i);
} else if (matchesCallback(line)) {
fail("Callback without Start: " + i);
}
}
return -1;
}
// Search the lines for the first callback log line in lines, starting
// after gcStart. Returns the index of that line, or -1 if no callback
// line is found (concurrent GC could start but not complete). Throws
// if a GC start or GC end log line is found first.
private static int findCallback(List<String> lines, int gcStart)
throws Exception
{
for (int i = gcStart + 1; i < lines.size(); ++i) {
String line = lines.get(i);
if (matchesCallback(line)) {
return i;
} else if (matchesEnd(line)) {
fail("Missing Callback in [" + gcStart + ", " + i + "]");
} else if (matchesStart(line)) {
fail("Two Starts: " + gcStart + ", " + i);
}
}
return -1;
}
// Search the lines for the first GC end log line in lines, starting
// after callback. Returns the index of that line, or -1 if no GC end
// line is found (concurrent GC could start but not complete). Throws
// if a GC start or a callback log line is found first.
private static int findEnd(List<String> lines, int gcStart, int callback)
throws Exception
{
for (int i = callback + 1; i < lines.size(); ++i) {
String line = lines.get(i);
if (matchesEnd(line)) {
return i;
} else if (matchesStart(line)) {
fail("Missing End for Start: " + gcStart + " at " + i);
} else if (matchesCallback(line)) {
fail("Multiple Callbacks for Start: " + gcStart + " at " + i);
}
}
return -1;
}
private static int check(List<String> lines, int fromIndex) throws Exception {
int gcStart = findStart(lines, fromIndex);
if (gcStart < 0) return -1;
int callback = findCallback(lines, gcStart);
if (callback < 0) return -1;
int gcEnd = findEnd(lines, gcStart, callback);
if (gcEnd < 0) return -1;
return gcEnd + 1;
}
private static void checkOutput(OutputAnalyzer output) throws Exception {
List<String> lines = output.asLines();
int count = -1;
int i = 0;
try {
for ( ; i >= 0; i = check(lines, i)) { ++count; }
} finally {
if (i < 0) {
System.out.println("Output check passed with " + count + " GCs");
} else {
System.out.println("--- Output check failed: " + count + " -----");
System.out.println(output.getOutput());
}
}
}
static class Tester {
private static volatile boolean stopRequested = false;
private static final TimeUnit durationUnits = TimeUnit.SECONDS;
public static void main(String[] args) throws Exception {
long duration = Long.parseLong(args[0]);
runTest(duration);
}
public static void runTest(long duration) throws Exception {
ScheduledExecutorService scheduler =
Executors.newScheduledThreadPool(1);
try {
ScheduledFuture<?> stopper =
scheduler.schedule(() -> stopRequested = true,
duration,
durationUnits);
try {
stringMaker(10000000, 100000, 50000);
} finally {
stopper.cancel(false);
}
} finally {
scheduler.shutdownNow();
}
}
private static void stringMaker(int maxSize, int growStep, int shrinkStep)
throws Exception
{
long stringNum = 0;
while (true) {
LinkedList<String> list = new LinkedList<String>();
for (int i = 0; i < maxSize; ++i, ++stringNum) {
if (stopRequested) {
return;
}
if ((i != 0) && ((i % growStep) == 0)) {
list.subList(0, shrinkStep).clear();
}
list.push(Long.toString(stringNum).intern());
}
// For generational collectors, try to move current list
// contents to old-gen before dropping the list.
System.gc();
}
}
}
}
|