| 1 |
package driver; |
| 2 |
|
| 3 |
import java.lang.Thread.State; |
| 4 |
import java.util.ArrayList; |
| 5 |
import java.util.concurrent.atomic.AtomicLong; |
| 6 |
import java.util.concurrent.atomic.AtomicLongFieldUpdater; |
| 7 |
|
| 8 |
import shared.Parse; |
| 9 |
import shared.PreParser; |
| 10 |
import shared.Shared; |
| 11 |
import shared.Timer; |
| 12 |
import shared.Tools; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
public class TestLockSpeed { |
| 20 |
|
| 21 |
public static void main(String[] args){ |
| 22 |
|
| 23 |
{ |
| 24 |
PreParser pp=new PreParser(args, new Object() { }.getClass().getEnclosingClass(), false); |
| 25 |
args=pp.args; |
| 26 |
|
| 27 |
} |
| 28 |
|
| 29 |
int mode=0; |
| 30 |
long max=1000000000; |
| 31 |
int threads=Shared.threads(); |
| 32 |
|
| 33 |
for(int i=0; i<args.length; i++){ |
| 34 |
String arg=args[i]; |
| 35 |
String[] split=arg.split("="); |
| 36 |
String a=split[0].toLowerCase(); |
| 37 |
String b=split.length>1 ? split[1] : null; |
| 38 |
|
| 39 |
if(a.equals("mode")){ |
| 40 |
mode=Integer.parseInt(b); |
| 41 |
}else if(a.equals("threads") || a.equals("t")){ |
| 42 |
threads=Integer.parseInt(b); |
| 43 |
}else if(a.equals("max")){ |
| 44 |
max=Parse.parseKMG(b); |
| 45 |
}else{ |
| 46 |
System.err.println("Unknown parameter "+args[i]); |
| 47 |
assert(false) : "Unknown parameter "+args[i]; |
| 48 |
|
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
CountBox box; |
| 53 |
if(mode==UNLOCKED || mode==LOCKED){ |
| 54 |
box=new LockBox(); |
| 55 |
}else if(mode==ATOMIC){ |
| 56 |
box=new AtomBox(); |
| 57 |
}else if(mode==VOLATILE || mode==FIELD || mode==STATICFIELD){ |
| 58 |
box=new VolatileBox(); |
| 59 |
}else{ |
| 60 |
throw new RuntimeException("Unknown mode "+mode); |
| 61 |
} |
| 62 |
|
| 63 |
ArrayList<CountThread> list=new ArrayList<CountThread>(threads); |
| 64 |
for(int i=0; i<threads; i++){ |
| 65 |
list.add(new CountThread(box, max, mode)); |
| 66 |
} |
| 67 |
|
| 68 |
Timer t=new Timer(); |
| 69 |
|
| 70 |
for(CountThread ct : list){ct.start();} |
| 71 |
for(CountThread ct : list){ |
| 72 |
while(ct.getState()!=State.TERMINATED){ |
| 73 |
try { |
| 74 |
ct.join(); |
| 75 |
} catch (InterruptedException e) { |
| 76 |
|
| 77 |
e.printStackTrace(); |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
t.stop(); |
| 83 |
System.out.println("Time: \t"+t); |
| 84 |
System.out.println("Count: \t"+box.value()); |
| 85 |
System.out.println("Speed: \t"+Tools.format("%.3f", (threads*max*1.0)/(t.elapsed))+" giga per second"); |
| 86 |
|
| 87 |
} |
| 88 |
|
| 89 |
static class CountThread extends Thread{ |
| 90 |
|
| 91 |
public CountThread(CountBox box_, long max_, int mode_){ |
| 92 |
box0=box_; |
| 93 |
max=max_; |
| 94 |
mode=mode_; |
| 95 |
} |
| 96 |
|
| 97 |
@Override |
| 98 |
public void run(){ |
| 99 |
if(mode==UNLOCKED){ |
| 100 |
final LockBox box=(LockBox)box0; |
| 101 |
for(long i=0; i<max; i++){ |
| 102 |
box.counter++; |
| 103 |
} |
| 104 |
}else if(mode==LOCKED){ |
| 105 |
final LockBox box=(LockBox)box0; |
| 106 |
for(long i=0; i<max; i++){ |
| 107 |
box.increment(); |
| 108 |
} |
| 109 |
}else if(mode==ATOMIC){ |
| 110 |
final AtomBox box=(AtomBox)box0; |
| 111 |
for(long i=0; i<max; i++){ |
| 112 |
box.increment(); |
| 113 |
} |
| 114 |
}else if(mode==VOLATILE){ |
| 115 |
final VolatileBox box=(VolatileBox)box0; |
| 116 |
for(long i=0; i<max; i++){ |
| 117 |
box.counter++; |
| 118 |
} |
| 119 |
}else if(mode==FIELD){ |
| 120 |
final VolatileBox box=(VolatileBox)box0; |
| 121 |
final AtomicLongFieldUpdater<VolatileBox> updater=AtomicLongFieldUpdater.newUpdater(VolatileBox.class, "counter"); |
| 122 |
for(long i=0; i<max; i++){ |
| 123 |
updater.incrementAndGet(box); |
| 124 |
} |
| 125 |
}else if(mode==STATICFIELD){ |
| 126 |
final VolatileBox box=(VolatileBox)box0; |
| 127 |
for(long i=0; i<max; i++){ |
| 128 |
box.increment(); |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
final CountBox box0; |
| 134 |
final long max; |
| 135 |
final int mode; |
| 136 |
} |
| 137 |
|
| 138 |
abstract static class CountBox{ |
| 139 |
abstract void increment(); |
| 140 |
abstract long value(); |
| 141 |
} |
| 142 |
|
| 143 |
static class LockBox extends CountBox{ |
| 144 |
@Override |
| 145 |
synchronized void increment(){counter++;} |
| 146 |
@Override |
| 147 |
long value(){return counter;} |
| 148 |
long counter; |
| 149 |
} |
| 150 |
|
| 151 |
static class AtomBox extends CountBox{ |
| 152 |
@Override |
| 153 |
void increment(){counter.incrementAndGet();} |
| 154 |
@Override |
| 155 |
long value(){return counter.longValue();} |
| 156 |
AtomicLong counter=new AtomicLong(0); |
| 157 |
} |
| 158 |
|
| 159 |
static class VolatileBox extends CountBox{ |
| 160 |
@Override |
| 161 |
void increment(){updater.incrementAndGet(this);} |
| 162 |
@Override |
| 163 |
long value(){return counter;} |
| 164 |
volatile long counter; |
| 165 |
static final AtomicLongFieldUpdater<VolatileBox> updater=AtomicLongFieldUpdater.newUpdater(VolatileBox.class, "counter"); |
| 166 |
} |
| 167 |
|
| 168 |
static final int UNLOCKED=0, LOCKED=1, ATOMIC=2, VOLATILE=3, FIELD=4, STATICFIELD=5; |
| 169 |
|
| 170 |
} |