dd7c07b0d56c66eaf5557ceead555f24daa2ffb4
[invirt/third/libt4.git] / lock_tester.cc
1 //
2 // Lock server tester
3 //
4
5 #include "lock_protocol.h"
6 #include "lock_client.h"
7 #include "rpc.h"
8 #include "jsl_log.h"
9 #include <arpa/inet.h>
10 #include <vector>
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include "lang/verify.h"
14 #include "lock_client_cache_rsm.h"
15 #include "tprintf.h"
16
17 char tprintf_thread_prefix = 'c';
18
19 // must be >= 2
20 int nt = 6; //XXX: lab1's rpc handlers are blocking. Since rpcs uses a thread pool of 10 threads, we cannot test more than 10 blocking rpc.
21 std::string dst;
22 lock_client_cache_rsm **lc = new lock_client_cache_rsm * [nt];
23 lock_protocol::lockid_t a = 1;
24 lock_protocol::lockid_t b = 2;
25 lock_protocol::lockid_t c = 3;
26
27 // check_grant() and check_release() check that the lock server
28 // doesn't grant the same lock to both clients.
29 // it assumes that lock names are distinct in the first byte.
30 int ct[256];
31 pthread_mutex_t count_mutex;
32
33 void
34 check_grant(lock_protocol::lockid_t lid)
35 {
36   ScopedLock ml(&count_mutex);
37   int x = lid & 0xff;
38   if(ct[x] != 0){
39     fprintf(stderr, "error: server granted %016llx twice\n", lid);
40     fprintf(stdout, "error: server granted %016llx twice\n", lid);
41     exit(1);
42   }
43   ct[x] += 1;
44 }
45
46 void
47 check_release(lock_protocol::lockid_t lid)
48 {
49   ScopedLock ml(&count_mutex);
50   int x = lid & 0xff;
51   if(ct[x] != 1){
52     fprintf(stderr, "error: client released un-held lock %016llx\n",  lid);
53     exit(1);
54   }
55   ct[x] -= 1;
56 }
57
58 void
59 test1(void)
60 {
61     tprintf ("acquire a release a acquire a release a\n");
62     lc[0]->acquire(a);
63     check_grant(a);
64     lc[0]->release(a);
65     check_release(a);
66     lc[0]->acquire(a);
67     check_grant(a);
68     lc[0]->release(a);
69     check_release(a);
70
71     tprintf ("acquire a acquire b release b release a\n");
72     lc[0]->acquire(a);
73     check_grant(a);
74     lc[0]->acquire(b);
75     check_grant(b);
76     lc[0]->release(b);
77     check_release(b);
78     lc[0]->release(a);
79     check_release(a);
80 }
81
82 void *
83 test2(void *x) 
84 {
85   int i = * (int *) x;
86
87   tprintf ("test2: client %d acquire a release a\n", i);
88   lc[i]->acquire(a);
89   tprintf ("test2: client %d acquire done\n", i);
90   check_grant(a);
91   sleep(1);
92   tprintf ("test2: client %d release\n", i);
93   check_release(a);
94   lc[i]->release(a);
95   tprintf ("test2: client %d release done\n", i);
96   return 0;
97 }
98
99 void *
100 test3(void *x)
101 {
102   int i = * (int *) x;
103
104   tprintf ("test3: client %d acquire a release a concurrent\n", i);
105   for (int j = 0; j < 10; j++) {
106     lc[i]->acquire(a);
107     check_grant(a);
108     tprintf ("test3: client %d got lock\n", i);
109     check_release(a);
110     lc[i]->release(a);
111   }
112   return 0;
113 }
114
115 void *
116 test4(void *x)
117 {
118   int i = * (int *) x;
119
120   tprintf ("test4: thread %d acquire a release a concurrent; same clnt\n", i);
121   for (int j = 0; j < 10; j++) {
122     lc[0]->acquire(a);
123     check_grant(a);
124     tprintf ("test4: thread %d on client 0 got lock\n", i);
125     check_release(a);
126     lc[0]->release(a);
127   }
128   return 0;
129 }
130
131 void *
132 test5(void *x)
133 {
134   int i = * (int *) x;
135
136   tprintf ("test5: client %d acquire a release a concurrent; same and diff clnt\n", i);
137   for (int j = 0; j < 10; j++) {
138     if (i < 5)  lc[0]->acquire(a);
139     else  lc[1]->acquire(a);
140     check_grant(a);
141     tprintf ("test5: client %d got lock\n", i);
142     check_release(a);
143     if (i < 5) lc[0]->release(a);
144     else lc[1]->release(a);
145   }
146   return 0;
147 }
148
149 int
150 main(int argc, char *argv[])
151 {
152     int r;
153     pthread_t th[nt];
154     int test = 0;
155
156     setvbuf(stdout, NULL, _IONBF, 0);
157     setvbuf(stderr, NULL, _IONBF, 0);
158     srandom(getpid());
159
160     //jsl_set_debug(2);
161
162     if(argc < 2) {
163       fprintf(stderr, "Usage: %s [host:]port [test]\n", argv[0]);
164       exit(1);
165     }
166
167     dst = argv[1]; 
168
169     if (argc > 2) {
170       test = atoi(argv[2]);
171       if(test < 1 || test > 5){
172         tprintf("Test number must be between 1 and 5\n");
173         exit(1);
174       }
175     }
176
177     VERIFY(pthread_mutex_init(&count_mutex, NULL) == 0);
178     tprintf("cache lock client\n");
179     for (int i = 0; i < nt; i++) lc[i] = new lock_client_cache_rsm(dst);
180
181     if(!test || test == 1){
182       test1();
183     }
184
185     if(!test || test == 2){
186       // test2
187       for (int i = 0; i < nt; i++) {
188         int *a = new int (i);
189         r = pthread_create(&th[i], NULL, test2, (void *) a);
190         VERIFY (r == 0);
191       }
192       for (int i = 0; i < nt; i++) {
193         pthread_join(th[i], NULL);
194       }
195     }
196
197     if(!test || test == 3){
198       tprintf("test 3\n");
199       
200       // test3
201       for (int i = 0; i < nt; i++) {
202         int *a = new int (i);
203         r = pthread_create(&th[i], NULL, test3, (void *) a);
204         VERIFY (r == 0);
205       }
206       for (int i = 0; i < nt; i++) {
207         pthread_join(th[i], NULL);
208       }
209     }
210
211     if(!test || test == 4){
212       tprintf("test 4\n");
213       
214       // test 4
215       for (int i = 0; i < 2; i++) {
216         int *a = new int (i);
217         r = pthread_create(&th[i], NULL, test4, (void *) a);
218         VERIFY (r == 0);
219       }
220       for (int i = 0; i < 2; i++) {
221         pthread_join(th[i], NULL);
222       }
223     }
224
225     if(!test || test == 5){
226       tprintf("test 5\n");
227       
228       // test 5
229       
230       for (int i = 0; i < nt; i++) {
231         int *a = new int (i);
232         r = pthread_create(&th[i], NULL, test5, (void *) a);
233         VERIFY (r == 0);
234       }
235       for (int i = 0; i < nt; i++) {
236         pthread_join(th[i], NULL);
237       }
238     }
239
240     tprintf ("%s: passed all tests successfully\n", argv[0]);
241
242 }