added files for new dsm directory
[IRC.git] / Robust / src / Benchmarks / Distributed / LookUpService / dsm / LookUpService.java
1 public class LookUpService extends Thread {
2   DistributedHashMap mydhmap;
3   /**
4    * The thread id involved 
5    **/
6   private int threadid;
7   /**
8    * The total number of threads
9    **/
10   private int numthreads;
11
12   /**
13    * The total number of transactions 
14    **/
15   private int numtrans;
16
17   /**
18    * The total number of objects created
19    **/
20   private int nobjs;
21
22   /**
23    * The probability of initiating a look up
24    * the read probability % between 0-99
25    **/
26   private int rdprob;
27
28   public LookUpService() {
29   }
30
31   public LookUpService(DistributedHashMap dmap, int threadid, int numthreads, int nobjs, int numtrans, int rdprob) {
32     mydhmap = dmap;
33     this.threadid = threadid;
34     this.numthreads = numthreads;
35     this.nobjs = nobjs;
36     this.numtrans = numtrans;
37     this.rdprob = rdprob;
38   }
39
40   public void run() {
41     Barrier barr;
42     barr = new Barrier("128.195.136.162");
43     //Add to the hash map
44     int ntrans;
45     atomic {
46       for(int i = 0; i < nobjs; i++) {
47         Integer key = global new Integer(threadid*nobjs+i);
48         Integer val = global new Integer(i);
49         Object o1 = key;
50         Object o2 = val;
51         mydhmap.put(o1, o2);
52       }
53       ntrans = numtrans;
54     }
55     Barrier.enterBarrier(barr);
56     // Do read/writes
57     for (int i = 0; i < ntrans; i++) {
58       atomic {
59         Random rand = new Random(i);
60         int rdwr = rand.nextInt(100);
61         int rwkey = rand.nextInt(nobjs*numthreads);
62         Integer key = global new Integer(rwkey);
63         Object o1 = key;
64         if (rdwr < rdprob) {
65           Object o3 = mydhmap.get(o1); //Read
66         } else {
67           Integer val = global new Integer(i);
68           Object o2 = val;
69           mydhmap.put(o1, o2); //Modify 
70         }
71       }
72     }
73   }
74
75   public static void main(String[] args) {
76     LookUpService ls = new LookUpService();
77     LookUpService.parseCmdLine(args,ls);
78     BarrierServer mybarr;
79
80     int nthreads = ls.numthreads;
81     int[] mid = new int[8];
82     mid[0] = (128<<24)|(195<<16)|(136<<8)|162;//dc-1
83     mid[1] = (128<<24)|(195<<16)|(136<<8)|163;//dc-2
84     mid[2] = (128<<24)|(195<<16)|(136<<8)|164;//dc-3
85     mid[3] = (128<<24)|(195<<16)|(136<<8)|165;//dc-4
86     mid[4] = (128<<24)|(195<<16)|(136<<8)|166;//dc-5
87     mid[5] = (128<<24)|(195<<16)|(136<<8)|167;//dc-6
88     mid[6] = (128<<24)|(195<<16)|(136<<8)|168;//dc-7
89     mid[7] = (128<<24)|(195<<16)|(136<<8)|169;//dc-8
90
91     LookUpService[] lus;
92     DistributedHashMap dhmap;
93     atomic {
94       mybarr = global new BarrierServer(nthreads);
95     }
96     
97     mybarr.start(mid[0]);
98     atomic {
99       dhmap = global new DistributedHashMap(100, 100, 0.75f);
100       lus = global new LookUpService[nthreads];
101       for(int i = 0; i<nthreads; i++) {
102         lus[i] = global new LookUpService(dhmap, i, ls.numthreads, ls.nobjs, ls.numtrans, ls.rdprob);
103       }
104     }
105
106     boolean waitfordone=true;
107     while(waitfordone) {
108       atomic {  //Master aborts are from here
109         if (mybarr.done)
110           waitfordone=false;
111       }
112     }
113
114     LookUpService tmp;
115     /* Start threads */
116     for(int i = 0; i<nthreads; i++) {
117       atomic {
118         tmp = lus[i];
119       }
120       tmp.start(mid[i]);
121     }
122
123     /* Join threads */
124     for(int i = 0; i<nthreads; i++) {
125       atomic {
126         tmp = lus[i];
127       }
128       tmp.join();
129     }
130
131     System.printString("Finished\n");
132   }
133
134   /**
135    * Parse the command line options.
136    **/
137   public static void parseCmdLine(String args[], LookUpService lus) {
138     int i = 0;
139     String arg;
140     while(i < args.length && args[i].startsWith("-")) {
141       arg = args[i++];
142       //check options
143       if(arg.equals("-N")) {
144         if(i < args.length) {
145           lus.numthreads = new Integer(args[i++]).intValue();
146         }
147       } else if(arg.equals("-nEntry")) {
148         if(i < args.length) {
149           lus.nobjs = new Integer(args[i++]).intValue();
150         }
151       } else if (arg.equals("-nTrans")) {
152         if(i < args.length) {
153           lus.numtrans =  new Integer(args[i++]).intValue();
154         }
155       } else if(arg.equals("-probRead")) {
156         if(i < args.length) {
157           lus.rdprob = new Integer(args[i++]).intValue();
158         }
159       } else if(arg.equals("-h")) {
160         lus.usage();
161       }
162     }
163
164     if(lus.nobjs == 0  || lus.numtrans == 0)
165       lus.usage();
166   }
167
168   /**
169    * The usage routine which describes the program options.
170    **/
171   public void usage() {
172     System.printString("usage: ./LookUpServiceN.bin master -N <threads> -nEntry <objects in hashmap> -nTrans <number of transactions> -probRead <read probability> \n");
173     System.printString("    -N the number of threads\n");
174     System.printString("    -nEntry the number of objects to be inserted into distributed hashmap\n");
175     System.printString("    -nTrans the number of transactions to run\n");
176     System.printString("    -probRead the probability of read given a transaction\n");
177     System.printString("    -h help with usage\n");
178   }
179 }