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