4 * Copyright 2006 Sun Microsystems, Inc., 4150 Network Circle, Santa
5 * Clara, California 95054, U.S.A. All rights reserved.
7 * Sun Microsystems, Inc. has intellectual property rights relating to
8 * technology embodied in the product that is described in this
9 * document. In particular, and without limitation, these
10 * intellectual property rights may include one or more of the
11 * U.S. patents listed at http://www.sun.com/patents and one or more
12 * additional patents or pending patent applications in the U.S. and
15 * U.S. Government Rights - Commercial software.
16 * Government users are subject to the Sun Microsystems, Inc. standard
17 * license agreement and applicable provisions of the FAR and its
18 * supplements. Use is subject to license terms. Sun, Sun
19 * Microsystems, the Sun logo and Java are trademarks or registered
20 * trademarks of Sun Microsystems, Inc. in the U.S. and other
23 * This product is covered and controlled by U.S. Export Control laws
24 * and may be subject to the export or import laws in other countries.
25 * Nuclear, missile, chemical biological weapons or nuclear maritime
26 * end uses or end users, whether direct or indirect, are strictly
27 * prohibited. Export or reexport to countries subject to
28 * U.S. embargo or to entities identified on U.S. export exclusion
29 * lists, including, but not limited to, the denied persons and
30 * specially designated nationals lists is strictly prohibited.
33 package dstm2.benchmark;
35 import TransactionalIO.exceptions.AbortedException;
36 import TransactionalIO.exceptions.GracefulException;
37 import TransactionalIO.exceptions.PanicException;
40 import TransactionalIO.benchmarks.benchmark;
41 import TransactionalIO.core.TransactionalFile;
42 import dstm2.util.Random;
43 import java.io.IOException;
44 import java.util.Iterator;
45 import java.util.concurrent.Callable;
46 import java.util.logging.Level;
47 import java.util.logging.Logger;
50 * This abstract class is the superclass for the integer set benchmarks.
51 * @author Maurice Herlihy
54 public abstract class IntSetBenchmark implements Benchmark, Iterable<Integer> {
57 * How large to initialize the integer set.
59 protected final int INITIAL_SIZE = 8;
62 * After the run is over, synchronize merging statistics with other threads.
64 static final Object lock = new Object();
75 * Number of calls to insert()
79 * number of calls to contains()
81 int containsCalls = 0;
83 * number of calls to remove()
87 * amount by which the set size has changed
92 * Give subclass a chance to intialize private fields.
94 protected abstract void init();
97 * Iterate through set. Not necessarily thread-safe.
99 public abstract Iterator<Integer> iterator();
102 * Add an element to the integer set, if it is not already there.
103 * @param v the integer value to add from the set
104 * @return true iff value was added.
106 public abstract boolean insert(int v);
109 * Tests wheter a value is in an the integer set.
110 * @param v the integer value to insert into the set
111 * @return true iff presence was confirmed.
113 public abstract boolean contains(int v);
116 * Removes an element from the integer set, if it is there.
117 * @param v the integer value to delete from the set
118 * @return true iff v was removed
120 public abstract boolean remove(int v);
123 * Creates a new test thread.
124 * @param percent Mix of mutators and observers.
125 * @return Thread to run.
127 public Thread createThread(int percent, char sample) {
129 TestThread testThread = new TestThread(this, percent, sample);
131 } catch (Exception e) {
132 e.printStackTrace(System.out);
138 * Prints an error message to <code>System.out</code>, including a
139 * standard header to identify the message as an error message.
140 * @param s String describing error
142 protected static void reportError(String s) {
143 System.out.println(" ERROR: " + s);
147 public void report() {
148 System.out.println("Insert/Remove calls:\t" + (insertCalls + removeCalls));
149 System.out.println("Contains calls:\t" + containsCalls);
152 private class TestThread extends Thread {
153 IntSetBenchmark intSet;
155 * Thread-local statistic.
157 int myInsertCalls = 0;
159 * Thread-local statistic.
161 int myRemoveCalls = 0;
163 * Thread-local statistic.
165 int myContainsCalls = 0;
167 * Thread-local statistic.
169 int myDelta = 0; // net change
170 public int percent = 0; // percent inserts
174 TestThread(IntSetBenchmark intSet, int percent, char sample) {
175 this.intSet = intSet;
176 this.percent = percent;
177 this.sample = sample;
183 Random random = new Random(this.hashCode());
184 random.setSeed(System.currentTimeMillis()); // comment out for determinstic
186 boolean toggle = true;
187 final TransactionalFile f1 = (TransactionalFile)benchmark.TransactionalFiles.get("0");
190 boolean result = true;
191 element = random.nextInt();
192 if (Math.abs(element) % 100 < percent) {
193 if (toggle) { // insert on even turns
194 value = element / 100;
195 result = Thread.doIt(new Callable<Boolean>() {
196 public Boolean call() {
197 //////////////////////////////////////////benchmark 1////////////////////////////
198 /* TransactionalFile f1 = (TransactionalFile)benchmark.m.get("2");
199 byte[] data = new byte[1];
200 char[] holder = new char[10000];
201 char[] word = new char[20];
202 boolean flag = false;
203 long toseek = Integer.valueOf(Thread.currentThread().getName().substring(7)) * 21169;
207 if (toseek != 0) //////////////// skipt the first word since its been read already
208 while (data[0] != '\n'){
217 boolean completeword = false;
219 while (f1.getFilePointer() < toseek +21169)
226 while ((data[0] != '\n' || completeword)){
229 // String str = Mixedbecnhmark.processInput(String.valueOf(word,0,counter-1));
231 // update data structure
232 // byte[] towrite = new byte[String.valueOf(holder,0,i).length()];
233 // towrite = String.valueOf(holder,0,i).getBytes();
235 // ((TransactionalFile) (benchmark.m.get("3"))).write(towrite);
237 // } catch (IOException ex) {
238 // Logger.getLogger(TestThread.class.getName()).log(Level.SEVERE, null, ex);
247 synchronized(benchmark.lock){
248 // if (!(Character.isWhitespace(word[counter])))
249 // System.out.println(String.valueOf(word,0,counter-1));
251 holder[i] = (char)data[0];
258 while(Character.isLetter((char)data[0]))
266 word[counter] = (char)data[0];
270 holder[i] = (char)data[0];
278 return intSet.insert(464);*/
279 ////////////////////////benchmark 2///////////////////
281 /* TransactionalFile f1 = (TransactionalFile)benchmark.m.get("0");
282 byte[] data = new byte[1];
283 char[] holder = new char[10000];
284 char[] word = new char[20];
285 boolean flag = false;
286 long toseek = Integer.valueOf(Thread.currentThread().getName().substring(7)) * 20448;
290 if (toseek != 0) //////////////// skipt the first word since its been read already
291 while (data[0] != '\n'){
301 while (f1.getFilePointer() < toseek +20448)
308 while (data[0] != '\n'){
315 holder[i] = (char)data[0];
320 byte[] towrite = new byte[String.valueOf(holder,0,i).length()];
321 towrite = String.valueOf(holder,0,i).getBytes();
322 // System.out.println(String.valueOf(holder,0,i).toLowerCase().substring(0, 1));
325 ((TransactionalFile) (benchmark.m.get(String.valueOf(holder,0,i).toLowerCase().substring(0, 1)))).write(towrite);
326 //update the memory //}
327 } catch (IOException ex) {
328 Logger.getLogger(TestThread.class.getName()).log(Level.SEVERE, null, ex);
342 // remove on odd turns
344 result = Thread.doIt(new Callable<Boolean>() {
345 public Boolean call() {
346 return intSet.remove(value);
355 Thread.doIt(new Callable<Void>() {
358 intSet.contains(element / 100);
365 } catch (GracefulException g) {
367 synchronized (lock) {
369 insertCalls += myInsertCalls;
370 removeCalls += myRemoveCalls;
371 containsCalls += myContainsCalls;
379 public void sanityCheck() {
380 long expected = INITIAL_SIZE + delta;
383 int prevValue = Integer.MIN_VALUE;
384 for (int value : this) {
386 if (value < prevValue) {
387 System.out.println("ERROR: set not sorted");
390 if (value == prevValue) {
391 System.out.println("ERROR: set has duplicates!");
394 if (length == expected) {
395 System.out.println("ERROR: set has bad length!");
400 System.out.println("Integer Set OK");
404 * Creates a new IntSetBenchmark
406 public IntSetBenchmark() {
409 Random random = new Random(this.hashCode());
410 while (size < INITIAL_SIZE) {
411 if (insert(random.nextInt())) {
417 public void printTree(){};