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 = 1000; |
11 |
> |
static int nkeys = 1000; |
12 |
|
static int pinsert = 60; |
13 |
|
static int premove = 2; |
14 |
|
static int maxThreads = 100; |
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 |
|
// normalize probabilities wrt random number generator |
50 |
|
removesPerMaxRandom = (int)(((double)premove/100.0 * 0x7FFFFFFFL)); |
51 |
|
insertsPerMaxRandom = (int)(((double)pinsert/100.0 * 0x7FFFFFFFL)); |
52 |
< |
|
52 |
> |
|
53 |
|
System.out.print("Class: " + mapClass.getName()); |
54 |
|
System.out.print(" threads: " + maxThreads); |
55 |
|
System.out.print(" size: " + nkeys); |
58 |
|
System.out.print(" ops: " + nops); |
59 |
|
System.out.println(); |
60 |
|
|
71 |
– |
int k = 1; |
61 |
|
int warmups = 2; |
62 |
< |
for (int i = 1; i <= maxThreads;) { |
62 |
> |
for (int k = 1, i = 1; i <= maxThreads;) { |
63 |
|
Thread.sleep(100); |
64 |
|
test(i, nkeys, mapClass); |
65 |
|
if (warmups > 0) |
67 |
|
else if (i == k) { |
68 |
|
k = i << 1; |
69 |
|
i = i + (i >>> 1); |
70 |
< |
} |
70 |
> |
} |
71 |
|
else if (i == 1 && k == 2) { |
72 |
|
i = k; |
73 |
|
warmups = 1; |
74 |
|
} |
75 |
< |
else |
75 |
> |
else |
76 |
|
i = k; |
77 |
|
} |
78 |
|
pool.shutdown(); |
81 |
|
static Integer[] makeKeys(int n) { |
82 |
|
LoopHelpers.SimpleRandom rng = new LoopHelpers.SimpleRandom(); |
83 |
|
Integer[] key = new Integer[n]; |
84 |
< |
for (int i = 0; i < key.length; ++i) |
84 |
> |
for (int i = 0; i < key.length; ++i) |
85 |
|
key[i] = new Integer(rng.next()); |
86 |
|
return key; |
87 |
|
} |
96 |
|
} |
97 |
|
} |
98 |
|
|
99 |
< |
static void test(int i, int nkeys, Class mapClass) throws Exception { |
99 |
> |
static void test(int i, int nkeys, Class<?> mapClass) throws Exception { |
100 |
|
System.out.print("Threads: " + i + "\t:"); |
101 |
|
Map<Integer, Integer> map = (Map<Integer,Integer>)mapClass.newInstance(); |
102 |
|
Integer[] key = makeKeys(nkeys); |
106 |
|
shuffleKeys(key); |
107 |
|
LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer(); |
108 |
|
CyclicBarrier barrier = new CyclicBarrier(i+1, timer); |
109 |
< |
for (int t = 0; t < i; ++t) |
109 |
> |
for (int t = 0; t < i; ++t) |
110 |
|
pool.execute(new Runner(t, map, key, barrier)); |
111 |
|
barrier.await(); |
112 |
|
barrier.await(); |
113 |
|
long time = timer.getTime(); |
114 |
< |
long tpo = time / (i * (long)nops); |
114 |
> |
long tpo = time / (i * (long) nops); |
115 |
|
System.out.print(LoopHelpers.rightJustify(tpo) + " ns per op"); |
116 |
< |
double secs = (double)(time) / 1000000000.0; |
116 |
> |
double secs = (double) time / 1000000000.0; |
117 |
|
System.out.println("\t " + secs + "s run time"); |
118 |
|
map.clear(); |
119 |
|
} |
126 |
|
int position; |
127 |
|
int total; |
128 |
|
|
129 |
< |
Runner(int id, Map<Integer,Integer> map, Integer[] key, CyclicBarrier barrier) { |
130 |
< |
this.map = map; |
131 |
< |
this.key = key; |
129 |
> |
Runner(int id, Map<Integer,Integer> map, Integer[] key, CyclicBarrier barrier) { |
130 |
> |
this.map = map; |
131 |
> |
this.key = key; |
132 |
|
this.barrier = barrier; |
133 |
< |
position = key.length / 2; |
133 |
> |
position = key.length / 2; |
134 |
|
rng = new LoopHelpers.SimpleRandom((id + 1) * 8862213513L); |
135 |
|
rng.next(); |
136 |
|
} |
137 |
|
|
138 |
|
int step() { |
139 |
< |
// random-walk around key positions, bunching accesses |
139 |
> |
// random-walk around key positions, bunching accesses |
140 |
|
int r = rng.next(); |
141 |
|
position += (r & 7) - 3; |
142 |
< |
while (position >= key.length) position -= key.length; |
142 |
> |
while (position >= key.length) position -= key.length; |
143 |
|
while (position < 0) position += key.length; |
144 |
|
|
145 |
|
Integer k = key[position]; |
146 |
|
Integer x = map.get(k); |
147 |
|
|
148 |
|
if (x != null) { |
149 |
< |
if (x.intValue() != k.intValue()) |
149 |
> |
if (x.intValue() != k.intValue()) |
150 |
|
throw new Error("bad mapping: " + x + " to " + k); |
151 |
|
|
152 |
|
if (r < removesPerMaxRandom) { |
160 |
|
++position; |
161 |
|
map.put(k, k); |
162 |
|
return 2; |
163 |
< |
} |
163 |
> |
} |
164 |
|
|
165 |
|
// Uncomment to add a little computation between accesses |
166 |
|
// total += LoopHelpers.compute1(k.intValue()); |
172 |
|
try { |
173 |
|
barrier.await(); |
174 |
|
int ops = nops; |
175 |
< |
while (ops > 0) |
175 |
> |
while (ops > 0) |
176 |
|
ops -= step(); |
177 |
|
barrier.await(); |
178 |
|
} |
182 |
|
} |
183 |
|
} |
184 |
|
} |
196 |
– |
|