1 |
/* |
2 |
* Written by Bill Scherer and Doug Lea with assistance from members |
3 |
* of JCP JSR-166 Expert Group and released to the public domain. Use, |
4 |
* modify, and redistribute this code in any way without |
5 |
* acknowledgement. |
6 |
*/ |
7 |
|
8 |
import java.util.concurrent.*; |
9 |
import java.util.concurrent.atomic.*; |
10 |
import java.util.concurrent.locks.*; |
11 |
|
12 |
public class ExchangeLoops { |
13 |
static final int NCPUS = Runtime.getRuntime().availableProcessors(); |
14 |
|
15 |
static final int DEFAULT_THREADS = NCPUS + 2; |
16 |
static final long DEFAULT_TRIAL_MILLIS = 10000; |
17 |
|
18 |
public static void main(String[] args) throws Exception { |
19 |
int maxThreads = DEFAULT_THREADS; |
20 |
long trialMillis = DEFAULT_TRIAL_MILLIS; |
21 |
int nReps = 3; |
22 |
|
23 |
// Parse and check args |
24 |
int argc = 0; |
25 |
while (argc < args.length) { |
26 |
String option = args[argc++]; |
27 |
if (option.equals("-t")) |
28 |
trialMillis = Integer.parseInt(args[argc]); |
29 |
else if (option.equals("-r")) |
30 |
nReps = Integer.parseInt(args[argc]); |
31 |
else |
32 |
maxThreads = Integer.parseInt(option); |
33 |
argc++; |
34 |
} |
35 |
|
36 |
// Display runtime parameters |
37 |
System.out.print("ExchangeTest"); |
38 |
System.out.print(" -t " + trialMillis); |
39 |
System.out.print(" -r " + nReps); |
40 |
System.out.print(" max threads " + maxThreads); |
41 |
System.out.println(); |
42 |
long warmupTime = 2000; |
43 |
long sleepTime = 100; |
44 |
int nw = maxThreads >= 3? 3 : 2; |
45 |
|
46 |
System.out.println("Warmups.."); |
47 |
oneRun(3, warmupTime); |
48 |
Thread.sleep(sleepTime); |
49 |
|
50 |
for (int i = maxThreads; i >= 2; i -= 1) { |
51 |
oneRun(i, warmupTime++); |
52 |
// System.gc(); |
53 |
Thread.sleep(sleepTime); |
54 |
} |
55 |
|
56 |
/* |
57 |
for (int i = maxThreads; i >= 2; i -= 1) { |
58 |
oneRun(i, warmupTime++); |
59 |
} |
60 |
*/ |
61 |
|
62 |
for (int j = 0; j < nReps; ++j) { |
63 |
System.out.println("Trial: " + j); |
64 |
for (int i = 2; i <= maxThreads; i += 2) { |
65 |
oneRun(i, trialMillis); |
66 |
// System.gc(); |
67 |
Thread.sleep(sleepTime); |
68 |
} |
69 |
for (int i = maxThreads; i >= 2; i -= 2) { |
70 |
oneRun(i, trialMillis); |
71 |
// System.gc(); |
72 |
Thread.sleep(sleepTime); |
73 |
} |
74 |
Thread.sleep(sleepTime); |
75 |
} |
76 |
|
77 |
|
78 |
} |
79 |
|
80 |
static void oneRun(int nThreads, long trialMillis) throws Exception { |
81 |
System.out.printf("%4d threads", nThreads); |
82 |
System.out.printf("%9dms", trialMillis); |
83 |
Exchanger x = new Exchanger(); |
84 |
Runner[] runners = new Runner[nThreads]; |
85 |
Thread[] threads = new Thread[nThreads]; |
86 |
for (int i = 0; i < nThreads; ++i) { |
87 |
runners[i] = new Runner(x); |
88 |
threads[i] = new Thread(runners[i]); |
89 |
// int h = System.identityHashCode(threads[i]); |
90 |
// h ^= h << 1; |
91 |
// h ^= h >>> 3; |
92 |
// h ^= h << 10; |
93 |
// System.out.printf("%10x\n", h); |
94 |
} |
95 |
|
96 |
long startTime = System.nanoTime(); |
97 |
for (int i = 0; i < nThreads; ++i) { |
98 |
threads[i].start(); |
99 |
} |
100 |
Thread.sleep(trialMillis); |
101 |
for (int i = 0; i < nThreads; ++i) |
102 |
threads[i].interrupt(); |
103 |
long elapsed = System.nanoTime() - startTime; |
104 |
for (int i = 0; i < nThreads; ++i) |
105 |
threads[i].join(); |
106 |
int iters = 1; |
107 |
// System.out.println(); |
108 |
for (int i = 0; i < nThreads; ++i) { |
109 |
int ipr = runners[i].iters; |
110 |
// System.out.println(ipr); |
111 |
iters += ipr; |
112 |
} |
113 |
long rate = iters * 1000L * 1000L * 1000L / elapsed; |
114 |
long npt = elapsed / iters; |
115 |
System.out.printf("%9d it/s ", rate); |
116 |
System.out.printf("%9d ns/it", npt); |
117 |
System.out.println(); |
118 |
// x.printStats(); |
119 |
} |
120 |
|
121 |
static final class Runner implements Runnable { |
122 |
final Exchanger exchanger; |
123 |
final Object mine = new Integer(2688); |
124 |
volatile int iters; |
125 |
Runner(Exchanger x) { this.exchanger = x; } |
126 |
|
127 |
public void run() { |
128 |
Exchanger x = exchanger; |
129 |
Object m = mine; |
130 |
int i = 0; |
131 |
try { |
132 |
for (;;) { |
133 |
Object e = x.exchange(m); |
134 |
if (e == null || e == m) |
135 |
throw new Error(); |
136 |
m = e; |
137 |
++i; |
138 |
} |
139 |
} catch (InterruptedException ie) { |
140 |
iters = i; |
141 |
} |
142 |
} |
143 |
} |
144 |
} |
145 |
|