1 |
– |
/* |
2 |
– |
* @test |
3 |
– |
* @synopsis Exercise multithreaded maps, by default |
4 |
– |
* ConcurrentHashMap. Each thread does a random walk though elements |
5 |
– |
* of "key" array. On each iteration, it checks if table includes key. |
6 |
– |
* If absent, with probablility pinsert it inserts it, and if present, |
7 |
– |
* with probablility premove it removes it. (pinsert and premove are |
8 |
– |
* expressed as percentages to simplify parsing from command line.) |
9 |
– |
*/ |
1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
< |
* Expert Group and released to the public domain. Use, modify, and |
4 |
< |
* redistribute this code in any way without acknowledgement. |
3 |
> |
* Expert Group and released to the public domain, as explained at |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
16 |
– |
|
7 |
|
import java.util.*; |
8 |
|
import java.util.concurrent.*; |
9 |
|
|
10 |
|
public class MapLoops { |
11 |
< |
static int nkeys = 10000; |
11 |
> |
static int nkeys = 1000; |
12 |
|
static int pinsert = 60; |
13 |
|
static int premove = 2; |
14 |
|
static int maxThreads = 100; |
15 |
< |
static int nops = 8000000; |
15 |
> |
static int nops = 1000000; |
16 |
|
static int removesPerMaxRandom; |
17 |
|
static int insertsPerMaxRandom; |
18 |
|
|
20 |
|
|
21 |
|
public static void main(String[] args) throws Exception { |
22 |
|
|
23 |
< |
Class mapClass = null; |
23 |
> |
Class<?> mapClass = null; |
24 |
|
if (args.length > 0) { |
25 |
|
try { |
26 |
|
mapClass = Class.forName(args[0]); |
27 |
< |
} catch(ClassNotFoundException e) { |
27 |
> |
} catch (ClassNotFoundException e) { |
28 |
|
throw new RuntimeException("Class " + args[0] + " not found."); |
29 |
|
} |
30 |
|
} |
31 |
< |
else |
31 |
> |
else |
32 |
|
mapClass = java.util.concurrent.ConcurrentHashMap.class; |
33 |
|
|
34 |
< |
if (args.length > 1) |
34 |
> |
if (args.length > 1) |
35 |
|
maxThreads = Integer.parseInt(args[1]); |
36 |
|
|
37 |
< |
if (args.length > 2) |
37 |
> |
if (args.length > 2) |
38 |
|
nkeys = Integer.parseInt(args[2]); |
39 |
|
|
40 |
< |
if (args.length > 3) |
40 |
> |
if (args.length > 3) |
41 |
|
pinsert = Integer.parseInt(args[3]); |
42 |
|
|
43 |
< |
if (args.length > 4) |
43 |
> |
if (args.length > 4) |
44 |
|
premove = Integer.parseInt(args[4]); |
45 |
|
|
46 |
< |
if (args.length > 5) |
46 |
> |
if (args.length > 5) |
47 |
|
nops = Integer.parseInt(args[5]); |
48 |
|
|
49 |
+ |
if (nops < nkeys) |
50 |
+ |
nops = nkeys; |
51 |
+ |
|
52 |
|
// normalize probabilities wrt random number generator |
53 |
|
removesPerMaxRandom = (int)(((double)premove/100.0 * 0x7FFFFFFFL)); |
54 |
|
insertsPerMaxRandom = (int)(((double)pinsert/100.0 * 0x7FFFFFFFL)); |
55 |
< |
|
55 |
> |
|
56 |
|
System.out.print("Class: " + mapClass.getName()); |
57 |
|
System.out.print(" threads: " + maxThreads); |
58 |
|
System.out.print(" size: " + nkeys); |
61 |
|
System.out.print(" ops: " + nops); |
62 |
|
System.out.println(); |
63 |
|
|
71 |
– |
int k = 1; |
64 |
|
int warmups = 2; |
65 |
< |
for (int i = 1; i <= maxThreads;) { |
66 |
< |
Thread.sleep(100); |
67 |
< |
test(i, nkeys, mapClass); |
68 |
< |
if (warmups > 0) |
69 |
< |
--warmups; |
70 |
< |
else if (i == k) { |
71 |
< |
k = i << 1; |
72 |
< |
i = i + (i >>> 1); |
73 |
< |
} |
74 |
< |
else if (i == 1 && k == 2) { |
75 |
< |
i = k; |
76 |
< |
warmups = 1; |
65 |
> |
for (int reps = 0; reps < 3; ++reps) { |
66 |
> |
int k = 1; |
67 |
> |
for (int i = 1; i <= maxThreads;) { |
68 |
> |
Thread.sleep(100); |
69 |
> |
test(i, nkeys, mapClass); |
70 |
> |
if (warmups > 0) |
71 |
> |
--warmups; |
72 |
> |
else if (i == k) { |
73 |
> |
k = i << 1; |
74 |
> |
i = i + (i >>> 1); |
75 |
> |
} |
76 |
> |
else if (i == 1 && k == 2) { |
77 |
> |
i = k; |
78 |
> |
warmups = 1; |
79 |
> |
} |
80 |
> |
else |
81 |
> |
i = k; |
82 |
|
} |
86 |
– |
else |
87 |
– |
i = k; |
83 |
|
} |
84 |
|
pool.shutdown(); |
85 |
|
} |
87 |
|
static Integer[] makeKeys(int n) { |
88 |
|
LoopHelpers.SimpleRandom rng = new LoopHelpers.SimpleRandom(); |
89 |
|
Integer[] key = new Integer[n]; |
90 |
< |
for (int i = 0; i < key.length; ++i) |
90 |
> |
for (int i = 0; i < key.length; ++i) |
91 |
|
key[i] = new Integer(rng.next()); |
92 |
|
return key; |
93 |
|
} |
102 |
|
} |
103 |
|
} |
104 |
|
|
105 |
< |
static void test(int i, int nkeys, Class mapClass) throws Exception { |
105 |
> |
static void test(int i, int nkeys, Class<?> mapClass) throws Exception { |
106 |
|
System.out.print("Threads: " + i + "\t:"); |
107 |
|
Map<Integer, Integer> map = (Map<Integer,Integer>)mapClass.newInstance(); |
108 |
|
Integer[] key = makeKeys(nkeys); |
109 |
|
// Uncomment to start with a non-empty table |
110 |
|
// for (int j = 0; j < nkeys; j += 4) // start 1/4 occupied |
111 |
|
// map.put(key[j], key[j]); |
112 |
+ |
shuffleKeys(key); |
113 |
|
LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer(); |
114 |
|
CyclicBarrier barrier = new CyclicBarrier(i+1, timer); |
115 |
< |
for (int t = 0; t < i; ++t) |
116 |
< |
pool.execute(new Runner(map, key, barrier)); |
115 |
> |
for (int t = 0; t < i; ++t) |
116 |
> |
pool.execute(new Runner(t, map, key, barrier)); |
117 |
|
barrier.await(); |
118 |
|
barrier.await(); |
119 |
|
long time = timer.getTime(); |
120 |
< |
long tpo = time / (i * (long)nops); |
120 |
> |
long tpo = time / (i * (long) nops); |
121 |
|
System.out.print(LoopHelpers.rightJustify(tpo) + " ns per op"); |
122 |
< |
double secs = (double)(time) / 1000000000.0; |
122 |
> |
double secs = (double) time / 1000000000.0; |
123 |
|
System.out.println("\t " + secs + "s run time"); |
124 |
|
map.clear(); |
125 |
|
} |
127 |
|
static class Runner implements Runnable { |
128 |
|
final Map<Integer,Integer> map; |
129 |
|
final Integer[] key; |
130 |
< |
final LoopHelpers.SimpleRandom rng = new LoopHelpers.SimpleRandom(); |
130 |
> |
final LoopHelpers.SimpleRandom rng; |
131 |
|
final CyclicBarrier barrier; |
132 |
|
int position; |
133 |
|
int total; |
134 |
|
|
135 |
< |
Runner(Map<Integer,Integer> map, Integer[] key, CyclicBarrier barrier) { |
136 |
< |
this.map = map; |
137 |
< |
this.key = key; |
135 |
> |
Runner(int id, Map<Integer,Integer> map, Integer[] key, CyclicBarrier barrier) { |
136 |
> |
this.map = map; |
137 |
> |
this.key = key; |
138 |
|
this.barrier = barrier; |
139 |
|
position = key.length / 2; |
140 |
+ |
rng = new LoopHelpers.SimpleRandom((id + 1) * 8862213513L); |
141 |
+ |
rng.next(); |
142 |
|
} |
143 |
|
|
144 |
|
int step() { |
145 |
< |
// random-walk around key positions, bunching accesses |
145 |
> |
// random-walk around key positions, bunching accesses |
146 |
|
int r = rng.next(); |
147 |
|
position += (r & 7) - 3; |
148 |
< |
while (position >= key.length) position -= key.length; |
148 |
> |
while (position >= key.length) position -= key.length; |
149 |
|
while (position < 0) position += key.length; |
150 |
|
|
151 |
|
Integer k = key[position]; |
152 |
|
Integer x = map.get(k); |
153 |
|
|
154 |
|
if (x != null) { |
155 |
< |
if (x.intValue() != k.intValue()) |
155 |
> |
if (x.intValue() != k.intValue()) |
156 |
|
throw new Error("bad mapping: " + x + " to " + k); |
157 |
|
|
158 |
|
if (r < removesPerMaxRandom) { |
164 |
|
} |
165 |
|
else if (r < insertsPerMaxRandom) { |
166 |
|
++position; |
167 |
< |
map.put(k, k); |
167 |
> |
map.putIfAbsent(k, k); |
168 |
|
return 2; |
169 |
< |
} |
169 |
> |
} |
170 |
|
|
171 |
|
// Uncomment to add a little computation between accesses |
172 |
|
// total += LoopHelpers.compute1(k.intValue()); |
178 |
|
try { |
179 |
|
barrier.await(); |
180 |
|
int ops = nops; |
181 |
< |
while (ops > 0) |
181 |
> |
while (ops > 0) |
182 |
|
ops -= step(); |
183 |
|
barrier.await(); |
184 |
|
} |
188 |
|
} |
189 |
|
} |
190 |
|
} |
193 |
– |
|