855364b169738dca23d33b773302f25bcef0b0f3
[jelmer/samba4-debian.git] / source / torture / raw / lockbench.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    locking benchmark
5
6    Copyright (C) Andrew Tridgell 2006
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "torture/torture.h"
24 #include "libcli/raw/libcliraw.h"
25 #include "system/time.h"
26 #include "system/filesys.h"
27 #include "libcli/libcli.h"
28 #include "torture/util.h"
29 #include "lib/events/events.h"
30 #include "lib/cmdline/popt_common.h"
31 #include "libcli/composite/composite.h"
32 #include "libcli/smb_composite/smb_composite.h"
33 #include "param/param.h"
34
35 #define BASEDIR "\\benchlock"
36 #define FNAME BASEDIR "\\lock.dat"
37
38 static int nprocs;
39 static int lock_failed;
40 static int num_connected;
41
42 enum lock_stage {LOCK_INITIAL, LOCK_LOCK, LOCK_UNLOCK};
43
44 struct benchlock_state {
45         struct event_context *ev;
46         struct smbcli_tree *tree;
47         TALLOC_CTX *mem_ctx;
48         int client_num;
49         int fnum;
50         enum lock_stage stage;
51         int lock_offset;
52         int unlock_offset;
53         int count;
54         int lastcount;
55         struct smbcli_request *req;
56         struct smb_composite_connect reconnect;
57         struct timed_event *te;
58
59         /* these are used for reconnections */
60         int dest_port;
61         const char *dest_host;
62         const char *called_name;
63         const char *service_type;
64 };
65
66 static void lock_completion(struct smbcli_request *);
67
68 /*
69   send the next lock request
70 */
71 static void lock_send(struct benchlock_state *state)
72 {
73         union smb_lock io;
74         struct smb_lock_entry lock;
75
76         switch (state->stage) {
77         case LOCK_INITIAL:
78                 io.lockx.in.ulock_cnt = 0;
79                 io.lockx.in.lock_cnt = 1;
80                 state->lock_offset = 0;
81                 state->unlock_offset = 0;
82                 lock.offset = state->lock_offset;
83                 break;
84         case LOCK_LOCK:
85                 io.lockx.in.ulock_cnt = 0;
86                 io.lockx.in.lock_cnt = 1;
87                 state->lock_offset = (state->lock_offset+1)%(nprocs+1);
88                 lock.offset = state->lock_offset;
89                 break;
90         case LOCK_UNLOCK:
91                 io.lockx.in.ulock_cnt = 1;
92                 io.lockx.in.lock_cnt = 0;
93                 lock.offset = state->unlock_offset;
94                 state->unlock_offset = (state->unlock_offset+1)%(nprocs+1);
95                 break;
96         }
97
98         lock.count = 1;
99         lock.pid = state->tree->session->pid;
100
101         io.lockx.level = RAW_LOCK_LOCKX;
102         io.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
103         io.lockx.in.timeout = 100000;
104         io.lockx.in.locks = &lock;
105         io.lockx.in.file.fnum = state->fnum;
106
107         state->req = smb_raw_lock_send(state->tree, &io);
108         if (state->req == NULL) {
109                 DEBUG(0,("Failed to setup lock\n"));
110                 lock_failed++;
111         }
112         state->req->async.private = state;
113         state->req->async.fn      = lock_completion;
114 }
115
116 static void reopen_connection(struct event_context *ev, struct timed_event *te, 
117                               struct timeval t, void *private_data);
118
119
120 static void reopen_file(struct event_context *ev, struct timed_event *te, 
121                                       struct timeval t, void *private_data)
122 {
123         struct benchlock_state *state = (struct benchlock_state *)private_data;
124
125         /* reestablish our open file */
126         state->fnum = smbcli_open(state->tree, FNAME, O_RDWR|O_CREAT, DENY_NONE);
127         if (state->fnum == -1) {
128                 printf("Failed to open %s on connection %d\n", FNAME, state->client_num);
129                 exit(1);
130         }
131
132         num_connected++;
133
134         DEBUG(0,("reconnect to %s finished (%u connected)\n", state->dest_host,
135                  num_connected));
136
137         state->stage = LOCK_INITIAL;
138         lock_send(state);
139 }
140
141 /*
142   complete an async reconnect
143  */
144 static void reopen_connection_complete(struct composite_context *ctx)
145 {
146         struct benchlock_state *state = (struct benchlock_state *)ctx->async.private_data;
147         NTSTATUS status;
148         struct smb_composite_connect *io = &state->reconnect;
149
150         status = smb_composite_connect_recv(ctx, state->mem_ctx);
151         if (!NT_STATUS_IS_OK(status)) {
152                 talloc_free(state->te);
153                 state->te = event_add_timed(state->ev, state->mem_ctx, 
154                                             timeval_current_ofs(1,0), 
155                                             reopen_connection, state);
156                 return;
157         }
158
159         talloc_free(state->tree);
160         state->tree = io->out.tree;
161
162         /* do the reopen as a separate event */
163         event_add_timed(state->ev, state->mem_ctx, timeval_zero(), reopen_file, state);
164 }
165
166         
167
168 /*
169   reopen a connection
170  */
171 static void reopen_connection(struct event_context *ev, struct timed_event *te, 
172                               struct timeval t, void *private_data)
173 {
174         struct benchlock_state *state = (struct benchlock_state *)private_data;
175         struct composite_context *ctx;
176         struct smb_composite_connect *io = &state->reconnect;
177         char *host, *share;
178
179         state->te = NULL;
180
181         if (!torture_get_conn_index(state->client_num, state->mem_ctx, &host, &share)) {
182                 DEBUG(0,("Can't find host/share for reconnect?!\n"));
183                 exit(1);
184         }
185
186         io->in.dest_host    = state->dest_host;
187         io->in.port         = state->dest_port;
188         io->in.called_name  = state->called_name;
189         io->in.service      = share;
190         io->in.service_type = state->service_type;
191         io->in.credentials  = cmdline_credentials;
192         io->in.fallback_to_anonymous = false;
193         io->in.workgroup    = lp_workgroup(global_loadparm);
194
195         /* kill off the remnants of the old connection */
196         talloc_free(state->tree);
197         state->tree = NULL;
198
199         ctx = smb_composite_connect_send(io, state->mem_ctx, state->ev);
200         if (ctx == NULL) {
201                 DEBUG(0,("Failed to setup async reconnect\n"));
202                 exit(1);
203         }
204
205         ctx->async.fn = reopen_connection_complete;
206         ctx->async.private_data = state;
207 }
208
209
210 /*
211   called when a lock completes
212 */
213 static void lock_completion(struct smbcli_request *req)
214 {
215         struct benchlock_state *state = (struct benchlock_state *)req->async.private;
216         NTSTATUS status = smbcli_request_simple_recv(req);
217         state->req = NULL;
218         if (!NT_STATUS_IS_OK(status)) {
219                 if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE) ||
220                     NT_STATUS_EQUAL(status, NT_STATUS_LOCAL_DISCONNECT)) {
221                         talloc_free(state->tree);
222                         state->tree = NULL;
223                         num_connected--;        
224                         DEBUG(0,("reopening connection to %s\n", state->dest_host));
225                         talloc_free(state->te);
226                         state->te = event_add_timed(state->ev, state->mem_ctx, 
227                                                     timeval_current_ofs(1,0), 
228                                                     reopen_connection, state);
229                 } else {
230                         DEBUG(0,("Lock failed - %s\n", nt_errstr(status)));
231                         lock_failed++;
232                 }
233                 return;
234         }
235
236         switch (state->stage) {
237         case LOCK_INITIAL:
238                 state->stage = LOCK_LOCK;
239                 break;
240         case LOCK_LOCK:
241                 state->stage = LOCK_UNLOCK;
242                 break;
243         case LOCK_UNLOCK:
244                 state->stage = LOCK_LOCK;
245                 break;
246         }
247
248         state->count++;
249         lock_send(state);
250 }
251
252
253 static void echo_completion(struct smbcli_request *req)
254 {
255         struct benchlock_state *state = (struct benchlock_state *)req->async.private;
256         NTSTATUS status = smbcli_request_simple_recv(req);
257         if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE) ||
258             NT_STATUS_EQUAL(status, NT_STATUS_LOCAL_DISCONNECT)) {
259                 talloc_free(state->tree);
260                 state->tree = NULL;
261                 num_connected--;        
262                 DEBUG(0,("reopening connection to %s\n", state->dest_host));
263                 talloc_free(state->te);
264                 state->te = event_add_timed(state->ev, state->mem_ctx, 
265                                             timeval_current_ofs(1,0), 
266                                             reopen_connection, state);
267         }
268 }
269
270 static void report_rate(struct event_context *ev, struct timed_event *te, 
271                         struct timeval t, void *private_data)
272 {
273         struct benchlock_state *state = talloc_get_type(private_data, 
274                                                         struct benchlock_state);
275         int i;
276         for (i=0;i<nprocs;i++) {
277                 printf("%5u ", (unsigned)(state[i].count - state[i].lastcount));
278                 state[i].lastcount = state[i].count;
279         }
280         printf("\r");
281         fflush(stdout);
282         event_add_timed(ev, state, timeval_current_ofs(1, 0), report_rate, state);
283
284         /* send an echo on each interface to ensure it stays alive - this helps
285            with IP takeover */
286         for (i=0;i<nprocs;i++) {
287                 struct smb_echo p;
288                 struct smbcli_request *req;
289
290                 if (!state[i].tree) {
291                         continue;
292                 }
293
294                 p.in.repeat_count = 1;
295                 p.in.size = 0;
296                 p.in.data = NULL;
297                 req = smb_raw_echo_send(state[i].tree->session->transport, &p);
298                 req->async.private = &state[i];
299                 req->async.fn      = echo_completion;
300         }
301 }
302
303 /* 
304    benchmark locking calls
305 */
306 bool torture_bench_lock(struct torture_context *torture)
307 {
308         bool ret = true;
309         TALLOC_CTX *mem_ctx = talloc_new(torture);
310         int i;
311         int timelimit = torture_setting_int(torture, "timelimit", 10);
312         struct timeval tv;
313         struct event_context *ev = event_context_find(mem_ctx);
314         struct benchlock_state *state;
315         int total = 0, minops=0;
316         struct smbcli_state *cli;
317         bool progress;
318
319         progress = torture_setting_bool(torture, "progress", true);
320
321         nprocs = lp_parm_int(global_loadparm, NULL, "torture", "nprocs", 4);
322
323         state = talloc_zero_array(mem_ctx, struct benchlock_state, nprocs);
324
325         printf("Opening %d connections\n", nprocs);
326         for (i=0;i<nprocs;i++) {
327                 state[i].mem_ctx = talloc_new(state);
328                 state[i].client_num = i;
329                 state[i].ev = ev;
330                 if (!torture_open_connection_ev(&cli, i, ev)) {
331                         return false;
332                 }
333                 talloc_steal(mem_ctx, state);
334                 state[i].tree = cli->tree;
335                 state[i].dest_host = talloc_strdup(state[i].mem_ctx, 
336                                                    cli->tree->session->transport->socket->hostname);
337                 state[i].dest_port = cli->tree->session->transport->socket->port;
338                 state[i].called_name  = talloc_strdup(state[i].mem_ctx,
339                                                       cli->tree->session->transport->called.name);
340                 state[i].service_type = talloc_strdup(state[i].mem_ctx,
341                                                       cli->tree->device);
342         }
343
344         num_connected = i;
345
346         if (!torture_setup_dir(cli, BASEDIR)) {
347                 goto failed;
348         }
349
350         for (i=0;i<nprocs;i++) {
351                 state[i].fnum = smbcli_open(state[i].tree, 
352                                             FNAME, 
353                                             O_RDWR|O_CREAT, DENY_NONE);
354                 if (state[i].fnum == -1) {
355                         printf("Failed to open %s on connection %d\n", FNAME, i);
356                         goto failed;
357                 }
358
359                 state[i].stage = LOCK_INITIAL;
360                 lock_send(&state[i]);
361         }
362
363         tv = timeval_current(); 
364
365         if (progress) {
366                 event_add_timed(ev, state, timeval_current_ofs(1, 0), report_rate, state);
367         }
368
369         printf("Running for %d seconds\n", timelimit);
370         while (timeval_elapsed(&tv) < timelimit) {
371                 event_loop_once(ev);
372
373                 if (lock_failed) {
374                         DEBUG(0,("locking failed\n"));
375                         goto failed;
376                 }
377         }
378
379         printf("%.2f ops/second\n", total/timeval_elapsed(&tv));
380         minops = state[0].count;
381         for (i=0;i<nprocs;i++) {
382                 printf("[%d] %u ops\n", i, state[i].count);
383                 if (state[i].count < minops) minops = state[i].count;
384         }
385         if (minops < 0.5*total/nprocs) {
386                 printf("Failed: unbalanced locking\n");
387                 goto failed;
388         }
389
390         for (i=0;i<nprocs;i++) {
391                 talloc_free(state[i].req);
392                 smb_raw_exit(state[i].tree->session);
393         }
394
395         smbcli_deltree(state[0].tree, BASEDIR);
396         talloc_free(mem_ctx);
397         printf("\n");
398         return ret;
399
400 failed:
401         talloc_free(mem_ctx);
402         return false;
403 }