Rewrote threaded log code to be more idiomatic.
[invirt/third/libt4.git] / lock_tester.cc
1 //
2 // Lock server tester
3 //
4
5 #include "lock_client.h"
6 #include <arpa/inet.h>
7 #include <unistd.h>
8
9 char log_thread_prefix = 'c';
10
11 // must be >= 2
12 const 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.
13 static string dst;
14 static lock_client **lc = new lock_client * [nt];
15 static lock_protocol::lockid_t a = "1";
16 static lock_protocol::lockid_t b = "2";
17 static lock_protocol::lockid_t c = "3";
18
19 // check_grant() and check_release() check that the lock server
20 // doesn't grant the same lock to both clients.
21 // it assumes that lock names are distinct in the first byte.
22 static int ct[256];
23 static mutex count_mutex;
24
25 static void check_grant(lock_protocol::lockid_t lid) {
26     lock ml(count_mutex);
27     int x = lid[0] & 0x0f;
28     if (ct[x] != 0) {
29         LOG_NONMEMBER << "error: server granted " << lid << " twice";
30         exit(1);
31     }
32     ct[x] += 1;
33 }
34
35 static void check_release(lock_protocol::lockid_t lid) {
36     lock ml(count_mutex);
37     int x = lid[0] & 0x0f;
38     if (ct[x] != 1) {
39         LOG_NONMEMBER << "error: client released un-held lock " << lid;
40         exit(1);
41     }
42     ct[x] -= 1;
43 }
44
45 static void test1(void) {
46     LOG_NONMEMBER << "acquire a release a acquire a release a";
47     lc[0]->acquire(a);
48     check_grant(a);
49     lc[0]->release(a);
50     check_release(a);
51     lc[0]->acquire(a);
52     check_grant(a);
53     lc[0]->release(a);
54     check_release(a);
55
56     LOG_NONMEMBER << "acquire a acquire b release b release a";
57     lc[0]->acquire(a);
58     check_grant(a);
59     lc[0]->acquire(b);
60     check_grant(b);
61     lc[0]->release(b);
62     check_release(b);
63     lc[0]->release(a);
64     check_release(a);
65 }
66
67 static void test2(int i) {
68     LOG_NONMEMBER << "test2: client " << i << " acquire a release a";
69     lc[i]->acquire(a);
70     LOG_NONMEMBER << "test2: client " << i << " acquire done";
71     check_grant(a);
72     usleep(100000);
73     LOG_NONMEMBER << "test2: client " << i << " release";
74     check_release(a);
75     lc[i]->release(a);
76     LOG_NONMEMBER << "test2: client " << i << " release done";
77 }
78
79 static void test3(int i) {
80     LOG_NONMEMBER << "test3: client " << i << " acquire a release a concurrent";
81     for (int j = 0; j < 10; j++) {
82         lc[i]->acquire(a);
83         check_grant(a);
84         LOG_NONMEMBER << "test3: client " << i << " got lock";
85         check_release(a);
86         lc[i]->release(a);
87     }
88 }
89
90 static void test4(int i) {
91     LOG_NONMEMBER << "test4: thread " << i << " acquire a release a concurrent; same clnt";
92     for (int j = 0; j < 10; j++) {
93         lc[0]->acquire(a);
94         check_grant(a);
95         LOG_NONMEMBER << "test4: thread " << i << " on client 0 got lock";
96         check_release(a);
97         lc[0]->release(a);
98     }
99 }
100
101 static void test5(int i) {
102     LOG_NONMEMBER << "test5: client " << i << " acquire a release a concurrent; same and diff clnt";
103     for (int j = 0; j < 10; j++) {
104         if (i < 5)  lc[0]->acquire(a);
105         else  lc[1]->acquire(a);
106         check_grant(a);
107         LOG_NONMEMBER << "test5: client " << i << " got lock";
108         check_release(a);
109         if (i < 5) lc[0]->release(a);
110         else lc[1]->release(a);
111     }
112 }
113
114 int
115 main(int argc, char *argv[])
116 {
117     thread th[nt];
118     int test = 0;
119
120     setvbuf(stdout, NULL, _IONBF, 0);
121     setvbuf(stderr, NULL, _IONBF, 0);
122     srandom((uint32_t)getpid());
123
124     if (argc < 2) {
125         LOG_NONMEMBER << "Usage: " << argv[0] << " [host:]port [test]";
126         exit(1);
127     }
128
129     dst = argv[1]; 
130
131     if (argc > 2) {
132         test = atoi(argv[2]);
133         if (test < 1 || test > 5) {
134             LOG_NONMEMBER << "Test number must be between 1 and 5";
135             exit(1);
136         }
137     }
138
139     LOG_NONMEMBER << "cache lock client";
140     for (int i = 0; i < nt; i++) lc[i] = new lock_client(dst);
141
142     if (!test || test == 1) {
143         test1();
144     }
145
146     if (!test || test == 2) {
147         // test2
148         for (int i = 0; i < nt; i++)
149             th[i] = thread(test2, i);
150         for (int i = 0; i < nt; i++)
151             th[i].join();
152     }
153
154     if (!test || test == 3) {
155         LOG_NONMEMBER << "test 3";
156
157         for (int i = 0; i < nt; i++)
158             th[i] = thread(test3, i);
159         for (int i = 0; i < nt; i++)
160             th[i].join();
161     }
162
163     if (!test || test == 4) {
164         LOG_NONMEMBER << "test 4";
165
166         for (int i = 0; i < 2; i++)
167             th[i] = thread(test4, i);
168         for (int i = 0; i < 2; i++)
169             th[i].join();
170     }
171
172     if (!test || test == 5) {
173         LOG_NONMEMBER << "test 5";
174
175         for (int i = 0; i < nt; i++)
176             th[i] = thread(test5, i);
177         for (int i = 0; i < nt; i++)
178             th[i].join();
179     }
180
181     LOG_NONMEMBER << argv[0] << ": passed all tests successfully";
182
183 }