try to get another bm for ppopp paper
[IRC.git] / Robust / src / Benchmarks / SingleTM / Vacation / Client.java
1 /* =============================================================================
2  *
3  * client.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 public class Client extends Thread {
73   int id;
74   Manager managerPtr;
75   Random randomPtr;
76   int numOperation;
77   int numQueryPerTransaction;
78   int queryRange;
79   int percentUser;
80
81   public Client() {}
82
83 /* =============================================================================
84  * client_alloc
85  * -- Returns NULL on failure
86  * =============================================================================
87  */
88   public Client(int id,
89                 Manager managerPtr,
90                 int numOperation,
91                 int numQueryPerTransaction,
92                 int queryRange,
93                 int percentUser) {
94     this.randomPtr = new Random();
95     this.randomPtr.random_alloc();
96     this.id=id;
97     this.managerPtr = managerPtr;
98     randomPtr.random_seed(id);
99     this.numOperation = numOperation;
100     this.numQueryPerTransaction = numQueryPerTransaction;
101     this.queryRange = queryRange;
102     this.percentUser = percentUser;
103   }
104
105
106 /* =============================================================================
107  * selectAction
108  * =============================================================================
109  */
110   public int selectAction (int r, int percentUser) {
111     if (r < percentUser) {
112       return ACTION_MAKE_RESERVATION;
113     } else if ((r & 1)==1) {
114       return ACTION_DELETE_CUSTOMER;
115     } else {
116       return ACTION_UPDATE_TABLES;
117     }
118   }
119
120
121 /* =============================================================================
122  * client_run
123  * -- Execute list operations on the database
124  * =============================================================================
125  */
126   public void run() {
127     int myId = id;
128
129     Manager managerPtr = this.managerPtr;
130     Random randomPtr  = this.randomPtr;
131
132     int numOperation           = this.numOperation;
133     int numQueryPerTransaction = this.numQueryPerTransaction;
134     int queryRange             = this.queryRange;
135     int percentUser            = this.percentUser;
136
137     int types[]  = new int[numQueryPerTransaction];
138     int ids[]    = new int[numQueryPerTransaction];
139     int ops[]    = new int[numQueryPerTransaction];
140     int prices[] = new int[numQueryPerTransaction];
141
142     for (int i = 0; i < numOperation; i++) {
143       int r = randomPtr.posrandom_generate() % 100;
144       int action = selectAction(r, percentUser);
145
146       switch (action) {
147       case ACTION_MAKE_RESERVATION: {
148         int maxPrices[]=new int[NUM_RESERVATION_TYPE];
149         int maxIds[]=new int[NUM_RESERVATION_TYPE];
150         maxPrices[0]=-1;
151         maxPrices[1]=-1;
152         maxPrices[2]=-1;
153         maxIds[0]=-1;
154         maxIds[1]=-1;
155         maxIds[2]=-1;
156         int n;
157         int numQuery = randomPtr.posrandom_generate() % numQueryPerTransaction + 1;
158         int customerId = randomPtr.posrandom_generate() % queryRange + 1;
159         for (n = 0; n < numQuery; n++) {
160           types[n] = randomPtr.random_generate() % NUM_RESERVATION_TYPE;
161           ids[n] = (randomPtr.random_generate() % queryRange) + 1;
162         }
163         boolean isFound = false;
164         //atomic 
165         {
166           for (n = 0; n < numQuery; n++) {
167             int t = types[n];
168             int id = ids[n];
169             int price = -1;
170             switch (t) {
171             case RESERVATION_CAR:
172               if (managerPtr.manager_queryCar(id) >= 0) {
173                 price = managerPtr.manager_queryCarPrice(id);
174               }
175               break;
176             case RESERVATION_FLIGHT:
177               if (managerPtr.manager_queryFlight(id) >= 0) {
178                 price = managerPtr.manager_queryFlightPrice(id);
179               }
180               break;
181             case RESERVATION_ROOM:
182               if (managerPtr.manager_queryRoom(id) >= 0) {
183                 price = managerPtr.manager_queryRoomPrice(id);
184               }
185               break;
186             default:
187               //assert(0);
188             }
189             if (price > maxPrices[t]) {
190               maxPrices[t] = price;
191               maxIds[t] = id;
192               isFound = true;
193             }
194           } /* for n */
195           if (isFound) {
196             managerPtr.manager_addCustomer(customerId);
197           }
198           if (maxIds[RESERVATION_CAR] > 0) {
199             managerPtr.manager_reserveCar(customerId, maxIds[RESERVATION_CAR]);
200           }
201           if (maxIds[RESERVATION_FLIGHT] > 0) {
202             managerPtr.manager_reserveFlight(customerId, maxIds[RESERVATION_FLIGHT]);
203           }
204           if (maxIds[RESERVATION_ROOM] > 0) {
205             managerPtr.manager_reserveRoom(customerId, maxIds[RESERVATION_ROOM]);
206           }
207         }
208         break;
209       }
210
211       case ACTION_DELETE_CUSTOMER: {
212         int customerId = randomPtr.posrandom_generate() % queryRange + 1;
213         //atomic 
214         {
215           int bill = managerPtr.manager_queryCustomerBill(customerId);
216           if (bill >= 0) {
217             managerPtr.manager_deleteCustomer(customerId);
218           }
219         }
220         break;
221       }
222
223       case ACTION_UPDATE_TABLES: {
224         int numUpdate = randomPtr.posrandom_generate() % numQueryPerTransaction + 1;
225         int n;
226         for (n = 0; n < numUpdate; n++) {
227           types[n] = randomPtr.posrandom_generate() % NUM_RESERVATION_TYPE;
228           ids[n] = (randomPtr.posrandom_generate() % queryRange) + 1;
229           ops[n] = randomPtr.posrandom_generate() % 2;
230           if (ops[n]==1) {
231             prices[n] = ((randomPtr.posrandom_generate() % 5) * 10) + 50;
232           }
233         }
234         //atomic 
235         {
236           for (n = 0; n < numUpdate; n++) {
237             int t = types[n];
238             int id = ids[n];
239             int doAdd = ops[n];
240             if (doAdd==1) {
241               int newPrice = prices[n];
242               switch (t) {
243               case RESERVATION_CAR:
244                 managerPtr.manager_addCar(id, 100, newPrice);
245                 break;
246               case RESERVATION_FLIGHT:
247                 managerPtr.manager_addFlight(id, 100, newPrice);
248                 break;
249               case RESERVATION_ROOM:
250                 managerPtr.manager_addRoom(id, 100, newPrice);
251                 break;
252               default:
253                 //assert(0);
254               }
255             } else { /* do delete */
256               switch (t) {
257               case RESERVATION_CAR:
258                 managerPtr.manager_deleteCar(id, 100);
259                 break;
260               case RESERVATION_FLIGHT:
261                 managerPtr.manager_deleteFlight(id);
262                 break;
263               case RESERVATION_ROOM:
264                 managerPtr.manager_deleteRoom(id, 100);
265                 break;
266               default:
267                 //assert(0);
268               }
269             }
270           }
271         }
272         break;
273       }
274         
275       default:
276         //assert(0);
277       } /* switch (action) */
278     } /* for i */
279   }
280   Barrier.enterBarrier();
281 }
282
283 /* =============================================================================
284  *
285  * End of client.c
286  *
287  * =============================================================================
288  */
289
290
291
292