changes.
[IRC.git] / Robust / Transactions / dstm2src / Main.java
1 /*
2  * Main.java
3  *
4  * Copyright 2006 Sun Microsystems, Inc., 4150 Network Circle, Santa
5  * Clara, California 95054, U.S.A.  All rights reserved.  
6  * 
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
13  * in other countries.
14  * 
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
21  * countries.  
22  * 
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.
31  */
32
33 package dstm2;
34 import static dstm2.Defaults.*;
35 import dstm2.benchmark.Benchmark;
36 import TransactionalIO.exceptions.PanicException;
37 import dstm2.factory.Factory;
38
39 public class Main {
40   
41   /**
42    * @param args the command line arguments
43    * usage: dstm.benchmark.Main  -b <benchmarkclass> [-m <managerclass>] [-t <#threads>] [-n <#time-in-ms>] [-e <experiment#>] [-f <factory>"
44    */
45   public static void main(String args[]) {
46     int numThreads = 20;//THREADS;
47     int numMillis  = TIME;
48     int experiment = EXPERIMENT;
49     String managerClassName = MANAGER;
50     Class managerClass = null;
51     String benchmarkClassName = null;
52     Class benchmarkClass = null;
53     
54     String adapterClassName = Defaults.ADAPTER;
55     
56     // discard statistics from previous runs
57     Thread.clear();
58     // Parse and check the args
59     int argc = 0;
60     try {
61       while (argc < args.length) {
62         String option = args[argc++];
63         if (option.equals("-m"))
64           managerClassName = args[argc];
65         else if (option.equals("-b"))
66           benchmarkClassName = args[argc];
67         else if (option.equals("-t"))
68           numThreads = Integer.parseInt(args[argc]);
69         else if (option.equals("-n"))
70           numMillis = Integer.parseInt(args[argc]);
71         else if (option.equals("-e"))
72           experiment = Integer.parseInt(args[argc]);
73         else if (option.equals("-a"))
74           adapterClassName = args[argc];
75         else
76           reportUsageErrorAndDie();
77         argc++;
78       }
79     } catch (NumberFormatException e) {
80       System.out.println("Expected a number: " + args[argc]);
81       System.exit(0);
82     } catch (Exception e) {
83       reportUsageErrorAndDie();
84     }
85     
86     // Initialize contention manager.
87     try {
88       managerClass = Class.forName(MANAGER);
89       Thread.setContentionManagerClass(managerClass);
90     } catch (ClassNotFoundException ex) {
91       reportUsageErrorAndDie();
92     }
93     
94     // Initialize adapter class
95     Thread.setAdapterClass(adapterClassName);
96     
97     // initialize benchmark
98     Benchmark benchmark = null;
99     try {
100       benchmarkClass = Class.forName(benchmarkClassName);
101       benchmark = (Benchmark) benchmarkClass.newInstance();
102     } catch (InstantiationException e) {
103       System.out.format("%s does not implement dstm.benchmark.Benchmark: %s\n", benchmarkClass, e);
104       System.exit(0);
105     } catch (ClassCastException e) {
106       System.out.format("Exception when creating class %s: %s\n", benchmarkClass, e);
107       System.exit(0);
108     } catch (Exception e) {
109       e.printStackTrace(System.out);
110       System.exit(0);
111     }
112     
113     // Set up the benchmark
114     long startTime = 0;
115     
116     Thread[] thread = new Thread[numThreads];
117     System.out.println("Benchmark: " + benchmarkClass);
118     System.out.println("Adapter: " + adapterClassName);
119     System.out.println("Contention manager: " + managerClassName);
120     System.out.println("Threads: " + numThreads);
121     System.out.println("Mix: " + experiment + "% updates");
122     
123     
124         TransactionalIO.benchmarks.benchmark.init();
125     
126     System.out.println((char)97);
127     int j = 97;
128     try {
129         for (int i = 0; i < numThreads; i++){
130              System.out.println((char)j);
131             thread[i] = benchmark.createThread(experiment, (char)j);
132             j++;
133         }
134       
135       startTime = System.currentTimeMillis();
136       for (int i = 0; i < numThreads; i++)
137         thread[i].start();
138       Thread.sleep(numMillis);
139       Thread.stop = true;     // notify threads to stop
140       for (int i = 0; i < numThreads; i++) {
141         thread[i].join();
142       }
143     } catch (Exception e) {
144       e.printStackTrace(System.out);
145       System.exit(0);
146     }
147     long stopTime = System.currentTimeMillis();
148     
149     double elapsed = (double)(stopTime - startTime) / 1000.0;
150     
151     // Run the sanity check for this benchmark
152     try {
153       benchmark.sanityCheck();
154     } catch (Exception e) {
155       e.printStackTrace(System.out);
156     }
157     
158     long committed = Thread.totalCommitted;
159     long total = Thread.totalTotal;
160     if (total > 0) {
161       System.out.printf("Committed: %d\nTotal: %d\nPercent committed: (%d%%)\n",
162           committed,
163           total,
164           (100 * committed) / total);
165     } else {
166       System.out.println("No transactions executed!");
167     }
168     benchmark.report();
169     System.out.println("Elapsed time: " + elapsed + " seconds.");
170     System.out.println("----------------------------------------");
171   }
172   
173   private static void reportUsageErrorAndDie() {
174     System.out.println("usage: dstm2.Main -b <benchmarkclass> [-m <managerclass>] [-t <#threads>] [-n <#time-in-ms>] [-e <experiment#>] [-a <adapter>]");
175     System.exit(0);
176   }
177   
178 }
179