Print this page
Split |
Close |
Expand all |
Collapse all |
--- old/test/java/util/concurrent/ConcurrentHashMap/MapLoops.java
+++ new/test/java/util/concurrent/ConcurrentHashMap/MapLoops.java
1 1 /*
2 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3 3 *
4 4 * This code is free software; you can redistribute it and/or modify it
5 5 * under the terms of the GNU General Public License version 2 only, as
6 6 * published by the Free Software Foundation.
7 7 *
8 8 * This code is distributed in the hope that it will be useful, but WITHOUT
9 9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
11 11 * version 2 for more details (a copy is included in the LICENSE file that
12 12 * accompanied this code).
13 13 *
14 14 * You should have received a copy of the GNU General Public License version
15 15 * 2 along with this work; if not, write to the Free Software Foundation,
16 16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17 17 *
18 18 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
19 19 * or visit www.oracle.com if you need additional information or have any
20 20 * questions.
21 21 */
22 22
23 23 /*
24 24 * This file is available under and governed by the GNU General Public
25 25 * License version 2 only, as published by the Free Software Foundation.
26 26 * However, the following notice accompanied the original version of this
↓ open down ↓ |
26 lines elided |
↑ open up ↑ |
27 27 * file:
28 28 *
29 29 * Written by Doug Lea with assistance from members of JCP JSR-166
30 30 * Expert Group and released to the public domain, as explained at
31 31 * http://creativecommons.org/licenses/publicdomain
32 32 */
33 33
34 34 /*
35 35 * @test
36 36 * @bug 4486658
37 - * @compile MapLoops.java
37 + * @compile -source 1.5 MapLoops.java
38 38 * @run main/timeout=1600 MapLoops
39 39 * @summary Exercise multithreaded maps, by default ConcurrentHashMap.
40 40 * Multithreaded hash table test. Each thread does a random walk
41 41 * though elements of "key" array. On each iteration, it checks if
42 42 * table includes key. If absent, with probability pinsert it
43 43 * inserts it, and if present, with probability premove it removes
44 44 * it. (pinsert and premove are expressed as percentages to simplify
45 45 * parsing from command line.)
46 46 */
47 47
48 48 import java.util.*;
49 49 import java.util.concurrent.*;
50 50
51 51 public class MapLoops {
52 52 static int nkeys = 10000;
53 53 static int pinsert = 60;
54 54 static int premove = 2;
55 55 static int maxThreads = 100;
56 56 static int nops = 100000;
57 57 static int removesPerMaxRandom;
58 58 static int insertsPerMaxRandom;
59 59
60 60 static final ExecutorService pool = Executors.newCachedThreadPool();
61 61
62 62 static final List<Throwable> throwables
63 63 = new CopyOnWriteArrayList<Throwable>();
64 64
65 65 public static void main(String[] args) throws Exception {
66 66
67 67 Class mapClass = null;
68 68 if (args.length > 0) {
69 69 try {
70 70 mapClass = Class.forName(args[0]);
71 71 } catch (ClassNotFoundException e) {
72 72 throw new RuntimeException("Class " + args[0] + " not found.");
73 73 }
74 74 }
75 75 else
76 76 mapClass = java.util.concurrent.ConcurrentHashMap.class;
77 77
78 78 if (args.length > 1)
79 79 maxThreads = Integer.parseInt(args[1]);
80 80
81 81 if (args.length > 2)
82 82 nkeys = Integer.parseInt(args[2]);
83 83
84 84 if (args.length > 3)
85 85 pinsert = Integer.parseInt(args[3]);
86 86
87 87 if (args.length > 4)
88 88 premove = Integer.parseInt(args[4]);
89 89
90 90 if (args.length > 5)
91 91 nops = Integer.parseInt(args[5]);
92 92
93 93 // normalize probabilities wrt random number generator
94 94 removesPerMaxRandom = (int)(((double)premove/100.0 * 0x7FFFFFFFL));
95 95 insertsPerMaxRandom = (int)(((double)pinsert/100.0 * 0x7FFFFFFFL));
96 96
97 97 System.out.print("Class: " + mapClass.getName());
98 98 System.out.print(" threads: " + maxThreads);
99 99 System.out.print(" size: " + nkeys);
100 100 System.out.print(" ins: " + pinsert);
101 101 System.out.print(" rem: " + premove);
102 102 System.out.print(" ops: " + nops);
103 103 System.out.println();
104 104
105 105 int k = 1;
106 106 int warmups = 2;
107 107 for (int i = 1; i <= maxThreads;) {
108 108 Thread.sleep(100);
109 109 test(i, nkeys, mapClass);
110 110 if (warmups > 0)
111 111 --warmups;
112 112 else if (i == k) {
113 113 k = i << 1;
114 114 i = i + (i >>> 1);
115 115 }
116 116 else if (i == 1 && k == 2) {
117 117 i = k;
118 118 warmups = 1;
119 119 }
120 120 else
121 121 i = k;
122 122 }
123 123 pool.shutdown();
124 124 if (! pool.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS))
125 125 throw new Error();
126 126
127 127 if (! throwables.isEmpty())
128 128 throw new Error
129 129 (throwables.size() + " thread(s) terminated abruptly.");
130 130 }
131 131
132 132 static Integer[] makeKeys(int n) {
133 133 LoopHelpers.SimpleRandom rng = new LoopHelpers.SimpleRandom();
134 134 Integer[] key = new Integer[n];
135 135 for (int i = 0; i < key.length; ++i)
136 136 key[i] = new Integer(rng.next());
137 137 return key;
138 138 }
139 139
140 140 static void shuffleKeys(Integer[] key) {
141 141 Random rng = new Random();
142 142 for (int i = key.length; i > 1; --i) {
143 143 int j = rng.nextInt(i);
144 144 Integer tmp = key[j];
145 145 key[j] = key[i-1];
146 146 key[i-1] = tmp;
147 147 }
148 148 }
149 149
150 150 static void test(int i, int nkeys, Class mapClass) throws Exception {
151 151 System.out.print("Threads: " + i + "\t:");
152 152 Map<Integer, Integer> map = (Map<Integer,Integer>)mapClass.newInstance();
153 153 Integer[] key = makeKeys(nkeys);
154 154 // Uncomment to start with a non-empty table
155 155 // for (int j = 0; j < nkeys; j += 4) // start 1/4 occupied
156 156 // map.put(key[j], key[j]);
157 157 LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
158 158 CyclicBarrier barrier = new CyclicBarrier(i+1, timer);
159 159 for (int t = 0; t < i; ++t)
160 160 pool.execute(new Runner(map, key, barrier));
161 161 barrier.await();
162 162 barrier.await();
163 163 long time = timer.getTime();
164 164 long tpo = time / (i * (long)nops);
165 165 System.out.print(LoopHelpers.rightJustify(tpo) + " ns per op");
166 166 double secs = (double)(time) / 1000000000.0;
167 167 System.out.println("\t " + secs + "s run time");
168 168 map.clear();
169 169 }
170 170
171 171 static class Runner implements Runnable {
172 172 final Map<Integer,Integer> map;
173 173 final Integer[] key;
174 174 final LoopHelpers.SimpleRandom rng = new LoopHelpers.SimpleRandom();
175 175 final CyclicBarrier barrier;
176 176 int position;
177 177 int total;
178 178
179 179 Runner(Map<Integer,Integer> map, Integer[] key, CyclicBarrier barrier) {
180 180 this.map = map;
181 181 this.key = key;
182 182 this.barrier = barrier;
183 183 position = key.length / 2;
184 184 }
185 185
186 186 int step() {
187 187 // random-walk around key positions, bunching accesses
188 188 int r = rng.next();
189 189 position += (r & 7) - 3;
190 190 while (position >= key.length) position -= key.length;
191 191 while (position < 0) position += key.length;
192 192
193 193 Integer k = key[position];
194 194 Integer x = map.get(k);
195 195
196 196 if (x != null) {
197 197 if (x.intValue() != k.intValue())
198 198 throw new Error("bad mapping: " + x + " to " + k);
199 199
200 200 if (r < removesPerMaxRandom) {
201 201 if (map.remove(k) != null) {
202 202 position = total % key.length; // move from position
203 203 return 2;
204 204 }
205 205 }
206 206 }
207 207 else if (r < insertsPerMaxRandom) {
208 208 ++position;
209 209 map.put(k, k);
210 210 return 2;
211 211 }
212 212
213 213 // Uncomment to add a little computation between accesses
214 214 // total += LoopHelpers.compute1(k.intValue());
215 215 total += r;
216 216 return 1;
217 217 }
↓ open down ↓ |
170 lines elided |
↑ open up ↑ |
218 218
219 219 public void run() {
220 220 try {
221 221 barrier.await();
222 222 int ops = nops;
223 223 while (ops > 0)
224 224 ops -= step();
225 225 barrier.await();
226 226 }
227 227 catch (Throwable throwable) {
228 - synchronized(System.err) {
228 + synchronized (System.err) {
229 229 System.err.println("--------------------------------");
230 230 throwable.printStackTrace();
231 231 }
232 232 throwables.add(throwable);
233 233 }
234 234 }
235 235 }
236 236 }
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX