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