r23181: prevent attempts to reopen the connection twice at the same time
[kai/samba-autobuild/.git] / source4 / torture / raw / openbench.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    open benchmark
5
6    Copyright (C) Andrew Tridgell 2007
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 2 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, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "torture/torture.h"
25 #include "libcli/raw/libcliraw.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
35 #define BASEDIR "\\benchopen"
36
37 static int nprocs;
38 static int open_failed;
39 static int open_retries;
40 static char **fnames;
41 static int num_connected;
42 static struct timed_event *report_te;
43
44 struct benchopen_state {
45         TALLOC_CTX *mem_ctx;
46         struct event_context *ev;
47         struct smbcli_state *cli;
48         struct smbcli_tree *tree;
49         int client_num;
50         int old_fnum;
51         int fnum;
52         int file_num;
53         int count;
54         int lastcount;
55         union smb_open open_parms;
56         union smb_close close_parms;
57         struct smbcli_request *req_open;
58         struct smbcli_request *req_close;
59         struct smb_composite_connect reconnect;
60         struct timed_event *te;
61
62         /* these are used for reconnections */
63         int dest_port;
64         const char *dest_host;
65         const char *called_name;
66         const char *service_type;
67 };
68
69 static void next_open(struct benchopen_state *state);
70 static void reopen_connection(struct event_context *ev, struct timed_event *te, 
71                               struct timeval t, void *private_data);
72
73
74 /*
75   complete an async reconnect
76  */
77 static void reopen_connection_complete(struct composite_context *ctx)
78 {
79         struct benchopen_state *state = (struct benchopen_state *)ctx->async.private_data;
80         NTSTATUS status;
81         struct smb_composite_connect *io = &state->reconnect;
82
83         status = smb_composite_connect_recv(ctx, state->mem_ctx);
84         if (!NT_STATUS_IS_OK(status)) {
85                 talloc_free(state->te);
86                 state->te = event_add_timed(state->ev, state->mem_ctx, 
87                                             timeval_current_ofs(1,0), 
88                                             reopen_connection, state);
89                 return;
90         }
91
92         state->tree = io->out.tree;
93
94         num_connected++;
95
96         DEBUG(0,("reconnect to %s finished (%u connected)\n", state->dest_host,
97                  num_connected));
98
99         state->fnum = -1;
100         state->old_fnum = -1;
101         next_open(state);
102 }
103
104         
105
106 /*
107   reopen a connection
108  */
109 static void reopen_connection(struct event_context *ev, struct timed_event *te, 
110                               struct timeval t, void *private_data)
111 {
112         struct benchopen_state *state = (struct benchopen_state *)private_data;
113         struct composite_context *ctx;
114         struct smb_composite_connect *io = &state->reconnect;
115         char *host, *share;
116
117         if (!torture_get_conn_index(state->client_num, state->mem_ctx, &host, &share)) {
118                 DEBUG(0,("Can't find host/share for reconnect?!\n"));
119                 exit(1);
120         }
121
122         io->in.dest_host    = state->dest_host;
123         io->in.port         = state->dest_port;
124         io->in.called_name  = state->called_name;
125         io->in.service      = share;
126         io->in.service_type = state->service_type;
127         io->in.credentials  = cmdline_credentials;
128         io->in.fallback_to_anonymous = False;
129         io->in.workgroup    = lp_workgroup();
130
131         /* kill off the remnants of the old connection */
132         talloc_free(state->tree);
133         state->tree = NULL;
134         state->fnum = -1;
135
136         ctx = smb_composite_connect_send(io, state->mem_ctx, state->ev);
137         if (ctx == NULL) {
138                 DEBUG(0,("Failed to setup async reconnect\n"));
139                 exit(1);
140         }
141
142         ctx->async.fn = reopen_connection_complete;
143         ctx->async.private_data = state;
144 }
145
146 static void open_completed(struct smbcli_request *req);
147 static void close_completed(struct smbcli_request *req);
148
149
150 static void next_open(struct benchopen_state *state)
151 {
152         state->count++;
153
154         state->file_num = (state->file_num+1) % (3*nprocs);
155
156         DEBUG(2,("[%d] opening %u\n", state->client_num, state->file_num));
157         state->open_parms.ntcreatex.level = RAW_OPEN_NTCREATEX;
158         state->open_parms.ntcreatex.in.flags = 0;
159         state->open_parms.ntcreatex.in.root_fid = 0;
160         state->open_parms.ntcreatex.in.access_mask = SEC_RIGHTS_FILE_ALL;
161         state->open_parms.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
162         state->open_parms.ntcreatex.in.alloc_size = 0;
163         state->open_parms.ntcreatex.in.share_access = 0;
164         state->open_parms.ntcreatex.in.open_disposition = NTCREATEX_DISP_OVERWRITE_IF;
165         state->open_parms.ntcreatex.in.create_options = 0;
166         state->open_parms.ntcreatex.in.impersonation = 0;
167         state->open_parms.ntcreatex.in.security_flags = 0;
168         state->open_parms.ntcreatex.in.fname = fnames[state->file_num];
169
170         state->req_open = smb_raw_open_send(state->tree, &state->open_parms);
171         state->req_open->async.fn = open_completed;
172         state->req_open->async.private = state;
173 }
174
175
176 static void next_close(struct benchopen_state *state)
177 {
178         DEBUG(2,("[%d] closing %d\n", state->client_num, state->old_fnum));
179         if (state->old_fnum == -1) {
180                 return;
181         }
182         state->close_parms.close.level = RAW_CLOSE_CLOSE;
183         state->close_parms.close.in.file.fnum = state->old_fnum;
184         state->close_parms.close.in.write_time = 0;
185
186         state->req_close = smb_raw_close_send(state->tree, &state->close_parms);
187         state->req_close->async.fn = close_completed;
188         state->req_close->async.private = state;
189         state->old_fnum = -1;
190 }
191
192 /*
193   called when a open completes
194 */
195 static void open_completed(struct smbcli_request *req)
196 {
197         struct benchopen_state *state = (struct benchopen_state *)req->async.private;
198         TALLOC_CTX *tmp_ctx = talloc_new(state->mem_ctx);
199         NTSTATUS status;
200
201         status = smb_raw_open_recv(req, tmp_ctx, &state->open_parms);
202
203         talloc_free(tmp_ctx);
204
205         state->req_open = NULL;
206
207         if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE) ||
208             NT_STATUS_EQUAL(status, NT_STATUS_LOCAL_DISCONNECT)) {
209                 talloc_free(state->tree);
210                 talloc_free(state->cli);
211                 state->tree = NULL;
212                 state->cli = NULL;
213                 num_connected--;        
214                 DEBUG(0,("reopening connection to %s\n", state->dest_host));
215                 talloc_free(state->te);
216                 state->te = event_add_timed(state->ev, state->mem_ctx, 
217                                             timeval_current_ofs(1,0), 
218                                             reopen_connection, state);
219                 return;
220         }
221
222         if (NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION)) {
223                 DEBUG(2,("[%d] retrying open\n", state->client_num));
224                 open_retries++;
225                 state->req_open = smb_raw_open_send(state->tree, &state->open_parms);
226                 state->req_open->async.fn = open_completed;
227                 state->req_open->async.private = state;
228                 return;
229         }
230
231         if (!NT_STATUS_IS_OK(status)) {
232                 open_failed++;
233                 DEBUG(0,("open failed - %s\n", nt_errstr(status)));
234                 return;
235         }
236
237         state->old_fnum = state->fnum;
238         state->fnum = state->open_parms.ntcreatex.out.file.fnum;
239
240         DEBUG(2,("[%d] open completed: fnum=%d old_fnum=%d\n", 
241                  state->client_num, state->fnum, state->old_fnum));
242
243         if (state->old_fnum != -1) {
244                 next_close(state);
245         }
246
247         next_open(state);
248 }       
249
250 /*
251   called when a close completes
252 */
253 static void close_completed(struct smbcli_request *req)
254 {
255         struct benchopen_state *state = (struct benchopen_state *)req->async.private;
256         NTSTATUS status = smbcli_request_simple_recv(req);
257
258         state->req_close = NULL;
259
260         if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE) ||
261             NT_STATUS_EQUAL(status, NT_STATUS_LOCAL_DISCONNECT)) {
262                 talloc_free(state->tree);
263                 talloc_free(state->cli);
264                 state->tree = NULL;
265                 state->cli = NULL;
266                 num_connected--;        
267                 DEBUG(0,("reopening connection to %s\n", state->dest_host));
268                 talloc_free(state->te);
269                 state->te = event_add_timed(state->ev, state->mem_ctx, 
270                                             timeval_current_ofs(1,0), 
271                                             reopen_connection, state);
272                 return;
273         }
274
275         if (!NT_STATUS_IS_OK(status)) {
276                 open_failed++;
277                 DEBUG(0,("close failed - %s\n", nt_errstr(status)));
278                 return;
279         }
280
281         DEBUG(2,("[%d] close completed: fnum=%d old_fnum=%d\n", 
282                  state->client_num, state->fnum, state->old_fnum));
283 }       
284
285 static void echo_completion(struct smbcli_request *req)
286 {
287         struct benchopen_state *state = talloc_get_type(req->async.private, 
288                                                         struct benchopen_state);
289         NTSTATUS status = smbcli_request_simple_recv(req);
290         if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE) ||
291             NT_STATUS_EQUAL(status, NT_STATUS_LOCAL_DISCONNECT)) {
292                 talloc_free(state->tree);
293                 state->tree = NULL;
294                 num_connected--;        
295                 DEBUG(0,("reopening connection to %s\n", state->dest_host));
296                 talloc_free(state->te);
297                 state->te = event_add_timed(state->ev, state->mem_ctx, 
298                                             timeval_current_ofs(1,0), 
299                                             reopen_connection, state);
300         }
301 }
302
303 static void report_rate(struct event_context *ev, struct timed_event *te, 
304                         struct timeval t, void *private_data)
305 {
306         struct benchopen_state *state = talloc_get_type(private_data, 
307                                                         struct benchopen_state);
308         int i;
309         for (i=0;i<nprocs;i++) {
310                 printf("%5u ", (unsigned)(state[i].count - state[i].lastcount));
311                 state[i].lastcount = state[i].count;
312         }
313         printf("\r");
314         fflush(stdout);
315         report_te = event_add_timed(ev, state, timeval_current_ofs(1, 0), 
316                                     report_rate, state);
317
318         /* send an echo on each interface to ensure it stays alive - this helps
319            with IP takeover */
320         for (i=0;i<nprocs;i++) {
321                 struct smb_echo p;
322                 struct smbcli_request *req;
323
324                 if (!state[i].tree) {
325                         continue;
326                 }
327
328                 p.in.repeat_count = 1;
329                 p.in.size = 0;
330                 p.in.data = NULL;
331                 req = smb_raw_echo_send(state[i].tree->session->transport, &p);
332                 req->async.private = state;
333                 req->async.fn      = echo_completion;
334         }
335 }
336
337 /* 
338    benchmark open calls
339 */
340 BOOL torture_bench_open(struct torture_context *torture)
341 {
342         BOOL ret = True;
343         TALLOC_CTX *mem_ctx = talloc_new(torture);
344         int i;
345         int timelimit = torture_setting_int(torture, "timelimit", 10);
346         struct timeval tv;
347         struct event_context *ev = event_context_find(mem_ctx);
348         struct benchopen_state *state;
349         int total = 0, minops=0;
350         bool progress=False;
351
352         progress = torture_setting_bool(torture, "progress", true);
353         
354         nprocs = lp_parm_int(-1, "torture", "nprocs", 4);
355
356         state = talloc_zero_array(mem_ctx, struct benchopen_state, nprocs);
357
358         printf("Opening %d connections\n", nprocs);
359         for (i=0;i<nprocs;i++) {
360                 state[i].mem_ctx = talloc_new(state);
361                 state[i].client_num = i;
362                 state[i].ev = ev;
363                 if (!torture_open_connection_ev(&state[i].cli, i, ev)) {
364                         return False;
365                 }
366                 talloc_steal(mem_ctx, state);
367                 state[i].tree = state[i].cli->tree;
368                 state[i].dest_host = talloc_strdup(state[i].mem_ctx, 
369                                                    state[i].cli->tree->session->transport->socket->hostname);
370                 state[i].dest_port = state[i].cli->tree->session->transport->socket->port;
371                 state[i].called_name  = talloc_strdup(state[i].mem_ctx,
372                                                       state[i].cli->tree->session->transport->called.name);
373                 state[i].service_type = talloc_strdup(state[i].mem_ctx,
374                                                       state[i].cli->tree->device);
375         }
376
377         num_connected = i;
378
379         if (!torture_setup_dir(state[0].cli, BASEDIR)) {
380                 goto failed;
381         }
382
383         fnames = talloc_array(mem_ctx, char *, 3*nprocs);
384         for (i=0;i<3*nprocs;i++) {
385                 fnames[i] = talloc_asprintf(fnames, "%s\\file%d.dat", BASEDIR, i);
386         }
387
388         for (i=0;i<nprocs;i++) {
389                 state[i].file_num = i;          
390                 state[i].fnum = smbcli_open(state[i].tree, 
391                                             fnames[state->file_num], 
392                                             O_RDWR|O_CREAT, DENY_ALL);
393                 state[i].old_fnum = -1;
394                 next_open(&state[i]);
395         }
396
397         tv = timeval_current(); 
398
399         if (progress) {
400                 report_te = event_add_timed(ev, state, timeval_current_ofs(1, 0), 
401                                             report_rate, state);
402         }
403
404         printf("Running for %d seconds\n", timelimit);
405         while (timeval_elapsed(&tv) < timelimit) {
406                 event_loop_once(ev);
407
408                 if (open_failed) {
409                         DEBUG(0,("open failed\n"));
410                         goto failed;
411                 }
412         }
413
414         talloc_free(report_te);
415
416         printf("%.2f ops/second (%d retries)\n", 
417                total/timeval_elapsed(&tv), open_retries);
418         minops = state[0].count;
419         for (i=0;i<nprocs;i++) {
420                 printf("[%d] %u ops\n", i, state[i].count);
421                 if (state[i].count < minops) minops = state[i].count;
422         }
423         if (minops < 0.5*total/nprocs) {
424                 printf("Failed: unbalanced open\n");
425                 goto failed;
426         }
427
428         for (i=0;i<nprocs;i++) {
429                 talloc_free(state[i].req_open);
430                 talloc_free(state[i].req_close);
431                 smb_raw_exit(state[i].tree->session);
432         }
433
434         smbcli_deltree(state[0].tree, BASEDIR);
435         talloc_free(mem_ctx);
436         return ret;
437
438 failed:
439         talloc_free(mem_ctx);
440         return False;
441 }