af363ab2badf4054357f4b18e1e1fdb020ab5b0b
[IRC.git] / Robust / src / Benchmarks / SingleTM / Vacation / Vacation.java
1 /* =============================================================================
2  *
3  * vacation.c
4  *
5  * =============================================================================
6  *
7  * Copyright (C) Stanford University, 2006.  All Rights Reserved.
8  * Author: Chi Cao Minh
9  *
10  * =============================================================================
11  *
12  * For the license of bayes/sort.h and bayes/sort.c, please see the header
13  * of the files.
14  * 
15  * ------------------------------------------------------------------------
16  * 
17  * For the license of kmeans, please see kmeans/LICENSE.kmeans
18  * 
19  * ------------------------------------------------------------------------
20  * 
21  * For the license of ssca2, please see ssca2/COPYRIGHT
22  * 
23  * ------------------------------------------------------------------------
24  * 
25  * For the license of lib/mt19937ar.c and lib/mt19937ar.h, please see the
26  * header of the files.
27  * 
28  * ------------------------------------------------------------------------
29  * 
30  * For the license of lib/rbtree.h and lib/rbtree.c, please see
31  * lib/LEGALNOTICE.rbtree and lib/LICENSE.rbtree
32  * 
33  * ------------------------------------------------------------------------
34  * 
35  * Unless otherwise noted, the following license applies to STAMP files:
36  * 
37  * Copyright (c) 2007, Stanford University
38  * All rights reserved.
39  * 
40  * Redistribution and use in source and binary forms, with or without
41  * modification, are permitted provided that the following conditions are
42  * met:
43  * 
44  *     * Redistributions of source code must retain the above copyright
45  *       notice, this list of conditions and the following disclaimer.
46  * 
47  *     * Redistributions in binary form must reproduce the above copyright
48  *       notice, this list of conditions and the following disclaimer in
49  *       the documentation and/or other materials provided with the
50  *       distribution.
51  * 
52  *     * Neither the name of Stanford University nor the names of its
53  *       contributors may be used to endorse or promote products derived
54  *       from this software without specific prior written permission.
55  * 
56  * THIS SOFTWARE IS PROVIDED BY STANFORD UNIVERSITY ``AS IS'' AND ANY
57  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
59  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL STANFORD UNIVERSITY BE LIABLE
60  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
61  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
62  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
63  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
64  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
65  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
66  * THE POSSIBILITY OF SUCH DAMAGE.
67  *
68  * =============================================================================
69  */
70
71
72
73
74 public class Vacation {
75 /* =============================================================================
76  * displayUsage
77  * =============================================================================
78  */
79   public Vacation() {
80   }
81
82   public static void displayUsage (String appName) {
83     System.out.println("Usage: %s [options]\n"+ appName);
84     System.out.println("\nOptions:                                             (defaults)\n");
85     System.out.println("    c <UINT>   Number of [c]lients                   (%i)\n"+
86                        PARAM_DEFAULT_CLIENTS);
87     System.out.println("    n <UINT>   [n]umber of user queries/transaction  (%i)\n"+
88                        PARAM_DEFAULT_NUMBER);
89     System.out.println("    q <UINT>   Percentage of relations [q]ueried     (%i)\n"+
90                        PARAM_DEFAULT_QUERIES);
91     System.out.println("    r <UINT>   Number of possible [r]elations        (%i)\n"+
92                        PARAM_DEFAULT_RELATIONS);
93     System.out.println("    t <UINT>   Number of [t]ransactions              (%i)\n"+
94                        PARAM_DEFAULT_TRANSACTIONS);
95     System.out.println("    u <UINT>   Percentage of [u]ser transactions     (%i)\n"+
96                        PARAM_DEFAULT_USER);
97     System.exit(1);
98   }
99
100
101 /* =============================================================================
102  * setDefaultParams
103  * =============================================================================
104  */
105
106   int CLIENTS;
107   int NUMBER;
108   int QUERIES;
109   int RELATIONS;
110   int TRANSACTIONS;
111   int USER;
112
113   public void setDefaultParams () {
114     CLIENTS      = PARAM_DEFAULT_CLIENTS;
115     NUMBER       = PARAM_DEFAULT_NUMBER;
116     QUERIES      = PARAM_DEFAULT_QUERIES;
117     RELATIONS    = PARAM_DEFAULT_RELATIONS;
118     TRANSACTIONS = PARAM_DEFAULT_TRANSACTIONS;
119     USER         = PARAM_DEFAULT_USER;
120   }
121
122
123 /* =============================================================================
124  * parseArgs
125  * =============================================================================
126  */
127   public void parseArgs (String argv[]) {
128     int opterr = 0;
129     
130     setDefaultParams();
131     for(int i=0;i<argv.length;i++) {
132       String arg=argv[i];
133       if (arg.equals("-c"))
134         CLIENTS=Integer.parseInt(argv[++i]);
135       else if (arg.equals("-n"))
136         NUMBER=Integer.parseInt(argv[++i]);
137       else if (arg.equals("-q"))
138         QUERIES=Integer.parseInt(argv[++i]);
139       else if (arg.equals("-r"))
140         RELATIONS=Integer.parseInt(argv[++i]);
141       else if (arg.equals("-t"))
142         TRANSACTIONS=Integer.parseInt(argv[++i]);
143       else if (arg.equals("-u"))
144         USER=Integer.parseInt(argv[++i]);
145       else 
146         opterr++;
147     }
148     
149     if (opterr>0) {
150       displayUsage(argv[0]);
151     }
152   }
153
154
155 /* =============================================================================
156  * addCustomer
157  * -- Wrapper function
158  * =============================================================================
159  */
160   public static boolean addCustomer (Manager managerPtr, int id, int num, int price) {
161     return managerPtr.manager_addCustomer(id);
162   }
163
164
165 /* =============================================================================
166  * initializeManager
167  * =============================================================================
168  */
169   public Manager initializeManager () {
170     int i;
171     int t;
172     System.out.println("Initializing manager... ");
173
174     Random randomPtr = new Random();
175     randomPtr.random_alloc();
176     Manager managerPtr = new Manager();
177
178     int numRelation = RELATIONS;
179     int ids[] = new int[numRelation];
180     for (i = 0; i < numRelation; i++) {
181       ids[i] = i + 1;
182     }
183     
184     for (t = 0; t < 4; t++) {
185       
186       /* Shuffle ids */
187       for (i = 0; i < numRelation; i++) {
188         int x = randomPtr.posrandom_generate() % numRelation;
189         int y = randomPtr.posrandom_generate() % numRelation;
190         int tmp = ids[x];
191         ids[x] = ids[y];
192         ids[y] = tmp;
193       }
194
195         /* Populate table */
196         for (i = 0; i < numRelation; i++) {
197             boolean status;
198             int id = ids[i];
199             int num = ((randomPtr.posrandom_generate() % 5) + 1) * 100;
200             int price = ((randomPtr.posrandom_generate() % 5) * 10) + 50;
201             if (t==0) {
202               status=managerPtr.manager_addCar(id, num, price);
203             } else if (t==1) {
204               status=managerPtr.manager_addFlight(id, num, price);
205             } else if (t==2) {
206               status=managerPtr.manager_addRoom(id, num, price);
207             } else if (t==3) {
208               status=managerPtr.manager_addCustomer(id);
209             }
210             //assert(status);
211         }
212         
213     } /* for t */
214     
215     System.out.println("done.");
216     return managerPtr;
217   }
218
219
220 /* =============================================================================
221  * initializeClients
222  * =============================================================================
223  */
224   public Client[] initializeClients (Manager managerPtr) {
225     Random randomPtr;
226     Client clients[];
227     int i;
228     int numClient = CLIENTS;
229     int numTransaction = TRANSACTIONS;
230     int numTransactionPerClient;
231     int numQueryPerTransaction = NUMBER;
232     int numRelation = RELATIONS;
233     int percentQuery = QUERIES;
234     int queryRange;
235     int percentUser = USER;
236
237     System.out.println("Initializing clients... ");
238
239     randomPtr = new Random();
240     randomPtr.random_alloc();
241
242     clients = new Client[numClient];
243
244     numTransactionPerClient = (int)((double)numTransaction / (double)numClient + 0.5);
245     queryRange = (int)((double)percentQuery / 100.0 * (double)numRelation + 0.5);
246
247     for (i = 0; i < numClient; i++) {
248       clients[i] = new Client(i,
249                               managerPtr,
250                               numTransactionPerClient,
251                               numQueryPerTransaction,
252                               queryRange,
253                               percentUser);
254     }
255
256     System.out.println("done.");
257     System.out.println("    Transactions        = "+ numTransaction);
258     System.out.println("    Clients             = "+ numClient);
259     System.out.println("    Transactions/client = "+ numTransactionPerClient);
260     System.out.println("    Queries/transaction = "+ numQueryPerTransaction);
261     System.out.println("    Relations           = "+ numRelation);
262     System.out.println("    Query percent       = "+ percentQuery);
263     System.out.println("    Query range         = "+ queryRange);
264     System.out.println("    Percent user        = "+ percentUser);
265     
266     return clients;
267 }
268
269
270   
271   /* =============================================================================
272  * main
273  * =============================================================================
274  */
275   public static void main(String argv[]) {
276     Manager managerPtr;
277     Client clients[];
278     long start;
279     long stop;
280     
281     /* Initialization */
282     Vacation vac=new Vacation();
283     vac.parseArgs(argv);
284     managerPtr = vac.initializeManager();
285     clients = vac.initializeClients(managerPtr);
286     int numThread = vac.CLIENTS;
287
288     /* Run transactions */
289     System.out.println("Running clients... ");
290
291     Barrier.setBarrier(numThread);
292
293     for(int i=1;i<numThread;i++) {
294       clients[i].start();
295     }
296
297     start=System.currentTimeMillis();
298     clients[0].run();
299     stop=System.currentTimeMillis();
300
301     System.out.print("done.");
302     long diff=stop-start;
303     System.out.println("TIME="+diff);
304     vac.checkTables(managerPtr);
305     
306     /* Clean up */
307     System.out.println("Deallocating memory... ");
308     /*
309      * TODO: The contents of the manager's table need to be deallocated.
310      */
311     System.out.println("done.");
312   }
313   
314   void checkTables (Manager managerPtr) {
315     int i;
316     int numRelation = RELATIONS;
317     RBTree customerTablePtr = managerPtr.customerTablePtr;
318     RBTree tables[]=new RBTree[3];
319     tables[0]=managerPtr.carTablePtr;
320     tables[1]=managerPtr.flightTablePtr;
321     tables[2]=managerPtr.roomTablePtr;
322     int numTable = 3;
323
324     int t;
325
326     System.out.println("Checking tables... ");
327
328     /* Check for unique customer IDs */
329     int percentQuery = QUERIES;
330     int queryRange = (int)((double)percentQuery / 100.0 * (double)numRelation + 0.5);
331     int maxCustomerId = queryRange + 1;
332     for (i = 1; i <= maxCustomerId; i++) {
333       if (customerTablePtr.find(i)!=null) {
334         if (customerTablePtr.remove(i)) {
335           if (customerTablePtr.find(i)!=null) {
336             System.out.println("ERROR");
337             System.exit(-1);
338           }
339         }
340       }
341     }
342
343     /* Check reservation tables for consistency and unique ids */
344     for (t = 0; t < numTable; t++) {
345       RBTree tablePtr = tables[t];
346       for (i = 1; i <= numRelation; i++) {
347         if (tablePtr.find(i)!=null) {
348           if (t==0) {
349             if (!managerPtr.manager_addCar(i,0,0)) {
350               System.out.println("ERROR3");
351               System.exit(-1);
352             }
353           } else if (t==1) {
354             if (!managerPtr.manager_addFlight(i, 0, 0)) {
355               System.out.println("ERROR3");
356               System.exit(-1);
357             }
358           } else if (t==2) {
359             if (!managerPtr.manager_addRoom(i,0,0)) {
360               System.out.println("ERROR3");
361               System.exit(-1);
362             }
363           }
364
365           if (tablePtr.remove(i)) {
366             if (tablePtr.remove(i)) {
367               System.out.println("ERROR2");
368               System.exit(-1);
369             }
370           }
371         }
372       }
373     }
374
375     System.out.println("done.");
376   }
377
378
379 }  
380   
381   /* =============================================================================
382    *
383    * End of vacation.c
384    *
385    * =============================================================================
386    */
387