ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/ExchangeLoops.java
Revision: 1.2
Committed: Mon Feb 13 12:39:23 2006 UTC (18 years, 3 months ago) by dl
Branch: MAIN
Changes since 1.1: +124 -84 lines
Log Message:
Update exchanger tests

File Contents

# User Rev Content
1 dl 1.1 /*
2 dl 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 dl 1.1 */
7    
8     import java.util.concurrent.*;
9 dl 1.2 import java.util.concurrent.atomic.*;
10     import java.util.concurrent.locks.*;
11 dl 1.1
12     public class ExchangeLoops {
13 dl 1.2 static final int NCPUS = Runtime.getRuntime().availableProcessors();
14 dl 1.1
15 dl 1.2 static final int DEFAULT_THREADS = NCPUS + 2;
16     static final long DEFAULT_TRIAL_MILLIS = 10000;
17 dl 1.1
18     public static void main(String[] args) throws Exception {
19 dl 1.2 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 dl 1.1
36 dl 1.2 // 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 dl 1.1
56 dl 1.2 /*
57     for (int i = maxThreads; i >= 2; i -= 1) {
58     oneRun(i, warmupTime++);
59 dl 1.1 }
60 dl 1.2 */
61 dl 1.1
62 dl 1.2 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 dl 1.1 }
69 dl 1.2 for (int i = maxThreads; i >= 2; i -= 2) {
70     oneRun(i, trialMillis);
71     // System.gc();
72     Thread.sleep(sleepTime);
73 dl 1.1 }
74 dl 1.2 Thread.sleep(sleepTime);
75 dl 1.1 }
76 dl 1.2
77    
78 dl 1.1 }
79    
80 dl 1.2 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 dl 1.1 }
120 dl 1.2
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 dl 1.1
127 dl 1.2 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 dl 1.1 }
145 dl 1.2