1 /*
2  * Copyright (c) 2010, 2018, Oracle and/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 package nsk.sysdict.share;
25 
26 import java.io.File;
27 import java.net.MalformedURLException;
28 import java.net.URL;
29 import java.net.URLClassLoader;
30 import java.util.ArrayList;
31 import java.util.Arrays;
32 import java.util.List;
33 import java.util.Random;
34 import nsk.share.ClassUnloader;
35 import nsk.share.TestFailure;
36 import nsk.share.gc.ThreadedGCTest;
37 import nsk.share.gc.gp.GarbageUtils;
38 import nsk.share.test.ExecutionController;
39 
40 /**
41  * This is the base class for btree & chain tests. It is a standard GCThreaded Test.
42  */
43 public abstract class SysDictTest extends ThreadedGCTest {
44 
45     static String PACKAGE_PREFIX = "nsk.sysdict.share.";
46     // Should we additionally use ClassUnloader.unload to stress GC
47     private boolean isHeapStressed = false;
48     // Should we use one JarLoader or a lot of them
49     private boolean useSingleLoader = true;
50     // Should we use fats.jar with little amount large classes or not
51     boolean useFats = false;
52     URL[] jars;
53 
parseArgs(String args[])54     protected void parseArgs(String args[]) {
55         for (int i = 0; i < args.length; i++) {
56             if (args[i].equals("-stressHeap")) {
57                 isHeapStressed = true;
58             }
59             if (args[i].equals("-useFatClass")) {
60                 useFats = true;
61             }
62             if (args[i].equals("-useSingleLoader")) {
63                 this.useSingleLoader = false;
64             }
65             // jar path is in useal classpath format
66             if (args[i].equals("-jarpath")) {
67                 String[] files = args[i + 1].split(File.pathSeparator);
68                 jars = new URL[files.length];
69                 for (int j = 0; j < files.length; j++) {
70                     try {
71                         jars[j] = new File(files[j]).toURI().toURL();
72                     } catch (MalformedURLException e) {
73                         throw new TestFailure(e);
74                     }
75                 }
76             }
77         }
78     }
79 
80     // each time we create a new classloader
createJarLoader()81     protected ClassLoader createJarLoader() {
82         return new URLClassLoader(jars);
83     }
84 
85     // The btree and chain tests differs by loaders and classes
86     // let define them in subclasses
createLoaders()87     abstract ClassLoader[] createLoaders();
88 
getClassNames()89     abstract String[] getClassNames();
90 
createClassLoadersInternal()91     ClassLoader[] createClassLoadersInternal() {
92         if (!useSingleLoader) {
93             return createLoaders();
94         } else {
95             ClassLoader[] single = new ClassLoader[1];
96             single[0] = createJarLoader();
97             return single;
98         }
99     }
100     volatile ClassLoader[] currentClassLoaders;
101 
102     class Worker implements Runnable {
103 
104         private ClassLoader loader;
105         private String[] names;
106         private ExecutionController stresser;
107         int index;
108         public String tmp;
109 
Worker(int number, String[] classnames)110         public Worker(int number, String[] classnames) {
111 
112             this.index = number;
113             this.names = new String[classnames.length];
114             List<String> listNames = new ArrayList<String>(classnames.length);
115             listNames.addAll(Arrays.asList(classnames));
116             for (int i = 0; i < classnames.length; i++) {
117                 int idx1 = new Random().nextInt(listNames.size());
118                 this.names[i] = listNames.remove(idx1);
119             }
120         }
121 
122         @Override
run()123         public void run() {
124             if (stresser == null) {
125                 stresser = getExecutionController();
126             }
127 
128             // only first thread update all classloaders
129             // do not care about synchronization
130             if (index == 0) {
131                 try {
132                     currentClassLoaders = createClassLoadersInternal();
133                 } catch (OutOfMemoryError oome) {
134                     // skip iterations until all loaders will be unloaded
135                     Thread.yield();
136                     return;
137                 }
138             }
139             for (int i = 0; i < names.length; i++) {
140                 try {
141                     String name = names[i];
142                     if (!stresser.continueExecution()) {
143                         return;
144                     }
145                     // just check if loader was updated
146                     loader = currentClassLoaders[index];
147                     Class clz = Class.forName(name, true, loader);
148                     // set name into public variable just to be sure
149                     // that class is loaded
150                     tmp = clz.getName();
151                 } catch (ClassNotFoundException cnfe) {
152                     throw new TestFailure(cnfe);
153                 } catch (OutOfMemoryError oome) {
154                     // just ignore
155                     // we do not check memory leaks in PermGen in this tests
156                 } catch (StackOverflowError soe) {
157                     // just ignore, chains could be too large
158                     // StackOverflowError could be in some sparcs
159                 }
160             }
161             if (isHeapStressed) {
162                 GarbageUtils.eatMemory(stresser, 50, 1024, 0);
163             }
164         }
165     }
166 
167     @Override
createRunnable(int i)168     protected Runnable createRunnable(int i) {
169         currentClassLoaders = createClassLoadersInternal();
170         return new Worker(i % currentClassLoaders.length, getClassNames());
171     }
172 }
173