-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGenerateHistory.java
More file actions
241 lines (219 loc) · 7.38 KB
/
GenerateHistory.java
File metadata and controls
241 lines (219 loc) · 7.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package ticketingsystem;
import java.util.List;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
class ThreadId {
// Atomic integer containing the next thread ID to be assigned
private static final AtomicInteger nextId = new AtomicInteger(0);
// Thread local variable containing each thread's ID
private static final ThreadLocal<Integer> threadId =
new ThreadLocal<Integer>() {
@Override protected Integer initialValue() {
return nextId.getAndIncrement();
}
};
// Returns the current thread's unique ID, assigning it if necessary
public static int get() {
return threadId.get();
}
}
class myInt {
volatile int value;
}
public class GenerateHistory {
static int threadnum;//input
static int testnum;//input
static boolean isSequential;//input
static int msec = 0;
static int nsec = 0;
static int totalPc;
static AtomicInteger sLock = new AtomicInteger(0); //Synchronization Lock
static boolean[] fin;
protected static boolean exOthNotFin(int tNum, int tid) {
boolean flag = false;
for (int k = 0; k < tNum; k++) {
if (k == tid) continue;
flag = (flag || !(fin[k]));
}
return flag;
}
static void SLOCK_TAKE() {
while (sLock.compareAndSet(0, 1) == false) {}
}
static void SLOCK_GIVE() {
sLock.set(0);
}
static boolean SLOCK_TRY() {
return (sLock.get() == 0);
}
/****************Manually Set Testing Information **************/
final static int routenum = 3; // route is designed from 1 to 3
final static int coachnum = 3; // coach is arranged from 1 to 5
final static int seatnum = 3; // seat is allocated from 1 to 20
final static int stationnum = 3; // station is designed from 1 to 5
static TicketingDS tds;
final static List<String> methodList = new ArrayList<String>();
final static List<Integer> freqList = new ArrayList<Integer>();
final static List<Ticket> currentTicket = new ArrayList<Ticket>();
final static List<String> currentRes = new ArrayList<String>();
final static ArrayList<List<Ticket>> soldTicket = new ArrayList<List<Ticket>>();
volatile static boolean initLock = false;
// final static AtomicInteger tidGen = new AtomicInteger(0);
final static Random rand = new Random();
public static void initialization(){
tds = new TicketingDS(routenum, coachnum, seatnum, stationnum, threadnum);
for(int i = 0; i < threadnum; i++){
List<Ticket> threadTickets = new ArrayList<Ticket>();
soldTicket.add(threadTickets);
currentTicket.add(null);
currentRes.add("");
}
methodList.add("refundTicket");
freqList.add(10);
methodList.add("buyTicket");
freqList.add(30);
methodList.add("inquiry");
freqList.add(0);
totalPc = 40;
}
public static String getPassengerName() {
long uid = rand.nextInt(testnum);
return "passenger" + uid;
}
public static void print(long preTime, long postTime, String actionName){
Ticket ticket = currentTicket.get(ThreadId.get());
System.out.println(preTime + " " + postTime + " " + ThreadId.get() + " " + actionName + " " + ticket.tid + " " + ticket.passenger + " " + ticket.route + " " + ticket.coach + " " + ticket.departure + " " + ticket.arrival + " " + ticket.seat + " " + currentRes.get(ThreadId.get()));
}
public static boolean execute(int num){
int route, departure, arrival;
Ticket ticket = new Ticket();;
switch(num){
case 0://refund
if(soldTicket.get(ThreadId.get()).size() == 0)
return false;
int n = rand.nextInt(soldTicket.get(ThreadId.get()).size());
ticket = soldTicket.get(ThreadId.get()).remove(n);
if(ticket == null){
return false;
}
currentTicket.set(ThreadId.get(), ticket);
boolean flag = tds.refundTicket(ticket);
currentRes.set(ThreadId.get(), "true");
return flag;
case 1://buy
String passenger = getPassengerName();
route = rand.nextInt(routenum) + 1;
departure = rand.nextInt(stationnum - 1) + 1;
arrival = departure + rand.nextInt(stationnum - departure) + 1;
ticket = tds.buyTicket(passenger, route, departure, arrival);
if(ticket == null){
ticket = new Ticket();
ticket.passenger = passenger;
ticket.route = route;
ticket.departure = departure;
ticket.arrival = arrival;
ticket.seat = 0;
currentTicket.set(ThreadId.get(), ticket);
currentRes.set(ThreadId.get(), "false");
return true;
}
currentTicket.set(ThreadId.get(), ticket);
currentRes.set(ThreadId.get(), "true");
soldTicket.get(ThreadId.get()).add(ticket);
return true;
case 2:
ticket.passenger = getPassengerName();
ticket.route = rand.nextInt(routenum) + 1;
ticket.departure = rand.nextInt(stationnum - 1) + 1;
ticket.arrival = ticket.departure + rand.nextInt(stationnum - ticket.departure) + 1; // arrival is always greater than departure
ticket.seat = tds.inquiry(ticket.route, ticket.departure, ticket.arrival);
currentTicket.set(ThreadId.get(), ticket);
currentRes.set(ThreadId.get(), "true");
return true;
default:
System.out.println("Error in execution.");
return false;
}
}
/***********VeriLin***********/
public static void main(String[] args) throws InterruptedException {
if(args.length != 5){
System.out.println("The arguments of GenerateHistory is threadNum, testNum, isSequential(0/1), delay(millionsec), delay(nanosec)");
return;
}
threadnum = Integer.parseInt(args[0]);
testnum = Integer.parseInt(args[1]);
if(args[2].equals("0")){
isSequential = false;
}
else if(args[2].equals("1")){
isSequential = true;
}
else{
System.out.println("The arguments of GenerateHistory is threadNum, testNum, isSequential(0/1)");
return;
}
msec = Integer.parseInt(args[3]);
nsec = Integer.parseInt(args[4]);
Thread[] threads = new Thread[threadnum];
myInt barrier = new myInt();
fin = new boolean[threadnum];
final long startTime = System.nanoTime();
for (int i = 0; i < threadnum; i++) {
threads[i] = new Thread(new Runnable() {
public void run() {
if(ThreadId.get() == 0){
initialization();
initLock = true;
}
else{
while(!initLock){
;
}
}
for(int k = 0; k < testnum; k++){
int sel = rand.nextInt(totalPc);
int cnt = 0;
if(isSequential){
while (ThreadId.get() != barrier.value && exOthNotFin(threadnum, ThreadId.get()) == true) {}
SLOCK_TAKE();
}
for(int j = 0; j < methodList.size(); j++){
if(sel >= cnt && sel < cnt + freqList.get(j)){
if(msec != 0 || nsec != 0){
try{
Thread.sleep(msec, nsec);
}catch(InterruptedException e){
return;
}
}
long preTime = System.nanoTime() - startTime;
boolean flag = execute(j);
long postTime = System.nanoTime() - startTime;
if(flag){
print(preTime, postTime, methodList.get(j));
}
cnt += freqList.get(j);
}
}
if (isSequential) {
if (k == testnum - 1)
fin[ThreadId.get()] = true;
if (exOthNotFin(threadnum, ThreadId.get()) == true) {
barrier.value = rand.nextInt(threadnum);
while (fin[barrier.value] == true) {
barrier.value = rand.nextInt(threadnum);
}
}
SLOCK_GIVE();
}
}
}
});
threads[i].start();
}
for (int i = 0; i< threadnum; i++) {
threads[i].join();
}
}
}