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 /*
25 * @test
26 *
27 * @summary converted from VM Testbase metaspace/stressDictionary.
28 * VM Testbase keywords: [nonconcurrent, javac]
29 *
30 * @library /vmTestbase /test/lib
31 * @run driver jdk.test.lib.FileInstaller . .
32 * @run main/othervm metaspace.stressDictionary.StressDictionary -stressTime 30
33 */
34
35 package metaspace.stressDictionary;
36
37 import java.util.*;
38 import java.lang.management.ManagementFactory;
39 import java.lang.reflect.InvocationTargetException;
40 import java.lang.reflect.Method;
41 import java.util.concurrent.*;
42 import java.util.concurrent.atomic.AtomicLong;
43
44 import nsk.share.gc.GCTestBase;
45 import nsk.share.test.*;
46 import vm.share.InMemoryJavaCompiler;
47
48 /**
49 * There is a data structure named "dictionary" in class BlockFreelist. It stores
50 * information about free memory blocks for further reusing. Allocation of new block goes
51 * from dictionary only if dictionary is fat enough. (At the moment of test creation this limit is 64K.)
52 * So to stress dictionary we should fill it permanently. The easiest way to fill the dictionary
106
107 public static void main(String[] args) {
108 StressDictionary.args = args;
109 Tests.runTest(new StressDictionary(), args);
110 }
111
112 public void run() {
113 random = new Random(runParams.getSeed());
114 stresser = new Stresser(args);
115 stresser.start(1);
116 // Generate some bytecodes.
117 bytecode = generateAndCompile();
118 List<Callable<Object>> tasks = new LinkedList<Callable<Object>>();
119 for (int i = 0; i < NUMBER_OF_CORRUPTING_THREADS; i++) {
120 tasks.add(this.new FillingDictionaryWorker());
121 }
122 for (int i = 0; i < NUMBER_OF_NOT_CORRUPTING_THREADS; i++) {
123 tasks.add(this.new RegularWorker());
124 }
125 ExecutorService executorService = Executors.newCachedThreadPool();
126 try {
127 executorService.invokeAll(tasks);
128 } catch (InterruptedException e) {
129 e.printStackTrace();
130 }
131 }
132
133 private byte[] generateAndCompile() {
134 Map<String, CharSequence> sources = new HashMap<String, CharSequence>();
135 String className = "MyClass" + classesCounter.incrementAndGet();
136 sources.put(className, generateSource(className));
137 return InMemoryJavaCompiler.compile(sources).values().iterator().next();
138 }
139
140 private CharSequence generateSource(String className) {
141 return "public class " + className + " { " +
142 "public static String s1 = \"s1" + random.nextInt() + "\"; " +
143 "public String s2 = \"s2" + random.nextInt() + "\"; " +
144 "public String " + methodName + "() {return s1 + s2; } " +
145 "}";
146 }
147
148 private void testClass(Class<?> clazz) {
149 try {
150 for (Method m : clazz.getMethods()) {
|
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 /*
25 * @test
26 *
27 * @summary converted from VM Testbase metaspace/stressDictionary.
28 * VM Testbase keywords: [nonconcurrent, javac]
29 *
30 * @library /vmTestbase /test/lib
31 * @run driver jdk.test.lib.FileInstaller . .
32 * @run main/othervm/timeout=300 metaspace.stressDictionary.StressDictionary -stressTime 30
33 */
34
35 package metaspace.stressDictionary;
36
37 import java.util.*;
38 import java.lang.management.ManagementFactory;
39 import java.lang.reflect.InvocationTargetException;
40 import java.lang.reflect.Method;
41 import java.util.concurrent.*;
42 import java.util.concurrent.atomic.AtomicLong;
43
44 import nsk.share.gc.GCTestBase;
45 import nsk.share.test.*;
46 import vm.share.InMemoryJavaCompiler;
47
48 /**
49 * There is a data structure named "dictionary" in class BlockFreelist. It stores
50 * information about free memory blocks for further reusing. Allocation of new block goes
51 * from dictionary only if dictionary is fat enough. (At the moment of test creation this limit is 64K.)
52 * So to stress dictionary we should fill it permanently. The easiest way to fill the dictionary
106
107 public static void main(String[] args) {
108 StressDictionary.args = args;
109 Tests.runTest(new StressDictionary(), args);
110 }
111
112 public void run() {
113 random = new Random(runParams.getSeed());
114 stresser = new Stresser(args);
115 stresser.start(1);
116 // Generate some bytecodes.
117 bytecode = generateAndCompile();
118 List<Callable<Object>> tasks = new LinkedList<Callable<Object>>();
119 for (int i = 0; i < NUMBER_OF_CORRUPTING_THREADS; i++) {
120 tasks.add(this.new FillingDictionaryWorker());
121 }
122 for (int i = 0; i < NUMBER_OF_NOT_CORRUPTING_THREADS; i++) {
123 tasks.add(this.new RegularWorker());
124 }
125 ExecutorService executorService = Executors.newCachedThreadPool();
126 List<Future<Object>> results = null;
127 try {
128 results = executorService.invokeAll(tasks);
129 } catch (InterruptedException e) {
130 e.printStackTrace();
131 }
132
133 int act_results = results.size();
134 int exp_results = NUMBER_OF_CORRUPTING_THREADS +
135 NUMBER_OF_NOT_CORRUPTING_THREADS;
136 if (act_results == exp_results) {
137 System.out.println("INFO: There are " + act_results + " results.");
138 } else {
139 throw new RuntimeException("Wrong # of results from invokeAll(); "
140 + "exp_results=" + exp_results + "; "
141 + "act_results=" + act_results + ".");
142 }
143
144 int cancelled_cnt = 0;
145 int not_done_cnt = 0;
146 for (int i = 0; i < act_results; i++) {
147 if (!results.get(i).isDone()) {
148 not_done_cnt++;
149 System.err.println("ERROR: task #" + i + " is not done.");
150 }
151 if (results.get(i).isCancelled()) {
152 cancelled_cnt++;
153 System.err.println("ERROR: task #" + i + " was canceled.");
154 }
155 }
156
157 if (cancelled_cnt == 0) {
158 System.out.println("INFO: no tasks were cancelled.");
159 }
160 if (not_done_cnt == 0) {
161 System.out.println("INFO: all tasks are done.");
162 }
163 if (cancelled_cnt != 0 && not_done_cnt != 0) {
164 throw new RuntimeException(cancelled_cnt
165 + " tasks were cancelled and "
166 + not_done_cnt
167 + " tasks are not done.");
168 } else if (cancelled_cnt != 0) {
169 throw new RuntimeException(cancelled_cnt
170 + " tasks were cancelled.");
171 } else if (not_done_cnt != 0) {
172 throw new RuntimeException(not_done_cnt + " tasks are not done.");
173 }
174 }
175
176 private byte[] generateAndCompile() {
177 Map<String, CharSequence> sources = new HashMap<String, CharSequence>();
178 String className = "MyClass" + classesCounter.incrementAndGet();
179 sources.put(className, generateSource(className));
180 return InMemoryJavaCompiler.compile(sources).values().iterator().next();
181 }
182
183 private CharSequence generateSource(String className) {
184 return "public class " + className + " { " +
185 "public static String s1 = \"s1" + random.nextInt() + "\"; " +
186 "public String s2 = \"s2" + random.nextInt() + "\"; " +
187 "public String " + methodName + "() {return s1 + s2; } " +
188 "}";
189 }
190
191 private void testClass(Class<?> clazz) {
192 try {
193 for (Method m : clazz.getMethods()) {
|