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
|
/*
* Copyright (c) 2013, 2024, 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
* @key randomness
*
* @summary converted from VM Testbase metaspace/stressDictionary.
* VM Testbase keywords: [nonconcurrent, javac]
*
* @library /vmTestbase /test/lib
* @run main/othervm/timeout=600 metaspace.stressDictionary.StressDictionary -stressTime 30
*/
package metaspace.stressDictionary;
import java.util.*;
import java.lang.management.ManagementFactory;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicLong;
import nsk.share.gc.GCTestBase;
import nsk.share.test.*;
import jdk.test.lib.compiler.InMemoryJavaCompiler;
/**
* There is a data structure named "dictionary" in class BlockFreelist. It stores
* information about free memory blocks for further reusing. Allocation of new block goes
* from dictionary only if dictionary is fat enough. (At the moment of test creation this limit is 64K.)
* So to stress dictionary we should fill it permanently. The easiest way to fill the dictionary
* is to fail class loading. This failed action will return allocated blocks to dictionary.
*
* There are two type of threads in this test: threads, failing classloading and threads,
* loading regular classes and checking they work properly.
*/
public class StressDictionary extends GCTestBase {
private static byte[] bytecode;
private class FillingDictionaryWorker implements Callable<Object> {
private final Random random;
public FillingDictionaryWorker(long seed) {
this.random = new Random(seed);
}
@Override
public Object call() throws Exception {
while (stresser.continueExecution()) {
try {
byte[] badBytecode = bytecode.clone();
badBytecode[random.nextInt(badBytecode.length)] = (byte) 42;
classloader.define(badBytecode);
} catch (Throwable e) {
// We can get ClassFormatError, ClassNotFoundException or anything else here
}
}
return null;
}
}
private class RegularWorker implements Callable<Object> {
@Override
public Object call() throws Exception {
while (stresser.continueExecution()) {
Class<?> c = classloader.define(bytecode);
testClass(c);
}
return null;
}
}
private static String[] args;
private static final String methodName = "myMethod";
private static final int NUMBER_OF_CORRUPTING_THREADS = 10;
private static final int NUMBER_OF_METHOD_CALLS = 50;
private static final int NUMBER_OF_NOT_CORRUPTING_THREADS = 10;
private AtomicLong classesCounter = new AtomicLong(0);
private volatile ClassloaderUnderTest classloader = new ClassloaderUnderTest();
private Random random;
private ExecutionController stresser;
public static void main(String[] args) {
StressDictionary.args = args;
Tests.runTest(new StressDictionary(), args);
}
public void run() {
random = new Random(runParams.getSeed());
stresser = new Stresser(args);
stresser.start(1);
// Generate some bytecodes.
bytecode = generateAndCompile();
List<Callable<Object>> tasks = new LinkedList<Callable<Object>>();
for (int i = 0; i < NUMBER_OF_CORRUPTING_THREADS; i++) {
tasks.add(this.new FillingDictionaryWorker(random.nextLong()));
}
for (int i = 0; i < NUMBER_OF_NOT_CORRUPTING_THREADS; i++) {
tasks.add(this.new RegularWorker());
}
ExecutorService executorService = Executors.newCachedThreadPool();
List<Future<Object>> results = null;
try {
results = executorService.invokeAll(tasks);
} catch (InterruptedException e) {
e.printStackTrace();
}
int act_results = results.size();
int exp_results = NUMBER_OF_CORRUPTING_THREADS +
NUMBER_OF_NOT_CORRUPTING_THREADS;
if (act_results == exp_results) {
System.err.println("INFO: There are " + act_results + " results.");
} else {
throw new RuntimeException("Wrong # of results from invokeAll(); "
+ "exp_results=" + exp_results + "; "
+ "act_results=" + act_results + ".");
}
int cancelled_cnt = 0;
int not_done_cnt = 0;
for (int i = 0; i < act_results; i++) {
if (!results.get(i).isDone()) {
not_done_cnt++;
System.err.println("ERROR: task #" + i + " is not done.");
}
if (results.get(i).isCancelled()) {
cancelled_cnt++;
System.err.println("ERROR: task #" + i + " was canceled.");
}
}
if (cancelled_cnt == 0) {
System.err.println("INFO: no tasks were cancelled.");
}
if (not_done_cnt == 0) {
System.err.println("INFO: all tasks are done.");
}
if (cancelled_cnt != 0 && not_done_cnt != 0) {
throw new RuntimeException(cancelled_cnt
+ " tasks were cancelled and "
+ not_done_cnt
+ " tasks are not done.");
} else if (cancelled_cnt != 0) {
throw new RuntimeException(cancelled_cnt
+ " tasks were cancelled.");
} else if (not_done_cnt != 0) {
throw new RuntimeException(not_done_cnt + " tasks are not done.");
}
}
private byte[] generateAndCompile() {
String className = "MyClass" + classesCounter.incrementAndGet();
return InMemoryJavaCompiler.compile(className, generateSource(className));
}
private CharSequence generateSource(String className) {
return "public class " + className + " { " +
"public static String s1 = \"s1" + random.nextInt() + "\"; " +
"public String s2 = \"s2" + random.nextInt() + "\"; " +
"public String " + methodName + "() {return s1 + s2; } " +
"}";
}
private void testClass(Class<?> clazz) {
try {
for (Method m : clazz.getMethods()) {
if (m.getName().equals(methodName)) {
for (int j = 0; j < NUMBER_OF_METHOD_CALLS; j++) {
m.invoke(clazz.newInstance());
}
}
}
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
log.error("Class check failed: " + e.getMessage());
e.printStackTrace();
setFailed(true);
}
}
}
|