author | dl |
Thu, 03 Mar 2016 10:43:07 -0800 | |
changeset 36233 | f85ed703cf7e |
parent 34347 | 4a17f9e90a0f |
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 |
* @summary Exercise multithreaded maps, by default ConcurrentHashMap. |
|
38 |
* Multithreaded hash table test. Each thread does a random walk |
|
39 |
* though elements of "key" array. On each iteration, it checks if |
|
40 |
* table includes key. If absent, with probability pinsert it |
|
41 |
* inserts it, and if present, with probability premove it removes |
|
42 |
* it. (pinsert and premove are expressed as percentages to simplify |
|
43 |
* parsing from command line.) |
|
36233
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
44 |
* @library /lib/testlibrary/ |
2 | 45 |
*/ |
46 |
||
36233
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
47 |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
34347 | 48 |
|
49 |
import java.util.Map; |
|
50 |
import java.util.SplittableRandom; |
|
51 |
import java.util.concurrent.CyclicBarrier; |
|
52 |
import java.util.concurrent.ExecutorService; |
|
53 |
import java.util.concurrent.Executors; |
|
36233
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
54 |
import jdk.testlibrary.Utils; |
2 | 55 |
|
56 |
public class MapLoops { |
|
36233
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
57 |
static final long LONG_DELAY_MS = Utils.adjustTimeout(10_000); |
2 | 58 |
static final int NKEYS = 100000; |
59 |
static int pinsert = 60; |
|
60 |
static int premove = 2; |
|
61 |
static int maxThreads = 5; |
|
34347 | 62 |
static int nops = 10000; // 1000000 |
2 | 63 |
static int removesPerMaxRandom; |
64 |
static int insertsPerMaxRandom; |
|
65 |
||
66 |
static final ExecutorService pool = Executors.newCachedThreadPool(); |
|
67 |
||
68 |
public static void main(String[] args) throws Exception { |
|
69 |
||
70 |
Class mapClass = null; |
|
71 |
if (args.length > 0) { |
|
72 |
try { |
|
73 |
mapClass = Class.forName(args[0]); |
|
7518 | 74 |
} catch (ClassNotFoundException e) { |
2 | 75 |
throw new RuntimeException("Class " + args[0] + " not found."); |
76 |
} |
|
77 |
} |
|
78 |
else |
|
79 |
mapClass = RWMap.class; |
|
80 |
||
81 |
if (args.length > 1) |
|
82 |
maxThreads = Integer.parseInt(args[1]); |
|
83 |
||
84 |
if (args.length > 2) |
|
85 |
nops = Integer.parseInt(args[2]); |
|
86 |
||
87 |
if (args.length > 3) |
|
88 |
pinsert = Integer.parseInt(args[3]); |
|
89 |
||
90 |
if (args.length > 4) |
|
91 |
premove = Integer.parseInt(args[4]); |
|
92 |
||
93 |
// normalize probabilities wrt random number generator |
|
94 |
removesPerMaxRandom = (int)(((double)premove/100.0 * 0x7FFFFFFFL)); |
|
95 |
insertsPerMaxRandom = (int)(((double)pinsert/100.0 * 0x7FFFFFFFL)); |
|
96 |
||
97 |
System.out.println("Using " + mapClass.getName()); |
|
98 |
||
34347 | 99 |
SplittableRandom rnd = new SplittableRandom(); |
2 | 100 |
Integer[] key = new Integer[NKEYS]; |
101 |
for (int i = 0; i < key.length; ++i) |
|
34347 | 102 |
key[i] = new Integer(rnd.nextInt()); |
2 | 103 |
|
104 |
// warmup |
|
105 |
System.out.println("Warmup..."); |
|
106 |
for (int k = 0; k < 2; ++k) { |
|
107 |
Map<Integer, Integer> map = (Map<Integer,Integer>)mapClass.newInstance(); |
|
108 |
LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer(); |
|
109 |
CyclicBarrier barrier = new CyclicBarrier(1, timer); |
|
34347 | 110 |
new Runner(map, key, barrier, rnd.split()).run(); |
2 | 111 |
map.clear(); |
112 |
} |
|
113 |
||
114 |
for (int i = 1; i <= maxThreads; i += (i+1) >>> 1) { |
|
115 |
System.out.print("Threads: " + i + "\t:"); |
|
116 |
Map<Integer, Integer> map = (Map<Integer,Integer>)mapClass.newInstance(); |
|
117 |
LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer(); |
|
118 |
CyclicBarrier barrier = new CyclicBarrier(i+1, timer); |
|
119 |
for (int k = 0; k < i; ++k) |
|
34347 | 120 |
pool.execute(new Runner(map, key, barrier, rnd.split())); |
2 | 121 |
barrier.await(); |
122 |
barrier.await(); |
|
123 |
long time = timer.getTime(); |
|
124 |
long tpo = time / (i * (long)nops); |
|
125 |
System.out.print(LoopHelpers.rightJustify(tpo) + " ns per op"); |
|
126 |
double secs = (double)(time) / 1000000000.0; |
|
127 |
System.out.println("\t " + secs + "s run time"); |
|
128 |
map.clear(); |
|
129 |
} |
|
130 |
pool.shutdown(); |
|
36233
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
131 |
if (! pool.awaitTermination(LONG_DELAY_MS, MILLISECONDS)) |
2 | 132 |
throw new Error(); |
133 |
} |
|
134 |
||
135 |
static class Runner implements Runnable { |
|
136 |
final Map<Integer,Integer> map; |
|
137 |
final Integer[] key; |
|
138 |
final CyclicBarrier barrier; |
|
34347 | 139 |
final SplittableRandom rnd; |
2 | 140 |
int position; |
141 |
int total; |
|
142 |
||
34347 | 143 |
Runner(Map<Integer,Integer> map, |
144 |
Integer[] key, |
|
145 |
CyclicBarrier barrier, |
|
146 |
SplittableRandom rnd) { |
|
2 | 147 |
this.map = map; |
148 |
this.key = key; |
|
149 |
this.barrier = barrier; |
|
34347 | 150 |
this.rnd = rnd; |
2 | 151 |
position = key.length / 2; |
152 |
} |
|
153 |
||
154 |
int step() { |
|
32990
299a81977f48
8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents:
22293
diff
changeset
|
155 |
// random-walk around key positions, bunching accesses |
34347 | 156 |
int r = rnd.nextInt(Integer.MAX_VALUE); |
2 | 157 |
position += (r & 7) - 3; |
158 |
while (position >= key.length) position -= key.length; |
|
159 |
while (position < 0) position += key.length; |
|
160 |
||
161 |
Integer k = key[position]; |
|
162 |
Integer x = map.get(k); |
|
163 |
||
164 |
if (x != null) { |
|
165 |
if (x.intValue() != k.intValue()) |
|
166 |
throw new Error("bad mapping: " + x + " to " + k); |
|
167 |
||
168 |
if (r < removesPerMaxRandom) { |
|
32990
299a81977f48
8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents:
22293
diff
changeset
|
169 |
// get away from this position |
2 | 170 |
position = r % key.length; |
171 |
map.remove(k); |
|
172 |
return 2; |
|
173 |
} |
|
174 |
else |
|
175 |
total += LoopHelpers.compute2(LoopHelpers.compute1(x.intValue())); |
|
176 |
} |
|
177 |
else { |
|
178 |
if (r < insertsPerMaxRandom) { |
|
179 |
map.put(k, k); |
|
180 |
return 2; |
|
181 |
} |
|
182 |
} |
|
183 |
return 1; |
|
184 |
} |
|
185 |
||
186 |
public void run() { |
|
187 |
try { |
|
188 |
barrier.await(); |
|
189 |
int ops = nops; |
|
190 |
while (ops > 0) |
|
191 |
ops -= step(); |
|
192 |
barrier.await(); |
|
193 |
} |
|
194 |
catch (Exception ex) { |
|
195 |
ex.printStackTrace(); |
|
196 |
} |
|
197 |
} |
|
198 |
} |
|
199 |
} |