ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/TimeoutExchangerLoops.java
Revision: 1.8
Committed: Thu Jan 15 18:34:19 2015 UTC (9 years, 4 months ago) by jsr166
Branch: MAIN
Changes since 1.7: +0 -1 lines
Log Message:
delete extraneous blank lines

File Contents

# Content
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, as
4 * explained at http://creativecommons.org/publicdomain/zero/1.0/
5 */
6
7 import java.util.concurrent.*;
8 import java.util.concurrent.atomic.*;
9 import java.util.concurrent.locks.*;
10
11 public class TimeoutExchangerLoops {
12 static final int NCPUS = Runtime.getRuntime().availableProcessors();
13
14 static final int DEFAULT_THREADS = NCPUS + 2;
15 static final long DEFAULT_PATIENCE_NANOS = 500000;
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 long patienceNanos = DEFAULT_PATIENCE_NANOS;
22 int nReps = 3;
23
24 // Parse and check args
25 int argc = 0;
26 while (argc < args.length) {
27 String option = args[argc++];
28 if (option.equals("-t"))
29 trialMillis = Integer.parseInt(args[argc]);
30 else if (option.equals("-p"))
31 patienceNanos = Long.parseLong(args[argc]);
32 else if (option.equals("-r"))
33 nReps = Integer.parseInt(args[argc]);
34 else
35 maxThreads = Integer.parseInt(option);
36 argc++;
37 }
38
39 // Display runtime parameters
40 System.out.print("TimeoutExchangerTest");
41 System.out.print(" -t " + trialMillis);
42 System.out.print(" -p " + patienceNanos);
43 System.out.print(" -r " + nReps);
44 System.out.print(" max threads " + maxThreads);
45 System.out.println();
46
47 System.out.println("Warmups..");
48 long warmupTime = 1000;
49 long sleepTime = 500;
50 if (false) {
51 for (int k = 0; k < 10; ++k) {
52 for (int j = 0; j < 10; ++j) {
53 oneRun(2, (j + 1) * 1000, patienceNanos);
54 Thread.sleep(sleepTime);
55 }
56 }
57 }
58
59 oneRun(3, warmupTime, patienceNanos);
60 Thread.sleep(sleepTime);
61
62 for (int i = maxThreads; i >= 2; i -= 1) {
63 oneRun(i, warmupTime, patienceNanos);
64 Thread.sleep(sleepTime);
65 }
66
67 for (int j = 0; j < nReps; ++j) {
68 System.out.println("Replication " + j);
69 for (int i = 2; i <= maxThreads; i += 2) {
70 oneRun(i, trialMillis, patienceNanos);
71 Thread.sleep(sleepTime);
72 }
73 }
74 }
75
76 static void oneRun(int nThreads, long trialMillis, long patienceNanos)
77 throws Exception {
78 System.out.printf("%4d threads", nThreads);
79 System.out.printf("%9dms", trialMillis);
80 final CountDownLatch start = new CountDownLatch(1);
81 Exchanger x = new Exchanger();
82 Runner[] runners = new Runner[nThreads];
83 Thread[] threads = new Thread[nThreads];
84 for (int i = 0; i < nThreads; ++i) {
85 runners[i] = new Runner(x, patienceNanos, start);
86 threads[i] = new Thread(runners[i]);
87 threads[i].start();
88 }
89 long startTime = System.nanoTime();
90 start.countDown();
91 Thread.sleep(trialMillis);
92 for (int i = 0; i < nThreads; ++i)
93 threads[i].interrupt();
94 long elapsed = System.nanoTime() - startTime;
95 for (int i = 0; i < nThreads; ++i)
96 threads[i].join();
97 int iters = 0;
98 long fails = 0;
99 for (int i = 0; i < nThreads; ++i) {
100 iters += runners[i].iters;
101 fails += runners[i].failures;
102 }
103 if (iters <= 0) iters = 1;
104 long rate = iters * 1000L * 1000L * 1000L / elapsed;
105 long npt = elapsed / iters;
106 double failRate = (fails * 100.0) / (double) iters;
107 System.out.printf("%9d it/s ", rate);
108 System.out.printf("%9d ns/it", npt);
109 System.out.printf("%9.5f%% fails", failRate);
110 System.out.println();
111 // x.printStats();
112 }
113
114 static final class Runner implements Runnable {
115 final Exchanger exchanger;
116 final CountDownLatch start;
117 final long patience;
118 volatile int iters;
119 volatile int failures;
120 Runner(Exchanger x, long patience, CountDownLatch start) {
121 this.exchanger = x;
122 this.patience = patience;
123 this.start = start;
124 }
125
126 public void run() {
127 int i = 0;
128 try {
129 Exchanger x = exchanger;
130 Object m = new Integer(17);
131 long p = patience;
132 start.await();
133 for (;;) {
134 try {
135 Object e = x.exchange(m, p, TimeUnit.NANOSECONDS);
136 if (e == null || e == m)
137 throw new Error();
138 m = e;
139 ++i;
140 } catch (TimeoutException to) {
141 if (Thread.interrupted()) {
142 iters = i;
143 return;
144 }
145 ++i;
146 ++failures;
147 }
148 }
149 } catch (InterruptedException ie) {
150 iters = i;
151 }
152 }
153 }
154 }