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